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

feat: use the new reserved rules api #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
52 changes: 40 additions & 12 deletions common/define-grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ const PREC = {
DEREF: 26,
};

const BASE_RESERVED_KEYWORD_SET = [
'abstract', 'and', 'as', 'break',
'callable', 'case', 'catch', 'class', 'clone', 'const',
'continue', 'declare', 'default', 'do', 'echo',
'else', 'elseif', 'enddeclare', 'endfor',
'endforeach', 'endif', 'endswitch', 'endwhile',
'extends', 'final', 'finally', 'fn', 'for',
'foreach', 'function', 'global', 'goto', 'if', 'implements',
'include', 'include_once', 'instanceof', 'insteadof',
'interface', 'match', 'namespace', 'new',
'or', 'print', 'private', 'protected', 'public', 'readonly',
'require', 'require_once', 'return', 'switch',
'throw', 'trait', 'try', 'use', 'while',
'xor', 'yield',
].map(r => keyword(r, false));

module.exports = function defineGrammar(dialect) {
if (dialect !== 'php' && dialect !== 'php_only') {
throw new Error(`Unknown dialect ${dialect}`);
Expand Down Expand Up @@ -96,6 +112,18 @@ module.exports = function defineGrammar(dialect) {
$._namespace_use_type,
],

reserved: {
global: _ => BASE_RESERVED_KEYWORD_SET.concat([keyword('static', false), keyword('var', false)]),
// 'var' is allowed in variable names
variables: _ => BASE_RESERVED_KEYWORD_SET.concat([keyword('static', false)]),
Comment on lines +117 to +118
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be empty like properties, all of these keywords are allowed as variables

// 'static' is allowed in types
types: _ => BASE_RESERVED_KEYWORD_SET.concat([keyword('var', false)]),
// 'class' is reserved in constants, but nothing else is
constants: _ => [keyword('class', false)],
// Everything is allowed in properties and methods
properties: _ => [],
},

supertypes: $ => [
$.statement,
$.expression,
Expand Down Expand Up @@ -349,10 +377,10 @@ module.exports = function defineGrammar(dialect) {
$.readonly_modifier,
)),

property_element: $ => seq(
property_element: $ => reserved('properties', seq(
field('name', $.variable_name),
optional(seq('=', field('default_value', $.expression))),
),
)),

property_hook_list: $ => seq('{', repeat($.property_hook), '}'),

Expand All @@ -374,7 +402,7 @@ module.exports = function defineGrammar(dialect) {
method_declaration: $ => seq(
optional(field('attributes', $.attribute_list)),
repeat($._modifier),
$._function_definition_header,
reserved('properties', $._function_definition_header),
choice(
field('body', $.compound_statement),
$._semicolon,
Expand Down Expand Up @@ -509,13 +537,13 @@ module.exports = function defineGrammar(dialect) {
optional($.property_hook_list),
),

simple_parameter: $ => seq(
simple_parameter: $ => reserved('properties', seq(
optional(field('attributes', $.attribute_list)),
field('type', optional($.type)),
optional(field('reference_modifier', $.reference_modifier)),
field('name', $.variable_name),
optional(seq('=', field('default_value', $.expression))),
),
)),

variadic_parameter: $ => seq(
optional(field('attributes', $.attribute_list)),
Expand All @@ -538,7 +566,7 @@ module.exports = function defineGrammar(dialect) {
$.primitive_type,
),

named_type: $ => choice($.name, $.qualified_name),
named_type: $ => reserved('types', choice($.name, $.qualified_name)),

optional_type: $ => seq(
'?',
Expand Down Expand Up @@ -591,7 +619,7 @@ module.exports = function defineGrammar(dialect) {

_return_type: $ => seq(':', field('return_type', choice($.type, $.bottom_type))),

const_element: $ => seq($._identifier, '=', $.expression),
const_element: $ => reserved('constants', seq($._identifier, '=', $.expression)),

echo_statement: $ => seq(keyword('echo'), $._expressions, $._semicolon),

Expand Down Expand Up @@ -950,14 +978,14 @@ module.exports = function defineGrammar(dialect) {

parenthesized_expression: $ => seq('(', $.expression, ')'),

class_constant_access_expression: $ => seq(
class_constant_access_expression: $ => reserved('constants', seq(
$._scope_resolution_qualifier,
'::',
choice(
$._identifier,
seq('{', alias($.expression, $.name), '}'),
),
),
)),

print_intrinsic: $ => seq(
keyword('print'), $.expression,
Expand Down Expand Up @@ -1214,7 +1242,7 @@ module.exports = function defineGrammar(dialect) {
),
),

_argument_name: $ => seq(
_argument_name: $ => reserved('properties', seq(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be great if this choices list can be cleaned up here---this was needed because the grammar was having trouble parsing named arguments that could also be casts or other types / functions:

e.g.,:

$this->foo(array: $bar); // named argument
$this->foo((array) $bar); // array cast
$this->foo(array(...)); // array function (long form of [...])

However, not sure if this new feature would clean this up or if this is merely an exclusion list

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the PR is currently adding support by default for non-reserved keywords to be usable as the word token if they overlap (the pattern for the word token matches the keyword in its entirety). So obviously operators wouldn't match, but any keywords like these would :) Really excited for that since a lot of grammars will no longer need this "_reserved_identifier" hack they currently have.

field('name', alias(
choice(
$.name,
Expand All @@ -1233,7 +1261,7 @@ module.exports = function defineGrammar(dialect) {
$.name,
)),
':',
),
)),

member_call_expression: $ => prec(PREC.CALL, seq(
field('object', $._dereferencable_expression),
Expand Down Expand Up @@ -1485,7 +1513,7 @@ module.exports = function defineGrammar(dialect) {
$.scoped_call_expression,
),

variable_name: $ => seq('$', $.name),
variable_name: $ => seq('$', reserved('variables', $.name)),

by_ref: $ => seq('&', $._variable),

Expand Down
Loading
Loading