| 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/astx/token" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | NoState = -1 |
| 14 | NumStates = 7 |
| 15 | NumSymbols = 10 |
| 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: '\t' |
| 136 | 4: '\n' |
| 137 | 5: '\r' |
| 138 | 6: 'a'-'z' |
| 139 | 7: 'A'-'Z' |
| 140 | 8: '0'-'9' |
| 141 | 9: . |
| 142 | */ |