1
0

Add function definition syntax

This commit is contained in:
Jeff 2024-02-22 23:38:49 -05:00
parent eed9a780e5
commit ac02230ef7
7 changed files with 24011 additions and 23883 deletions

View File

@ -53,7 +53,7 @@ impl AnonymousFunction {
impl AbstractTree for AnonymousFunction { impl AbstractTree for AnonymousFunction {
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> { fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
SyntaxError::expect_syntax_node("function", node)?; SyntaxError::expect_syntax_node("anonymous_function", node)?;
let child_count = node.child_count(); let child_count = node.child_count();
let mut parameters = Vec::new(); let mut parameters = Vec::new();

View File

@ -38,7 +38,7 @@ impl AbstractTree for ValueNode {
let value_node = match child.kind() { let value_node = match child.kind() {
"boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()), "boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()),
"float" => ValueNode::Float(source[child.byte_range()].to_string()), "float" => ValueNode::Float(source[child.byte_range()].to_string()),
"function" => { "anonymous_function" => {
let function_node = AnonymousFunction::from_syntax(child, source, context)?; let function_node = AnonymousFunction::from_syntax(child, source, context)?;
ValueNode::Function(Function::ContextDefined(function_node)) ValueNode::Function(Function::ContextDefined(function_node))

View File

@ -247,3 +247,35 @@ from_json(read('file.json'))
(expression (expression
(value (value
(string)))))))))) (string))))))))))
================================================================================
Function Definition
================================================================================
fn foo<T> (bar <T>) <T> {
bar
}
--------------------------------------------------------------------------------
(root
(statement
(statement_kind
(type_definition
(function_definition
(identifier)
(type_arguments
(type
(identifier)))
(identifier)
(type_specification
(type
(identifier)))
(type_specification
(type
(identifier)))
(block
(statement
(statement_kind
(expression
(identifier))))))))))

View File

@ -478,6 +478,7 @@ module.exports = grammar({
type_definition: $ => type_definition: $ =>
choice( choice(
$.enum_definition, $.enum_definition,
$.function_definition,
$.struct_definition, $.struct_definition,
), ),
@ -555,5 +556,23 @@ module.exports = grammar({
struct_instance: $ => struct_instance: $ =>
seq($.identifier, '::', $.map), seq($.identifier, '::', $.map),
function_definition: $ =>
seq(
'fn',
$.identifier,
optional($.type_arguments),
'(',
repeat(
seq(
$.identifier,
$.type_specification,
optional(','),
),
),
')',
$.type_specification,
$.block,
),
}, },
}); });

View File

@ -1451,6 +1451,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "enum_definition" "name": "enum_definition"
}, },
{
"type": "SYMBOL",
"name": "function_definition"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "struct_definition" "name": "struct_definition"
@ -1717,6 +1721,75 @@
"name": "map" "name": "map"
} }
] ]
},
"function_definition": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "fn"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "type_arguments"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "("
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "BLANK"
}
]
}
]
}
},
{
"type": "STRING",
"value": ")"
},
{
"type": "SYMBOL",
"name": "type_specification"
},
{
"type": "SYMBOL",
"name": "block"
}
]
} }
}, },
"extras": [ "extras": [

View File

@ -309,6 +309,33 @@
] ]
} }
}, },
{
"type": "function_definition",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "block",
"named": true
},
{
"type": "identifier",
"named": true
},
{
"type": "type_arguments",
"named": true
},
{
"type": "type_specification",
"named": true
}
]
}
},
{ {
"type": "function_expression", "type": "function_expression",
"named": true, "named": true,
@ -822,6 +849,10 @@
"type": "enum_definition", "type": "enum_definition",
"named": true "named": true
}, },
{
"type": "function_definition",
"named": true
},
{ {
"type": "struct_definition", "type": "struct_definition",
"named": true "named": true
@ -1074,6 +1105,10 @@
"type": "float", "type": "float",
"named": false "named": false
}, },
{
"type": "fn",
"named": false
},
{ {
"type": "for", "type": "for",
"named": false "named": false

File diff suppressed because it is too large Load Diff