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({ module.exports = grammar({
name: 'Groovy', name: 'Groovy',
conflicts: $ => [
[$.qualified_name]
],
rules: { rules: {
// TODO: add the actual grammar rules // TODO: add the actual grammar rules
source_file: $ => seq( source_file: $ => seq(
optional($._shebang_comment), optional($._shebang_comment),
optional(seq($.package_definition, repeat($._nl_semicolon))), optional(seq($.package_declaration, repeat($._sep))),
repeat( optional($._script_statements)
choice(
seq(
$.import_statement,
repeat1($._nl_semicolon)
),
seq(
$.script_part,
repeat1($._nl_semicolon)
)
)
),
optional($.script_part)
), ),
static_modifier: $ => 'static', 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', 'import',
optional($.static_modifier), optional($.static_modifier),
field('name', field('name',
seq( seq($.qualified_name,
$.identifier, optional($.wildcard_import)
repeat(seq('.', $.identifier)),
optional(seq('.', '*')
)
) )
) )
), ),
_shebang_comment: $ => token.immediate(/#!.*\n/), wildcard_import: $ => seq('.','*'),
package_definition: $ => seq( package_declaration: $ => seq(
optional($._annotations),
'package', 'package',
field('name', seq( field('name', $.qualified_name)
$.identifier,
repeat(seq('.', $.identifier))))
), ),
script_part: $ => choice( _annotations: $ => seq(
$.annotation,
repeat($.annotation)
),
annotation: $ => seq(
'@',
$.qualified_name
),
_script_statement: $ => choice(
$.import_declaration,
$.statement, $.statement,
$.method_declaration $.method_declaration
), ),
expression: $ => choice( expression: $ => choice(
$.path_expression $.qualified_name
), ),
path_expression: $ => seq( qualified_name: $ => seq(
repeat( $.identifier,
seq($.identifier, '.') repeat(seq('.', $.identifier))
),
$.identifier
), ),
identifier: $ => /[A-Za-z_][A-Za-z_0-9]*/, identifier: $ => /[A-Za-z_][A-Za-z_0-9]*/,
statement: $ => choice( statement: $ => choice(
$.expression $.expression
), ),
method_declaration: $ => seq('def', field('name', $.identifier)), method_declaration: $ => seq('def', field('name', $.identifier)),
_nl_semicolon: $ => choice('\n', ';'), _sep: $ => choice('\n', ';'),
word: $ => $.identifier word: $ => $.identifier
} }
}); });

View File

@ -24,13 +24,13 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "package_definition" "name": "package_declaration"
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "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", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "script_part" "name": "_script_statements"
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -98,7 +58,46 @@
"type": "STRING", "type": "STRING",
"value": "static" "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", "type": "SEQ",
"members": [ "members": [
{ {
@ -125,28 +124,26 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "qualified_name"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{
"type": "SYMBOL",
"name": "identifier"
}
]
}
}, },
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "SYMBOL",
"name": "wildcard_import"
},
{
"type": "BLANK"
}
]
}
]
}
}
]
},
"wildcard_import": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -159,26 +156,21 @@
} }
] ]
}, },
"package_declaration": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_annotations"
},
{ {
"type": "BLANK" "type": "BLANK"
} }
] ]
}
]
}
}
]
}, },
"_shebang_comment": {
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "#!.*\\n"
}
},
"package_definition": {
"type": "SEQ",
"members": [
{ {
"type": "STRING", "type": "STRING",
"value": "package" "value": "package"
@ -187,36 +179,48 @@
"type": "FIELD", "type": "FIELD",
"name": "name", "name": "name",
"content": { "content": {
"type": "SYMBOL",
"name": "qualified_name"
}
}
]
},
"_annotations": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "annotation"
}, },
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL",
"name": "annotation"
}
}
]
},
"annotation": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "." "value": "@"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "qualified_name"
}
]
}
}
]
}
} }
] ]
}, },
"script_part": { "_script_statement": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "import_declaration"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "statement" "name": "statement"
@ -232,32 +236,32 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "path_expression" "name": "qualified_name"
} }
] ]
}, },
"path_expression": { "qualified_name": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{ {
"type": "STRING", "type": "STRING",
"value": "." "value": "."
},
{
"type": "SYMBOL",
"name": "identifier"
} }
] ]
} }
},
{
"type": "SYMBOL",
"name": "identifier"
} }
] ]
}, },
@ -291,7 +295,7 @@
} }
] ]
}, },
"_nl_semicolon": { "_sep": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{ {
@ -315,7 +319,11 @@
"value": "\\s" "value": "\\s"
} }
], ],
"conflicts": [], "conflicts": [
[
"qualified_name"
]
],
"precedences": [], "precedences": [],
"externals": [], "externals": [],
"inline": [], "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", "type": "expression",
"named": true, "named": true,
@ -8,14 +23,14 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "path_expression", "type": "qualified_name",
"named": true "named": true
} }
] ]
} }
}, },
{ {
"type": "import_statement", "type": "import_declaration",
"named": true, "named": true,
"fields": { "fields": {
"name": { "name": {
@ -23,15 +38,11 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "*", "type": "qualified_name",
"named": false "named": true
}, },
{ {
"type": ".", "type": "wildcard_import",
"named": false
},
{
"type": "identifier",
"named": true "named": true
} }
] ]
@ -65,54 +76,41 @@
} }
}, },
{ {
"type": "package_definition", "type": "package_declaration",
"named": true, "named": true,
"fields": { "fields": {
"name": { "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, "multiple": false,
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "method_declaration", "type": "qualified_name",
"named": true "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 "named": true
} }
] ]
@ -127,15 +125,19 @@
"required": false, "required": false,
"types": [ "types": [
{ {
"type": "import_statement", "type": "import_declaration",
"named": true "named": true
}, },
{ {
"type": "package_definition", "type": "method_declaration",
"named": true "named": true
}, },
{ {
"type": "script_part", "type": "package_declaration",
"named": true
},
{
"type": "statement",
"named": true "named": true
} }
] ]
@ -156,6 +158,11 @@
] ]
} }
}, },
{
"type": "wildcard_import",
"named": true,
"fields": {}
},
{ {
"type": "\n", "type": "\n",
"named": false "named": false
@ -172,6 +179,10 @@
"type": ";", "type": ";",
"named": false "named": false
}, },
{
"type": "@",
"named": false
},
{ {
"type": "def", "type": "def",
"named": false "named": false

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,9 @@ import wibble
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(import_statement (import_declaration
(identifier))) (qualified_name
(identifier))))
================================================================================ ================================================================================
multiple identifier import multiple identifier import
@ -17,9 +18,10 @@ import wobble.qq
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(import_statement (import_declaration
(qualified_name
(identifier) (identifier)
(identifier))) (identifier))))
================================================================================ ================================================================================
wildcard import wildcard import
@ -29,9 +31,11 @@ import flobble.fnurfle.*
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(import_statement (import_declaration
(qualified_name
(identifier) (identifier)
(identifier))) (identifier))
(wildcard_import)))
================================================================================ ================================================================================
multiple imports semicolon separated multiple imports semicolon separated
@ -40,11 +44,14 @@ import qq.ss; import flobble.*;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(import_statement (import_declaration
(qualified_name
(identifier) (identifier)
(identifier))
(import_statement
(identifier))) (identifier)))
(import_declaration
(qualified_name
(identifier))
(wildcard_import)))
================================================================================ ================================================================================
static import static import
@ -53,7 +60,21 @@ import static qq.q.*;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(import_statement (import_declaration
(static_modifier) (static_modifier)
(qualified_name
(identifier) (identifier)
(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 (source_file
(package_definition (package_declaration
(identifier))) (qualified_name
(identifier))))
================================================================================ ================================================================================
Dotted package definition Dotted package definition
@ -15,6 +16,7 @@ package flob.wibble
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(package_definition (package_declaration
(qualified_name
(identifier) (identifier)
(identifier))) (identifier))))

View File

@ -6,11 +6,10 @@ hello
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier)))))) (identifier)))))
================================================================================ ================================================================================
Shebang not ignored after first character Shebang not ignored after first character
@ -20,14 +19,14 @@ Shebang not ignored after first character
(source_file (source_file
(ERROR (ERROR
(UNEXPECTED '#') (UNEXPECTED '#'))
(identifier)
(UNEXPECTED '/')
(identifier)
(UNEXPECTED '/')
(identifier))
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier)))))) (identifier))))
(ERROR
(UNEXPECTED '/')
(identifier)
(UNEXPECTED '/')
(identifier)
(identifier)))

View File

@ -13,11 +13,10 @@ hello
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier)))))) (identifier)))))
================================================================================ ================================================================================
statement - Simple method definition statement - Simple method definition
@ -26,9 +25,8 @@ def hello
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(script_part
(method_declaration (method_declaration
(identifier)))) (identifier)))
================================================================================ ================================================================================
statement - Multiple statements same line statement - Multiple statements same line
@ -37,19 +35,16 @@ hello;goodbye;def flunk
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier)))))
(script_part
(statement
(expression
(path_expression
(identifier)))))
(script_part
(method_declaration
(identifier)))) (identifier))))
(statement
(expression
(qualified_name
(identifier))))
(method_declaration
(identifier)))
================================================================================ ================================================================================
statement - Multiple Statements, Multiple Lines statement - Multiple Statements, Multiple Lines
@ -60,19 +55,16 @@ goodbye;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier))))) (identifier))))
(script_part
(method_declaration (method_declaration
(identifier))) (identifier))
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier)))))) (identifier)))))
================================================================================ ================================================================================
statement - path expression multiple parts statement - path expression multiple parts
@ -81,10 +73,9 @@ wibble.wobble.q
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(script_part
(statement (statement
(expression (expression
(path_expression (qualified_name
(identifier) (identifier)
(identifier) (identifier)
(identifier)))))) (identifier)))))