diff --git a/tree-sitter-p4/grammar.js b/tree-sitter-p4/grammar.js index 2ac59a2..6ee1344 100644 --- a/tree-sitter-p4/grammar.js +++ b/tree-sitter-p4/grammar.js @@ -22,7 +22,7 @@ export default grammar({ name: 'p4', rules: { // Start symbol - p4program: $ => optional(repeat($.declaration)), + p4program: $ => optional(repeat(seq(choice($.declaration, $.instantiation), $.semicolon))), // Common @@ -59,8 +59,11 @@ export default grammar({ annotation: $ => choice(seq('@', "[a-z]+")),// seq('@', "[a-z]+", '(', /* empty for now*/ ')'), seq('@', "[a-z]+", '[', /* empty for now */ ']')), + // Instantiation + instantiation: $ => seq($.typeRef, '(', optional($.parameterList), ')', $.identifier), + // Declarations - declaration: $ => seq(choice($.parserDeclaration, $.parserTypeDeclaration), $.semicolon), + declaration: $ => seq(choice($.parserDeclaration, $.parserTypeDeclaration)), // 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), '(', optional($.parameterList), ')'), diff --git a/tree-sitter-p4/test.txt b/tree-sitter-p4/test.txt new file mode 100644 index 0000000..0ffde94 --- /dev/null +++ b/tree-sitter-p4/test.txt @@ -0,0 +1,7 @@ +parser simple() { + state start { + bool l; + transition accept; + } +} + diff --git a/tree-sitter-p4/test/corpus/declarations.txt b/tree-sitter-p4/test/corpus/declarations.txt index 6ff4f6f..8dd5482 100644 --- a/tree-sitter-p4/test/corpus/declarations.txt +++ b/tree-sitter-p4/test/corpus/declarations.txt @@ -43,8 +43,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) ========================= @@ -92,8 +92,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) ========================= @@ -141,8 +141,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) ========================= @@ -194,7 +194,7 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) diff --git a/tree-sitter-p4/test/corpus/instantiations.txt b/tree-sitter-p4/test/corpus/instantiations.txt new file mode 100644 index 0000000..6ab3c67 --- /dev/null +++ b/tree-sitter-p4/test/corpus/instantiations.txt @@ -0,0 +1,17 @@ +========================= +Simple Instantiation +========================= +bool() main; +--- +(p4program + (instantiation + (typeRef + (baseType + (bool) + ) + ) + (identifier) + ) + (semicolon) +) + diff --git a/tree-sitter-p4/test/corpus/parsers.txt b/tree-sitter-p4/test/corpus/parsers.txt index f8bce39..c9d6344 100644 --- a/tree-sitter-p4/test/corpus/parsers.txt +++ b/tree-sitter-p4/test/corpus/parsers.txt @@ -29,8 +29,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) @@ -75,6 +75,6 @@ parser imple(bool pname) { ) ) ) - (semicolon) ) + (semicolon) ) diff --git a/tree-sitter-p4/test/corpus/statements.txt b/tree-sitter-p4/test/corpus/statements.txt index 713d0cc..a725d23 100644 --- a/tree-sitter-p4/test/corpus/statements.txt +++ b/tree-sitter-p4/test/corpus/statements.txt @@ -45,8 +45,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) ========================= @@ -101,8 +101,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) ========================= @@ -181,7 +181,7 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) diff --git a/tree-sitter-p4/test/corpus/transitions.txt b/tree-sitter-p4/test/corpus/transitions.txt index 770492c..1cda980 100644 --- a/tree-sitter-p4/test/corpus/transitions.txt +++ b/tree-sitter-p4/test/corpus/transitions.txt @@ -29,8 +29,8 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) ) ========================= @@ -83,6 +83,6 @@ parser simple() { ) ) ) - (semicolon) ) + (semicolon) )