Skip to content

Commit

Permalink
Implement parsing of implementations and traits
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Apr 23, 2024
1 parent 4223066 commit 8917c7b
Show file tree
Hide file tree
Showing 9 changed files with 8,453 additions and 2,332 deletions.
116 changes: 108 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = grammar({
name: 'inko',
word: $ => $.identifier,
extras: $ => [
$.comment,
$.line_comment,
/\s/,
],

Expand All @@ -31,6 +31,9 @@ module.exports = grammar({
$.external_function,
$.module_method,
$.class,
$.trait,
$.implement_trait,
$.reopen_class,
),

// Imports
Expand All @@ -53,7 +56,7 @@ module.exports = grammar({
// Methods
external_function: $ => seq(
'fn',
field('visibility', optional($.public)),
field('visibility', optional($.visibility)),
'extern',
field('name', $.identifier),
field('arguments', optional(alias($.extern_arguments, $.arguments))),
Expand All @@ -68,7 +71,7 @@ module.exports = grammar({
),
module_method: $ => seq(
'fn',
field('visibility', optional($.public)),
field('visibility', optional($.visibility)),
field('name', $.identifier),
field('type_parameters', optional($.type_parameters)),
field('arguments', optional(alias($.method_arguments, $.arguments))),
Expand Down Expand Up @@ -101,17 +104,113 @@ module.exports = grammar({
alias($.constant, $.type),
),
mutable_requirement: _ => 'mut',
public: _ => 'pub',
visibility: _ => 'pub',
_method_modifier: _ => choice(
'mut',
'move',
'async mut',
'async',
'static',
),

// Classes
// TODO: modifiers
// TODO: type parameters
class: $ => seq(
'class',
field('visibility', optional($.visibility)),
field('modifier', optional(alias($._class_modifier, $.modifier))),
field('name', $.constant),
field('type_parameters', optional($.type_parameters)),
field('body', $.class_body),
),
class_body: $ => seq('{', '}'),
_class_modifier: $ => choice('async', 'builtin', 'enum'),
class_body: $ => seq('{', repeat($._class_expression) , '}'),
_class_expression: $ => choice(
$.field,
$.case,
alias($.class_method, $.method),
),
field: $ => seq(
'let',
field('name', $.field_name),
':',
field('type', $._type),
),
case: $ => seq(
'case',
field('name', $.constant),
field('arguments', optional(alias($.case_arguments, $.arguments))),
),
case_arguments: $ => seq('(', comma_list($._type), ')'),
class_method: $ => seq(
'fn',
field('visibility', optional($.visibility)),
field('modifier', optional(alias($._method_modifier, $.modifier))),
field('name', $.identifier),
field('type_parameters', optional($.type_parameters)),
field('arguments', optional(alias($.method_arguments, $.arguments))),
field('returns', optional($._returns)),
field('body', $.block),
),

// Traits
trait: $ => seq(
'trait',
field('visibility', optional($.visibility)),
field('name', $.constant),
field('type_parameters', optional($.type_parameters)),
field('requirements', optional($.required_traits)),
field('body', $.trait_body),
),
trait_body: $ => seq('{', repeat(alias($.trait_method, $.method)) , '}'),
trait_method: $ => seq(
'fn',
field('visibility', optional($.visibility)),
field('modifier', optional(alias($._trait_method_modifier, $.modifier))),
field('name', $.identifier),
field('type_parameters', optional($.type_parameters)),
field('arguments', optional(alias($.method_arguments, $.arguments))),
field('returns', optional($._returns)),
field('body', optional($.block)),
),
_trait_method_modifier: _ => choice('mut', 'move'),
required_traits: $ => seq(':', list($._required_trait, '+', false)),
_required_trait: $ => choice($.generic_type, alias($.constant, $.type)),

// Implementing traits
implement_trait: $ => seq(
'impl',
field('trait', choice($.generic_type, alias($.constant, $.type))),
'for',
field('class', $.constant),
field('bounds', optional($.bounds)),
field('body', $.implement_trait_body),
),
implement_trait_body: $ => seq(
'{',
repeat(alias($.class_method, $.method)) ,
'}'
),
bounds: $ => seq('if', comma_list($.bound)),
bound: $ => seq(
field('name', $.constant),
field('requirements', alias($.bound_requirements, $.requirements)),
),
bound_requirements: $ => seq(
':',
list($._type_parameter_requirement, '+', false),
),

// Reopening classes
reopen_class: $ => seq(
'impl',
field('name', $.constant),
field('body', $.reopen_class_body),
),
reopen_class_body: $ => seq(
'{',
repeat(alias($.class_method, $.method)) ,
'}'
),

// Type signatures
_type: $ => choice(
Expand Down Expand Up @@ -142,8 +241,9 @@ module.exports = grammar({

// Various terminals (e.g. identifiers)
self: _ => 'self',
comment: _ => token(seq('#', /.*/)),
line_comment: _ => token(seq('#', /.*/)),
identifier: _ => /([a-z]|_)[a-zA-Z0-9_]*/,
field_name: _ => /@[a-zA-Z0-9_]+/,
constant: _ => /[A-Z][a-zA-Z0-9_]*/
}
});
Loading

0 comments on commit 8917c7b

Please sign in to comment.