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: comment precedence (again) #224

Merged
merged 2 commits into from
Feb 25, 2024
Merged
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
6 changes: 3 additions & 3 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ module.exports = function defineGrammar(dialect) {

text: _ => repeat1(choice(
token(prec(-1, /</)),
/[^\s<][^<]*/,
token(prec(1, /[^\s<][^<]*/)),
)),

_statement: $ => choice(
Expand Down Expand Up @@ -1553,7 +1553,7 @@ module.exports = function defineGrammar(dialect) {
keyword('static'),
),

comment: _ => token(prec(-1, choice(
comment: _ => token(choice(
seq(
choice('//', /#[^?\[?\r?\n]/),
repeat(/[^?\r?\n]|\?[^>\r\n]/),
Expand All @@ -1565,7 +1565,7 @@ module.exports = function defineGrammar(dialect) {
/[^*]*\*+([^/*][^*]*\*+)*/,
'/',
),
))),
)),

_semicolon: $ => choice($._automatic_semicolon, ';'),
},
Expand Down
24 changes: 23 additions & 1 deletion common/test/corpus/bugs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ foo(void: null);
function: (name)
arguments: (arguments (argument name: (name) (null))))))


=========================================
#221: Error when closing tag is in a string
=========================================
Expand Down Expand Up @@ -209,3 +208,26 @@ $foo = '?>';
(assignment_expression
left: (variable_name (name))
right: (string (string_value)))))

=========================================
#223: Error with comment inside binary expression
=========================================

<?php
if (
true
// this is a comment
|| false
) {}

---

(program
(php_tag)
(if_statement
condition: (parenthesized_expression
(binary_expression
left: (boolean)
(comment)
right: (boolean)))
body: (compound_statement)))
2 changes: 2 additions & 0 deletions common/test/corpus/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ okay to do';
'#valid regexp#';
'hello#world';
'hello//world';
'//hello world';
'/*valid regexp*/';
'/*valid regexp';

Expand All @@ -474,6 +475,7 @@ okay to do';
(expression_statement (string (string_value)))
(expression_statement (string (string_value)))
(expression_statement (string (string_value)))
(expression_statement (string (string_value)))
(expression_statement (string (string_value))))

=========================================
Expand Down
8 changes: 4 additions & 4 deletions common/test/highlight/literals.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
OMG;
// <- string

echo true, TRUE, false, FALSE
echo true, TRUE, false, FALSE;
// ^ constant.builtin
// ^ constant.builtin
// ^ constant.builtin
// ^ constant.builtin

echo PI_314
echo PI_314;
// ^ constant

echo __DIR__
// ^ constant.builtin
echo __DIR__;
// ^ constant.builtin
129 changes: 66 additions & 63 deletions php/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,15 @@
}
},
{
"type": "PATTERN",
"value": "[^\\s<][^<]*"
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[^\\s<][^<]*"
}
}
}
]
}
Expand Down Expand Up @@ -8980,71 +8987,67 @@
"comment": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": -1,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "//"
},
{
"type": "PATTERN",
"value": "#[^?\\[?\\r?\\n]"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "//"
},
{
"type": "PATTERN",
"value": "[^?\\r?\\n]|\\?[^>\\r\\n]"
"value": "#[^?\\[?\\r?\\n]"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\?\\r?\\n"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": "#"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
]
},
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
"value": "[^?\\r?\\n]|\\?[^>\\r\\n]"
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "\\?\\r?\\n"
},
{
"type": "BLANK"
}
]
}
]
},
{
"type": "STRING",
"value": "#"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
}
]
}
]
}
},
"_semicolon": {
Expand Down
Loading
Loading