forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompile.go
209 lines (196 loc) · 5.27 KB
/
compile.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
/*
* 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 cl
import (
"fmt"
"strconv"
"github.com/goplus/gop/tpl/ast"
"github.com/goplus/gop/tpl/matcher"
"github.com/goplus/gop/tpl/token"
"github.com/qiniu/x/ctype"
"github.com/qiniu/x/errors"
)
var (
// ErrNoDocFound error
ErrNoDocFound = errors.New("no document rule found")
)
// Result represents the result of compiling a set of rules.
type Result struct {
Doc *matcher.Var
Rules map[string]*matcher.Var
}
type context struct {
rules map[string]*matcher.Var
errs errors.List
fset *token.FileSet
}
func (p *context) newErrorf(pos token.Pos, format string, args ...any) error {
return &matcher.Error{Fset: p.fset, Pos: pos, Msg: fmt.Sprintf(format, args...)}
}
func (p *context) addErrorf(pos token.Pos, format string, args ...any) {
p.errs.Add(p.newErrorf(pos, format, args...))
}
func (p *context) addError(pos token.Pos, msg string) {
p.errs.Add(&matcher.Error{Fset: p.fset, Pos: pos, Msg: msg})
}
// New compiles a set of rules from the given files.
func New(fset *token.FileSet, files ...*ast.File) (ret Result, err error) {
var doc *matcher.Var
rules := make(map[string]*matcher.Var)
ctx := &context{rules: rules, fset: fset}
for _, f := range files {
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.Rule:
ident := decl.Name
name := ident.Name
if old, ok := rules[name]; ok {
oldPos := fset.Position(old.Pos)
ctx.addErrorf(ident.Pos(),
"duplicate rule `%s`, previous declaration at %v", name, oldPos)
continue
}
v := matcher.NewVar(ident.Pos(), name)
rules[name] = v
if r, ok := compileExpr(decl.Expr, ctx); ok {
if e := v.Assign(r); e != nil {
ctx.addError(ident.Pos(), e.Error())
}
if doc == nil {
doc = v
}
}
default:
ctx.addError(decl.Pos(), "unknown declaration")
}
}
}
if doc == nil {
err = ErrNoDocFound
return
}
return Result{doc, rules}, ctx.errs.ToError()
}
var (
idents = map[string]token.Token{
"EOF": token.EOF,
"COMMENT": token.COMMENT,
"IDENT": token.IDENT,
"INT": token.INT,
"FLOAT": token.FLOAT,
"IMAG": token.IMAG,
"CHAR": token.CHAR,
"STRING": token.STRING,
}
)
func compileExpr(expr ast.Expr, ctx *context) (matcher.Matcher, bool) {
switch expr := expr.(type) {
case *ast.Ident:
name := expr.Name
if v, ok := ctx.rules[name]; ok {
return v, true
}
if tok, ok := idents[name]; ok {
return matcher.Token(tok), true
}
ctx.addErrorf(expr.Pos(), "`%s` is undefined", name)
case *ast.BasicLit:
lit := expr.Value
switch expr.Kind {
case token.CHAR:
v, multibyte, tail, e := strconv.UnquoteChar(lit[1:len(lit)-1], '\'')
if e != nil {
ctx.addErrorf(expr.Pos(), "invalid literal %s: %v", lit, e)
break
}
if tail != "" || multibyte {
ctx.addErrorf(expr.Pos(), "invalid literal %s", lit)
break
}
return tokenExpr(v, expr, ctx)
case token.STRING:
v, e := strconv.Unquote(lit)
if e != nil {
ctx.addErrorf(expr.Pos(), "invalid literal %s: %v", lit, e)
break
}
if len(v) == 1 {
return tokenExpr(rune(v[0]), expr, ctx)
}
if ctype.IsCSymbol(v) {
return matcher.Literal(token.IDENT, v), true
}
fallthrough
default:
ctx.addErrorf(expr.Pos(), "invalid literal %s", lit)
}
case *ast.Sequence:
items := make([]matcher.Matcher, len(expr.Items))
for i, item := range expr.Items {
if r, ok := compileExpr(item, ctx); ok {
items[i] = r
} else {
return nil, false
}
}
return matcher.Sequence(items...), true
case *ast.Choice:
options := make([]matcher.Matcher, len(expr.Options))
for i, option := range expr.Options {
if r, ok := compileExpr(option, ctx); ok {
options[i] = r
} else {
return nil, false
}
}
return matcher.Choice(options...), true
case *ast.UnaryExpr:
if x, ok := compileExpr(expr.X, ctx); ok {
switch expr.Op {
case token.QUESTION:
return matcher.Repeat01(x), true
case token.MUL:
return matcher.Repeat0(x), true
case token.ADD:
return matcher.Repeat1(x), true
default:
ctx.addErrorf(expr.Pos(), "invalid token %v", expr.Op)
}
}
case *ast.BinaryExpr:
x, ok1 := compileExpr(expr.X, ctx)
y, ok2 := compileExpr(expr.Y, ctx)
if ok1 && ok2 {
switch expr.Op {
case token.REM: // %
return matcher.List(x, y), true
default:
ctx.addErrorf(expr.Pos(), "invalid token %v", expr.Op)
}
}
default:
ctx.addError(expr.Pos(), "unknown expression")
}
return nil, false
}
func tokenExpr(v rune, expr *ast.BasicLit, ctx *context) (matcher.Matcher, bool) {
tok := token.Token(v)
if tok.Len() > 0 {
return matcher.Token(tok), true
}
ctx.addErrorf(expr.Pos(), "invalid token: %s", expr.Value)
return nil, false
}