Modify enum variant syntax

This commit is contained in:
Jeff 2024-02-15 02:22:04 -05:00
parent b8f2fe7eb4
commit 540f59e6d8
7 changed files with 24862 additions and 23365 deletions

View File

@ -88,13 +88,13 @@ impl AbstractTree for ValueNode {
ValueNode::Range(start..=end)
}
"enum_instance" => {
let name_node = child.child(1).unwrap();
let name_node = child.child(0).unwrap();
let name = Identifier::from_syntax(name_node, source, context)?;
let variant_node = child.child(3).unwrap();
let variant_node = child.child(2).unwrap();
let variant = Identifier::from_syntax(variant_node, source, context)?;
let expression = if let Some(expression_node) = child.child(5) {
let expression = if let Some(expression_node) = child.child(4) {
Some(Box::new(Expression::from_syntax(expression_node, source, context)?))
} else {
None

View File

@ -3,7 +3,7 @@ use dust_lang::*;
#[test]
fn option() {
assert_eq!(
interpret("new Option:None"),
interpret("Option::None"),
Ok(Value::Enum(EnumInstance::new(
"Option".to_string(),
"None".to_string(),
@ -13,7 +13,7 @@ fn option() {
assert_eq!(
interpret(
"
opt <Option<int>> = new Option:Some(1);
opt <Option<int>> = Option::Some(1);
opt
"
@ -29,7 +29,7 @@ fn option() {
#[test]
fn result() {
assert_eq!(
interpret("new Result:Ok(1)"),
interpret("Result::Ok(1)"),
Ok(Value::Enum(EnumInstance::new(
"Result".to_string(),
"Ok".to_string(),
@ -39,7 +39,7 @@ fn result() {
assert_eq!(
interpret(
"
result = new Result:Error('uh-oh!')
result <Result<int, str>> = Result::Error('uh-oh!')
result
"
),

View File

@ -49,7 +49,7 @@ enum Foobar {
Simple Enum Instance
================================================================================
new Foobar:Foo
Foobar::Foo
--------------------------------------------------------------------------------
@ -65,7 +65,7 @@ new Foobar:Foo
Nested Enum Instance
================================================================================
new Foobar:Bar(new BazBuf:Baz(123))
FooBar::Bar(BazBuf::Baz(123))
--------------------------------------------------------------------------------

View File

@ -339,7 +339,14 @@ module.exports = grammar({
$.identifier,
seq(
$.identifier,
$.type_specification,
'<',
repeat1(
seq(
$.type,
optional(','),
),
),
'>',
),
seq('[', $.type, ']'),
seq(
@ -447,9 +454,8 @@ module.exports = grammar({
enum_instance: $ =>
prec.right(
seq(
'new',
$.identifier,
':',
'::',
$.identifier,
optional(
seq('(', $.expression, ')'),

View File

@ -1037,9 +1037,37 @@
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": "<"
},
{
"type": "REPEAT1",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "type_specification"
"name": "type"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ">"
}
]
},
@ -1385,17 +1413,13 @@
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "new"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": ":"
"value": "::"
},
{
"type": "SYMBOL",

View File

@ -696,10 +696,6 @@
{
"type": "type",
"named": true
},
{
"type": "type_specification",
"named": true
}
]
}
@ -864,6 +860,10 @@
"type": ":",
"named": false
},
{
"type": "::",
"named": false
},
{
"type": ";",
"named": false

File diff suppressed because it is too large Load Diff