Debian Codegraph @

internal/lexer/gen/golang/lexer.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package golang
16
17 import (
18 "bytes"
19 "path"
20 "text/template"
21
22 "github.com/goccmack/gocc/internal/config"
23 "github.com/goccmack/gocc/internal/io"
24 "github.com/goccmack/gocc/internal/lexer/items"
25 )
26
27 func genLexer(pkg, outDir string, itemsets *items.ItemSets, cfg config.Config) {
28 tmpl, err := template.New("lexer").Parse(lexerSrc[1:])
29 if err != nil {
30 panic(err)
31 }
32 buf := new(bytes.Buffer)
33 err = tmpl.Execute(buf, getLexerData(pkg, outDir, itemsets, cfg))
34 if err != nil {
35 panic(err)
36 }
37 io.WriteFile(path.Join(outDir, "lexer", "lexer.go"), buf.Bytes())
38 }
39
40 func getLexerData(pkg, outDir string, itemsets *items.ItemSets, cfg config.Config) *lexerData {
41 lexSymbols := itemsets.Symbols().List()
42 return &lexerData{
43 Debug: cfg.DebugLexer(),
44 TokenImport: path.Join(pkg, "token"),
45 UtilImport: path.Join(pkg, "util"),
46 NumStates: itemsets.Size(),
47 NumSymbols: len(lexSymbols),
48 Symbols: lexSymbols,
49 }
50 }
51
52 type lexerData struct {
53 Debug bool
54 TokenImport string
55 UtilImport string
56 NumStates int
57 NumSymbols int
58 NextState []byte
59 Symbols []string
60 }
61
62 const lexerSrc string = `
63 // Code generated by gocc; DO NOT EDIT.
64
65 package lexer
66
67 import (
68 {{if .Debug}} "fmt"
69 {{end}} "os"
70 "unicode/utf8"
71
72 {{if .Debug}} "{{.UtilImport}}"
73 {{end}} "{{.TokenImport}}"
74 )
75
76 const (
77 NoState = -1
78 NumStates = {{.NumStates}}
79 NumSymbols = {{.NumSymbols}}
80 )
81
82 type Lexer struct {
83 src []byte
84 pos int
85 line int
86 column int
87 Context token.Context
88 }
89
90 func NewLexer(src []byte) *Lexer {
91 lexer := &Lexer{
92 src: src,
93 pos: 0,
94 line: 1,
95 column: 1,
96 Context: nil,
97 }
98 return lexer
99 }
100
101 // SourceContext is a simple instance of a token.Context which
102 // contains the name of the source file.
103 type SourceContext struct {
104 Filepath string
105 }
106
107 func (s *SourceContext) Source() string {
108 return s.Filepath
109 }
110
111 func NewLexerFile(fpath string) (*Lexer, error) {
112 src, err := os.ReadFile(fpath)
113 if err != nil {
114 return nil, err
115 }
116 lexer := NewLexer(src)
117 lexer.Context = &SourceContext{Filepath: fpath}
118 return lexer, nil
119 }
120
121 func (l *Lexer) Scan() (tok *token.Token) {
122 {{- if .Debug}}
123 fmt.Printf("Lexer.Scan() pos=%d\n", l.pos)
124 {{- end}}
125 tok = &token.Token{}
126 if l.pos >= len(l.src) {
127 tok.Type = token.EOF
128 tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = l.pos, l.line, l.column
129 tok.Pos.Context = l.Context
130 return
131 }
132 start, startLine, startColumn, end := l.pos, l.line, l.column, 0
133 tok.Type = token.INVALID
134 state, rune1, size := 0, rune(-1), 0
135 for state != -1 {
136 {{- if .Debug}}
137 fmt.Printf("\tpos=%d, line=%d, col=%d, state=%d\n", l.pos, l.line, l.column, state)
138 {{- end}}
139 if l.pos >= len(l.src) {
140 rune1 = -1
141 } else {
142 rune1, size = utf8.DecodeRune(l.src[l.pos:])
143 l.pos += size
144 }
145
146 nextState := -1
147 if rune1 != -1 {
148 nextState = TransTab[state](rune1)
149 }
150 {{- if .Debug}}
151 fmt.Printf("\tS%d, : tok=%s, rune == %s(%x), next state == %d\n", state, token.TokMap.Id(tok.Type), util.RuneToString(rune1), rune1, nextState)
152 fmt.Printf("\t\tpos=%d, size=%d, start=%d, end=%d\n", l.pos, size, start, end)
153 if nextState != -1 {
154 fmt.Printf("\t\taction:%s\n", ActTab[nextState].String())
155 }
156 {{- end}}
157 state = nextState
158
159 if state != -1 {
160
161 switch rune1 {
162 case '\n':
163 l.line++
164 l.column = 1
165 case '\r':
166 l.column = 1
167 case '\t':
168 l.column += 4
169 default:
170 l.column++
171 }
172
173 switch {
174 case ActTab[state].Accept != -1:
175 tok.Type = ActTab[state].Accept
176 end = l.pos
177 case ActTab[state].Ignore != "":
178 start, startLine, startColumn = l.pos, l.line, l.column
179 state = 0
180 if start >= len(l.src) {
181 tok.Type = token.EOF
182 }
183
184 }
185 } else {
186 if tok.Type == token.INVALID {
187 end = l.pos
188 }
189 }
190 }
191 if end > start {
192 l.pos = end
193 tok.Lit = l.src[start:end]
194 } else {
195 tok.Lit = []byte{}
196 }
197 tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn
198 tok.Pos.Context = l.Context
199
200 {{- if .Debug}}
201 fmt.Printf("Token at %s: %s \"%s\"\n", tok.String(), token.TokMap.Id(tok.Type), tok.Lit)
202 {{- end}}
203
204 return
205 }
206
207 func (l *Lexer) Reset() {
208 l.pos = 0
209 }
210
211 /*
212 Lexer symbols:
213 {{- range $i, $sym := .Symbols}}
214 {{- printf "\n%d: %s" $i $sym}}
215 {{- end}}
216 */
217 `