Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions internal/php8/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ func (lex *Lexer) ungetStr(suffix string) {
}
}

// addHaltCommentToken adds the comment that starts at the token, between
// __halt_compiler and its semicolon, where PHP takes comments as anywhere
// else, and reports whether there is one.
func (lex *Lexer) addHaltCommentToken(t *token.Token) bool {
rest := lex.data[lex.ts:lex.pe]
id := token.T_COMMENT
var n int
switch {
case bytes.HasPrefix(rest, []byte("/*")):
n = len(rest)
if end := bytes.Index(rest[2:], []byte("*/")); end >= 0 {
n = end + 4
}
if n > 4 && bytes.HasPrefix(rest, []byte("/**")) {
id = token.T_DOC_COMMENT
}
case bytes.HasPrefix(rest, []byte("//")) || bytes.HasPrefix(rest, []byte("#")) && !bytes.HasPrefix(rest, []byte("#[")):
n = len(rest)
for i := 1; i < len(rest); i++ {
if rest[i] == '\n' || rest[i] == '\r' || rest[i] == '?' && i+1 < len(rest) && rest[i+1] == '>' {
n = i
break
}
}
default:
return false
}
for i := lex.ts; i < lex.ts+n; i++ {
if lex.data[i] == '\n' || lex.data[i] == '\r' && (i+1 == lex.pe || lex.data[i+1] != '\n') {
lex.newLines.Append(i + 1)
}
}
lex.te = lex.ts + n
lex.p = lex.te - 1
lex.addFreeFloatingToken(t, id, lex.ts, lex.te)
return true
}

func (lex *Lexer) ungetCnt(count int) {
lex.p = lex.p - count
lex.te = lex.te - count
Expand Down
36 changes: 24 additions & 12 deletions internal/php8/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24783,8 +24783,10 @@ func (lex *Lexer) Lex() *token.Token {
// line internal/php8/scanner.rl:501
lex.te = (lex.p) + 1
{
lex.ungetCnt(1)
lex.cs = 128
if !lex.addHaltCommentToken(tkn) {
lex.ungetCnt(1)
lex.cs = 128
}
}
goto _again
tr819:
Expand Down Expand Up @@ -24825,8 +24827,10 @@ func (lex *Lexer) Lex() *token.Token {
lex.te = (lex.p)
(lex.p)--
{
lex.ungetCnt(1)
lex.cs = 128
if !lex.addHaltCommentToken(tkn) {
lex.ungetCnt(1)
lex.cs = 128
}
}
goto _again
st538:
Expand Down Expand Up @@ -25005,8 +25009,10 @@ func (lex *Lexer) Lex() *token.Token {
// line internal/php8/scanner.rl:507
lex.te = (lex.p) + 1
{
lex.ungetCnt(1)
lex.cs = 128
if !lex.addHaltCommentToken(tkn) {
lex.ungetCnt(1)
lex.cs = 128
}
}
goto _again
tr830:
Expand Down Expand Up @@ -25047,8 +25053,10 @@ func (lex *Lexer) Lex() *token.Token {
lex.te = (lex.p)
(lex.p)--
{
lex.ungetCnt(1)
lex.cs = 128
if !lex.addHaltCommentToken(tkn) {
lex.ungetCnt(1)
lex.cs = 128
}
}
goto _again
st542:
Expand Down Expand Up @@ -25227,8 +25235,10 @@ func (lex *Lexer) Lex() *token.Token {
// line internal/php8/scanner.rl:513
lex.te = (lex.p) + 1
{
lex.ungetCnt(1)
lex.cs = 128
if !lex.addHaltCommentToken(tkn) {
lex.ungetCnt(1)
lex.cs = 128
}
}
goto _again
tr841:
Expand Down Expand Up @@ -25269,8 +25279,10 @@ func (lex *Lexer) Lex() *token.Token {
lex.te = (lex.p)
(lex.p)--
{
lex.ungetCnt(1)
lex.cs = 128
if !lex.addHaltCommentToken(tkn) {
lex.ungetCnt(1)
lex.cs = 128
}
}
goto _again
st546:
Expand Down
6 changes: 3 additions & 3 deletions internal/php8/scanner.rl
Original file line number Diff line number Diff line change
Expand Up @@ -498,19 +498,19 @@ func (lex *Lexer) Lex() *token.Token {
halt_compiller_open_parenthesis := |*
whitespace_line* => {lex.addFreeFloatingToken(tkn, token.T_WHITESPACE, lex.ts, lex.te)};
"(" => {lex.setTokenPosition(tkn); tok = token.ID(int('(')); fnext halt_compiller_close_parenthesis; fbreak;};
any => {lex.ungetCnt(1); fnext php;};
any => {if !lex.addHaltCommentToken(tkn) {lex.ungetCnt(1); fnext php;}};
*|;

halt_compiller_close_parenthesis := |*
whitespace_line* => {lex.addFreeFloatingToken(tkn, token.T_WHITESPACE, lex.ts, lex.te)};
")" => {lex.setTokenPosition(tkn); tok = token.ID(int(')')); fnext halt_compiller_close_semicolon; fbreak;};
any => {lex.ungetCnt(1); fnext php;};
any => {if !lex.addHaltCommentToken(tkn) {lex.ungetCnt(1); fnext php;}};
*|;

halt_compiller_close_semicolon := |*
whitespace_line* => {lex.addFreeFloatingToken(tkn, token.T_WHITESPACE, lex.ts, lex.te)};
";" => {lex.setTokenPosition(tkn); tok = token.ID(int(';')); fnext halt_compiller_end; fbreak;};
any => {lex.ungetCnt(1); fnext php;};
any => {if !lex.addHaltCommentToken(tkn) {lex.ungetCnt(1); fnext php;}};
*|;

halt_compiller_end := |*
Expand Down
46 changes: 46 additions & 0 deletions internal/php8/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,52 @@ func TestShebang(test *testing.T) {
assert.DeepEqual(test, expected, actual)
}

func TestHaltCompilerComments(test *testing.T) {
src := "<?php __halt_compiler/* a */ /** b */( // c\n) # d\n;\x00/*data"

config := conf.Config{
Version: &version.Version{
Major: 8,
Minor: 0,
},
}
lexer := NewLexer([]byte(src), config)

ids := []token.ID{}
var last *token.Token
for {
tkn := lexer.Lex()
if tkn.ID == 0 {
last = tkn
break
}
ids = append(ids, tkn.ID)
}

assert.DeepEqual(test, []token.ID{token.T_HALT_COMPILER, token.ID('('), token.ID(')'), token.ID(';')}, ids)
assert.Equal(test, "\x00/*data", string(last.FreeFloating[len(last.FreeFloating)-1].Value))
}

func TestHaltCompilerMultilineComment(test *testing.T) {
src := "<?php __halt_compiler/*\n\n*/(\n);"

config := conf.Config{
Version: &version.Version{
Major: 8,
Minor: 0,
},
}
lexer := NewLexer([]byte(src), config)

lexer.Lex()
tkn := lexer.Lex()
assert.Equal(test, token.ID('('), tkn.ID)
assert.Equal(test, 3, tkn.Position.StartLine)
tkn = lexer.Lex()
assert.Equal(test, token.ID(')'), tkn.ID)
assert.Equal(test, 4, tkn.Position.StartLine)
}

func TestShebangHtml(test *testing.T) {
src := `#!/usr/bin/env php
<br/><?php
Expand Down