php-parser/pkg/parser/parser.go

44 lines
915 B
Go
Raw Normal View History

package parser
2018-04-10 17:15:15 +00:00
import (
2020-05-12 21:16:36 +00:00
"github.com/z7zmey/php-parser/internal/php5"
"github.com/z7zmey/php-parser/internal/php7"
2020-06-29 20:00:56 +00:00
"github.com/z7zmey/php-parser/internal/scanner"
2020-05-12 21:16:36 +00:00
"github.com/z7zmey/php-parser/internal/version"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors"
2018-04-10 17:15:15 +00:00
)
// Parser interface
2018-04-10 17:15:15 +00:00
type Parser interface {
Parse() int
2020-05-12 21:16:36 +00:00
GetRootNode() ast.Vertex
2018-04-10 17:15:15 +00:00
}
2019-12-26 15:57:56 +00:00
2020-06-29 20:00:56 +00:00
type Config struct {
WithTokens bool
WithPositions bool
ErrorHandlerFunc func(e *errors.Error)
}
func Parse(src []byte, ver string, cfg Config) (ast.Vertex, error) {
2019-12-26 15:57:56 +00:00
var parser Parser
2020-06-29 20:00:56 +00:00
r, err := version.Compare(ver, "7.0")
2019-12-26 15:57:56 +00:00
if err != nil {
return nil, err
}
2020-08-17 17:31:04 +00:00
lexer := scanner.NewLexer(src, ver, cfg.ErrorHandlerFunc)
2020-06-29 20:00:56 +00:00
2019-12-26 15:57:56 +00:00
if r == -1 {
parser = php5.NewParser(lexer, cfg.ErrorHandlerFunc)
2019-12-26 15:57:56 +00:00
} else {
parser = php7.NewParser(lexer, cfg.ErrorHandlerFunc)
2019-12-26 15:57:56 +00:00
}
2020-06-29 20:00:56 +00:00
parser.Parse()
return parser.GetRootNode(), nil
2019-12-26 15:57:56 +00:00
}