1
0

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) ValueNode::Range(start..=end)
} }
"enum_instance" => { "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 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 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)?)) Some(Box::new(Expression::from_syntax(expression_node, source, context)?))
} else { } else {
None None

View File

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

View File

@ -49,7 +49,7 @@ enum Foobar {
Simple Enum Instance Simple Enum Instance
================================================================================ ================================================================================
new Foobar:Foo Foobar::Foo
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -65,7 +65,7 @@ new Foobar:Foo
Nested Enum Instance 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, $.identifier,
seq( seq(
$.identifier, $.identifier,
$.type_specification, '<',
repeat1(
seq(
$.type,
optional(','),
),
),
'>',
), ),
seq('[', $.type, ']'), seq('[', $.type, ']'),
seq( seq(
@ -447,9 +454,8 @@ module.exports = grammar({
enum_instance: $ => enum_instance: $ =>
prec.right( prec.right(
seq( seq(
'new',
$.identifier, $.identifier,
':', '::',
$.identifier, $.identifier,
optional( optional(
seq('(', $.expression, ')'), seq('(', $.expression, ')'),

View File

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

View File

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

File diff suppressed because it is too large Load Diff