error tolerant parsing
This commit is contained in:
@@ -7,16 +7,26 @@ import (
|
||||
|
||||
"github.com/cznic/golex/lex"
|
||||
|
||||
"github.com/z7zmey/php-parser/errors"
|
||||
"github.com/z7zmey/php-parser/scanner"
|
||||
"github.com/z7zmey/php-parser/token"
|
||||
)
|
||||
|
||||
type lexer struct {
|
||||
scanner.Lexer
|
||||
lastToken *token.Token
|
||||
errors []*errors.Error
|
||||
}
|
||||
|
||||
func (l *lexer) Lex(lval *yySymType) int {
|
||||
return l.Lexer.Lex(lval)
|
||||
t := l.Lexer.Lex(lval)
|
||||
l.lastToken = &lval.token
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (l *lexer) Error(msg string) {
|
||||
l.errors = append(l.errors, errors.NewError(msg, *l.lastToken))
|
||||
}
|
||||
|
||||
func (lval *yySymType) Token(t token.Token) {
|
||||
@@ -30,12 +40,16 @@ func newLexer(src io.Reader, fName string) *lexer {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
scanner := scanner.Lexer{
|
||||
Lexer: lx,
|
||||
StateStack: []int{0},
|
||||
PhpDocComment: "",
|
||||
Comments: nil,
|
||||
}
|
||||
|
||||
return &lexer{
|
||||
scanner.Lexer{
|
||||
Lexer: lx,
|
||||
StateStack: []int{0},
|
||||
PhpDocComment: "",
|
||||
Comments: nil,
|
||||
},
|
||||
scanner,
|
||||
nil,
|
||||
nil,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package php5
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/z7zmey/php-parser/errors"
|
||||
"github.com/z7zmey/php-parser/node/expr"
|
||||
|
||||
"github.com/z7zmey/php-parser/comment"
|
||||
@@ -21,15 +22,17 @@ var positionBuilder position.Builder
|
||||
var parentNode node.Node
|
||||
|
||||
// Parse the php5 parser entrypoint
|
||||
func Parse(src io.Reader, fName string) (node.Node, comment.Comments, position.Positions) {
|
||||
func Parse(src io.Reader, fName string) (node.Node, comment.Comments, position.Positions, []*errors.Error) {
|
||||
yyDebug = 0
|
||||
yyErrorVerbose = true
|
||||
rootnode = stmt.NewStmtList([]node.Node{}) //reset
|
||||
comments = comment.Comments{}
|
||||
positions = position.Positions{}
|
||||
positionBuilder = position.Builder{Positions: &positions}
|
||||
yyParse(newLexer(src, fName))
|
||||
return rootnode, comments, positions
|
||||
|
||||
lexer := newLexer(src, fName)
|
||||
yyParse(lexer)
|
||||
return rootnode, comments, positions, lexer.errors
|
||||
}
|
||||
|
||||
// ListGetFirstNodeComments returns comments of a first node in the list
|
||||
|
||||
5606
php5/php5.go
5606
php5/php5.go
File diff suppressed because it is too large
Load Diff
27
php5/php5.y
27
php5/php5.y
@@ -239,7 +239,12 @@ start:
|
||||
;
|
||||
|
||||
top_statement_list:
|
||||
top_statement_list top_statement { $$ = append($1, $2) }
|
||||
top_statement_list top_statement
|
||||
{
|
||||
if $2 != nil {
|
||||
$$ = append($1, $2)
|
||||
}
|
||||
}
|
||||
| /* empty */ { $$ = []node.Node{} }
|
||||
;
|
||||
|
||||
@@ -261,7 +266,12 @@ namespace_name:
|
||||
;
|
||||
|
||||
top_statement:
|
||||
statement
|
||||
error
|
||||
{
|
||||
// error
|
||||
$$ = nil
|
||||
}
|
||||
| statement
|
||||
{ $$ = $1 }
|
||||
| function_declaration_statement
|
||||
{ $$ = $1 }
|
||||
@@ -534,14 +544,23 @@ constant_declaration:
|
||||
|
||||
inner_statement_list:
|
||||
inner_statement_list inner_statement
|
||||
{ $$ = append($1, $2) }
|
||||
{
|
||||
if $2 != nil {
|
||||
$$ = append($1, $2)
|
||||
}
|
||||
}
|
||||
| /* empty */
|
||||
{ $$ = []node.Node{} }
|
||||
;
|
||||
|
||||
|
||||
inner_statement:
|
||||
statement
|
||||
error
|
||||
{
|
||||
// error
|
||||
$$ = nil
|
||||
}
|
||||
| statement
|
||||
{ $$ = $1 }
|
||||
| function_declaration_statement
|
||||
{ $$ = $1 }
|
||||
|
||||
Reference in New Issue
Block a user