Skip to content

Commit

Permalink
Merge pull request #280 from tamasvajk/contextual-keywords-var
Browse files Browse the repository at this point in the history
Add `var` to contextual keywords
  • Loading branch information
tamasvajk authored Jan 13, 2023
2 parents fcdfa2b + 524eb59 commit 98a2879
Show file tree
Hide file tree
Showing 5 changed files with 1,011,702 additions and 1,006,998 deletions.
98 changes: 98 additions & 0 deletions corpus/contextual-keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,101 @@ class set { }
(class_declaration
(identifier)
(declaration_list)))

================================================================================
Var contextual keyword
================================================================================

void var() { }
void m(var p) { }
void m(int var) { }
void m()
{
var v = null;
int var = var;
var var = 1;

var();
m(var);

var x = var + 1;
}

class var { }

--------------------------------------------------------------------------------

(compilation_unit
(global_statement
(local_function_statement
(predefined_type)
(identifier)
(parameter_list)
(block)))
(global_statement
(local_function_statement
(predefined_type)
(identifier)
(parameter_list
(parameter
(implicit_type)
(identifier)))
(block)))
(global_statement
(local_function_statement
(predefined_type)
(identifier)
(parameter_list
(parameter
(predefined_type)
(identifier)))
(block)))
(global_statement
(local_function_statement
(predefined_type)
(identifier)
(parameter_list)
(block
(local_declaration_statement
(variable_declaration
(implicit_type)
(variable_declarator
(identifier)
(equals_value_clause
(null_literal)))))
(local_declaration_statement
(variable_declaration
(predefined_type)
(variable_declarator
(identifier)
(equals_value_clause
(identifier)))))
(local_declaration_statement
(variable_declaration
(implicit_type)
(variable_declarator
(identifier)
(equals_value_clause
(integer_literal)))))
(expression_statement
(invocation_expression
(identifier)
(argument_list)))
(expression_statement
(invocation_expression
(identifier)
(argument_list
(argument
(identifier)))))
(local_declaration_statement
(variable_declaration
(implicit_type)
(variable_declarator
(identifier)
(equals_value_clause
(binary_expression
(identifier)
(integer_literal)))))))))
(class_declaration
(identifier)
(declaration_list)))
26 changes: 16 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module.exports = grammar({
[$._contextual_keywords, $.scoped_type],
[$._contextual_keywords, $.scoped_type, $._parameter_type_with_modifiers],
[$._contextual_keywords, $._parameter_type_with_modifiers],
[$._contextual_keywords, $.implicit_type],

[$._type, $.attribute],
[$._type, $._nullable_base_type],
Expand Down Expand Up @@ -228,7 +229,7 @@ module.exports = grammar({
),

attribute: $ => seq(
field('name', $._name),
field('name', $._type_name),
optional($.attribute_argument_list)
),

Expand Down Expand Up @@ -681,7 +682,7 @@ module.exports = grammar({
_type: $ => choice(
$.implicit_type,
$.array_type,
$._name,
$._type_name,
$.nullable_type,
$.pointer_type,
$.function_pointer_type,
Expand All @@ -691,7 +692,12 @@ module.exports = grammar({
$.scoped_type,
),

implicit_type: $ => 'var',
_type_name: $ => prec.dynamic(0, // `var` could be `_type_name`, but we prefer `implicit_type`
$._name
),

implicit_type: $ => prec.dynamic(1, // Higher precedence than `_type_name`
'var'),

array_type: $ => seq(
field('type', $._array_base_type),
Expand All @@ -700,7 +706,7 @@ module.exports = grammar({

_array_base_type: $ => choice(
$.array_type,
$._name,
$._type_name,
$.nullable_type,
$.pointer_type,
$.function_pointer_type,
Expand All @@ -716,15 +722,15 @@ module.exports = grammar({

_nullable_base_type: $ => choice(
$.array_type,
$._name,
$._type_name,
$.predefined_type,
$.tuple_type
),

pointer_type: $ => seq($._pointer_base_type, '*'),

_pointer_base_type: $ => choice(
$._name,
$._type_name,
$.nullable_type,
$.pointer_type,
$.function_pointer_type,
Expand Down Expand Up @@ -799,7 +805,7 @@ module.exports = grammar({
_ref_base_type: $ => choice(
$.implicit_type,
$.array_type,
$._name,
$._type_name,
$.nullable_type,
$.pointer_type,
$.function_pointer_type,
Expand All @@ -813,7 +819,7 @@ module.exports = grammar({
),

_scoped_base_type: $ => choice(
$._name,
$._type_name,
$.ref_type,
),

Expand Down Expand Up @@ -1376,7 +1382,7 @@ module.exports = grammar({
)),

_object_creation_type: $ => choice(
$._name,
$._type_name,
$.nullable_type,
$.predefined_type,
),
Expand Down Expand Up @@ -1833,7 +1839,7 @@ module.exports = grammar({
// 'set',
'unmanaged',
// 'value',
// 'var',
'var',
'when',
'where',
// 'with',
Expand Down
6 changes: 3 additions & 3 deletions script/file_sizes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
src/grammar.json 0.2MB 10861
src/grammar.json 0.2MB 10881
src/node-types.json 0.1MB 7645
src/parser.c 49.1MB 1546389
src/parser.c 49.3MB 1550969
src/scanner.c 0.0MB 29
total 49.5MB 1564924
total 49.7MB 1569524
Loading

0 comments on commit 98a2879

Please sign in to comment.