Miscallleneous stuff (#27)

* Null literal

* Unit expression, required for `for {...} ()` case

* Add `return` expression

* Add `throw` expression

* Apply changes per review

Remove redundant token fun call.
pull/101/head
Denis Pyshev 2021-07-19 23:26:02 +07:00 committed by GitHub
parent bfa2a81388
commit f80daaa212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43398 additions and 37052 deletions

@ -150,6 +150,7 @@ def main() {
doThing()
} catch {
case e: SomeException => prinlnt(e.message)
case NonFatal(ex) => throw new SomeException(ex)
} finally {
tryAnotherThing()
}
@ -172,16 +173,28 @@ def main() {
(finally_clause (infix_expression (identifier) (operator_identifier) (integer_literal))))
(try_expression
(block (call_expression (identifier) (arguments)))
(catch_clause (case_block (case_clause
(typed_pattern
(identifier)
(type_identifier))
(call_expression
(identifier)
(arguments (field_expression
(identifier)
(identifier)))))))
(finally_clause (block (call_expression (identifier) (arguments)))))
(catch_clause
(case_block
(case_clause
(typed_pattern
(identifier)
(type_identifier))
(call_expression
(identifier)
(arguments (field_expression
(identifier)
(identifier)))))
(case_clause
(case_class_pattern
(type_identifier)
(identifier))
(throw_expression (instance_expression (call_expression (identifier) (arguments (identifier))))
)
)
)
)
(finally_clause (block (call_expression (identifier) (arguments))))
)
(try_expression (call_expression (identifier) (arguments))
(catch_clause (case_block (case_clause
(identifier)
@ -331,3 +344,43 @@ class C {
(prefix_expression (identifier))
(operator_identifier)
(identifier)))))))
===============================
Unit expressions
===============================
val x = ()
def f(): Unit = { (
); }
---
(compilation_unit
(val_definition (identifier) (unit))
(function_definition (identifier) (parameters) (type_identifier) (block (unit)))
)
===============================
Return expressions
===============================
def f: Unit = return
def g(a: A): B = { f(); return null.asInstanceOf[B] }
---
(compilation_unit
(function_definition (identifier) (type_identifier) (return_expression))
(function_definition
(identifier)
(parameters (parameter (identifier) (type_identifier)))
(type_identifier)
(block
(call_expression
(identifier)
(arguments))
(return_expression
(generic_function (field_expression (null_literal) (identifier))
(type_arguments (type_identifier))))
)
)
)

@ -164,3 +164,12 @@ val myOtherSymbol = 'thing
(compilation_unit
(val_definition (identifier) (symbol_literal))
(val_definition (identifier) (symbol_literal)))
==========================
Null
==========================
lazy val nullObject: String = null
---
(compilation_unit (val_definition (modifiers) (identifier) (type_identifier) (null_literal)))

@ -518,7 +518,10 @@ module.exports = grammar({
$.case_block,
$.block,
$.identifier,
$.literal
$.literal,
$.unit,
$.return_expression,
$.throw_expression
),
if_expression: $ => prec.right(seq(
@ -643,7 +646,8 @@ module.exports = grammar({
$.boolean_literal,
$.character_literal,
$.symbol_literal,
$.string
$.string,
$.null_literal
),
integer_literal: $ => token(
@ -751,6 +755,14 @@ module.exports = grammar({
$._automatic_semicolon
),
null_literal: $ => 'null',
unit: $ => seq('(', ')'),
return_expression: $ => prec.left(seq('return', optional($._expression))),
throw_expression: $ => prec.left(seq('throw', $._expression)),
comment: $ => token(choice(
seq('//', /.*/),
seq(

75
src/grammar.json vendored

@ -2681,6 +2681,18 @@
{
"type": "SYMBOL",
"name": "literal"
},
{
"type": "SYMBOL",
"name": "unit"
},
{
"type": "SYMBOL",
"name": "return_expression"
},
{
"type": "SYMBOL",
"name": "throw_expression"
}
]
},
@ -3352,6 +3364,10 @@
{
"type": "SYMBOL",
"name": "string"
},
{
"type": "SYMBOL",
"name": "null_literal"
}
]
},
@ -3768,6 +3784,65 @@
}
]
},
"null_literal": {
"type": "STRING",
"value": "null"
},
"unit": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "STRING",
"value": ")"
}
]
},
"return_expression": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "return"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "BLANK"
}
]
}
]
}
},
"throw_expression": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "throw"
},
{
"type": "SYMBOL",
"name": "_expression"
}
]
}
},
"comment": {
"type": "TOKEN",
"content": {

@ -125,6 +125,10 @@
"type": "match_expression",
"named": true
},
{
"type": "null_literal",
"named": true
},
{
"type": "parenthesized_expression",
"named": true
@ -133,6 +137,10 @@
"type": "prefix_expression",
"named": true
},
{
"type": "return_expression",
"named": true
},
{
"type": "string",
"named": true
@ -141,6 +149,10 @@
"type": "symbol_literal",
"named": true
},
{
"type": "throw_expression",
"named": true
},
{
"type": "try_expression",
"named": true
@ -148,6 +160,10 @@
{
"type": "tuple_expression",
"named": true
},
{
"type": "unit",
"named": true
}
]
},
@ -191,6 +207,10 @@
"type": "integer_literal",
"named": true
},
{
"type": "null_literal",
"named": true
},
{
"type": "string",
"named": true
@ -2219,6 +2239,21 @@
}
}
},
{
"type": "return_expression",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": false,
"types": [
{
"type": "_expression",
"named": true
}
]
}
},
{
"type": "stable_identifier",
"named": true,
@ -2285,6 +2320,21 @@
]
}
},
{
"type": "throw_expression",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "_expression",
"named": true
}
]
}
},
{
"type": "trait_definition",
"named": true,
@ -2695,6 +2745,11 @@
}
}
},
{
"type": "unit",
"named": true,
"fields": {}
},
{
"type": "upper_bound",
"named": true,
@ -3263,6 +3318,10 @@
"type": "new",
"named": false
},
{
"type": "null_literal",
"named": true
},
{
"type": "object",
"named": false
@ -3287,6 +3346,10 @@
"type": "protected",
"named": false
},
{
"type": "return",
"named": false
},
{
"type": "sealed",
"named": false
@ -3295,6 +3358,10 @@
"type": "symbol_literal",
"named": true
},
{
"type": "throw",
"named": false
},
{
"type": "trait",
"named": false

80210
src/parser.c vendored

File diff suppressed because it is too large Load Diff