Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move string_value to external scanner #235

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 3 additions & 4 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module.exports = function defineGrammar(dialect) {

externals: $ => [
$._automatic_semicolon,
$.string_value,
$.encapsed_string_chars,
$.encapsed_string_chars_after_variable,
$.execution_string_chars,
Expand Down Expand Up @@ -1348,16 +1349,14 @@ module.exports = function defineGrammar(dialect) {
'"',
)),

string: $ => seq(
string: $ => prec.right(seq(
choice(/[bB]'/, '\''),
repeat(choice(
alias(token(choice('\\\\', '\\\'')), $.escape_sequence),
$.string_value,
)),
'\'',
),

string_value: _ => token(prec(1, repeat1(/\\?[^'\\]/))),
)),

heredoc_body: $ => seq($._new_line,
repeat1(prec.right(
Expand Down
23 changes: 23 additions & 0 deletions common/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

enum TokenType {
AUTOMATIC_SEMICOLON,
STRING_VALUE,
ENCAPSED_STRING_CHARS,
ENCAPSED_STRING_CHARS_AFTER_VARIABLE,
EXECUTION_STRING_CHARS,
Expand Down Expand Up @@ -288,6 +289,23 @@ static inline bool scan_nowdoc_string(Scanner *scanner, TSLexer *lexer) {
return false;
}

static bool scan_string_value(Scanner *scanner, TSLexer *lexer) {
for (bool has_content = false;; has_content = true) {
lexer->mark_end(lexer);
if (lexer->lookahead == '\'' || lexer->eof(lexer)) {
return has_content;
}
if (lexer->lookahead == '\\') {
advance(lexer);
if (lexer->lookahead == '\'' || lexer->lookahead == '\\') {
return has_content;
}
}
advance(lexer);
}
return false;
}

static bool scan_encapsed_part_string(Scanner *scanner, TSLexer *lexer, bool is_after_variable, bool is_heredoc,
bool is_execution_string) {
bool has_consumed_content = false;
Expand Down Expand Up @@ -447,6 +465,11 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {

lexer->mark_end(lexer);

if (valid_symbols[STRING_VALUE]) {
lexer->result_symbol = STRING_VALUE;
return scan_string_value(scanner, lexer);
}

if (valid_symbols[ENCAPSED_STRING_CHARS_AFTER_VARIABLE]) {
lexer->result_symbol = ENCAPSED_STRING_CHARS_AFTER_VARIABLE;
return scan_encapsed_part_string(scanner, lexer,
Expand Down
15 changes: 15 additions & 0 deletions common/test/corpus/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,18 @@ Unicode escape sequences
(expression_statement (encapsed_string (escape_sequence))) (comment)
(expression_statement (encapsed_string (escape_sequence))) (comment))

==============================
#234 Single quoted string with escape sequences and comment chars
==============================

<?php

'#^(\\\')#';

---

(program
(php_tag)
(expression_statement
(string
(string_value) (escape_sequence) (escape_sequence) (string_value))))
110 changes: 52 additions & 58 deletions php/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -7280,72 +7280,62 @@
}
},
"string": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[bB]'"
},
{
"type": "STRING",
"value": "'"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\\\\"
},
{
"type": "STRING",
"value": "\\'"
}
]
}
},
"named": true,
"value": "escape_sequence"
"type": "PATTERN",
"value": "[bB]'"
},
{
"type": "SYMBOL",
"name": "string_value"
"type": "STRING",
"value": "'"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\\\\"
},
{
"type": "STRING",
"value": "\\'"
}
]
}
},
"named": true,
"value": "escape_sequence"
},
{
"type": "SYMBOL",
"name": "string_value"
}
]
}
},
{
"type": "STRING",
"value": "'"
}
},
{
"type": "STRING",
"value": "'"
}
]
},
"string_value": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "REPEAT1",
"content": {
"type": "PATTERN",
"value": "\\\\?[^'\\\\]"
}
}
]
}
},
"heredoc_body": {
Expand Down Expand Up @@ -9168,6 +9158,10 @@
"type": "SYMBOL",
"name": "_automatic_semicolon"
},
{
"type": "SYMBOL",
"name": "string_value"
},
{
"type": "SYMBOL",
"name": "encapsed_string_chars"
Expand Down
Loading
Loading