#67: skip unexpected character in input

This commit is contained in:
z7zmey
2018-11-05 16:56:27 +02:00
parent 4133a65afe
commit 69e3111221
12 changed files with 2633 additions and 2433 deletions

View File

@@ -19,7 +19,6 @@ type Parser struct {
path string
currentToken *scanner.Token
positionBuilder *parser.PositionBuilder
errors []*errors.Error
rootNode node.Node
comments parser.Comments
positions parser.Positions
@@ -37,7 +36,6 @@ func NewParser(src io.Reader, path string) *Parser {
nil,
nil,
nil,
nil,
}
}
@@ -49,13 +47,13 @@ func (l *Parser) Lex(lval *yySymType) int {
}
func (l *Parser) Error(msg string) {
l.errors = append(l.errors, errors.NewError(msg, l.currentToken))
l.Lexer.Errors = append(l.Lexer.Errors, errors.NewError(msg, l.currentToken.Position))
}
// Parse the php7 Parser entrypoint
func (l *Parser) Parse() int {
// init
l.errors = nil
l.Lexer.Errors = nil
l.rootNode = nil
l.comments = parser.Comments{}
l.positions = parser.Positions{}
@@ -81,7 +79,7 @@ func (l *Parser) GetRootNode() node.Node {
// GetErrors returns errors list
func (l *Parser) GetErrors() []*errors.Error {
return l.errors
return l.Lexer.Errors
}
// GetComments returns comments list

View File

@@ -1,3 +1,5 @@
// Code generated by goyacc -o php7/php7.go php7/php7.y. DO NOT EDIT.
//line php7/php7.y:2
package php7

View File

@@ -6,12 +6,14 @@ import (
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/node/expr"
"github.com/z7zmey/php-parser/node/expr/assign"
"github.com/z7zmey/php-parser/node/expr/binary"
"github.com/z7zmey/php-parser/node/expr/cast"
"github.com/z7zmey/php-parser/node/name"
"github.com/z7zmey/php-parser/node/scalar"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/node/stmt"
@@ -3336,3 +3338,23 @@ CAD;
actual := php7parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestPhp7ControlCharsErrors(t *testing.T) {
src := "<?php \004 echo $b; \"$a[\005test]\";"
expected := []*errors.Error{
{
Msg: "WARNING: Unexpected character in input: '\004' (ASCII=4)",
Pos: &position.Position{1, 1, 7, 7},
},
{
Msg: "WARNING: Unexpected character in input: '\005' (ASCII=5)",
Pos: &position.Position{1, 1, 22, 22},
},
}
php7parser := php7.NewParser(bytes.NewBufferString(src), "test.php")
php7parser.Parse()
actual := php7parser.GetErrors()
assertEqual(t, expected, actual)
}