forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.go
272 lines (239 loc) · 5.79 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parser
import (
"github.com/goplus/gop/parser/iox"
"github.com/goplus/gop/tpl/ast"
"github.com/goplus/gop/tpl/scanner"
"github.com/goplus/gop/tpl/token"
)
// -----------------------------------------------------------------------------
// A Mode value is a set of flags (or 0).
// They control the amount of source code parsed and other optional
// parser functionality.
type Mode uint
// ParseFile parses a file and returns the AST.
func ParseFile(fset *token.FileSet, filename string, src any, _ Mode) (f *ast.File, err error) {
b, err := iox.ReadSourceLocal(filename, src)
if err != nil {
return nil, err
}
var p parser
p.init(fset, filename, b)
f = p.parseFile()
switch p.errors.Len() {
case 0:
case 1:
err = p.errors[0]
default:
p.errors.Sort()
err = p.errors
}
return
}
// -----------------------------------------------------------------------------
// parser represents a parser.
type parser struct {
scanner scanner.Scanner
file *token.File
// Current token
pos token.Pos
tok token.Token
lit string
// Error handling
errors scanner.ErrorList
}
func (p *parser) init(fset *token.FileSet, filename string, src []byte) {
p.file = fset.AddFile(filename, -1, len(src))
eh := func(pos token.Position, msg string) { p.errors.Add(pos, msg) }
p.scanner.Init(p.file, src, eh, 0)
p.next() // initialize first token
}
// next advances to the next token.
func (p *parser) next() {
t := p.scanner.Scan()
p.pos, p.tok, p.lit = t.Pos, t.Tok, t.Lit
}
func (p *parser) errorExpected(pos token.Pos, msg string) {
msg = "expected " + msg
if pos == p.pos {
// the error happened at the current position;
// make the error message more specific
switch {
case p.tok == token.SEMICOLON && p.lit == "\n":
msg += ", found newline"
case len(p.lit) > 0:
// print 123 rather than 'INT', etc.
msg += ", found " + p.lit
default:
msg += ", found '" + p.tok.String() + "'"
}
}
p.error(pos, msg)
}
// expect consumes the current token if it matches the expected token.
// If not, it adds an error.
func (p *parser) expect(tok token.Token) token.Pos {
pos := p.pos
if p.tok != tok {
p.errorExpected(pos, "'"+tok.String()+"'")
}
p.next() // make progress
return pos
}
func (p *parser) error(pos token.Pos, msg string) {
epos := p.file.Position(pos)
p.errors.Add(epos, msg)
}
// parseFile parses a file and returns the AST.
func (p *parser) parseFile() *ast.File {
file := &ast.File{
FileStart: p.pos,
}
for p.tok != token.EOF {
rule := p.parseRule()
file.Decls = append(file.Decls, rule)
}
return file
}
func (p *parser) parseIdent() *ast.Ident {
pos := p.pos
name := "_"
if p.tok == token.IDENT {
name = p.lit
p.next()
} else {
p.errorExpected(p.pos, "'IDENT'")
}
return &ast.Ident{NamePos: pos, Name: name}
}
// parseRule parses a rule: IDENT '=' expr ';'
func (p *parser) parseRule() *ast.Rule {
if p.tok != token.IDENT {
p.errorExpected(p.pos, "'IDENT'")
return nil
}
name := p.parseIdent()
tokPos := p.expect(token.ASSIGN)
expr := p.parseExpr()
if expr == nil {
return nil
}
p.expect(token.SEMICOLON)
return &ast.Rule{
Name: name,
TokPos: tokPos,
Expr: expr,
}
}
// parseExpr parses an expression: +term % '|'
func (p *parser) parseExpr() ast.Expr {
termList := p.parseTermList()
for p.tok != token.OR {
return termList
}
options := make([]ast.Expr, 0, 4)
options = append(options, termList)
for p.tok == token.OR {
p.next()
termList = p.parseTermList()
options = append(options, termList)
}
return &ast.Choice{Options: options}
}
// parseTermList parses a list of terms: +term
func (p *parser) parseTermList() ast.Expr {
terms := make([]ast.Expr, 0, 1)
for {
term, ok := p.parseTerm()
if !ok {
break
}
terms = append(terms, term)
}
switch n := len(terms); n {
case 1:
return terms[0]
case 0:
p.error(p.pos, "expected factor")
fallthrough // TODO(xsw): BadExpr
default:
return &ast.Sequence{Items: terms}
}
}
// parseTerm parses a term: factor % '%'
func (p *parser) parseTerm() (ast.Expr, bool) {
x, ok := p.parseFactor()
if !ok {
return nil, false
}
for p.tok == token.REM {
opPos := p.pos
p.next()
y, ok := p.parseFactor()
if !ok {
p.error(p.pos, "expected factor")
}
x = &ast.BinaryExpr{
X: x,
OpPos: opPos,
Op: token.REM,
Y: y,
}
}
return x, true
}
// parseFactor: IDENT | CHAR | STRING | ('*' | '+' | '?') factor | '(' expr ')'
func (p *parser) parseFactor() (ast.Expr, bool) {
switch tok := p.tok; tok {
case token.IDENT:
ident := &ast.Ident{
NamePos: p.pos,
Name: p.lit,
}
p.next()
return ident, true
case token.CHAR, token.STRING:
lit := &ast.BasicLit{
ValuePos: p.pos,
Kind: tok,
Value: p.lit,
}
p.next()
return lit, true
case token.MUL, token.ADD, token.QUESTION:
opPos := p.pos
p.next()
factor, ok := p.parseFactor()
if !ok {
p.error(p.pos, "expected factor")
}
ret := &ast.UnaryExpr{
OpPos: opPos,
Op: tok,
X: factor,
}
return ret, true
case token.LPAREN:
p.next()
expr := p.parseExpr()
p.expect(token.RPAREN)
return expr, true
default:
return nil, false
}
}
// -----------------------------------------------------------------------------