Skip to content

Commit

Permalink
feat: handle same rules as fields
Browse files Browse the repository at this point in the history
  • Loading branch information
joker1007 committed Nov 22, 2023
1 parent 5ef9eaa commit 4cbe7f6
Show file tree
Hide file tree
Showing 4 changed files with 5,767 additions and 5,641 deletions.
14 changes: 8 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ module.exports = grammar({

singleton_type: $ => seq("singleton", "(", $.class_name, ")"),

union_type: $ => prec.left(1, seq($.type, "|", $.type)),
union_type: $ => prec.left(1, seq(field("left", $.type), "|", field("right", $.type))),

intersection_type: $ => prec.left(2, seq($.type, "&", $.type)),
intersection_type: $ => prec.left(2, seq(field("left", $.type), "&", field("right", $.type))),

optional_type: $ => prec(3, seq($.type, token.immediate("?"))),

Expand Down Expand Up @@ -176,8 +176,8 @@ module.exports = grammar({

module_self_type_binds: $ => seq(":", $.module_self_types),

class_alias_decl: $ => seq("class", $.class_name, "=", $.class_name),
module_alias_decl: $ => seq("module", alias($.class_name, $.module_name), "=", alias($.class_name, $.module_name)),
class_alias_decl: $ => seq("class", field("new_name", $.class_name), "=", field("origin_name", $.class_name)),
module_alias_decl: $ => seq("module", field("new_name", alias($.class_name, $.module_name)), "=", field("origin_name", alias($.class_name, $.module_name))),

module_self_types: $ => choice(
seq($.class_name, optional($.type_arguments), optional(seq(",", $.module_self_types))),
Expand Down Expand Up @@ -319,9 +319,11 @@ module.exports = grammar({
seq("prepend", $.interface_name, optional($.type_arguments)),
),

singleton_method_name: $ => seq($.self, ".", $.method_name),

alias_member: $ => choice(
seq("alias", $.method_name, $.method_name),
seq("alias", $.self, ".", $.method_name, $.self, ".", $.method_name),
seq("alias", field("new_name", $.method_name), field("origin_name", $.method_name)),
seq("alias", field("new_name", $.singleton_method_name), field("origin_name", $.singleton_method_name)),
),

ivar_name: $ => /@[a-zA-Z]\w+/,
Expand Down
145 changes: 97 additions & 48 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,24 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "type"
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "type"
}
},
{
"type": "STRING",
"value": "|"
},
{
"type": "SYMBOL",
"name": "type"
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "type"
}
}
]
}
Expand All @@ -286,16 +294,24 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "type"
"type": "FIELD",
"name": "left",
"content": {
"type": "SYMBOL",
"name": "type"
}
},
{
"type": "STRING",
"value": "&"
},
{
"type": "SYMBOL",
"name": "type"
"type": "FIELD",
"name": "right",
"content": {
"type": "SYMBOL",
"name": "type"
}
}
]
}
Expand Down Expand Up @@ -1174,16 +1190,24 @@
"value": "class"
},
{
"type": "SYMBOL",
"name": "class_name"
"type": "FIELD",
"name": "new_name",
"content": {
"type": "SYMBOL",
"name": "class_name"
}
},
{
"type": "STRING",
"value": "="
},
{
"type": "SYMBOL",
"name": "class_name"
"type": "FIELD",
"name": "origin_name",
"content": {
"type": "SYMBOL",
"name": "class_name"
}
}
]
},
Expand All @@ -1195,26 +1219,34 @@
"value": "module"
},
{
"type": "ALIAS",
"type": "FIELD",
"name": "new_name",
"content": {
"type": "SYMBOL",
"name": "class_name"
},
"named": true,
"value": "module_name"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "class_name"
},
"named": true,
"value": "module_name"
}
},
{
"type": "STRING",
"value": "="
},
{
"type": "ALIAS",
"type": "FIELD",
"name": "origin_name",
"content": {
"type": "SYMBOL",
"name": "class_name"
},
"named": true,
"value": "module_name"
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "class_name"
},
"named": true,
"value": "module_name"
}
}
]
},
Expand Down Expand Up @@ -2810,6 +2842,23 @@
}
]
},
"singleton_method_name": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "self"
},
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "method_name"
}
]
},
"alias_member": {
"type": "CHOICE",
"members": [
Expand All @@ -2821,12 +2870,20 @@
"value": "alias"
},
{
"type": "SYMBOL",
"name": "method_name"
"type": "FIELD",
"name": "new_name",
"content": {
"type": "SYMBOL",
"name": "method_name"
}
},
{
"type": "SYMBOL",
"name": "method_name"
"type": "FIELD",
"name": "origin_name",
"content": {
"type": "SYMBOL",
"name": "method_name"
}
}
]
},
Expand All @@ -2838,28 +2895,20 @@
"value": "alias"
},
{
"type": "SYMBOL",
"name": "self"
},
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "method_name"
},
{
"type": "SYMBOL",
"name": "self"
},
{
"type": "STRING",
"value": "."
"type": "FIELD",
"name": "new_name",
"content": {
"type": "SYMBOL",
"name": "singleton_method_name"
}
},
{
"type": "SYMBOL",
"name": "method_name"
"type": "FIELD",
"name": "origin_name",
"content": {
"type": "SYMBOL",
"name": "singleton_method_name"
}
}
]
}
Expand Down
Loading

0 comments on commit 4cbe7f6

Please sign in to comment.