[#107] fix infinite loop when $ appear between variable and the end of a template string

This commit is contained in:
Vadym Slizov 2020-06-14 23:37:44 +03:00
parent d7e0c8f6da
commit 7d4cc3b6e5
3 changed files with 6017 additions and 5858 deletions

File diff suppressed because it is too large Load Diff

View File

@ -419,10 +419,10 @@ func (lex *Lexer) Lex(lval Lval) int {
*|;
backqote := |*
"{$" => {lex.ungetCnt(1); lex.setTokenPosition(token); tok = T_CURLY_OPEN; lex.call(ftargs, fentry(php)); goto _out;};
"${" => {lex.setTokenPosition(token); tok = T_DOLLAR_OPEN_CURLY_BRACES; lex.call(ftargs, fentry(string_var_name)); goto _out;};
"$" => {lex.ungetCnt(1); fcall string_var;};
'`' => {lex.setTokenPosition(token); tok = TokenID(int('`')); fnext php; fbreak;};
"{$" => {lex.ungetCnt(1); lex.setTokenPosition(token); tok = T_CURLY_OPEN; lex.call(ftargs, fentry(php)); goto _out;};
"${" => {lex.setTokenPosition(token); tok = T_DOLLAR_OPEN_CURLY_BRACES; lex.call(ftargs, fentry(string_var_name)); goto _out;};
"$" varname_first => {lex.ungetCnt(2); fcall string_var;};
'`' => {lex.setTokenPosition(token); tok = TokenID(int('`')); fnext php; fbreak;};
any_line* when is_not_backqoute_end_or_var => {
lex.setTokenPosition(token);
tok = T_ENCAPSED_AND_WHITESPACE;
@ -431,10 +431,10 @@ func (lex *Lexer) Lex(lval Lval) int {
*|;
template_string := |*
"{$" => {lex.ungetCnt(1); lex.setTokenPosition(token); tok = T_CURLY_OPEN; lex.call(ftargs, fentry(php)); goto _out;};
"${" => {lex.setTokenPosition(token); tok = T_DOLLAR_OPEN_CURLY_BRACES; lex.call(ftargs, fentry(string_var_name)); goto _out;};
"$" => {lex.ungetCnt(1); fcall string_var;};
'"' => {lex.setTokenPosition(token); tok = TokenID(int('"')); fnext php; fbreak;};
"{$" => {lex.ungetCnt(1); lex.setTokenPosition(token); tok = T_CURLY_OPEN; lex.call(ftargs, fentry(php)); goto _out;};
"${" => {lex.setTokenPosition(token); tok = T_DOLLAR_OPEN_CURLY_BRACES; lex.call(ftargs, fentry(string_var_name)); goto _out;};
"$" varname_first => {lex.ungetCnt(2); fcall string_var;};
'"' => {lex.setTokenPosition(token); tok = TokenID(int('"')); fnext php; fbreak;};
any_line* when is_not_string_end_or_var => {
lex.setTokenPosition(token);
tok = T_ENCAPSED_AND_WHITESPACE;

View File

@ -598,6 +598,8 @@ func TestTeplateStringTokens(t *testing.T) {
"$/$foo"
"$0$foo"
"$foo$"
`
expected := []string{
@ -653,6 +655,11 @@ func TestTeplateStringTokens(t *testing.T) {
T_ENCAPSED_AND_WHITESPACE.String(),
T_VARIABLE.String(),
TokenID(int('"')).String(),
TokenID(int('"')).String(),
T_VARIABLE.String(),
T_ENCAPSED_AND_WHITESPACE.String(),
TokenID(int('"')).String(),
}
lexer := NewLexer([]byte(src))
@ -684,6 +691,7 @@ func TestBackquoteStringTokens(t *testing.T) {
` + "`$foo/100`" + `
` + "`$/$foo`" + `
` + "`$0$foo`" + `
` + "`$foo$`" + `
`
expected := []string{
@ -739,6 +747,11 @@ func TestBackquoteStringTokens(t *testing.T) {
T_ENCAPSED_AND_WHITESPACE.String(),
T_VARIABLE.String(),
TokenID(int('`')).String(),
TokenID(int('`')).String(),
T_VARIABLE.String(),
T_ENCAPSED_AND_WHITESPACE.String(),
TokenID(int('`')).String(),
}
lexer := NewLexer([]byte(src))