Improve grammar for package declarations

Adds support for package blocks and package objects
pull/101/head
Mads Hartmann 2018-02-24 13:45:13 +07:00
parent 7265c8f855
commit c92323ddf5
5 changed files with 53065 additions and 52167 deletions

@ -1,3 +1,37 @@
================================
Package
================================
package a.b
package c {
object A
}
---
(compilation_unit
(package_clause (package_identifier (identifier) (identifier)))
(package_clause (package_identifier (identifier))
(template_body
(object_definition (identifier)))))
================================
Package object
================================
package object d extends A {
val hello: String = "there"
}
---
(compilation_unit
(package_object
(identifier)
(extends_clause (type_identifier))
(template_body
(val_definition (identifier) (type_identifier) (string)))))
================================ ================================
Imports Imports
================================ ================================

@ -0,0 +1,9 @@
package a.b
package c {
object A
}
package c {
package object d {
val hello: String = "there"
}
}

@ -39,6 +39,7 @@ module.exports = grammar({
_definition: $ => choice( _definition: $ => choice(
$.package_clause, $.package_clause,
$.package_object,
$.class_definition, $.class_definition,
$.import_declaration, $.import_declaration,
$.object_definition, $.object_definition,
@ -54,7 +55,22 @@ module.exports = grammar({
package_clause: $ => seq( package_clause: $ => seq(
'package', 'package',
choice($.identifier, $.stable_identifier) $.package_identifier,
// This is slightly more permissive than the EBNF in that it allows any
// kind of delcaration inside of the package blocks. As we're more
// concerned with the structure rather than the validity of the program
// we'll allow it.
optional($.template_body)
),
package_identifier: $ => sep1(
'.', $.identifier
),
package_object: $ => seq(
'package',
'object',
$._object_definition
), ),
import_declaration: $ => seq( import_declaration: $ => seq(

81
src/grammar.json vendored

@ -15,6 +15,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "package_clause" "name": "package_clause"
}, },
{
"type": "SYMBOL",
"name": "package_object"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "class_definition" "name": "class_definition"
@ -68,18 +72,63 @@
"type": "STRING", "type": "STRING",
"value": "package" "value": "package"
}, },
{
"type": "SYMBOL",
"name": "package_identifier"
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "template_body"
},
{
"type": "BLANK"
}
]
}
]
},
"package_identifier": {
"type": "SEQ",
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "identifier"
}, },
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "."
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "stable_identifier" "name": "identifier"
}
]
}
} }
] ]
},
"package_object": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "package"
},
{
"type": "STRING",
"value": "object"
},
{
"type": "SYMBOL",
"name": "_object_definition"
} }
] ]
}, },
@ -90,6 +139,36 @@
"type": "STRING", "type": "STRING",
"value": "import" "value": "import"
}, },
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_import_expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "SYMBOL",
"name": "_import_expression"
}
]
}
}
]
}
]
},
"_import_expression": {
"type": "SEQ",
"members": [
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [

105086
src/parser.c vendored

File diff suppressed because it is too large Load Diff