rename meta to freefloating; refactoring
This commit is contained in:
@@ -9,10 +9,10 @@ import (
|
||||
"unicode"
|
||||
|
||||
"github.com/z7zmey/php-parser/errors"
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
"github.com/z7zmey/php-parser/position"
|
||||
|
||||
"github.com/cznic/golex/lex"
|
||||
"github.com/z7zmey/php-parser/meta"
|
||||
)
|
||||
|
||||
// Allocate Character classes anywhere in [0x80, 0xFF].
|
||||
@@ -33,7 +33,7 @@ type Lexer struct {
|
||||
*lex.Lexer
|
||||
StateStack []int
|
||||
PhpDocComment string
|
||||
Meta meta.Collection
|
||||
FreeFloating []freefloating.String
|
||||
heredocLabel string
|
||||
tokenBytesBuf *bytes.Buffer
|
||||
TokenPool *TokenPool
|
||||
@@ -74,7 +74,7 @@ func NewLexer(src io.Reader, fName string) *Lexer {
|
||||
Lexer: lx,
|
||||
StateStack: []int{0},
|
||||
PhpDocComment: "",
|
||||
Meta: nil,
|
||||
FreeFloating: nil,
|
||||
heredocLabel: "",
|
||||
tokenBytesBuf: &bytes.Buffer{},
|
||||
TokenPool: &TokenPool{},
|
||||
@@ -140,7 +140,7 @@ func (l *Lexer) createToken(chars []lex.Char) *Token {
|
||||
lastChar := chars[len(chars)-1]
|
||||
|
||||
token := l.TokenPool.Get()
|
||||
token.Meta = l.Meta
|
||||
token.FreeFloating = l.FreeFloating
|
||||
token.Value = l.tokenString(chars)
|
||||
|
||||
// fmt.Println(l.tokenString(chars))
|
||||
@@ -165,7 +165,7 @@ func (l *Lexer) tokenString(chars []lex.Char) string {
|
||||
|
||||
// meta
|
||||
|
||||
func (l *Lexer) addMeta(mt meta.Type, chars []lex.Char) {
|
||||
func (l *Lexer) addFreeFloating(t freefloating.StringType, chars []lex.Char) {
|
||||
if !l.WithMeta {
|
||||
return
|
||||
}
|
||||
@@ -180,9 +180,9 @@ func (l *Lexer) addMeta(mt meta.Type, chars []lex.Char) {
|
||||
int(lastChar.Pos()),
|
||||
)
|
||||
|
||||
l.Meta.Push(&meta.Data{
|
||||
Value: l.tokenString(chars),
|
||||
Type: mt,
|
||||
Position: pos,
|
||||
l.FreeFloating = append(l.FreeFloating, freefloating.String{
|
||||
StringType: t,
|
||||
Value: l.tokenString(chars),
|
||||
Position: pos,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ package scanner
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/cznic/golex/lex"
|
||||
"github.com/z7zmey/php-parser/meta"
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -34,7 +34,7 @@ func isValidFirstVarNameRune(r rune) bool {
|
||||
}
|
||||
|
||||
func (l *Lexer) Lex(lval Lval) int {
|
||||
l.Meta = nil
|
||||
l.FreeFloating = nil
|
||||
c := l.Enter()
|
||||
|
||||
yystate0:
|
||||
@@ -7632,7 +7632,7 @@ yystate611:
|
||||
|
||||
yyrule1: // [ \t\n\r]+
|
||||
{
|
||||
l.addMeta(meta.WhiteSpaceType, l.Token())
|
||||
l.addFreeFloating(freefloating.WhiteSpaceType, l.Token())
|
||||
goto yystate0
|
||||
}
|
||||
yyrule2: // .
|
||||
@@ -7660,14 +7660,14 @@ yyrule2: // .
|
||||
}
|
||||
yyrule3: // \<\?php([ \t]|{NEW_LINE})
|
||||
{
|
||||
l.addMeta(meta.TokenType, l.Token()[:5])
|
||||
l.addFreeFloating(freefloating.TokenType, l.Token()[:5])
|
||||
l.Begin(PHP)
|
||||
l.ungetChars(len(l.Token()) - 5)
|
||||
goto yystate0
|
||||
}
|
||||
yyrule4: // \<\?
|
||||
{
|
||||
l.addMeta(meta.TokenType, l.Token())
|
||||
l.addFreeFloating(freefloating.TokenType, l.Token())
|
||||
l.Begin(PHP)
|
||||
goto yystate0
|
||||
}
|
||||
@@ -7680,7 +7680,7 @@ yyrule5: // \<\?=
|
||||
}
|
||||
yyrule6: // [ \t\n\r]+
|
||||
{
|
||||
l.addMeta(meta.WhiteSpaceType, l.Token())
|
||||
l.addFreeFloating(freefloating.WhiteSpaceType, l.Token())
|
||||
goto yystate0
|
||||
}
|
||||
yyrule7: // [;][ \t\n\r]*\?\>{NEW_LINE}?
|
||||
@@ -8478,13 +8478,13 @@ yyrule126: // (#|[/][/])
|
||||
}
|
||||
break
|
||||
}
|
||||
l.addMeta(meta.CommentType, tb)
|
||||
l.addFreeFloating(freefloating.CommentType, tb)
|
||||
goto yystate0
|
||||
}
|
||||
yyrule127: // [/][*][*][/]
|
||||
{
|
||||
|
||||
l.addMeta(meta.CommentType, l.Token())
|
||||
l.addFreeFloating(freefloating.CommentType, l.Token())
|
||||
goto yystate0
|
||||
}
|
||||
yyrule128: // ([/][*])|([/][*][*])
|
||||
@@ -8509,9 +8509,9 @@ yyrule128: // ([/][*])|([/][*][*])
|
||||
}
|
||||
if is_doc_comment {
|
||||
l.PhpDocComment = string(l.TokenBytes(nil))
|
||||
l.addMeta(meta.CommentType, l.Token())
|
||||
l.addFreeFloating(freefloating.CommentType, l.Token())
|
||||
} else {
|
||||
l.addMeta(meta.CommentType, l.Token())
|
||||
l.addFreeFloating(freefloating.CommentType, l.Token())
|
||||
}
|
||||
goto yystate0
|
||||
}
|
||||
@@ -8557,7 +8557,7 @@ yyrule134: // ->
|
||||
}
|
||||
yyrule135: // [ \t\n\r]+
|
||||
{
|
||||
l.addMeta(meta.WhiteSpaceType, l.Token())
|
||||
l.addFreeFloating(freefloating.WhiteSpaceType, l.Token())
|
||||
goto yystate0
|
||||
}
|
||||
yyrule136: // ->
|
||||
@@ -9024,7 +9024,7 @@ yyrule167: // .
|
||||
}
|
||||
yyrule168: // .|[ \t\n\r]
|
||||
{
|
||||
l.addMeta(meta.TokenType, l.Token())
|
||||
l.addFreeFloating(freefloating.TokenType, l.Token())
|
||||
goto yystate0
|
||||
}
|
||||
yyrule169: // {ANY_CHAR}
|
||||
|
||||
@@ -9,7 +9,7 @@ package scanner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/z7zmey/php-parser/meta"
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
"github.com/cznic/golex/lex"
|
||||
)
|
||||
|
||||
@@ -33,9 +33,8 @@ func isValidFirstVarNameRune(r rune) bool {
|
||||
}
|
||||
|
||||
func (l *Lexer) Lex(lval Lval) int {
|
||||
l.Meta = nil
|
||||
l.FreeFloating = nil
|
||||
c := l.Enter()
|
||||
|
||||
%}
|
||||
|
||||
%s PHP STRING STRING_VAR STRING_VAR_INDEX STRING_VAR_NAME PROPERTY HEREDOC_END NOWDOC HEREDOC BACKQUOTE HALT_COMPILER
|
||||
@@ -60,7 +59,7 @@ ANY_CHAR .
|
||||
%%
|
||||
c = l.Rule0()
|
||||
|
||||
<INITIAL>[ \t\n\r]+ l.addMeta(meta.WhiteSpaceType, l.Token())
|
||||
<INITIAL>[ \t\n\r]+ l.addFreeFloating(freefloating.WhiteSpaceType, l.Token())
|
||||
<INITIAL>.
|
||||
tb := []lex.Char{}
|
||||
|
||||
@@ -87,12 +86,12 @@ ANY_CHAR .
|
||||
lval.Token(l.createToken(tb))
|
||||
return int(T_INLINE_HTML)
|
||||
|
||||
<INITIAL>\<\?php([ \t]|{NEW_LINE}) l.addMeta(meta.TokenType, l.Token()[:5]);l.Begin(PHP);l.ungetChars(len(l.Token())-5)
|
||||
<INITIAL>\<\? l.addMeta(meta.TokenType, l.Token());l.Begin(PHP);
|
||||
<INITIAL>\<\?php([ \t]|{NEW_LINE}) l.addFreeFloating(freefloating.TokenType, l.Token()[:5]);l.Begin(PHP);l.ungetChars(len(l.Token())-5)
|
||||
<INITIAL>\<\? l.addFreeFloating(freefloating.TokenType, l.Token());l.Begin(PHP);
|
||||
<INITIAL>\<\?= l.Begin(PHP);lval.Token(l.createToken(l.Token())); return int(T_ECHO);
|
||||
|
||||
|
||||
<PHP>[ \t\n\r]+ l.addMeta(meta.WhiteSpaceType, l.Token())
|
||||
<PHP>[ \t\n\r]+ l.addFreeFloating(freefloating.WhiteSpaceType, l.Token())
|
||||
<PHP>[;][ \t\n\r]*\?\>{NEW_LINE}? l.Begin(INITIAL);lval.Token(l.createToken(l.Token())); return Rune2Class(';');
|
||||
<PHP>\?\>{NEW_LINE}? l.Begin(INITIAL);lval.Token(l.createToken(l.Token())); return Rune2Class(';');
|
||||
|
||||
@@ -289,10 +288,10 @@ ANY_CHAR .
|
||||
break;
|
||||
}
|
||||
|
||||
l.addMeta(meta.CommentType, tb)
|
||||
l.addFreeFloating(freefloating.CommentType, tb)
|
||||
|
||||
<PHP>[/][*][*][/]
|
||||
l.addMeta(meta.CommentType, l.Token())
|
||||
l.addFreeFloating(freefloating.CommentType, l.Token())
|
||||
<PHP>([/][*])|([/][*][*])
|
||||
tb := l.Token()
|
||||
is_doc_comment := false
|
||||
@@ -317,9 +316,9 @@ ANY_CHAR .
|
||||
|
||||
if is_doc_comment {
|
||||
l.PhpDocComment = string(l.TokenBytes(nil))
|
||||
l.addMeta(meta.CommentType, l.Token())
|
||||
l.addFreeFloating(freefloating.CommentType, l.Token())
|
||||
} else {
|
||||
l.addMeta(meta.CommentType, l.Token())
|
||||
l.addFreeFloating(freefloating.CommentType, l.Token())
|
||||
}
|
||||
|
||||
<PHP>{OPERATORS} lval.Token(l.createToken(l.Token())); return Rune2Class(rune(l.TokenBytes(nil)[0]))
|
||||
@@ -330,7 +329,7 @@ ANY_CHAR .
|
||||
<PHP>{VAR_NAME} lval.Token(l.createToken(l.Token())); return int(T_STRING)
|
||||
|
||||
<PHP>-> l.Begin(PROPERTY);lval.Token(l.createToken(l.Token())); return int(T_OBJECT_OPERATOR);
|
||||
<PROPERTY>[ \t\n\r]+ l.addMeta(meta.WhiteSpaceType, l.Token())
|
||||
<PROPERTY>[ \t\n\r]+ l.addFreeFloating(freefloating.WhiteSpaceType, l.Token())
|
||||
<PROPERTY>-> lval.Token(l.createToken(l.Token())); return int(T_OBJECT_OPERATOR);
|
||||
<PROPERTY>{VAR_NAME} l.Begin(PHP);lval.Token(l.createToken(l.Token())); return int(T_STRING);
|
||||
<PROPERTY>. l.ungetChars(1);l.Begin(PHP)
|
||||
@@ -674,7 +673,7 @@ ANY_CHAR .
|
||||
<STRING_VAR_NAME>{VAR_NAME}[\[\}] l.popState();l.pushState(PHP);lval.Token(l.createToken(l.ungetChars(1))); return int(T_STRING_VARNAME)
|
||||
<STRING_VAR_NAME>. l.ungetChars(1);l.popState();l.pushState(PHP)
|
||||
|
||||
<HALT_COMPILER>.|[ \t\n\r] l.addMeta(meta.TokenType, l.Token())
|
||||
<HALT_COMPILER>.|[ \t\n\r] l.addFreeFloating(freefloating.TokenType, l.Token())
|
||||
|
||||
<PHP>{ANY_CHAR} l.Error(fmt.Sprintf("WARNING: Unexpected character in input: '%c' (ASCII=%d)", l.TokenBytes(nil)[0], l.TokenBytes(nil)[0]));l.Abort();
|
||||
|
||||
|
||||
@@ -5,10 +5,9 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
"github.com/z7zmey/php-parser/position"
|
||||
|
||||
"github.com/z7zmey/php-parser/meta"
|
||||
|
||||
"github.com/z7zmey/php-parser/scanner"
|
||||
|
||||
"github.com/kylelemons/godebug/pretty"
|
||||
@@ -967,21 +966,21 @@ func TestSlashAfterVariable(t *testing.T) {
|
||||
func TestCommentEnd(t *testing.T) {
|
||||
src := `<?php //test`
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "//test",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 12),
|
||||
{
|
||||
Value: "//test",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 12),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -991,7 +990,7 @@ func TestCommentEnd(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lexer.Meta
|
||||
actual := lexer.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -999,21 +998,21 @@ func TestCommentEnd(t *testing.T) {
|
||||
func TestCommentNewLine(t *testing.T) {
|
||||
src := "<?php //test\n$a"
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "//test\n",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 13),
|
||||
{
|
||||
Value: "//test\n",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 13),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1023,7 +1022,7 @@ func TestCommentNewLine(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1031,21 +1030,21 @@ func TestCommentNewLine(t *testing.T) {
|
||||
func TestCommentNewLine1(t *testing.T) {
|
||||
src := "<?php //test\r$a"
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "//test\r",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 13),
|
||||
{
|
||||
Value: "//test\r",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 13),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1055,7 +1054,7 @@ func TestCommentNewLine1(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1063,21 +1062,21 @@ func TestCommentNewLine1(t *testing.T) {
|
||||
func TestCommentNewLine2(t *testing.T) {
|
||||
src := "<?php #test\r\n$a"
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 1, 6, 6),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "#test\r\n",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 13),
|
||||
{
|
||||
Value: "#test\r\n",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(1, 1, 7, 13),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1087,7 +1086,7 @@ func TestCommentNewLine2(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1096,21 +1095,21 @@ func TestCommentWithPhpEndTag(t *testing.T) {
|
||||
src := `<?php
|
||||
//test?> test`
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "//test",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 13),
|
||||
{
|
||||
Value: "//test",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 13),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1120,7 +1119,7 @@ func TestCommentWithPhpEndTag(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1129,21 +1128,21 @@ func TestInlineComment(t *testing.T) {
|
||||
src := `<?php
|
||||
/*test*/`
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "/*test*/",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 15),
|
||||
{
|
||||
Value: "/*test*/",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 15),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1153,7 +1152,7 @@ func TestInlineComment(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1162,21 +1161,21 @@ func TestInlineComment2(t *testing.T) {
|
||||
src := `<?php
|
||||
/*/*/`
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "/*/*/",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 12),
|
||||
{
|
||||
Value: "/*/*/",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 12),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1186,7 +1185,7 @@ func TestInlineComment2(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lexer.Meta
|
||||
actual := lexer.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1195,26 +1194,26 @@ func TestEmptyInlineComment(t *testing.T) {
|
||||
src := `<?php
|
||||
/**/ `
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "/**/",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 11),
|
||||
{
|
||||
Value: "/**/",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 11),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 12, 12),
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 12, 12),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1224,7 +1223,7 @@ func TestEmptyInlineComment(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lexer.Meta
|
||||
actual := lexer.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1233,21 +1232,21 @@ func TestEmptyInlineComment2(t *testing.T) {
|
||||
src := `<?php
|
||||
/***/`
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "/***/",
|
||||
Type: meta.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 12),
|
||||
{
|
||||
Value: "/***/",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: position.NewPosition(2, 2, 8, 12),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1257,7 +1256,7 @@ func TestEmptyInlineComment2(t *testing.T) {
|
||||
|
||||
lexer.Lex(lv)
|
||||
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
@@ -1270,86 +1269,86 @@ func TestMethodCallTokens(t *testing.T) {
|
||||
lexer.WithMeta = true
|
||||
lv := &lval{}
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 10, 10),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 10, 10),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 13, 13),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 13, 13),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 17, 17),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 17, 17),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 19, 19),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 19, 19),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 22, 22),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 22, 22),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 24, 24),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 24, 24),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
@@ -1361,31 +1360,31 @@ func TestYieldFromTokens(t *testing.T) {
|
||||
lexer.WithMeta = true
|
||||
lv := &lval{}
|
||||
|
||||
expected := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "<?php",
|
||||
Type: meta.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
expected := []freefloating.String{
|
||||
{
|
||||
Value: "<?php",
|
||||
StringType: freefloating.TokenType,
|
||||
Position: position.NewPosition(1, 1, 1, 5),
|
||||
},
|
||||
&meta.Data{
|
||||
Value: "\n\t",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
{
|
||||
Value: "\n\t",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(1, 2, 6, 7),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual := lv.Tkn.Meta
|
||||
actual := lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
|
||||
expected = meta.Collection{
|
||||
&meta.Data{
|
||||
Value: " ",
|
||||
Type: meta.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 18, 18),
|
||||
expected = []freefloating.String{
|
||||
{
|
||||
Value: " ",
|
||||
StringType: freefloating.WhiteSpaceType,
|
||||
Position: position.NewPosition(2, 2, 18, 18),
|
||||
},
|
||||
}
|
||||
lexer.Lex(lv)
|
||||
actual = lv.Tkn.Meta
|
||||
actual = lv.Tkn.FreeFloating
|
||||
assertEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,35 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/meta"
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
"github.com/z7zmey/php-parser/position"
|
||||
)
|
||||
|
||||
// Token value returned by lexer
|
||||
type Token struct {
|
||||
Value string
|
||||
Meta meta.Collection
|
||||
StartLine int
|
||||
EndLine int
|
||||
StartPos int
|
||||
EndPos int
|
||||
Value string
|
||||
FreeFloating []freefloating.String
|
||||
StartLine int
|
||||
EndLine int
|
||||
StartPos int
|
||||
EndPos int
|
||||
}
|
||||
|
||||
func (t *Token) String() string {
|
||||
return string(t.Value)
|
||||
}
|
||||
|
||||
func (t *Token) GetFreeFloatingToken() []freefloating.String {
|
||||
return []freefloating.String{
|
||||
{
|
||||
StringType: freefloating.TokenType,
|
||||
Value: t.Value,
|
||||
Position: &position.Position{
|
||||
StartLine: t.StartLine,
|
||||
EndLine: t.EndLine,
|
||||
StartPos: t.StartPos,
|
||||
EndPos: t.EndPos,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/meta"
|
||||
|
||||
"github.com/z7zmey/php-parser/freefloating"
|
||||
"github.com/z7zmey/php-parser/scanner"
|
||||
)
|
||||
|
||||
@@ -18,17 +17,17 @@ func TestToken(t *testing.T) {
|
||||
EndPos: 3,
|
||||
}
|
||||
|
||||
c := meta.Collection{
|
||||
&meta.Data{
|
||||
Value: "test comment",
|
||||
Type: meta.CommentType,
|
||||
Position: nil,
|
||||
c := []freefloating.String{
|
||||
{
|
||||
Value: "test comment",
|
||||
StringType: freefloating.CommentType,
|
||||
Position: nil,
|
||||
},
|
||||
}
|
||||
|
||||
tkn.Meta = c
|
||||
tkn.FreeFloating = c
|
||||
|
||||
if !reflect.DeepEqual(tkn.Meta, c) {
|
||||
if !reflect.DeepEqual(tkn.FreeFloating, c) {
|
||||
t.Errorf("comments are not equal\n")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user