Files
azalea/schema/lexer/lexer.go
2026-01-28 19:52:13 -06:00

237 lines
3.1 KiB
Go

// Code generated by gocc; DO NOT EDIT.
package lexer
import (
"os"
"unicode/utf8"
"azalea/schema/token"
)
const (
NoState = -1
NumStates = 278
NumSymbols = 104
)
type Lexer struct {
src []byte
pos int
line int
column int
Context token.Context
}
func NewLexer(src []byte) *Lexer {
lexer := &Lexer{
src: src,
pos: 0,
line: 1,
column: 1,
Context: nil,
}
return lexer
}
// SourceContext is a simple instance of a token.Context which
// contains the name of the source file.
type SourceContext struct {
Filepath string
}
func (s *SourceContext) Source() string {
return s.Filepath
}
func NewLexerFile(fpath string) (*Lexer, error) {
src, err := os.ReadFile(fpath)
if err != nil {
return nil, err
}
lexer := NewLexer(src)
lexer.Context = &SourceContext{Filepath: fpath}
return lexer, nil
}
func (l *Lexer) Scan() (tok *token.Token) {
tok = &token.Token{}
if l.pos >= len(l.src) {
tok.Type = token.EOF
tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = l.pos, l.line, l.column
tok.Pos.Context = l.Context
return
}
start, startLine, startColumn, end := l.pos, l.line, l.column, 0
tok.Type = token.INVALID
state, rune1, size := 0, rune(-1), 0
for state != -1 {
if l.pos >= len(l.src) {
rune1 = -1
} else {
rune1, size = utf8.DecodeRune(l.src[l.pos:])
l.pos += size
}
nextState := -1
if rune1 != -1 {
nextState = TransTab[state](rune1)
}
state = nextState
if state != -1 {
switch rune1 {
case '\n':
l.line++
l.column = 1
case '\r':
l.column = 1
case '\t':
l.column += 4
default:
l.column++
}
switch {
case ActTab[state].Accept != -1:
tok.Type = ActTab[state].Accept
end = l.pos
case ActTab[state].Ignore != "":
start, startLine, startColumn = l.pos, l.line, l.column
state = 0
if start >= len(l.src) {
tok.Type = token.EOF
}
}
} else {
if tok.Type == token.INVALID {
end = l.pos
}
}
}
if end > start {
l.pos = end
tok.Lit = l.src[start:end]
} else {
tok.Lit = []byte{}
}
tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn
tok.Pos.Context = l.Context
return
}
func (l *Lexer) Reset() {
l.pos = 0
}
/*
Lexer symbols:
0: '`'
1: '`'
2: '"'
3: '"'
4: '''
5: '''
6: 'i'
7: '('
8: ')'
9: '.'
10: '\'
11: 'u'
12: '\'
13: 'U'
14: '\'
15: 'a'
16: 'b'
17: 'f'
18: 'n'
19: 'r'
20: 't'
21: 'v'
22: '\'
23: '''
24: '"'
25: '\'
26: '\'
27: 'x'
28: '_'
29: '_'
30: '_'
31: '_'
32: '-'
33: '+'
34: '0'
35: 'b'
36: 'B'
37: '-'
38: '+'
39: '0'
40: 'o'
41: 'O'
42: '-'
43: '+'
44: '-'
45: '+'
46: '0'
47: 'x'
48: 'X'
49: 'e'
50: 'E'
51: '+'
52: '-'
53: '-'
54: '+'
55: '.'
56: '.'
57: 'p'
58: 'P'
59: '+'
60: '-'
61: '_'
62: '.'
63: '_'
64: '.'
65: '-'
66: '+'
67: '0'
68: 'x'
69: 'X'
70: '_'
71: '~'
72: '!'
73: '@'
74: '#'
75: '$'
76: '%'
77: '^'
78: '&'
79: '*'
80: '-'
81: '_'
82: '+'
83: '='
84: '?'
85: '/'
86: '.'
87: ' '
88: '\t'
89: '\n'
90: '\r'
91: ';'
92: '\n'
93: ' '-'!'
94: '#'-'['
95: ']'-\U0010ffff
96: '0'-'1'
97: '2'-'7'
98: '8'-'9'
99: 'A'-'F'
100: 'a'-'f'
101: 'A'-'Z'
102: 'a'-'z'
103: .
*/