tweaks to make closer to real parser, checked in as reference
This commit is contained in:
parent
bd6ecaee2c
commit
2aa998b6a5
1305
GroovyParser.g4
Normal file
1305
GroovyParser.g4
Normal file
File diff suppressed because it is too large
Load Diff
72
grammar.js
72
grammar.js
@ -1,64 +1,68 @@
|
||||
module.exports = grammar({
|
||||
name: 'Groovy',
|
||||
|
||||
conflicts: $ => [
|
||||
[$.qualified_name]
|
||||
],
|
||||
rules: {
|
||||
// TODO: add the actual grammar rules
|
||||
source_file: $ => seq(
|
||||
optional($._shebang_comment),
|
||||
optional(seq($.package_definition, repeat($._nl_semicolon))),
|
||||
repeat(
|
||||
choice(
|
||||
seq(
|
||||
$.import_statement,
|
||||
repeat1($._nl_semicolon)
|
||||
),
|
||||
seq(
|
||||
$.script_part,
|
||||
repeat1($._nl_semicolon)
|
||||
)
|
||||
)
|
||||
),
|
||||
optional($.script_part)
|
||||
optional(seq($.package_declaration, repeat($._sep))),
|
||||
optional($._script_statements)
|
||||
),
|
||||
static_modifier: $ => 'static',
|
||||
import_statement: $ => seq(
|
||||
_shebang_comment: $ => token.immediate(/#!.*\n/),
|
||||
_script_statements: $ => seq(
|
||||
$._script_statement,
|
||||
repeat(
|
||||
seq(
|
||||
$._sep,
|
||||
$._script_statement
|
||||
)
|
||||
),
|
||||
repeat($._sep)
|
||||
),
|
||||
import_declaration: $ => seq(
|
||||
'import',
|
||||
optional($.static_modifier),
|
||||
field('name',
|
||||
seq(
|
||||
$.identifier,
|
||||
repeat(seq('.', $.identifier)),
|
||||
optional(seq('.', '*')
|
||||
)
|
||||
seq($.qualified_name,
|
||||
optional($.wildcard_import)
|
||||
)
|
||||
)
|
||||
),
|
||||
_shebang_comment: $ => token.immediate(/#!.*\n/),
|
||||
package_definition: $ => seq(
|
||||
wildcard_import: $ => seq('.','*'),
|
||||
package_declaration: $ => seq(
|
||||
optional($._annotations),
|
||||
'package',
|
||||
field('name', seq(
|
||||
$.identifier,
|
||||
repeat(seq('.', $.identifier))))
|
||||
field('name', $.qualified_name)
|
||||
),
|
||||
script_part: $ => choice(
|
||||
_annotations: $ => seq(
|
||||
$.annotation,
|
||||
repeat($.annotation)
|
||||
),
|
||||
annotation: $ => seq(
|
||||
'@',
|
||||
$.qualified_name
|
||||
),
|
||||
_script_statement: $ => choice(
|
||||
$.import_declaration,
|
||||
$.statement,
|
||||
$.method_declaration
|
||||
),
|
||||
expression: $ => choice(
|
||||
$.path_expression
|
||||
$.qualified_name
|
||||
),
|
||||
path_expression: $ => seq(
|
||||
repeat(
|
||||
seq($.identifier, '.')
|
||||
),
|
||||
$.identifier
|
||||
qualified_name: $ => seq(
|
||||
$.identifier,
|
||||
repeat(seq('.', $.identifier))
|
||||
),
|
||||
identifier: $ => /[A-Za-z_][A-Za-z_0-9]*/,
|
||||
statement: $ => choice(
|
||||
$.expression
|
||||
),
|
||||
method_declaration: $ => seq('def', field('name', $.identifier)),
|
||||
_nl_semicolon: $ => choice('\n', ';'),
|
||||
_sep: $ => choice('\n', ';'),
|
||||
word: $ => $.identifier
|
||||
}
|
||||
});
|
||||
|
||||
204
src/grammar.json
204
src/grammar.json
@ -24,13 +24,13 @@
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "package_definition"
|
||||
"name": "package_declaration"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "_nl_semicolon"
|
||||
"name": "_sep"
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -40,52 +40,12 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "import_statement"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "_nl_semicolon"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "script_part"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "_nl_semicolon"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "script_part"
|
||||
"name": "_script_statements"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
@ -98,7 +58,46 @@
|
||||
"type": "STRING",
|
||||
"value": "static"
|
||||
},
|
||||
"import_statement": {
|
||||
"_shebang_comment": {
|
||||
"type": "IMMEDIATE_TOKEN",
|
||||
"content": {
|
||||
"type": "PATTERN",
|
||||
"value": "#!.*\\n"
|
||||
}
|
||||
},
|
||||
"_script_statements": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_script_statement"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_sep"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_script_statement"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "_sep"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"import_declaration": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
@ -125,28 +124,26 @@
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "."
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
}
|
||||
]
|
||||
}
|
||||
"name": "qualified_name"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "wildcard_import"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"wildcard_import": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
@ -159,26 +156,21 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"package_declaration": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_annotations"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"_shebang_comment": {
|
||||
"type": "IMMEDIATE_TOKEN",
|
||||
"content": {
|
||||
"type": "PATTERN",
|
||||
"value": "#!.*\\n"
|
||||
}
|
||||
},
|
||||
"package_definition": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "package"
|
||||
@ -187,36 +179,48 @@
|
||||
"type": "FIELD",
|
||||
"name": "name",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "qualified_name"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"_annotations": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
"name": "annotation"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "annotation"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"annotation": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "."
|
||||
"value": "@"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
"name": "qualified_name"
|
||||
}
|
||||
]
|
||||
},
|
||||
"script_part": {
|
||||
"_script_statement": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "import_declaration"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "statement"
|
||||
@ -232,32 +236,32 @@
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "path_expression"
|
||||
"name": "qualified_name"
|
||||
}
|
||||
]
|
||||
},
|
||||
"path_expression": {
|
||||
"qualified_name": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "."
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -291,7 +295,7 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"_nl_semicolon": {
|
||||
"_sep": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
@ -315,7 +319,11 @@
|
||||
"value": "\\s"
|
||||
}
|
||||
],
|
||||
"conflicts": [],
|
||||
"conflicts": [
|
||||
[
|
||||
"qualified_name"
|
||||
]
|
||||
],
|
||||
"precedences": [],
|
||||
"externals": [],
|
||||
"inline": [],
|
||||
|
||||
@ -1,4 +1,19 @@
|
||||
[
|
||||
{
|
||||
"type": "annotation",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "qualified_name",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "expression",
|
||||
"named": true,
|
||||
@ -8,14 +23,14 @@
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "path_expression",
|
||||
"type": "qualified_name",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "import_statement",
|
||||
"type": "import_declaration",
|
||||
"named": true,
|
||||
"fields": {
|
||||
"name": {
|
||||
@ -23,15 +38,11 @@
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "*",
|
||||
"named": false
|
||||
"type": "qualified_name",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": ".",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "identifier",
|
||||
"type": "wildcard_import",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
@ -65,54 +76,41 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "package_definition",
|
||||
"type": "package_declaration",
|
||||
"named": true,
|
||||
"fields": {
|
||||
"name": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": ".",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "identifier",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "path_expression",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "identifier",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "script_part",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": false,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "method_declaration",
|
||||
"type": "qualified_name",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": "annotation",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "statement",
|
||||
"type": "qualified_name",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "identifier",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
@ -127,15 +125,19 @@
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": "import_statement",
|
||||
"type": "import_declaration",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "package_definition",
|
||||
"type": "method_declaration",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "script_part",
|
||||
"type": "package_declaration",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "statement",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
@ -156,6 +158,11 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "wildcard_import",
|
||||
"named": true,
|
||||
"fields": {}
|
||||
},
|
||||
{
|
||||
"type": "\n",
|
||||
"named": false
|
||||
@ -172,6 +179,10 @@
|
||||
"type": ";",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "@",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "def",
|
||||
"named": false
|
||||
|
||||
1748
src/parser.c
1748
src/parser.c
File diff suppressed because it is too large
Load Diff
@ -6,8 +6,9 @@ import wibble
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(import_statement
|
||||
(identifier)))
|
||||
(import_declaration
|
||||
(qualified_name
|
||||
(identifier))))
|
||||
|
||||
================================================================================
|
||||
multiple identifier import
|
||||
@ -17,9 +18,10 @@ import wobble.qq
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(import_statement
|
||||
(import_declaration
|
||||
(qualified_name
|
||||
(identifier)
|
||||
(identifier)))
|
||||
(identifier))))
|
||||
|
||||
================================================================================
|
||||
wildcard import
|
||||
@ -29,9 +31,11 @@ import flobble.fnurfle.*
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(import_statement
|
||||
(import_declaration
|
||||
(qualified_name
|
||||
(identifier)
|
||||
(identifier)))
|
||||
(identifier))
|
||||
(wildcard_import)))
|
||||
|
||||
================================================================================
|
||||
multiple imports semicolon separated
|
||||
@ -40,11 +44,14 @@ import qq.ss; import flobble.*;
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(import_statement
|
||||
(import_declaration
|
||||
(qualified_name
|
||||
(identifier)
|
||||
(identifier))
|
||||
(import_statement
|
||||
(identifier)))
|
||||
(import_declaration
|
||||
(qualified_name
|
||||
(identifier))
|
||||
(wildcard_import)))
|
||||
|
||||
================================================================================
|
||||
static import
|
||||
@ -53,7 +60,21 @@ import static qq.q.*;
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(import_statement
|
||||
(import_declaration
|
||||
(static_modifier)
|
||||
(qualified_name
|
||||
(identifier)
|
||||
(identifier)))
|
||||
(identifier))
|
||||
(wildcard_import)))
|
||||
|
||||
================================================================================
|
||||
simple wildcard
|
||||
================================================================================
|
||||
import wibble.*;
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(import_declaration
|
||||
(qualified_name
|
||||
(identifier))
|
||||
(wildcard_import)))
|
||||
|
||||
@ -5,8 +5,9 @@ package flob
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(package_definition
|
||||
(identifier)))
|
||||
(package_declaration
|
||||
(qualified_name
|
||||
(identifier))))
|
||||
|
||||
================================================================================
|
||||
Dotted package definition
|
||||
@ -15,6 +16,7 @@ package flob.wibble
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(package_definition
|
||||
(package_declaration
|
||||
(qualified_name
|
||||
(identifier)
|
||||
(identifier)))
|
||||
(identifier))))
|
||||
|
||||
@ -6,11 +6,10 @@ hello
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(script_part
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier))))))
|
||||
(qualified_name
|
||||
(identifier)))))
|
||||
|
||||
================================================================================
|
||||
Shebang not ignored after first character
|
||||
@ -20,14 +19,14 @@ Shebang not ignored after first character
|
||||
|
||||
(source_file
|
||||
(ERROR
|
||||
(UNEXPECTED '#')
|
||||
(identifier)
|
||||
(UNEXPECTED '/')
|
||||
(identifier)
|
||||
(UNEXPECTED '/')
|
||||
(identifier))
|
||||
(script_part
|
||||
(UNEXPECTED '#'))
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier))))))
|
||||
(qualified_name
|
||||
(identifier))))
|
||||
(ERROR
|
||||
(UNEXPECTED '/')
|
||||
(identifier)
|
||||
(UNEXPECTED '/')
|
||||
(identifier)
|
||||
(identifier)))
|
||||
|
||||
@ -13,11 +13,10 @@ hello
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(script_part
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier))))))
|
||||
(qualified_name
|
||||
(identifier)))))
|
||||
|
||||
================================================================================
|
||||
statement - Simple method definition
|
||||
@ -26,9 +25,8 @@ def hello
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(script_part
|
||||
(method_declaration
|
||||
(identifier))))
|
||||
(identifier)))
|
||||
|
||||
================================================================================
|
||||
statement - Multiple statements same line
|
||||
@ -37,19 +35,16 @@ hello;goodbye;def flunk
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(script_part
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier)))))
|
||||
(script_part
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier)))))
|
||||
(script_part
|
||||
(method_declaration
|
||||
(qualified_name
|
||||
(identifier))))
|
||||
(statement
|
||||
(expression
|
||||
(qualified_name
|
||||
(identifier))))
|
||||
(method_declaration
|
||||
(identifier)))
|
||||
|
||||
================================================================================
|
||||
statement - Multiple Statements, Multiple Lines
|
||||
@ -60,19 +55,16 @@ goodbye;
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(script_part
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier)))))
|
||||
(script_part
|
||||
(qualified_name
|
||||
(identifier))))
|
||||
(method_declaration
|
||||
(identifier)))
|
||||
(script_part
|
||||
(identifier))
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(identifier))))))
|
||||
(qualified_name
|
||||
(identifier)))))
|
||||
|
||||
================================================================================
|
||||
statement - path expression multiple parts
|
||||
@ -81,10 +73,9 @@ wibble.wobble.q
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
(source_file
|
||||
(script_part
|
||||
(statement
|
||||
(expression
|
||||
(path_expression
|
||||
(qualified_name
|
||||
(identifier)
|
||||
(identifier)
|
||||
(identifier))))))
|
||||
(identifier)))))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user