Debian Codegraph @

example/usercontext/lexer/lexer.go

raw symbols SCIP
1 // Code generated by gocc; DO NOT EDIT.
2
3 package lexer
4
5 import (
6 "os"
7 "unicode/utf8"
8
9 "github.com/goccmack/gocc/example/usercontext/token"
10 )
11
12 const (
13 NoState = -1
14 NumStates = 13
15 NumSymbols = 18
16 )
17
18 type Lexer struct {
19 src []byte
20 pos int
21 line int
22 column int
23 Context token.Context
24 }
25
26 func NewLexer(src []byte) *Lexer {
27 lexer := &Lexer{
28 src: src,
29 pos: 0,
30 line: 1,
31 column: 1,
32 Context: nil,
33 }
34 return lexer
35 }
36
37 // SourceContext is a simple instance of a token.Context which
38 // contains the name of the source file.
39 type SourceContext struct {
40 Filepath string
41 }
42
43 func (s *SourceContext) Source() string {
44 return s.Filepath
45 }
46
47 func NewLexerFile(fpath string) (*Lexer, error) {
48 src, err := os.ReadFile(fpath)
49 if err != nil {
50 return nil, err
51 }
52 lexer := NewLexer(src)
53 lexer.Context = &SourceContext{Filepath: fpath}
54 return lexer, nil
55 }
56
57 func (l *Lexer) Scan() (tok *token.Token) {
58 tok = &token.Token{}
59 if l.pos >= len(l.src) {
60 tok.Type = token.EOF
61 tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = l.pos, l.line, l.column
62 tok.Pos.Context = l.Context
63 return
64 }
65 start, startLine, startColumn, end := l.pos, l.line, l.column, 0
66 tok.Type = token.INVALID
67 state, rune1, size := 0, rune(-1), 0
68 for state != -1 {
69 if l.pos >= len(l.src) {
70 rune1 = -1
71 } else {
72 rune1, size = utf8.DecodeRune(l.src[l.pos:])
73 l.pos += size
74 }
75
76 nextState := -1
77 if rune1 != -1 {
78 nextState = TransTab[state](rune1)
79 }
80 state = nextState
81
82 if state != -1 {
83
84 switch rune1 {
85 case '\n':
86 l.line++
87 l.column = 1
88 case '\r':
89 l.column = 1
90 case '\t':
91 l.column += 4
92 default:
93 l.column++
94 }
95
96 switch {
97 case ActTab[state].Accept != -1:
98 tok.Type = ActTab[state].Accept
99 end = l.pos
100 case ActTab[state].Ignore != "":
101 start, startLine, startColumn = l.pos, l.line, l.column
102 state = 0
103 if start >= len(l.src) {
104 tok.Type = token.EOF
105 }
106
107 }
108 } else {
109 if tok.Type == token.INVALID {
110 end = l.pos
111 }
112 }
113 }
114 if end > start {
115 l.pos = end
116 tok.Lit = l.src[start:end]
117 } else {
118 tok.Lit = []byte{}
119 }
120 tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn
121 tok.Pos.Context = l.Context
122
123 return
124 }
125
126 func (l *Lexer) Reset() {
127 l.pos = 0
128 }
129
130 /*
131 Lexer symbols:
132 0: '.'
133 1: '.'
134 2: '.'
135 3: '4'
136 4: '2'
137 5: '.'
138 6: '.'
139 7: '.'
140 8: ' '
141 9: '\r'
142 10: '\n'
143 11: '\n'
144 12: 'a'-'z'
145 13: 'a'-'z'
146 14: 'A'-'Z'
147 15: 'a'-'z'
148 16: 'A'-'Z'
149 17: .
150 */