tweaks to make closer to real parser, checked in as reference

This commit is contained in:
Peter Hart 2022-09-05 00:12:34 -04:00
parent bd6ecaee2c
commit 2aa998b6a5
9 changed files with 2489 additions and 1128 deletions

1305
GroovyParser.g4 Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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
}
});

View File

@ -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,39 +124,14 @@
"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": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "STRING",
"value": "*"
}
]
"type": "SYMBOL",
"name": "wildcard_import"
},
{
"type": "BLANK"
@ -169,16 +143,34 @@
}
]
},
"_shebang_comment": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "#!.*\\n"
}
},
"package_definition": {
"wildcard_import": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "STRING",
"value": "*"
}
]
},
"package_declaration": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_annotations"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "package"
@ -187,36 +179,48 @@
"type": "FIELD",
"name": "name",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
}
]
"type": "SYMBOL",
"name": "qualified_name"
}
}
]
},
"script_part": {
"_annotations": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "annotation"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "annotation"
}
}
]
},
"annotation": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "SYMBOL",
"name": "qualified_name"
}
]
},
"_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": [],

View File

@ -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,27 +76,33 @@
}
},
{
"type": "package_definition",
"type": "package_declaration",
"named": true,
"fields": {
"name": {
"multiple": true,
"multiple": false,
"required": true,
"types": [
{
"type": ".",
"named": false
},
{
"type": "identifier",
"type": "qualified_name",
"named": true
}
]
}
},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "annotation",
"named": true
}
]
}
},
{
"type": "path_expression",
"type": "qualified_name",
"named": true,
"fields": {},
"children": {
@ -99,25 +116,6 @@
]
}
},
{
"type": "script_part",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "method_declaration",
"named": true
},
{
"type": "statement",
"named": true
}
]
}
},
{
"type": "source_file",
"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

File diff suppressed because it is too large Load Diff

View File

@ -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
(identifier)
(identifier)))
(import_declaration
(qualified_name
(identifier)
(identifier))))
================================================================================
wildcard import
@ -29,9 +31,11 @@ import flobble.fnurfle.*
--------------------------------------------------------------------------------
(source_file
(import_statement
(identifier)
(identifier)))
(import_declaration
(qualified_name
(identifier)
(identifier))
(wildcard_import)))
================================================================================
multiple imports semicolon separated
@ -40,11 +44,14 @@ import qq.ss; import flobble.*;
--------------------------------------------------------------------------------
(source_file
(import_statement
(identifier)
(identifier))
(import_statement
(identifier)))
(import_declaration
(qualified_name
(identifier)
(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)
(identifier)
(identifier)))
(qualified_name
(identifier)
(identifier))
(wildcard_import)))
================================================================================
simple wildcard
================================================================================
import wibble.*;
--------------------------------------------------------------------------------
(source_file
(import_declaration
(qualified_name
(identifier))
(wildcard_import)))

View File

@ -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
(identifier)
(identifier)))
(package_declaration
(qualified_name
(identifier)
(identifier))))

View File

@ -6,11 +6,10 @@ hello
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier))))))
(statement
(expression
(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 '#'))
(statement
(expression
(qualified_name
(identifier))))
(ERROR
(UNEXPECTED '/')
(identifier)
(UNEXPECTED '/')
(identifier))
(script_part
(statement
(expression
(path_expression
(identifier))))))
(identifier)
(identifier)))

View File

@ -13,11 +13,10 @@ hello
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier))))))
(statement
(expression
(qualified_name
(identifier)))))
================================================================================
statement - Simple method definition
@ -26,9 +25,8 @@ def hello
--------------------------------------------------------------------------------
(source_file
(script_part
(method_declaration
(identifier))))
(method_declaration
(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
(identifier))))
(statement
(expression
(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
(method_declaration
(identifier)))
(script_part
(statement
(expression
(path_expression
(identifier))))))
(statement
(expression
(qualified_name
(identifier))))
(method_declaration
(identifier))
(statement
(expression
(qualified_name
(identifier)))))
================================================================================
statement - path expression multiple parts
@ -81,10 +73,9 @@ wibble.wobble.q
--------------------------------------------------------------------------------
(source_file
(script_part
(statement
(expression
(path_expression
(identifier)
(identifier)
(identifier))))))
(statement
(expression
(qualified_name
(identifier)
(identifier)
(identifier)))))