[refactoring] parsing error handler
This commit is contained in:
@@ -12,25 +12,20 @@ import (
|
||||
|
||||
// Parser structure
|
||||
type Parser struct {
|
||||
Lexer *scanner.Lexer
|
||||
currentToken *scanner.Token
|
||||
rootNode ast.Vertex
|
||||
errors []*errors.Error
|
||||
withTokens bool
|
||||
Lexer *scanner.Lexer
|
||||
currentToken *scanner.Token
|
||||
rootNode ast.Vertex
|
||||
withTokens bool
|
||||
errHandlerFunc func(*errors.Error)
|
||||
}
|
||||
|
||||
// NewParser creates and returns new Parser
|
||||
func NewParser(src []byte, v string, withTokens bool) *Parser {
|
||||
parser := &Parser{
|
||||
withTokens: withTokens,
|
||||
func NewParser(lexer *scanner.Lexer, withTokens bool, errHandlerFunc func(*errors.Error)) *Parser {
|
||||
return &Parser{
|
||||
withTokens: withTokens,
|
||||
Lexer: lexer,
|
||||
errHandlerFunc: errHandlerFunc,
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(src, v, withTokens, func(e *errors.Error) {
|
||||
parser.errors = append(parser.errors, e)
|
||||
})
|
||||
parser.Lexer = lexer
|
||||
|
||||
return parser
|
||||
}
|
||||
|
||||
// Lex proxy to scanner Lex
|
||||
@@ -45,23 +40,12 @@ func (p *Parser) Lex(lval *yySymType) int {
|
||||
|
||||
func (p *Parser) Error(msg string) {
|
||||
var pos = p.currentToken.Position
|
||||
|
||||
p.errors = append(p.errors, errors.NewError(msg, &pos))
|
||||
}
|
||||
|
||||
// GetErrors returns errors list
|
||||
func (p *Parser) GetErrors() []*errors.Error {
|
||||
return p.errors
|
||||
p.errHandlerFunc(errors.NewError(msg, &pos))
|
||||
}
|
||||
|
||||
// Parse the php7 Parser entrypoint
|
||||
func (p *Parser) Parse() int {
|
||||
// init
|
||||
p.errors = nil
|
||||
p.rootNode = nil
|
||||
|
||||
// parse
|
||||
|
||||
return yyParse(p)
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/internal/php5"
|
||||
"github.com/z7zmey/php-parser/internal/scanner"
|
||||
)
|
||||
|
||||
func BenchmarkPhp5(b *testing.B) {
|
||||
@@ -413,7 +414,8 @@ CAD;
|
||||
`
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
php5parser := php5.NewParser([]byte(src), "5.6", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
|
||||
php5parser := php5.NewParser(lexer, false, nil)
|
||||
php5parser.Parse()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package php5_test
|
||||
|
||||
import (
|
||||
"gotest.tools/assert"
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
|
||||
"github.com/z7zmey/php-parser/internal/php5"
|
||||
"github.com/z7zmey/php-parser/internal/scanner"
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/errors"
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
@@ -22455,7 +22457,8 @@ func TestPhp5(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
php5parser := php5.NewParser([]byte(src), "5.6", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
|
||||
php5parser := php5.NewParser(lexer, false, nil)
|
||||
php5parser.Parse()
|
||||
actual := php5parser.GetRootNode()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
@@ -22592,7 +22595,8 @@ func TestPhp5Strings(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
php5parser := php5.NewParser([]byte(src), "5.6", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
|
||||
php5parser := php5.NewParser(lexer, false, nil)
|
||||
php5parser.Parse()
|
||||
actual := php5parser.GetRootNode()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
@@ -22818,7 +22822,8 @@ CAD;
|
||||
},
|
||||
}
|
||||
|
||||
php5parser := php5.NewParser([]byte(src), "5.6", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
|
||||
php5parser := php5.NewParser(lexer, false, nil)
|
||||
php5parser.Parse()
|
||||
actual := php5parser.GetRootNode()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
@@ -22838,8 +22843,13 @@ func TestPhp5ControlCharsErrors(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
php5parser := php5.NewParser([]byte(src), "5.6", false)
|
||||
parserErrors := []*errors.Error{}
|
||||
errorHandlerFunc := func(e *errors.Error) {
|
||||
parserErrors = append(parserErrors, e)
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer([]byte(src), "5.6", false, errorHandlerFunc)
|
||||
php5parser := php5.NewParser(lexer, false, errorHandlerFunc)
|
||||
php5parser.Parse()
|
||||
actual := php5parser.GetErrors()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
assert.DeepEqual(t, expected, parserErrors)
|
||||
}
|
||||
|
||||
@@ -11,25 +11,20 @@ import (
|
||||
|
||||
// Parser structure
|
||||
type Parser struct {
|
||||
Lexer *scanner.Lexer
|
||||
currentToken *scanner.Token
|
||||
rootNode ast.Vertex
|
||||
errors []*errors.Error
|
||||
withTokens bool
|
||||
Lexer *scanner.Lexer
|
||||
currentToken *scanner.Token
|
||||
rootNode ast.Vertex
|
||||
withTokens bool
|
||||
errHandlerFunc func(*errors.Error)
|
||||
}
|
||||
|
||||
// NewParser creates and returns new Parser
|
||||
func NewParser(src []byte, v string, withTokens bool) *Parser {
|
||||
parser := &Parser{
|
||||
withTokens: withTokens,
|
||||
func NewParser(lexer *scanner.Lexer, withTokens bool, errHandlerFunc func(*errors.Error)) *Parser {
|
||||
return &Parser{
|
||||
withTokens: withTokens,
|
||||
Lexer: lexer,
|
||||
errHandlerFunc: errHandlerFunc,
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer(src, v, withTokens, func(e *errors.Error) {
|
||||
parser.errors = append(parser.errors, e)
|
||||
})
|
||||
parser.Lexer = lexer
|
||||
|
||||
return parser
|
||||
}
|
||||
|
||||
func (p *Parser) Lex(lval *yySymType) int {
|
||||
@@ -43,23 +38,13 @@ func (p *Parser) Lex(lval *yySymType) int {
|
||||
|
||||
func (p *Parser) Error(msg string) {
|
||||
var pos = p.currentToken.Position
|
||||
|
||||
p.errors = append(p.errors, errors.NewError(msg, &pos))
|
||||
}
|
||||
|
||||
// GetErrors returns errors list
|
||||
func (p *Parser) GetErrors() []*errors.Error {
|
||||
return p.errors
|
||||
p.errHandlerFunc(errors.NewError(msg, &pos))
|
||||
}
|
||||
|
||||
// Parse the php7 Parser entrypoint
|
||||
func (p *Parser) Parse() int {
|
||||
// init
|
||||
p.errors = nil
|
||||
p.rootNode = nil
|
||||
|
||||
// parse
|
||||
|
||||
return yyParse(p)
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/internal/php7"
|
||||
"github.com/z7zmey/php-parser/internal/scanner"
|
||||
)
|
||||
|
||||
func BenchmarkPhp7(b *testing.B) {
|
||||
@@ -381,7 +382,8 @@ CAD;
|
||||
`
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
php7parser := php7.NewParser([]byte(src), "7.4", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
|
||||
php7parser := php7.NewParser(lexer, false, nil)
|
||||
php7parser.Parse()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"gotest.tools/assert"
|
||||
|
||||
"github.com/z7zmey/php-parser/internal/php7"
|
||||
"github.com/z7zmey/php-parser/internal/scanner"
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/errors"
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
@@ -19633,7 +19634,8 @@ func TestPhp7(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
php7parser := php7.NewParser([]byte(src), "7.4", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
|
||||
php7parser := php7.NewParser(lexer, false, nil)
|
||||
php7parser.Parse()
|
||||
actual := php7parser.GetRootNode()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
@@ -19770,7 +19772,8 @@ func TestPhp5Strings(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
php7parser := php7.NewParser([]byte(src), "7.4", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
|
||||
php7parser := php7.NewParser(lexer, false, nil)
|
||||
php7parser.Parse()
|
||||
actual := php7parser.GetRootNode()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
@@ -19996,7 +19999,8 @@ CAD;
|
||||
},
|
||||
}
|
||||
|
||||
php7parser := php7.NewParser([]byte(src), "7.4", false)
|
||||
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
|
||||
php7parser := php7.NewParser(lexer, false, nil)
|
||||
php7parser.Parse()
|
||||
actual := php7parser.GetRootNode()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
@@ -20016,8 +20020,13 @@ func TestPhp7ControlCharsErrors(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
php7parser := php7.NewParser([]byte(src), "7.4", false)
|
||||
parserErrors := []*errors.Error{}
|
||||
errorHandlerFunc := func(e *errors.Error) {
|
||||
parserErrors = append(parserErrors, e)
|
||||
}
|
||||
|
||||
lexer := scanner.NewLexer([]byte(src), "7.4", false, errorHandlerFunc)
|
||||
php7parser := php7.NewParser(lexer, false, errorHandlerFunc)
|
||||
php7parser.Parse()
|
||||
actual := php7parser.GetErrors()
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
assert.DeepEqual(t, expected, parserErrors)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user