#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

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/scanner"
)
// Error parsing error
@@ -14,13 +13,18 @@ type Error struct {
}
// NewError creates and returns new Error
func NewError(msg string, t *scanner.Token) *Error {
func NewError(msg string, p *position.Position) *Error {
return &Error{
Msg: msg,
Pos: t.Position,
Pos: p,
}
}
func (e *Error) String() string {
return fmt.Sprintf("%s at line %d", e.Msg, e.Pos.StartLine)
atLine := ""
if e.Pos != nil {
atLine = fmt.Sprintf(" at line %d", e.Pos.StartLine)
}
return fmt.Sprintf("%s%s", e.Msg, atLine)
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/scanner"
"github.com/kylelemons/godebug/pretty"
)
@@ -27,12 +26,8 @@ func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
func TestConstructor(t *testing.T) {
pos := position.NewPosition(1, 2, 3, 4)
token := &scanner.Token{
Value: `test`,
Position: pos,
}
actual := errors.NewError("message", token)
actual := errors.NewError("message", pos)
expected := &errors.Error{
Msg: "message",
@@ -44,12 +39,8 @@ func TestConstructor(t *testing.T) {
func TestPrint(t *testing.T) {
pos := position.NewPosition(1, 2, 3, 4)
token := &scanner.Token{
Value: `test`,
Position: pos,
}
Error := errors.NewError("message", token)
Error := errors.NewError("message", pos)
actual := Error.String()
@@ -57,3 +48,13 @@ func TestPrint(t *testing.T) {
assertEqual(t, expected, actual)
}
func TestPrintWithotPos(t *testing.T) {
Error := errors.NewError("message", nil)
actual := Error.String()
expected := "message"
assertEqual(t, expected, actual)
}