diff --git a/tree-sitter-p4/grammar.js b/tree-sitter-p4/grammar.js index b656965..8192a1f 100644 --- a/tree-sitter-p4/grammar.js +++ b/tree-sitter-p4/grammar.js @@ -64,12 +64,14 @@ export default grammar({ instantiation: $ => seq($.typeRef, '(', optional($.parameter_list), ')', $.identifier), // Declarations - declaration: $ => seq(choice($.parserDeclaration, $.parserTypeDeclaration, $.type_declaration)), + declaration: $ => seq(choice($.parserDeclaration, $.parserTypeDeclaration, $.type_declaration, $.function_declaration)), type_declaration: $=> choice($.struct_declaration), struct_declaration: $ => seq($.struct, $.identifier, '{', optional($.struct_declaration_fields), '}'), 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. parserTypeDeclaration: $ => seq(optional($.annotations), $.parser, field('parser_name', $.identifier), optional($.typeParameters), $.parameters), parserDeclaration: $ => seq($.parserType, '{', optional($.parserLocalElements), $.parserStates, '}'), diff --git a/tree-sitter-p4/test/corpus/declarations.txt b/tree-sitter-p4/test/corpus/declarations.txt index 0a7983c..7d57b4d 100644 --- a/tree-sitter-p4/test/corpus/declarations.txt +++ b/tree-sitter-p4/test/corpus/declarations.txt @@ -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) + ) + ) + ) + ) + ) + ) + ) +)