issue #2 - fix single line comment

fix handling end of file
add handling php close tag
This commit is contained in:
z7zmey 2018-03-30 17:15:26 +03:00
parent d31819db30
commit 148cf59e9d
4 changed files with 2525 additions and 2379 deletions

View File

@ -346,3 +346,17 @@ func TestPhp5ParameterNode(t *testing.T) {
actual, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php") actual, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
} }
func TestCommentEndFile(t *testing.T) {
src := `<? //comment at the end)`
expected := &stmt.StmtList{
Stmts: []node.Node{},
}
actual, _, _ := php7.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
actual, _, _ = php5.Parse(bytes.NewBufferString(src), "test.php")
assertEqual(t, expected, actual)
}

File diff suppressed because it is too large Load Diff

View File

@ -251,7 +251,48 @@ NEW_LINE (\r|\n|\r\n)
<PHP>\<\< lval.Token(l.newToken(l.Token())); return T_SL <PHP>\<\< lval.Token(l.newToken(l.Token())); return T_SL
<PHP>\>\> lval.Token(l.newToken(l.Token())); return T_SR <PHP>\>\> lval.Token(l.newToken(l.Token())); return T_SR
<PHP>\?\? lval.Token(l.newToken(l.Token())); return T_COALESCE <PHP>\?\? lval.Token(l.newToken(l.Token())); return T_COALESCE
<PHP>(#|[/][/]).*{NEW_LINE} lval.Token(l.newToken(l.Token())); l.addComment(comment.NewPlainComment(string(l.TokenBytes(nil))))// return T_COMMENT; // TODO: handle ?> <PHP>(#|[/][/])
tb := []rune{}
for _, chr := range(l.Token()) {
tb = append(tb, chr.Rune)
}
for {
if c == -1 {
break
}
tb = append(tb, rune(c))
switch c {
case '\r':
c = l.Next()
if c == '\n' {
continue
}
case '\n':
case '?':
c = l.Next()
if c == '>' {
l.ungetChars(1)
tb = tb[:len(tb)-1]
break
}
continue
default:
c = l.Next()
continue
}
break;
}
l.addComment(comment.NewPlainComment(string(tb)))
<PHP>([/][*])|([/][*][*]) <PHP>([/][*])|([/][*][*])
tb := l.Token() tb := l.Token()
is_doc_comment := false is_doc_comment := false

View File

@ -5,6 +5,8 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/scanner" "github.com/z7zmey/php-parser/scanner"
"github.com/z7zmey/php-parser/token" "github.com/z7zmey/php-parser/token"
@ -693,3 +695,89 @@ func TestSlashAfterVariable(t *testing.T) {
assertEqual(t, expected, actual) assertEqual(t, expected, actual)
assertEqual(t, expectedTokens, actualTokens) assertEqual(t, expectedTokens, actualTokens)
} }
func TestCommentEnd(t *testing.T) {
src := `<?php //test`
expected := []comment.Comment{
comment.NewPlainComment("//test"),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
lv := &lval{}
lexer.Lex(lv)
actual := lexer.Comments
assertEqual(t, expected, actual)
}
func TestCommentNewLine(t *testing.T) {
src := "<?php //test\n$a"
expected := []comment.Comment{
comment.NewPlainComment("//test\n"),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
lv := &lval{}
lexer.Lex(lv)
actual := lexer.Comments
assertEqual(t, expected, actual)
}
func TestCommentNewLine1(t *testing.T) {
src := "<?php //test\r$a"
expected := []comment.Comment{
comment.NewPlainComment("//test\r"),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
lv := &lval{}
lexer.Lex(lv)
actual := lexer.Comments
assertEqual(t, expected, actual)
}
func TestCommentNewLine2(t *testing.T) {
src := "<?php #test\r\n$a"
expected := []comment.Comment{
comment.NewPlainComment("#test\r\n"),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
lv := &lval{}
lexer.Lex(lv)
actual := lexer.Comments
assertEqual(t, expected, actual)
}
func TestCommentWithPhpEndTag(t *testing.T) {
src := `<?php
//test?> test`
expected := []comment.Comment{
comment.NewPlainComment("//test"),
}
lexer := scanner.NewLexer(bytes.NewBufferString(src), "test.php")
lv := &lval{}
lexer.Lex(lv)
actual := lexer.Comments
assertEqual(t, expected, actual)
}