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

Allow identifiers with trailing ? or ! in more places #88

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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,
],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this necessary?

],

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(
Copy link
Member Author

Choose a reason for hiding this comment

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

Outside of params, this and type declaration I don't expect to change. What are your thoughts on just adding these?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think I understand, which ones specifically?

Copy link
Member Author

Choose a reason for hiding this comment

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

type declaration names and c struct / union fields

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,
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
))
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 @@ -2478,6 +2529,7 @@ module.exports = grammar({
'out',
choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.underscore,
$.macro_var,
Expand All @@ -2489,6 +2541,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 @@ -2552,6 +2605,7 @@ module.exports = grammar({

const lhs = field('lhs', choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand All @@ -2570,6 +2624,7 @@ module.exports = grammar({
lhs_splat: $ => seq('*', choice(
$.underscore,
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand All @@ -2583,6 +2638,7 @@ module.exports = grammar({
const lhs_basic = choice(
$.underscore,
$.identifier,
alias($.identifier_method_call, $.identifier),
$.instance_var,
$.class_var,
$.macro_var,
Expand All @@ -2608,7 +2664,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 @@ -2618,7 +2678,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 @@ -2643,11 +2705,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
Loading