Skip to content

Commit

Permalink
feat: support enum
Browse files Browse the repository at this point in the history
  • Loading branch information
victorhqc committed Sep 19, 2019
1 parent eaf6eb9 commit dbfd7c4
Show file tree
Hide file tree
Showing 5 changed files with 5,435 additions and 4,881 deletions.
20 changes: 20 additions & 0 deletions corpus/enum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=====================
Enum declaration
=====================

enum Role {
USER
ADMIN
}

---

(program
(enum_declaration
(identifier)
(enum_block
(enumeral)
(enumeral)
)
)
)
15 changes: 15 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,18 @@ module.exports = grammar({
),
)),

enum_declaration: $ => seq(
'enum',
$.identifier,
$.enum_block,
),

_declaration: $ => choice(
$.datasource_declaration,
$.model_declaration,
$.generator_declaration,
$.type_declaration,
$.enum_declaration
),

comment: $ => token(
Expand All @@ -96,6 +103,12 @@ module.exports = grammar({
'}'
)),

enum_block: $ => prec.right(seq(
'{',
repeat($.enumeral),
'}'
)),

_statement: $ => choice(
$.column_declaration,
$.block_attribute_declaration,
Expand Down Expand Up @@ -252,6 +265,8 @@ module.exports = grammar({
seq('"', /([^"\n]|\\(.|\n))*/, '"')
)),

enumeral: $ => /[a-zA-Z-_][a-zA-Z0-9-_]*/,

number: $ => /\d+/,

array: $ => seq(
Expand Down
49 changes: 49 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
]
}
},
"enum_declaration": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "enum"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "enum_block"
}
]
},
"_declaration": {
"type": "CHOICE",
"members": [
Expand All @@ -97,6 +114,10 @@
{
"type": "SYMBOL",
"name": "type_declaration"
},
{
"type": "SYMBOL",
"name": "enum_declaration"
}
]
},
Expand Down Expand Up @@ -140,6 +161,30 @@
]
}
},
"enum_block": {
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "enumeral"
}
},
{
"type": "STRING",
"value": "}"
}
]
}
},
"_statement": {
"type": "CHOICE",
"members": [
Expand Down Expand Up @@ -1085,6 +1130,10 @@
]
}
},
"enumeral": {
"type": "PATTERN",
"value": "[a-zA-Z-_][a-zA-Z0-9-_]*"
},
"number": {
"type": "PATTERN",
"value": "\\d+"
Expand Down
56 changes: 52 additions & 4 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,45 @@
]
}
},
{
"type": "enum_block",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "enumeral",
"named": true
}
]
}
},
{
"type": "enum_declaration",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "enum_block",
"named": true
},
{
"type": "identifier",
"named": true
}
]
}
},
{
"type": "enumeral",
"named": true,
"fields": {}
},
{
"type": "formal_parameters",
"named": true,
Expand Down Expand Up @@ -586,6 +625,11 @@
]
}
},
{
"type": "identifier",
"named": true,
"fields": {}
},
{
"type": "member_expression",
"named": true,
Expand Down Expand Up @@ -712,6 +756,10 @@
"type": "datasource_declaration",
"named": true
},
{
"type": "enum_declaration",
"named": true
},
{
"type": "generator_declaration",
"named": true
Expand Down Expand Up @@ -900,6 +948,10 @@
"type": "type",
"named": false
},
{
"type": "enum",
"named": false
},
{
"type": "comment",
"named": true
Expand Down Expand Up @@ -1032,10 +1084,6 @@
"type": ")",
"named": false
},
{
"type": "identifier",
"named": true
},
{
"type": "string",
"named": true
Expand Down
Loading

0 comments on commit dbfd7c4

Please sign in to comment.