issue #2 - fix single line comment
fix handling end of file add handling php close tag
This commit is contained in:
parent
d31819db30
commit
148cf59e9d
@ -346,3 +346,17 @@ func TestPhp5ParameterNode(t *testing.T) {
|
||||
actual, _, _ := php5.Parse(bytes.NewBufferString(src), "test.php")
|
||||
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)
|
||||
}
|
||||
|
4759
scanner/scanner.go
4759
scanner/scanner.go
File diff suppressed because it is too large
Load Diff
@ -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_SR
|
||||
<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>([/][*])|([/][*][*])
|
||||
tb := l.Token()
|
||||
is_doc_comment := false
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/comment"
|
||||
|
||||
"github.com/z7zmey/php-parser/scanner"
|
||||
"github.com/z7zmey/php-parser/token"
|
||||
|
||||
@ -693,3 +695,89 @@ func TestSlashAfterVariable(t *testing.T) {
|
||||
assertEqual(t, expected, actual)
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user