php-parser/pkg/parser/parser.go

45 lines
1.0 KiB
Go
Raw Permalink Normal View History

package parser
2018-04-10 17:15:15 +00:00
import (
2020-12-29 19:23:22 +00:00
"errors"
2023-12-09 21:36:19 +00:00
"git.maride.cc/maride/php-parser/internal/php7"
"git.maride.cc/maride/php-parser/internal/php8"
"git.maride.cc/maride/php-parser/pkg/ast"
"git.maride.cc/maride/php-parser/pkg/conf"
"git.maride.cc/maride/php-parser/pkg/version"
2020-12-29 19:23:22 +00:00
)
// ErrVersionOutOfRange is returned if the version is not supported
var ErrVersionOutOfRange = errors.New("the version is out of supported range")
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
2021-02-13 21:54:34 +00:00
func Parse(src []byte, config conf.Config) (ast.Vertex, error) {
2019-12-26 15:57:56 +00:00
var parser Parser
2020-12-29 19:23:22 +00:00
if config.Version == nil {
config.Version = &version.Version{Major: 7, Minor: 4}
2019-12-26 15:57:56 +00:00
}
if config.Version.InPhp7Range() {
2023-03-25 14:00:17 +00:00
lexer := php7.NewLexer(src, config)
2020-12-29 19:23:22 +00:00
parser = php7.NewParser(lexer, config)
parser.Parse()
return parser.GetRootNode(), nil
}
2020-06-29 20:00:56 +00:00
if config.Version.InPhp8Range() {
lexer := php8.NewLexer(src, config)
parser = php8.NewParser(lexer, config)
parser.Parse()
return parser.GetRootNode(), nil
}
2020-12-29 19:23:22 +00:00
return nil, ErrVersionOutOfRange
2019-12-26 15:57:56 +00:00
}