[refactoring] parsing error handler

This commit is contained in:
Vadym Slizov
2020-06-29 23:00:56 +03:00
parent 424f7a132c
commit d7652b1c7f
12 changed files with 1191 additions and 631 deletions

View File

@@ -3,6 +3,7 @@ package parser
import (
"github.com/z7zmey/php-parser/internal/php5"
"github.com/z7zmey/php-parser/internal/php7"
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/internal/version"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors"
@@ -12,22 +13,31 @@ import (
type Parser interface {
Parse() int
GetRootNode() ast.Vertex
GetErrors() []*errors.Error
}
func NewParser(src []byte, v string, withTokens bool) (Parser, error) {
type Config struct {
WithTokens bool
WithPositions bool
ErrorHandlerFunc func(e *errors.Error)
}
func Parse(src []byte, ver string, cfg Config) (ast.Vertex, error) {
var parser Parser
r, err := version.Compare(v, "7.0")
r, err := version.Compare(ver, "7.0")
if err != nil {
return nil, err
}
lexer := scanner.NewLexer(src, ver, cfg.WithTokens, cfg.ErrorHandlerFunc)
if r == -1 {
parser = php5.NewParser(src, v, withTokens)
parser = php5.NewParser(lexer, cfg.WithTokens, cfg.ErrorHandlerFunc)
} else {
parser = php7.NewParser(src, v, withTokens)
parser = php7.NewParser(lexer, cfg.WithTokens, cfg.ErrorHandlerFunc)
}
return parser, nil
parser.Parse()
return parser.GetRootNode(), nil
}

View File

@@ -2,15 +2,18 @@ package printer_test
import (
"bytes"
"github.com/z7zmey/php-parser/pkg/ast"
"testing"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/internal/php5"
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/printer"
)
func parsePhp5(src string) ast.Vertex {
php5parser := php5.NewParser([]byte(src), "5.6", true)
lexer := scanner.NewLexer([]byte(src), "5.6", true, nil)
php5parser := php5.NewParser(lexer, true, nil)
php5parser.Parse()
return php5parser.GetRootNode()

View File

@@ -2,11 +2,13 @@ package printer_test
import (
"bytes"
"github.com/z7zmey/php-parser/pkg/ast"
"os"
"testing"
"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/internal/php7"
"github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/printer"
)
@@ -27,7 +29,8 @@ abstract class Bar extends Baz
// parse
php7parser := php7.NewParser([]byte(src), "7.4", true)
lexer := scanner.NewLexer([]byte(src), "7.4", true, nil)
php7parser := php7.NewParser(lexer, true, nil)
php7parser.Parse()
rootNode := php7parser.GetRootNode()
@@ -58,7 +61,8 @@ abstract class Bar extends Baz
}
func parse(src string) ast.Vertex {
php7parser := php7.NewParser([]byte(src), "7.4", true)
lexer := scanner.NewLexer([]byte(src), "7.4", true, nil)
php7parser := php7.NewParser(lexer, true, nil)
php7parser.Parse()
return php7parser.GetRootNode()