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

Add support for using keywords in type declarations #69

Draft
wants to merge 6 commits 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
138 changes: 110 additions & 28 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const operator_tokens = [
'&**',
]

function reserved(wordset, rule) {
return rule
}

module.exports = grammar({
name: 'crystal',

Expand Down Expand Up @@ -340,6 +344,49 @@ module.exports = grammar({
],
],

reserved: {
global: $ => [
'abstract',
'alias',
'annotation',
'asm',
'begin',
'break',
'case',
'class',
'def',
'end',
'enum',
'extend',
'false',
'fun',
'include',
'instance_sizeof',
'lib',
'macro',
'module',
'next',
'nil',
'offsetof',
'private',
'protected',
'require',
'return',
'select',
'sizeof',
'struct',
'true',
'typeof',
'until',
'while',
'with',
'yield',
],

method_name: $ => [],
type_declaration: $ => [],
},

rules: {
expressions: $ => seq(
optional($._statements),
Expand Down Expand Up @@ -543,6 +590,8 @@ module.exports = grammar({
$.typeof,
$.sizeof,
$.instance_sizeof,
$.alignof,
$.instance_alignof,
$.offsetof,
// TODO
// super
Expand Down Expand Up @@ -1043,7 +1092,7 @@ module.exports = grammar({
'annotation',
field('name', $.constant),
repeat($._terminator),
'end',
$._end,
),

annotation: $ => {
Expand Down Expand Up @@ -1080,7 +1129,7 @@ module.exports = grammar({
'module',
field('name', choice($.constant, $.generic_type)),
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
),

class_def: $ => seq(
Expand All @@ -1092,7 +1141,7 @@ module.exports = grammar({
'<', field('superclass', choice($.constant, $.generic_instance_type)),
)),
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
),

struct_def: $ => seq(
Expand All @@ -1104,7 +1153,7 @@ module.exports = grammar({
'<', field('superclass', choice($.constant, $.generic_instance_type)),
)),
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
),

enum_def: $ => seq(
Expand All @@ -1113,15 +1162,15 @@ module.exports = grammar({
field('name', alias($._constant_segment, $.constant)),
optional(field('type', seq(/:\s/, $._bare_type))),
field('body', seq(optional(alias($._enum_statements, $.expressions)))),
'end',
$._end,
),

lib_def: $ => seq(
optional(field('visibility', $.private)),
'lib',
field('name', $.constant, $.generic_type),
field('body', seq(optional(alias($._lib_statements, $.expressions)))),
'end',
$._end,
),

top_level_fun_def: $ => {
Expand All @@ -1146,7 +1195,7 @@ module.exports = grammar({
optional(return_type),
)),
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
)
},

Expand Down Expand Up @@ -1221,7 +1270,7 @@ module.exports = grammar({
'struct',
name,
$._c_struct_expressions,
'end',
$._end,
)
},

Expand Down Expand Up @@ -1262,7 +1311,7 @@ module.exports = grammar({
'union',
name,
$._union_expressions,
'end',
$._end,
)
},

Expand Down Expand Up @@ -1344,7 +1393,7 @@ module.exports = grammar({
optional($._terminator),
field('body', seq(optional(alias($._statements, $.expressions)))),
optional($._rescue_else_ensure),
'end',
$._end,
)
},

Expand Down Expand Up @@ -1385,7 +1434,7 @@ module.exports = grammar({
)),
$._terminator,
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
)
},

Expand Down Expand Up @@ -1494,17 +1543,30 @@ module.exports = grammar({
alias($.argument_list_no_parens, $.argument_list),
),

return: $ => seq('return', optional($._control_expressions)),
return: $ => seq(
'return',
optional($._control_expressions),
),

next: $ => seq('next', optional($._control_expressions)),
next: $ => seq(
'next',
optional($._control_expressions),
),

break: $ => seq('break', optional($._control_expressions)),
break: $ => seq(
'break',
optional($._control_expressions),
),

yield: $ => {
const with_expr = field('with', $._expression)

return seq(
optional(seq('with', with_expr, $._end_of_with_expression)),
optional(seq(
'with',
with_expr,
$._end_of_with_expression,
)),
'yield',
optional($._control_expressions),
)
Expand Down Expand Up @@ -1533,6 +1595,20 @@ module.exports = grammar({
')',
),

alignof: $ => seq(
'alignof',
'(',
$._bare_type,
')',
),

instance_alignof: $ => seq(
'instance_alignof',
'(',
$._bare_type,
')',
),

offsetof: $ => seq(
'offsetof',
'(',
Expand Down Expand Up @@ -1699,6 +1775,8 @@ module.exports = grammar({
$.constant,
$.sizeof,
$.instance_sizeof,
$.alignof,
$.instance_alignof,
$.offsetof,
),

Expand Down Expand Up @@ -1842,12 +1920,12 @@ module.exports = grammar({
_dot_call: $ => {
const receiver = field('receiver', $._expression)

const method = field('method', choice(
const method = field('method', reserved('method_name', choice(
$.identifier,
alias($.identifier_method_call, $.identifier),
alias($._operator_token, $.operator),
$.instance_var,
))
)))

return prec('dot_operator', seq(receiver, '.', method))
},
Expand Down Expand Up @@ -2494,7 +2572,9 @@ module.exports = grammar({
},

type_declaration: $ => {
const variable = field('var', choice($.identifier, $.instance_var, $.class_var, $.macro_var))
const variable = field('var', reserved('method_name', choice(
$.identifier, $.instance_var, $.class_var, $.macro_var,
)))
const type = field('type', $._bare_type)
const value = field('value', $._expression)

Expand Down Expand Up @@ -2559,7 +2639,7 @@ module.exports = grammar({
optional(params),
field('body', seq(optional(alias($._statements, $.expressions)))),
optional($._rescue_else_ensure),
'end',
$._end,
)
},

Expand Down Expand Up @@ -2589,9 +2669,11 @@ module.exports = grammar({
optional($._terminator),
field('body', seq(optional(alias($._statements, $.expressions)))),
optional($._rescue_else_ensure),
'end',
$._end,
),

_end: $ => 'end',

rescue: $ => {
const rescue_variable = field('variable', $.identifier)
const rescue_type = field('type', $._bare_type)
Expand Down Expand Up @@ -2651,15 +2733,15 @@ module.exports = grammar({
field('cond', $._expression),
$._terminator,
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
),

until: $ => seq(
'until',
field('cond', $._expression),
$._terminator,
field('body', seq(optional(alias($._statements, $.expressions)))),
'end',
$._end,
),

if: $ => {
Expand All @@ -2673,7 +2755,7 @@ module.exports = grammar({
$._terminator,
optional(then),
optional(else_),
'end',
$._end,
)
},

Expand All @@ -2688,7 +2770,7 @@ module.exports = grammar({
$._terminator,
optional(then),
optional(else_),
'end',
$._end,
)
},

Expand Down Expand Up @@ -2759,7 +2841,7 @@ module.exports = grammar({
optional(cond),
repeat($.when),
optional($.else),
'end',
$._end,
)
},

Expand All @@ -2768,7 +2850,7 @@ module.exports = grammar({
'select',
repeat($.when),
optional($.else),
'end',
$._end,
)
},

Expand Down Expand Up @@ -2798,12 +2880,12 @@ module.exports = grammar({
'case',
cond,
repeat1($.in),
'end',
$._end,
)
},

asm: $ => seq(
token(seq('asm', /\s*/, '(')),
seq('asm', /\s*/, '('),
field('text', $.string),
optional($._asm_outputs),
')',
Expand Down
Loading