Skip to content

Commit b3a3ce0

Browse files
authored
Lex <?php at the end of the input as an open tag (#21)
PHP 7.4 made <?php at the end of the file open PHP code, where it had been <? followed by the name php. The PHP 8 scanner still read it the old way, so a file ending in ?><?php failed with an unexpected end. Before a name, <?phpinfo, it stays <? and the name.
1 parent 6ecb78a commit b3a3ce0

4 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎internal/php8/lexer.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ func (lex *Lexer) ungetStr(suffix string) {
230230
}
231231
}
232232

233+
// addOpenTagToken adds the open tag the token starts with: <?, or <?php
234+
// where php is all that follows, which opens PHP code since PHP 7.4.
235+
func (lex *Lexer) addOpenTagToken(t *token.Token) {
236+
if lex.pe-lex.te == 3 && strings.EqualFold(string(lex.data[lex.te:lex.pe]), "php") {
237+
lex.p += 3
238+
lex.te += 3
239+
}
240+
lex.addFreeFloatingToken(t, token.T_OPEN_TAG, lex.ts, lex.te)
241+
}
242+
233243
func (lex *Lexer) ungetCnt(count int) {
234244
lex.p = lex.p - count
235245
lex.te = lex.te - count

‎internal/php8/scanner.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,7 @@ func (lex *Lexer) Lex() *token.Token {
24112411
// line internal/php8/scanner.rl:144
24122412
(lex.p) = (lex.te) - 1
24132413
{
2414-
lex.addFreeFloatingToken(tkn, token.T_OPEN_TAG, lex.ts, lex.te)
2414+
lex.addOpenTagToken(tkn)
24152415
lex.cs = 128
24162416
}
24172417
goto _again
@@ -2463,7 +2463,7 @@ func (lex *Lexer) Lex() *token.Token {
24632463
lex.te = (lex.p)
24642464
(lex.p)--
24652465
{
2466-
lex.addFreeFloatingToken(tkn, token.T_OPEN_TAG, lex.ts, lex.te)
2466+
lex.addOpenTagToken(tkn)
24672467
lex.cs = 128
24682468
}
24692469
goto _again

‎internal/php8/scanner.rl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (lex *Lexer) Lex() *token.Token {
142142
fbreak;
143143
};
144144
'<?' => {
145-
lex.addFreeFloatingToken(tkn, token.T_OPEN_TAG, lex.ts, lex.te)
145+
lex.addOpenTagToken(tkn)
146146
fnext php;
147147
};
148148
'<?php'i ( [ \t] | newline ) => {

‎internal/php8/scanner_test.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,50 @@ func TestShebang(test *testing.T) {
405405
assert.DeepEqual(test, expected, actual)
406406
}
407407

408+
func TestOpenTagAtEnd(test *testing.T) {
409+
src := `<?php echo 1 ?><?php`
410+
411+
config := conf.Config{
412+
Version: &version.Version{
413+
Major: 8,
414+
Minor: 0,
415+
},
416+
}
417+
lexer := NewLexer([]byte(src), config)
418+
419+
ids := []token.ID{}
420+
var last *token.Token
421+
for {
422+
tkn := lexer.Lex()
423+
if tkn.ID == 0 {
424+
last = tkn
425+
break
426+
}
427+
ids = append(ids, tkn.ID)
428+
}
429+
430+
assert.DeepEqual(test, []token.ID{token.T_ECHO, token.T_LNUMBER, token.ID(';')}, ids)
431+
assert.Equal(test, token.T_OPEN_TAG, last.FreeFloating[0].ID)
432+
assert.Equal(test, "<?php", string(last.FreeFloating[0].Value))
433+
}
434+
435+
func TestShortOpenTagBeforeName(test *testing.T) {
436+
src := `<?phpinfo();`
437+
438+
config := conf.Config{
439+
Version: &version.Version{
440+
Major: 8,
441+
Minor: 0,
442+
},
443+
}
444+
lexer := NewLexer([]byte(src), config)
445+
446+
tkn := lexer.Lex()
447+
assert.Equal(test, token.T_STRING, tkn.ID)
448+
assert.Equal(test, "phpinfo", string(tkn.Value))
449+
assert.Equal(test, "<?", string(tkn.FreeFloating[0].Value))
450+
}
451+
408452
func TestShebangHtml(test *testing.T) {
409453
src := `#!/usr/bin/env php
410454
<br/><?php

0 commit comments

Comments
 (0)