-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseutils.go
60 lines (50 loc) · 1.27 KB
/
parseutils.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
package main
import (
"bytes"
"errors"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/conf"
phperrors "github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/parser"
"github.com/z7zmey/php-parser/pkg/version"
)
func Parse(code []byte) (ast.Vertex, error) {
if bytes.HasPrefix(code, []byte("<?")) || bytes.HasPrefix(code, []byte("<?php")) {
return ParseCode(code)
}
return ParseStmtList(code)
}
func ParseCode(code []byte) (*ast.Root, error) {
phpVersion, err := version.New("7.4")
if err != nil {
return nil, err
}
code = append(code, []byte("\n;")...)
var parserErrors []*phperrors.Error
rootNode, err := parser.Parse(code, conf.Config{
Version: phpVersion,
ErrorHandlerFunc: func(e *phperrors.Error) {
parserErrors = append(parserErrors, e)
},
})
if err != nil {
return nil, err
}
if len(parserErrors) != 0 {
return nil, errors.New(parserErrors[0].String())
}
res := rootNode.(*ast.Root)
var stmtList []ast.Vertex
// 过滤空语句
for _, s := range res.Stmts {
if _, ok := s.(*ast.StmtNop); !ok {
stmtList = append(stmtList, s)
}
}
res.Stmts = stmtList
return res, nil
}
func ParseStmtList(code []byte) (*ast.Root, error) {
code = append([]byte("<?php "), code...)
return ParseCode(code)
}