Support Parsing Function Declarations

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-02 02:48:43 -04:00
parent 07f1943611
commit fc0276ad42
2 changed files with 40 additions and 1 deletions
+3 -1
View File
@@ -64,12 +64,14 @@ export default grammar({
instantiation: $ => seq($.typeRef, '(', optional($.parameter_list), ')', $.identifier), instantiation: $ => seq($.typeRef, '(', optional($.parameter_list), ')', $.identifier),
// Declarations // Declarations
declaration: $ => seq(choice($.parserDeclaration, $.parserTypeDeclaration, $.type_declaration)), declaration: $ => seq(choice($.parserDeclaration, $.parserTypeDeclaration, $.type_declaration, $.function_declaration)),
type_declaration: $=> choice($.struct_declaration), type_declaration: $=> choice($.struct_declaration),
struct_declaration: $ => seq($.struct, $.identifier, '{', optional($.struct_declaration_fields), '}'), struct_declaration: $ => seq($.struct, $.identifier, '{', optional($.struct_declaration_fields), '}'),
struct_declaration_fields: $=> repeat1(seq($.variableDeclaration)), struct_declaration_fields: $=> repeat1(seq($.variableDeclaration)),
function_declaration: $=> seq($.typeRef, $.identifier, $.parameters, $.statement),
// Make separate productions for the parser type and the parser type declaration because the latter can have type parameters. // Make separate productions for the parser type and the parser type declaration because the latter can have type parameters.
parserTypeDeclaration: $ => seq(optional($.annotations), $.parser, field('parser_name', $.identifier), optional($.typeParameters), $.parameters), parserTypeDeclaration: $ => seq(optional($.annotations), $.parser, field('parser_name', $.identifier), optional($.typeParameters), $.parameters),
parserDeclaration: $ => seq($.parserType, '{', optional($.parserLocalElements), $.parserStates, '}'), parserDeclaration: $ => seq($.parserType, '{', optional($.parserLocalElements), $.parserStates, '}'),
@@ -265,3 +265,40 @@ struct Testing {
) )
) )
=========================
Simple Function Declaration
=========================
bool functionb() {
string fieldA;
};
---
(p4program
(declaration
(function_declaration
(typeRef
(baseType
(bool)
)
)
(identifier)
(parameters
)
(statement
(blockStatement
(statements
(statement
(variableDeclaration
(typeRef
(baseType
(string)
)
)
(identifier)
)
)
)
)
)
)
)
)