Skip to content

Commit

Permalink
Allow identifiers with trailing ? or ! in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere committed Jan 6, 2025
1 parent 6172504 commit 08ec53f
Show file tree
Hide file tree
Showing 7 changed files with 976,015 additions and 959,980 deletions.
90 changes: 78 additions & 12 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ module.exports = grammar({
$.pseudo_call_argument_list,
$._parenthesized_type,
],

// Ternary has precedence over type declaration
[
$.call,
$.type_declaration,
],

// Want to prefer identifiers ending with `?`
[
$.identifier_method_call,
$.identifier,
],
],

conflicts: $ => [
Expand Down Expand Up @@ -981,6 +993,7 @@ module.exports = grammar({
method_proc: $ => {
const receiver = field('receiver', choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.self,
Expand Down Expand Up @@ -1180,7 +1193,10 @@ module.exports = grammar({
},

fun_param: $ => {
const name = field('name', choice($.identifier, $.constant))
const name = field('name', choice(
$.identifier, $.constant,
alias($.identifier_method_call, $.identifier),
))
const type = field('type', seq(/[ \t]:\s/, $._bare_type))

return seq(
Expand Down Expand Up @@ -1227,7 +1243,19 @@ module.exports = grammar({
),

c_struct_fields: $ => {
const names = seq($.identifier, repeat(seq(',', $.identifier)))
const names = seq(
choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
),
repeat(seq(
',',
choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
),
)),
)

return seq(
names,
Expand Down Expand Up @@ -1266,7 +1294,19 @@ module.exports = grammar({
),

union_fields: $ => {
const names = seq($.identifier, repeat(seq(',', $.identifier)))
const names = seq(
choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
),
repeat(seq(
',',
choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
),
)),
)

return seq(
names,
Expand Down Expand Up @@ -1425,7 +1465,9 @@ module.exports = grammar({
param: $ => {
const extern_name = field('extern_name', $.identifier)
const name = field('name', choice(
$.identifier, $.instance_var, $.class_var, $.macro_var, $.macro_expression,
$.identifier, alias($.identifier_method_call, $.identifier),
$.instance_var, $.class_var,
$.macro_var, $.macro_expression,
))
const type = field('type', seq(/[ \t]:\s/, $._bare_type))
const default_value = field('default', seq('=', $._expression))
Expand All @@ -1441,7 +1483,9 @@ module.exports = grammar({

splat_param: $ => {
const name = field('name', choice(
$.identifier, $.instance_var, $.class_var, $.macro_var, $.macro_expression,
$.identifier, alias($.identifier_method_call, $.identifier),
$.instance_var, $.class_var,
$.macro_var, $.macro_expression,
))
const type = field('type', seq(/[ \t]:\s/, $._bare_type))

Expand All @@ -1455,7 +1499,9 @@ module.exports = grammar({

double_splat_param: $ => {
const name = field('name', choice(
$.identifier, $.instance_var, $.class_var, $.macro_var, $.macro_expression,
$.identifier, alias($.identifier_method_call, $.identifier),
$.instance_var, $.class_var,
$.macro_var, $.macro_expression,
))
const type = field('type', seq(/[ \t]:\s/, $._bare_type))

Expand All @@ -1469,7 +1515,9 @@ module.exports = grammar({

block_param: $ => {
const name = field('name', choice(
$.identifier, $.instance_var, $.class_var, $.macro_var, $.macro_expression,
$.identifier, alias($.identifier_method_call, $.identifier),
$.instance_var, $.class_var,
$.macro_var, $.macro_expression,
))
const type = field('type', seq(/:\s/, $._bare_type))

Expand Down Expand Up @@ -1930,7 +1978,10 @@ module.exports = grammar({
)),

macro_for: $ => {
const var_name = field('var', choice($.underscore, $.identifier))
const var_name = field('var', choice(
$.underscore, $.identifier,
alias($.identifier_method_call, $.identifier),
))

return seq(
'for',
Expand Down Expand Up @@ -2477,6 +2528,7 @@ module.exports = grammar({
'out',
choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.underscore,
$.macro_var,
Expand All @@ -2488,6 +2540,7 @@ module.exports = grammar({
const lhs = field('lhs', choice(
$.underscore,
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand Down Expand Up @@ -2551,6 +2604,7 @@ module.exports = grammar({

const lhs = field('lhs', choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand All @@ -2569,6 +2623,7 @@ module.exports = grammar({
lhs_splat: $ => seq('*', choice(
$.underscore,
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand All @@ -2582,6 +2637,7 @@ module.exports = grammar({
const lhs_basic = choice(
$.underscore,
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand All @@ -2607,7 +2663,11 @@ module.exports = grammar({

uninitialized_assign: $ => {
return seq(
field('lhs', choice($.identifier, $.instance_var, $.class_var, $.global_var, $.macro_var)),
field('lhs', choice(
$.identifier, alias($.identifier_method_call, $.identifier),
$.instance_var, $.class_var,
$.global_var, $.macro_var,
)),
'=',
field('rhs', $.uninitialized_var),
)
Expand All @@ -2617,7 +2677,9 @@ module.exports = grammar({

type_declaration: $ => {
const variable = field('var', choice(
$.identifier, $.instance_var, $.class_var, $.macro_var, $.macro_expression,
$.identifier, alias($.identifier_method_call, $.identifier),
$.instance_var, $.class_var,
$.macro_var, $.macro_expression,
))
const type = field('type', $._bare_type)
const value = field('value', $._expression)
Expand All @@ -2642,11 +2704,15 @@ module.exports = grammar({
field('type', $._bare_type),
),

block_body_param: $ => field('name', $.identifier),
block_body_param: $ => field('name', choice(
$.identifier, alias($.identifier_method_call, $.identifier),
)),

block_body_splat_param: $ => seq(
'*',
field('name', $.identifier),
field('name', choice(
$.identifier, alias($.identifier_method_call, $.identifier),
)),
),

_block_body_nested_param: $ => {
Expand Down
Loading

0 comments on commit 08ec53f

Please sign in to comment.