Skip to content

Commit 65c7bc9

Browse files
committed
Add checker.ParseCheck func
1 parent cae6003 commit 65c7bc9

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

checker/checker.go

+39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@ import (
1313
"github.com/expr-lang/expr/parser"
1414
)
1515

16+
// ParseCheck parses input expression and checks its types. Also, it applies
17+
// all provided patchers. In case of error, it returns error with a tree.
18+
func ParseCheck(input string, config *conf.Config) (*parser.Tree, error) {
19+
tree, err := parser.ParseWithConfig(input, config)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
if len(config.Visitors) > 0 {
25+
for i := 0; i < 1000; i++ {
26+
more := false
27+
for _, v := range config.Visitors {
28+
// We need to perform types check, because some visitors may rely on
29+
// types information available in the tree.
30+
_, _ = Check(tree, config)
31+
32+
ast.Walk(&tree.Node, v)
33+
34+
if v, ok := v.(interface {
35+
ShouldRepeat() bool
36+
}); ok {
37+
more = more || v.ShouldRepeat()
38+
}
39+
}
40+
if !more {
41+
break
42+
}
43+
}
44+
}
45+
_, err = Check(tree, config)
46+
if err != nil {
47+
return tree, err
48+
}
49+
50+
return tree, nil
51+
}
52+
53+
// Check checks types of the expression tree. It returns type of the expression
54+
// and error if any. If config is nil, then default configuration will be used.
1655
func Check(tree *parser.Tree, config *conf.Config) (t reflect.Type, err error) {
1756
if config == nil {
1857
config = conf.New(nil)

expr.go

+1-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/expr-lang/expr/conf"
1414
"github.com/expr-lang/expr/file"
1515
"github.com/expr-lang/expr/optimizer"
16-
"github.com/expr-lang/expr/parser"
1716
"github.com/expr-lang/expr/patcher"
1817
"github.com/expr-lang/expr/vm"
1918
)
@@ -206,33 +205,7 @@ func Compile(input string, ops ...Option) (*vm.Program, error) {
206205
}
207206
config.Check()
208207

209-
tree, err := parser.ParseWithConfig(input, config)
210-
if err != nil {
211-
return nil, err
212-
}
213-
214-
if len(config.Visitors) > 0 {
215-
for i := 0; i < 1000; i++ {
216-
more := false
217-
for _, v := range config.Visitors {
218-
// We need to perform types check, because some visitors may rely on
219-
// types information available in the tree.
220-
_, _ = checker.Check(tree, config)
221-
222-
ast.Walk(&tree.Node, v)
223-
224-
if v, ok := v.(interface {
225-
ShouldRepeat() bool
226-
}); ok {
227-
more = more || v.ShouldRepeat()
228-
}
229-
}
230-
if !more {
231-
break
232-
}
233-
}
234-
}
235-
_, err = checker.Check(tree, config)
208+
tree, err := checker.ParseCheck(input, config)
236209
if err != nil {
237210
return nil, err
238211
}

0 commit comments

Comments
 (0)