-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
43 lines (36 loc) · 965 Bytes
/
parse.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
package main
import (
"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 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)
stmtLast := res.Stmts[len(res.Stmts)-1]
// 处理最后一个空 stmt
if _, ok := stmtLast.(*ast.StmtNop); ok {
res.Stmts = res.Stmts[:len(res.Stmts)-1]
}
return res, nil
}