scanner: emit T_STRING when enum not in enum declaration context

This commit is contained in:
Pavel Selitskas 2023-07-23 18:50:39 +03:00 committed by Laytan Laats
parent 87718f9993
commit deeda4f6a7
5 changed files with 21832 additions and 24343 deletions

View File

@ -67,6 +67,24 @@ func (lex *Lexer) setTokenPosition(token *token.Token) {
token.Position = pos token.Position = pos
} }
func (lex *Lexer) setTokenPrefixPosition(token *token.Token, n int) {
pos := lex.positionPool.Get()
endPos := lex.ts + n
sl, slb := lex.newLines.GetLine(lex.ts)
el, elb := lex.newLines.GetLine(endPos)
pos.StartLine = sl
pos.EndLine = el
pos.StartPos = lex.ts
pos.EndPos = endPos
pos.StartCol = lex.ts - slb
pos.EndCol = endPos - elb
token.Position = pos
}
func (lex *Lexer) addFreeFloatingToken(t *token.Token, id token.ID, ps, pe int) { func (lex *Lexer) addFreeFloatingToken(t *token.Token, id token.ID, ps, pe int) {
skippedTkn := lex.tokenPool.Get() skippedTkn := lex.tokenPool.Get()
skippedTkn.ID = id skippedTkn.ID = id
@ -198,6 +216,11 @@ func (lex *Lexer) ungetStr(s string) {
} }
} }
func (lex *Lexer) ungetFromStart(n int) {
tokenLength := lex.te - lex.ts
lex.ungetCnt(tokenLength - n)
}
func (lex *Lexer) ungetCnt(n int) { func (lex *Lexer) ungetCnt(n int) {
lex.p = lex.p - n lex.p = lex.p - n
lex.te = lex.te - n lex.te = lex.te - n

View File

@ -4,8 +4,8 @@ package php8
import ( import (
"strconv" "strconv"
"github.com/VKCOM/php-parser/pkg/ast" "github.com/laytan/php-parser/pkg/ast"
"github.com/VKCOM/php-parser/pkg/token" "github.com/laytan/php-parser/pkg/token"
) )
%} %}

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/VKCOM/php-parser/pkg/token" "github.com/laytan/php-parser/pkg/token"
) )
%%{ %%{
@ -240,7 +240,8 @@ func (lex *Lexer) Lex() *token.Token {
'endif'i => {lex.setTokenPosition(tkn); tok = token.T_ENDIF; fbreak;}; 'endif'i => {lex.setTokenPosition(tkn); tok = token.T_ENDIF; fbreak;};
'endswitch'i => {lex.setTokenPosition(tkn); tok = token.T_ENDSWITCH; fbreak;}; 'endswitch'i => {lex.setTokenPosition(tkn); tok = token.T_ENDSWITCH; fbreak;};
'endwhile'i => {lex.setTokenPosition(tkn); tok = token.T_ENDWHILE; fbreak;}; 'endwhile'i => {lex.setTokenPosition(tkn); tok = token.T_ENDWHILE; fbreak;};
'enum'i => {lex.setTokenPosition(tkn); tok = token.T_ENUM; fbreak;}; 'enum'i whitespace+ varname_first => {lex.setTokenPrefixPosition(tkn, 4); tok = token.T_ENUM; lex.ungetFromStart(4); fbreak;};
'enum'i whitespace+ ('extends'i | 'implements'i) => {lex.setTokenPrefixPosition(tkn, 4); tok = token.T_STRING; lex.ungetFromStart(4); fbreak;};
'eval'i => {lex.setTokenPosition(tkn); tok = token.T_EVAL; fbreak;}; 'eval'i => {lex.setTokenPosition(tkn); tok = token.T_EVAL; fbreak;};
'exit'i | 'die'i => {lex.setTokenPosition(tkn); tok = token.T_EXIT; fbreak;}; 'exit'i | 'die'i => {lex.setTokenPosition(tkn); tok = token.T_EXIT; fbreak;};
'extends'i => {lex.setTokenPosition(tkn); tok = token.T_EXTENDS; fbreak;}; 'extends'i => {lex.setTokenPosition(tkn); tok = token.T_EXTENDS; fbreak;};

View File

@ -181,6 +181,31 @@ func TestEnumTokens(t *testing.T) {
suite.Run() suite.Run()
} }
func TestClassNameEnum(t *testing.T) {
suite := tester.NewLexerTokenStructTestSuite(t)
suite.UsePHP8()
suite.Code = "<?php class Enum {}"
suite.Expected = []*token.Token{
{
ID: token.T_CLASS,
Value: []byte("class"),
},
{
ID: token.T_STRING,
Value: []byte("Enum"),
},
{
ID: '{',
Value: []byte("{"),
},
{
ID: '}',
Value: []byte("}"),
},
}
suite.Run()
}
func TestAmpersandFollowedByEllipsisTokens(t *testing.T) { func TestAmpersandFollowedByEllipsisTokens(t *testing.T) {
suite := tester.NewLexerTokenStructTestSuite(t) suite := tester.NewLexerTokenStructTestSuite(t)
suite.UsePHP8() suite.UsePHP8()