diff --git a/internal/php8/lexer.go b/internal/php8/lexer.go index 8bb9d89..45a426e 100644 --- a/internal/php8/lexer.go +++ b/internal/php8/lexer.go @@ -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 diff --git a/internal/php8/scanner.go b/internal/php8/scanner.go index 5d7951e..751a5bd 100644 --- a/internal/php8/scanner.go +++ b/internal/php8/scanner.go @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/internal/php8/scanner.rl b/internal/php8/scanner.rl index a457a1e..cc99322 100644 --- a/internal/php8/scanner.rl +++ b/internal/php8/scanner.rl @@ -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 := |* diff --git a/internal/php8/scanner_test.go b/internal/php8/scanner_test.go index 90809b9..d7ca569 100644 --- a/internal/php8/scanner_test.go +++ b/internal/php8/scanner_test.go @@ -405,6 +405,52 @@ func TestShebang(test *testing.T) { assert.DeepEqual(test, expected, actual) } +func TestHaltCompilerComments(test *testing.T) { + src := "