diff --git a/tree-sitter-p4/grammar.js b/tree-sitter-p4/grammar.js index 0089805..5bff43c 100644 --- a/tree-sitter-p4/grammar.js +++ b/tree-sitter-p4/grammar.js @@ -29,7 +29,7 @@ export default grammar({ // Common - Parameters typeParameters: $ => seq('<', $.typeParameterList, '>'), typeParameterList: $ => choice("[a-z]+", seq($.typeParameterList, ',', "[a-z]+")), - parameter_list: $ => choice($.parameter, seq($.parameter_list, ',', $.parameter)), + parameter_list: $ => choice(seq($.parameter_list, ',', $.parameter), $.parameter), parameter: $ => choice(seq(optional($.annotations), optional($.direction), $.typeRef, $.identifier), seq(optional($.annotations), optional($.direction), $.typeRef, $.identifier, '=', $.expression)), direction: $ => choice($.in, $.out, $.inout), parameters: $=> seq('(', optional($.parameter_list), ')'), diff --git a/tree-sitter-p4/test/corpus/declarations.txt b/tree-sitter-p4/test/corpus/declarations.txt index 7d57b4d..3e189af 100644 --- a/tree-sitter-p4/test/corpus/declarations.txt +++ b/tree-sitter-p4/test/corpus/declarations.txt @@ -272,6 +272,43 @@ bool functionb() { string fieldA; }; --- +(p4program + (declaration + (function_declaration + (typeRef + (baseType + (bool) + ) + ) + (identifier) + (parameters) + (statement + (blockStatement + (statements + (statement + (variableDeclaration + (typeRef + (baseType + (string) + ) + ) + (identifier) + ) + ) + ) + ) + ) + ) + ) +) + +========================= +Function Declaration With Parameters +========================= +bool functionb(bool a, int b) { + string fieldA; +}; +--- (p4program (declaration (function_declaration @@ -282,6 +319,102 @@ bool functionb() { ) (identifier) (parameters + (parameter_list + (parameter_list + (parameter + (typeRef + (baseType + (bool) + ) + ) + (identifier) + ) + ) + (parameter + (typeRef + (baseType + (int) + ) + ) + (identifier) + ) + ) + ) + (statement + (blockStatement + (statements + (statement + (variableDeclaration + (typeRef + (baseType + (string) + ) + ) + (identifier) + ) + ) + ) + ) + ) + ) + ) +) +========================= +Function Declaration With Parameters And Directions +========================= +bool functionb(in bool a, out int b, inout string c) { + string fieldA; +}; +--- +(p4program + (declaration + (function_declaration + (typeRef + (baseType + (bool) + ) + ) + (identifier) + (parameters + (parameter_list + (parameter_list + (parameter_list + (parameter + (direction + (in) + ) + (typeRef + (baseType + (bool) + ) + ) + (identifier) + ) + ) + (parameter + (direction + (out) + ) + (typeRef + (baseType + (int) + ) + ) + (identifier) + ) + ) + (parameter + (direction + (inout) + ) + (typeRef + (baseType + (string) + ) + ) + (identifier) + ) + ) ) (statement (blockStatement