Merge branch 'master' into dev

This commit is contained in:
z7zmey
2018-11-05 17:14:09 +02:00
14 changed files with 2667 additions and 2446 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/z7zmey/php-parser/meta"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/scanner"
)
@@ -20,7 +21,6 @@ type Parser struct {
path string
currentToken *scanner.Token
positionBuilder *parser.PositionBuilder
errors []*errors.Error
rootNode node.Node
}
@@ -34,7 +34,6 @@ func NewParser(src io.Reader, path string) *Parser {
nil,
nil,
nil,
nil,
}
}
@@ -46,7 +45,14 @@ func (l *Parser) Lex(lval *yySymType) int {
}
func (l *Parser) Error(msg string) {
l.errors = append(l.errors, errors.NewError(msg, l.currentToken))
pos := &position.Position{
StartLine: l.currentToken.StartLine,
EndLine: l.currentToken.EndLine,
StartPos: l.currentToken.StartPos,
EndPos: l.currentToken.EndPos,
}
l.Lexer.Errors = append(l.Lexer.Errors, errors.NewError(msg, pos))
}
func (l *Parser) WithMeta() {
@@ -56,7 +62,7 @@ func (l *Parser) WithMeta() {
// Parse the php7 Parser entrypoint
func (l *Parser) Parse() int {
// init
l.errors = nil
l.Lexer.Errors = nil
l.rootNode = nil
l.positionBuilder = &parser.PositionBuilder{}
@@ -87,7 +93,7 @@ func (l *Parser) GetRootNode() node.Node {
// GetErrors returns errors list
func (l *Parser) GetErrors() []*errors.Error {
return l.errors
return l.Lexer.Errors
}
// helpers

View File

@@ -1,3 +1,5 @@
// Code generated by goyacc -o php5/php5.go php5/php5.y. DO NOT EDIT.
//line php5/php5.y:2
package php5
@@ -347,7 +349,6 @@ const yyErrCode = 2
const yyInitialStackSize = 16
//line php5/php5.y:6993
type simpleIndirectReference struct {
all []*expr.Variable
last *expr.Variable

View File

@@ -6,6 +6,7 @@ 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"
@@ -18708,3 +18709,23 @@ CAD;
actual := php5parser.GetRootNode()
assertEqual(t, expected, actual)
}
func TestPhp5ControlCharsErrors(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},
},
}
php5parser := php5.NewParser(bytes.NewBufferString(src), "test.php")
php5parser.Parse()
actual := php5parser.GetErrors()
assertEqual(t, expected, actual)
}