Debian Codegraph @

doc/resources/case_changing_stream.go

raw
1 package antlr_resource
2
3 import (
4 "unicode"
5
6 "github.com/antlr/antlr4/runtime/Go/antlr"
7 )
8
9 // CaseChangingStream wraps an existing CharStream, but upper cases, or
10 // lower cases the input before it is tokenized.
11 type CaseChangingStream struct {
12 antlr.CharStream
13
14 upper bool
15 }
16
17 // NewCaseChangingStream returns a new CaseChangingStream that forces
18 // all tokens read from the underlying stream to be either upper case
19 // or lower case based on the upper argument.
20 func NewCaseChangingStream(in antlr.CharStream, upper bool) *CaseChangingStream {
21 return &CaseChangingStream{in, upper}
22 }
23
24 // LA gets the value of the symbol at offset from the current position
25 // from the underlying CharStream and converts it to either upper case
26 // or lower case.
27 func (is *CaseChangingStream) LA(offset int) int {
28 in := is.CharStream.LA(offset)
29 if in < 0 {
30 // Such as antlr.TokenEOF which is -1
31 return in
32 }
33 if is.upper {
34 return int(unicode.ToUpper(rune(in)))
35 }
36 return int(unicode.ToLower(rune(in)))
37 }