php-parser/pkg/parser/parser.go

45 lines
1.0 KiB
Go
Raw Normal View History

package parser
2018-04-10 17:15:15 +00:00
import (
2020-12-29 19:23:22 +00:00
"errors"
"github.com/laytan/php-parser/internal/php7"
"github.com/laytan/php-parser/internal/php8"
"github.com/laytan/php-parser/pkg/ast"
"github.com/laytan/php-parser/pkg/conf"
"github.com/laytan/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
}