#51 saving optional tokes and tokens that have different representation as meta

This commit is contained in:
z7zmey
2018-07-29 11:44:38 +03:00
parent 0138749c6d
commit 4989d31874
223 changed files with 9832 additions and 5976 deletions

115
meta/collection.go Normal file
View File

@@ -0,0 +1,115 @@
package meta
// Collection of Meta objects
type Collection []*Data
// SetTokenName sets TokenName for the all elements in the collection
func (mc *Collection) SetTokenName(tn TokenName) *Collection {
for _, m := range *mc {
m.TokenName = tn
}
return mc
}
// Push adds elements to the end of an Collection
func (mc *Collection) Push(mm ...*Data) *Collection {
*mc = append(*mc, mm...)
return mc
}
// Unshift prepends elements to the beginning of an Collection
func (mc *Collection) Unshift(mm ...*Data) *Collection {
*mc = append(mm, *mc...)
return mc
}
// AppendTo - appends elements of the collection to the end of the target collection
func (mc *Collection) AppendTo(target *Collection) *Collection {
if len(*mc) == 0 {
return mc
}
*target = append(*target, *mc...)
return mc
}
// PrependTo - prepends elements of the collection to the start of the target collection
func (mc *Collection) PrependTo(target *Collection) *Collection {
if len(*mc) == 0 {
return mc
}
*target = append(*mc, *target...)
return mc
}
// Cut elements by TokenName
func (mc *Collection) Cut(f Filter) *Collection {
collection := (*mc)[:0]
cutted := Collection{}
for _, m := range *mc {
if fr := f(m); fr {
cutted = append(cutted, m)
} else {
collection = append(collection, m)
}
}
*mc = collection
return &cutted
}
// Filter function signature
type Filter func(d *Data) bool
// TokenNameFilter generates filter function that returns true
// if data.TokenName exactly same as given
func TokenNameFilter(tn TokenName) Filter {
return func(d *Data) bool {
return d.TokenName == tn
}
}
// TypeFilter generates filter function that returns true
// if data.Type exactly same as given
func TypeFilter(t Type) Filter {
return func(d *Data) bool {
return d.Type == t
}
}
// AndFilter generates filter function that returns true
// if all given filters return true
func AndFilter(filters ...Filter) Filter {
return func(d *Data) bool {
for _, filter := range filters {
if result := filter(d); !result {
return false
}
}
return true
}
}
// OrFilter generates filter function that returns true
// if one of given filters return true
func OrFilter(filters ...Filter) Filter {
return func(d *Data) bool {
for _, filter := range filters {
if result := filter(d); result {
return true
}
}
return false
}
}
// NotFilter negates given filter
func NotFilter(f Filter) Filter {
return func(d *Data) bool {
return !f(d)
}
}

422
meta/collection_test.go Normal file
View File

@@ -0,0 +1,422 @@
package meta_test
import (
"reflect"
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/z7zmey/php-parser/meta"
)
func TestCollectionSetTokenName(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
comment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
}
baseCollection := meta.Collection{whiteSpace, comment}
// action
baseCollection.SetTokenName(meta.OpenParenthesisToken)
// check
for _, m := range baseCollection {
if m.TokenName != meta.OpenParenthesisToken {
t.Error("The TokenName must be set for all Meta objects")
}
}
}
func TestCollectionPush(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
comment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
}
actualCollection := meta.Collection{whiteSpace}
expectedCollection := meta.Collection{whiteSpace, comment}
// action
actualCollection.Push(comment)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
}
func TestCollectionUnshift(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
comment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
}
actualCollection := meta.Collection{comment}
expectedCollection := meta.Collection{whiteSpace, comment}
// action
actualCollection.Unshift(whiteSpace)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
}
func TestCollectionAppendTo(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
comment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
}
actualCollection := meta.Collection{whiteSpace}
expectedCollection := meta.Collection{whiteSpace, comment}
baseCollection := meta.Collection{comment}
// action
baseCollection.AppendTo(&actualCollection)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
}
func TestEmptyCollectionAppendTo(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
actualCollection := meta.Collection{whiteSpace}
expectedCollection := meta.Collection{whiteSpace}
var baseCollection meta.Collection = nil
// action
baseCollection.AppendTo(&actualCollection)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
}
func TestCollectionPrependTo(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
comment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
}
actualCollection := meta.Collection{comment}
expectedCollection := meta.Collection{whiteSpace, comment}
baseCollection := meta.Collection{whiteSpace}
// action
baseCollection.PrependTo(&actualCollection)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
}
func TestEmptyCollectionPrependTo(t *testing.T) {
// prepare
comment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
}
actualCollection := meta.Collection{comment}
expectedCollection := meta.Collection{comment}
baseCollection := meta.Collection{}
// action
baseCollection.PrependTo(&actualCollection)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
}
func TestCollectionCutByTokenName(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
OpenParenthesisComment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
TokenName: meta.OpenParenthesisToken,
}
OpenParenthesisToken := &meta.Data{
Type: meta.TokenType,
Value: "(",
TokenName: meta.OpenParenthesisToken,
}
CloseParenthesisToken := &meta.Data{
Type: meta.TokenType,
Value: ")",
TokenName: meta.CloseParenthesisToken,
}
actualCollection := meta.Collection{whiteSpace, OpenParenthesisComment, OpenParenthesisToken, CloseParenthesisToken}
expectedCollection := meta.Collection{whiteSpace, CloseParenthesisToken}
expectedCuttedCollection := &meta.Collection{OpenParenthesisComment, OpenParenthesisToken}
// action
actualCuttedCollection := actualCollection.Cut(
meta.TokenNameFilter(meta.OpenParenthesisToken),
)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
if !reflect.DeepEqual(expectedCuttedCollection, actualCuttedCollection) {
diff := pretty.Compare(expectedCuttedCollection, actualCuttedCollection)
if diff != "" {
t.Errorf("\nexpected and actual cutted collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual cutted collections are not equal\n")
}
}
}
func TestCollectionCutByTokenTypes(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
OpenParenthesisComment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
TokenName: meta.OpenParenthesisToken,
}
OpenParenthesisToken := &meta.Data{
Type: meta.TokenType,
Value: "(",
TokenName: meta.OpenParenthesisToken,
}
CloseParenthesisToken := &meta.Data{
Type: meta.TokenType,
Value: ")",
TokenName: meta.CloseParenthesisToken,
}
actualCollection := meta.Collection{whiteSpace, OpenParenthesisComment, OpenParenthesisToken, CloseParenthesisToken}
expectedCollection := meta.Collection{OpenParenthesisToken, CloseParenthesisToken}
expectedCuttedCollection := &meta.Collection{whiteSpace, OpenParenthesisComment}
// action
actualCuttedCollection := actualCollection.Cut(meta.OrFilter(
meta.TypeFilter(meta.CommentType),
meta.TypeFilter(meta.WhiteSpaceType)),
)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
if !reflect.DeepEqual(expectedCuttedCollection, actualCuttedCollection) {
diff := pretty.Compare(expectedCuttedCollection, actualCuttedCollection)
if diff != "" {
t.Errorf("\nexpected and actual cutted collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual cutted collections are not equal\n")
}
}
}
func TestCollectionCutByTokenNameButNotType(t *testing.T) {
// prepare
whiteSpace := &meta.Data{
Type: meta.WhiteSpaceType,
Value: "\n",
}
OpenParenthesisComment := &meta.Data{
Type: meta.CommentType,
Value: "// some comment",
TokenName: meta.OpenParenthesisToken,
}
OpenParenthesisToken := &meta.Data{
Type: meta.TokenType,
Value: "(",
TokenName: meta.OpenParenthesisToken,
}
CloseParenthesisToken := &meta.Data{
Type: meta.TokenType,
Value: ")",
TokenName: meta.CloseParenthesisToken,
}
actualCollection := meta.Collection{
whiteSpace,
OpenParenthesisComment,
OpenParenthesisToken,
CloseParenthesisToken,
}
expectedCollection := meta.Collection{
whiteSpace,
OpenParenthesisToken,
CloseParenthesisToken,
}
expectedCuttedCollection := &meta.Collection{
OpenParenthesisComment,
}
// action
actualCuttedCollection := actualCollection.Cut(meta.AndFilter(
meta.TokenNameFilter(meta.OpenParenthesisToken),
meta.NotFilter(meta.TypeFilter(meta.TokenType))),
)
// check
if !reflect.DeepEqual(expectedCollection, actualCollection) {
diff := pretty.Compare(expectedCollection, actualCollection)
if diff != "" {
t.Errorf("\nexpected and actual collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual collections are not equal\n")
}
}
if !reflect.DeepEqual(expectedCuttedCollection, actualCuttedCollection) {
diff := pretty.Compare(expectedCuttedCollection, actualCuttedCollection)
if diff != "" {
t.Errorf("\nexpected and actual cutted collections are not equal\ndiff: (-expected +actual)\n%s", diff)
} else {
t.Errorf("\nexpected and actual cutted collections are not equal\n")
}
}
}

View File

@@ -1,39 +0,0 @@
package meta
import (
"github.com/z7zmey/php-parser/position"
)
// Comment aggrigates information about comment /**
type Comment struct {
Value string
Position *position.Position
TokenName TokenName
}
// NewComment - Comment constructor
func NewComment(value string, pos *position.Position) *Comment {
return &Comment{
Value: value,
Position: pos,
TokenName: UnknownToken,
}
}
// SetTokenName sets token name
func (c *Comment) SetTokenName(tokenName TokenName) {
c.TokenName = tokenName
}
// GetTokenName returns token name
func (c *Comment) GetTokenName() TokenName {
return c.TokenName
}
func (c *Comment) String() string {
return c.Value
}
func (c *Comment) GetPosition() *position.Position {
return c.Position
}

View File

@@ -1,44 +0,0 @@
package meta_test
import (
"testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/meta"
)
func TestCommentPrint(t *testing.T) {
expected := "/** hello world */"
comment := meta.NewComment(expected, nil)
actual := comment.String()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}
func TestCommentSetGetTokenName(t *testing.T) {
expected := meta.ArrayToken
c := meta.NewComment("/** hello world */", nil)
c.SetTokenName(expected)
actual := c.GetTokenName()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}
func TestCommentGetPosition(t *testing.T) {
expected := position.NewPosition(1, 1, 1, 1)
c := meta.NewComment("/** hello world */", expected)
actual := c.GetPosition()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}

17
meta/data.go Normal file
View File

@@ -0,0 +1,17 @@
package meta
import (
"github.com/z7zmey/php-parser/position"
)
// Data contain additional information that isn't part of AST
type Data struct {
Value string
Type Type
Position *position.Position
TokenName TokenName
}
func (d *Data) String() string {
return d.Value
}

21
meta/data_test.go Normal file
View File

@@ -0,0 +1,21 @@
package meta_test
import (
"testing"
"github.com/z7zmey/php-parser/meta"
)
func TestCommentPrint(t *testing.T) {
expected := "/** hello world */"
comment := meta.Data{
Value: expected,
}
actual := comment.String()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}

View File

@@ -1,12 +0,0 @@
package meta
import (
"github.com/z7zmey/php-parser/position"
)
type Meta interface {
String() string
SetTokenName(tn TokenName)
GetTokenName() TokenName
GetPosition() *position.Position
}

View File

@@ -36,7 +36,6 @@ const (
EnddeclareToken
AsToken
SwitchToken
SwitchSemicolonToken
EndswitchToken
CaseToken
DefaultToken
@@ -68,9 +67,11 @@ const (
CallableToken
StartHeredocToken
DollarOpenCurlyBracesToken
DollarCloseCurlyBracesToken
CurlyOpenToken
PaamayimNekudotayimToken
NamespaceToken
UseLeadingNsSeparatorToken
NsSeparatorToken
EllipsisToken
EvalToken
@@ -80,7 +81,7 @@ const (
LogicalXorToken
LogicalAndToken
InstanceofToken
NewToken
NewAnchor
CloneToken
ElseifToken
ElseToken

View File

@@ -4,9 +4,9 @@ package meta
import "strconv"
const _TokenName_name = "UnknownTokenNodeStartNodeEndMagicConstantTokenIncludeTokenIncludeOnceTokenExitTokenIfTokenLnumberTokenDnumberTokenStringTokenStringVarnameTokenVariableTokenNumStringTokenInlineHTMLTokenEncapsedAndWhitespaceTokenConstantEncapsedStringTokenEchoTokenDoTokenWhileTokenEndwhileTokenForInitSemicolonTokenForCondSemicolonTokenForTokenEndforTokenForeachTokenEndforeachTokenDeclareTokenEnddeclareTokenAsTokenSwitchTokenSwitchSemicolonTokenEndswitchTokenCaseTokenDefaultTokenBreakTokenContinueTokenGotoTokenFunctionTokenConstTokenReturnTokenTryTokenCatchTokenFinallyTokenThrowTokenUseTokenInsteadofTokenGlobalTokenVarTokenUnsetTokenIssetTokenEmptyTokenClassTokenTraitTokenInterfaceTokenExtendsTokenImplementsTokenDoubleArrowTokenListTokenArrayTokenCallableTokenStartHeredocTokenDollarOpenCurlyBracesTokenCurlyOpenTokenPaamayimNekudotayimTokenNamespaceTokenNsSeparatorTokenEllipsisTokenEvalTokenRequireTokenRequireOnceTokenLogicalOrTokenLogicalXorTokenLogicalAndTokenInstanceofTokenNewTokenCloneTokenElseifTokenElseTokenEndifTokenPrintTokenYieldTokenStaticTokenAbstractTokenFinalTokenPrivateTokenProtectedTokenPublicTokenIncTokenDecTokenYieldFromTokenObjectOperatorTokenIntCastTokenDoubleCastTokenStringCastTokenArrayCastTokenObjectCastTokenBoolCastTokenUnsetCastTokenCoalesceTokenSpaceshipTokenPlusEqualTokenMinusEqualTokenMulEqualTokenPowEqualTokenDivEqualTokenConcatEqualTokenModEqualTokenAndEqualTokenOrEqualTokenXorEqualTokenSlEqualTokenSrEqualTokenBooleanOrTokenBooleanAndTokenPowTokenSlTokenSrTokenIsIdenticalTokenIsNotIdenticalTokenIsEqualTokenIsNotEqualTokenIsSmallerOrEqualTokenIsGreaterOrEqualTokenHaltCompilerTokenIdentifierTokenCaseSeparatorTokenDoubleQuoteTokenBackquoteTokenOpenCurlyBracesTokenCloseCurlyBracesTokenSemiColonTokenColonTokenOpenParenthesisTokenCloseParenthesisTokenOpenSquareBracketCloseSquareBracketQuestionMarkTokenAmpersandTokenMinusTokenPlusTokenExclamationMarkTokenTildeTokenAtTokenDollarTokenCommaTokenVerticalBarTokenEqualTokenCaretTokenAsteriskTokenSlashTokenPercentTokenLessTokenGreaterTokenDotToken"
const _TokenName_name = "UnknownTokenNodeStartNodeEndMagicConstantTokenIncludeTokenIncludeOnceTokenExitTokenIfTokenLnumberTokenDnumberTokenStringTokenStringVarnameTokenVariableTokenNumStringTokenInlineHTMLTokenEncapsedAndWhitespaceTokenConstantEncapsedStringTokenEchoTokenDoTokenWhileTokenEndwhileTokenForInitSemicolonTokenForCondSemicolonTokenForTokenEndforTokenForeachTokenEndforeachTokenDeclareTokenEnddeclareTokenAsTokenSwitchTokenEndswitchTokenCaseTokenDefaultTokenBreakTokenContinueTokenGotoTokenFunctionTokenConstTokenReturnTokenTryTokenCatchTokenFinallyTokenThrowTokenUseTokenInsteadofTokenGlobalTokenVarTokenUnsetTokenIssetTokenEmptyTokenClassTokenTraitTokenInterfaceTokenExtendsTokenImplementsTokenDoubleArrowTokenListTokenArrayTokenCallableTokenStartHeredocTokenDollarOpenCurlyBracesTokenDollarCloseCurlyBracesTokenCurlyOpenTokenPaamayimNekudotayimTokenNamespaceTokenUseLeadingNsSeparatorTokenNsSeparatorTokenEllipsisTokenEvalTokenRequireTokenRequireOnceTokenLogicalOrTokenLogicalXorTokenLogicalAndTokenInstanceofTokenNewAnchorCloneTokenElseifTokenElseTokenEndifTokenPrintTokenYieldTokenStaticTokenAbstractTokenFinalTokenPrivateTokenProtectedTokenPublicTokenIncTokenDecTokenYieldFromTokenObjectOperatorTokenIntCastTokenDoubleCastTokenStringCastTokenArrayCastTokenObjectCastTokenBoolCastTokenUnsetCastTokenCoalesceTokenSpaceshipTokenPlusEqualTokenMinusEqualTokenMulEqualTokenPowEqualTokenDivEqualTokenConcatEqualTokenModEqualTokenAndEqualTokenOrEqualTokenXorEqualTokenSlEqualTokenSrEqualTokenBooleanOrTokenBooleanAndTokenPowTokenSlTokenSrTokenIsIdenticalTokenIsNotIdenticalTokenIsEqualTokenIsNotEqualTokenIsSmallerOrEqualTokenIsGreaterOrEqualTokenHaltCompilerTokenIdentifierTokenCaseSeparatorTokenDoubleQuoteTokenBackquoteTokenOpenCurlyBracesTokenCloseCurlyBracesTokenSemiColonTokenColonTokenOpenParenthesisTokenCloseParenthesisTokenOpenSquareBracketCloseSquareBracketQuestionMarkTokenAmpersandTokenMinusTokenPlusTokenExclamationMarkTokenTildeTokenAtTokenDollarTokenCommaTokenVerticalBarTokenEqualTokenCaretTokenAsteriskTokenSlashTokenPercentTokenLessTokenGreaterTokenDotToken"
var _TokenName_index = [...]uint16{0, 12, 21, 28, 46, 58, 74, 83, 90, 102, 114, 125, 143, 156, 170, 185, 211, 238, 247, 254, 264, 277, 298, 319, 327, 338, 350, 365, 377, 392, 399, 410, 430, 444, 453, 465, 475, 488, 497, 510, 520, 531, 539, 549, 561, 571, 579, 593, 604, 612, 622, 632, 642, 652, 662, 676, 688, 703, 719, 728, 738, 751, 768, 794, 808, 832, 846, 862, 875, 884, 896, 912, 926, 941, 956, 971, 979, 989, 1000, 1009, 1019, 1029, 1039, 1050, 1063, 1073, 1085, 1099, 1110, 1118, 1126, 1140, 1159, 1171, 1186, 1201, 1215, 1230, 1243, 1257, 1270, 1284, 1298, 1313, 1326, 1339, 1352, 1368, 1381, 1394, 1406, 1419, 1431, 1443, 1457, 1472, 1480, 1487, 1494, 1510, 1529, 1541, 1556, 1577, 1598, 1615, 1630, 1648, 1664, 1678, 1698, 1719, 1733, 1743, 1763, 1784, 1801, 1819, 1836, 1850, 1860, 1869, 1889, 1899, 1906, 1917, 1927, 1943, 1953, 1963, 1976, 1986, 1998, 2007, 2019, 2027}
var _TokenName_index = [...]uint16{0, 12, 21, 28, 46, 58, 74, 83, 90, 102, 114, 125, 143, 156, 170, 185, 211, 238, 247, 254, 264, 277, 298, 319, 327, 338, 350, 365, 377, 392, 399, 410, 424, 433, 445, 455, 468, 477, 490, 500, 511, 519, 529, 541, 551, 559, 573, 584, 592, 602, 612, 622, 632, 642, 656, 668, 683, 699, 708, 718, 731, 748, 774, 801, 815, 839, 853, 879, 895, 908, 917, 929, 945, 959, 974, 989, 1004, 1013, 1023, 1034, 1043, 1053, 1063, 1073, 1084, 1097, 1107, 1119, 1133, 1144, 1152, 1160, 1174, 1193, 1205, 1220, 1235, 1249, 1264, 1277, 1291, 1304, 1318, 1332, 1347, 1360, 1373, 1386, 1402, 1415, 1428, 1440, 1453, 1465, 1477, 1491, 1506, 1514, 1521, 1528, 1544, 1563, 1575, 1590, 1611, 1632, 1649, 1664, 1682, 1698, 1712, 1732, 1753, 1767, 1777, 1797, 1818, 1835, 1853, 1870, 1884, 1894, 1903, 1923, 1933, 1940, 1951, 1961, 1977, 1987, 1997, 2010, 2020, 2032, 2041, 2053, 2061}
func (i TokenName) String() string {
if i < 0 || i >= TokenName(len(_TokenName_index)-1) {

11
meta/type.go Normal file
View File

@@ -0,0 +1,11 @@
package meta
// Type TODO
type Type int
//go:generate stringer -type=Type -output ./type_string.go
const (
CommentType Type = iota
WhiteSpaceType
TokenType
)

16
meta/type_string.go Normal file
View File

@@ -0,0 +1,16 @@
// Code generated by "stringer -type=Type -output ./type_string.go"; DO NOT EDIT.
package meta
import "strconv"
const _Type_name = "CommentTypeWhiteSpaceTypeTokenType"
var _Type_index = [...]uint8{0, 11, 25, 34}
func (i Type) String() string {
if i < 0 || i >= Type(len(_Type_index)-1) {
return "Type(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Type_name[_Type_index[i]:_Type_index[i+1]]
}

29
meta/type_test.go Normal file
View File

@@ -0,0 +1,29 @@
package meta_test
import (
"testing"
"github.com/z7zmey/php-parser/meta"
)
func TestTypeString(t *testing.T) {
c := meta.CommentType
expected := "CommentType"
actual := c.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}
func TestWrongTypeString(t *testing.T) {
c := meta.Type(-1)
expected := "Type(-1)"
actual := c.String()
if expected != actual {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
}
}

View File

@@ -1,37 +0,0 @@
package meta
import (
"github.com/z7zmey/php-parser/position"
)
type WhiteSpace struct {
Value string
Position *position.Position
TokenName TokenName
}
func NewWhiteSpace(value string, pos *position.Position) *WhiteSpace {
return &WhiteSpace{
Value: value,
Position: pos,
TokenName: UnknownToken,
}
}
// SetTokenName sets token name
func (c *WhiteSpace) SetTokenName(tokenName TokenName) {
c.TokenName = tokenName
}
// GetTokenName returns token name
func (c *WhiteSpace) GetTokenName() TokenName {
return c.TokenName
}
func (el *WhiteSpace) String() string {
return el.Value
}
func (el *WhiteSpace) GetPosition() *position.Position {
return el.Position
}

View File

@@ -1,44 +0,0 @@
package meta_test
import (
"testing"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/meta"
)
func TestWhiteSpacePrint(t *testing.T) {
expected := "\n "
w := meta.NewWhiteSpace(expected, nil)
actual := w.String()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}
func TestWhiteSpaceSetGetTokenName(t *testing.T) {
expected := meta.ArrayToken
w := meta.NewWhiteSpace("\n ", nil)
w.SetTokenName(expected)
actual := w.GetTokenName()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}
func TestWhiteSpaceGetPosition(t *testing.T) {
expected := position.NewPosition(1, 1, 1, 1)
q := meta.NewWhiteSpace("\n ", expected)
actual := q.GetPosition()
if expected != actual {
t.Errorf("expected and actual are not equal\n")
}
}