Skip to content

Commit

Permalink
Implemente php_only grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Oct 23, 2023
1 parent cd0b1b3 commit 5cbcad3
Show file tree
Hide file tree
Showing 9 changed files with 90,677 additions and 101,311 deletions.
45 changes: 33 additions & 12 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const PREC = {
};

module.exports = function defineGrammar(dialect) {
if (dialect !== 'php' && dialect !== 'php_only') {
throw new Error(`Unknown dialect ${dialect}`);
}

return grammar({
name: dialect,

Expand Down Expand Up @@ -87,20 +91,37 @@ module.exports = function defineGrammar(dialect) {
$._variable_name,
],

extras: $ => [
$.comment,
/[\s\uFEFF\u2060\u200B\u00A0]/,
$.text_interpolation
],
extras: $ => {
const extras = [
$.comment,
/[\s\uFEFF\u2060\u200B\u00A0]/,
];

if (dialect === 'php') {
extras.push($.text_interpolation);
}

return extras;
},

rules: {
program: $ => seq(
optional($.text),
optional(seq(
$.php_tag,
repeat($._statement)
))
),
program: $ => {
if (dialect === 'php') {
return seq(
optional($.text),
optional(seq(
$.php_tag,
repeat($._statement)
)),
);
}

return seq(
optional($.php_tag),
repeat($._statement),
optional('?>'),
);
},

php_tag: $ => /<\?([pP][hH][pP]|=)?/,

Expand Down
29 changes: 7 additions & 22 deletions common/test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ Associativity of conditional

<?php
true ? false : true ? "a" : "b";
?>

---

Expand All @@ -415,16 +414,14 @@ true ? false : true ? "a" : "b";
(boolean)
(boolean))
(encapsed_string (string_value))
(encapsed_string (string_value))))
(text_interpolation))
(encapsed_string (string_value)))))

=================================================
Associativity of null-coalescence
=================================================

<?php
NULL ?? NULL ?? 1;
?>

---

Expand All @@ -435,8 +432,7 @@ NULL ?? NULL ?? 1;
(null)
(binary_expression
(null)
(integer))))
(text_interpolation))
(integer)))))

=========================================
Associativity of negation
Expand All @@ -445,7 +441,6 @@ Associativity of negation
<?php
-1 * -1;
!$i instanceof object;
?>

---

Expand All @@ -462,8 +457,7 @@ Associativity of negation
(binary_expression
(variable_name
(name))
(name))))
(text_interpolation))
(name)))))

====================================
Augmented assignment
Expand All @@ -474,7 +468,6 @@ $i .= $j = "a";
$i += $j += 3;
$i .= "a" . "b";
"a" . $i .= "b";
?>

---

Expand Down Expand Up @@ -509,8 +502,7 @@ $i .= "a" . "b";
(augmented_assignment_expression
(variable_name
(name))
(encapsed_string (string_value)))))
(text_interpolation))
(encapsed_string (string_value))))))

=======================================
Nested assignemnts
Expand Down Expand Up @@ -649,7 +641,6 @@ Arrays
print_r([1, 2, 3]);
print_r(["foo" => "orange", "bar" => "apple", "baz" => "lemon"]);
$a = [...$values];
?>

---

Expand Down Expand Up @@ -678,13 +669,7 @@ $a = [...$values];
(array_creation_expression
(array_element_initializer
(variadic_unpacking
(variable_name (name))
)
)
)
)
)
(text_interpolation))
(variable_name (name))))))))

===============================================
Anonymous functions
Expand Down Expand Up @@ -1020,15 +1005,15 @@ function foo() {}
(arguments (variadic_placeholder))
)
)

(comment)
(expression_statement
(object_creation_expression
(name)
(arguments (variadic_placeholder))
)
)

(function_definition
(attribute_list
(attribute_group
Expand Down
12 changes: 6 additions & 6 deletions common/test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ TRUE;
false;
False;
FALSE;
?>

---

Expand All @@ -20,8 +19,7 @@ FALSE;
(expression_statement (boolean))
(expression_statement (boolean))
(expression_statement (boolean))
(expression_statement (boolean))
(text_interpolation))
(expression_statement (boolean)))

==========================
Floats
Expand Down Expand Up @@ -85,14 +83,16 @@ Integers
Testing string scanner confirmance
==============================

<?php echo "\"\t\\'" . '\n\\\'a\\\b\\' ?>
<?php
echo "\"\t\\'" . '\n\\\'a\\\b\\';

---

(program
(php_tag)
(echo_statement (binary_expression (encapsed_string (escape_sequence) (escape_sequence) (escape_sequence) (string_value)) (string (string_value))))
(text_interpolation))
(echo_statement (binary_expression
(encapsed_string (escape_sequence) (escape_sequence) (escape_sequence) (string_value))
(string (string_value)))))

==========================
Shell command
Expand Down
50 changes: 1 addition & 49 deletions common/test/corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,52 +83,6 @@ endif;
body: (colon_block
(echo_statement (integer))))))

====================================
Wordpress colon blocks
====================================

<?php

if ($post) :
?>
<?php

if ( $open ) {
$attachment_id;
}

?>
<?php
else :
$post;

endif;

?>

---

(program
(php_tag)
(if_statement
condition: (parenthesized_expression (variable_name (name)))
body: (colon_block
(text_interpolation (php_tag))
(if_statement
condition: (parenthesized_expression (variable_name (name)))
body: (compound_statement (expression_statement (variable_name (name))))
)
)
(text_interpolation (php_tag))
alternative: (else_clause
body: (colon_block
(expression_statement (variable_name (name)))
)
)
)
(text_interpolation)
)

==============================
While statements
==============================
Expand Down Expand Up @@ -222,7 +176,6 @@ switch ($a) {
echo "bad";
break;
}
?>

---

Expand All @@ -238,8 +191,7 @@ switch ($a) {
value: (integer)
(echo_statement (encapsed_string (string_value))) (break_statement))
(default_statement
(echo_statement (encapsed_string (string_value))) (break_statement))))
(text_interpolation))
(echo_statement (encapsed_string (string_value))) (break_statement)))))

==============================
Alternative switch statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ echo "hi";


=======================================
Singel line comment without any content
Single line comment without any content
=======================================

<?php
Expand Down Expand Up @@ -186,3 +186,50 @@ a ?> b <?php c;
(php_tag)
(expression_statement
(name)))

====================================
WordPress colon blocks
====================================

<?php

if ($post) :
?>
<?php

if ( $open ) {
$attachment_id;
}

?>
<?php
else :
$post;

endif;

?>

---

(program
(php_tag)
(if_statement
condition: (parenthesized_expression (variable_name (name)))
body: (colon_block
(text_interpolation (php_tag))
(if_statement
condition: (parenthesized_expression (variable_name (name)))
body: (compound_statement (expression_statement (variable_name (name))))
)
)
(text_interpolation (php_tag))
alternative: (else_clause
body: (colon_block
(expression_statement (variable_name (name)))
)
)
)
(text_interpolation)
)

29 changes: 10 additions & 19 deletions php_only/src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@
"members": [
{
"type": "SYMBOL",
"name": "text"
"name": "php_tag"
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_statement"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "php_tag"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_statement"
}
}
]
"type": "STRING",
"value": "?>"
},
{
"type": "BLANK"
Expand Down Expand Up @@ -8944,10 +8939,6 @@
{
"type": "PATTERN",
"value": "[\\s\\uFEFF\\u2060\\u200B\\u00A0]"
},
{
"type": "SYMBOL",
"name": "text_interpolation"
}
],
"conflicts": [
Expand Down
Loading

0 comments on commit 5cbcad3

Please sign in to comment.