From d8f6ff3b0ba5b308127cb47648ea1f65d19a8efc Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Fri, 30 Jan 2026 08:24:44 -0500 Subject: [PATCH] Update Variable Declarations Refactor a literal '=' to a production and allow expressions of string-literal type. Signed-off-by: Will Hawkins --- tree-sitter-p4/grammar.js | 5 +- tree-sitter-p4/test/corpus/declarations.txt | 151 ++++++++++++++++++++ 2 files changed, 154 insertions(+), 2 deletions(-) diff --git a/tree-sitter-p4/grammar.js b/tree-sitter-p4/grammar.js index 0268666..2ac59a2 100644 --- a/tree-sitter-p4/grammar.js +++ b/tree-sitter-p4/grammar.js @@ -66,7 +66,7 @@ export default grammar({ parserTypeDeclaration: $ => seq(optional($.annotations), $.parser, field('parser_name', $.identifier), optional($.typeParameters), '(', optional($.parameterList), ')'), parserDeclaration: $ => seq($.parserType, optional($.constructorParameters), '{', optional($.parserLocalElements), $.parserStates, '}'), - variableDeclaration: $ => seq(optional($.annotations), $.typeRef, field('variable_name', $.identifier), optional(seq('=', $.expression))), + variableDeclaration: $ => seq(optional($.annotations), $.typeRef, field('variable_name', $.identifier), optional(seq($.assignment, $.expression))), // Statements @@ -84,7 +84,7 @@ export default grammar({ parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression), // Expressions - expression: $ => choice($.identifier, $.integer, $.true, $.false), // Very limited. + expression: $ => choice($.identifier, $.integer, $.true, $.false, $.string_literal), // Very limited. selectExpression: $ => seq($.select, '(', $.expression, ')', '{', $.selectBody, '}'), // TODO: Should be expression list and not just a single expression transitionSelectionExpression: $ => choice($.identifier, $.selectExpression), keysetExpression: $ => $.expression, @@ -92,6 +92,7 @@ export default grammar({ // Tokens semicolon: $ => ";", colon: $ => ":", + assignment: $ => "=", todo: $ => "todo", abstract: $ => "abstract", action: $ => "action", diff --git a/tree-sitter-p4/test/corpus/declarations.txt b/tree-sitter-p4/test/corpus/declarations.txt index 05f34bc..6ff4f6f 100644 --- a/tree-sitter-p4/test/corpus/declarations.txt +++ b/tree-sitter-p4/test/corpus/declarations.txt @@ -47,3 +47,154 @@ parser simple() { ) ) +========================= +Simple Declaration (int) +========================= +parser simple() { + state start { + int l; + transition accept; + } +}; + +--- +(p4program + (declaration + (parserDeclaration + (parserType + (parser) + (identifier) + ) + (parserStates + (parserState + (state) + (identifier) + (parserLocalElements + (parserLocalElement + (variableDeclaration + (typeRef + (baseType + (int) + ) + ) + (identifier) + ) + ) + (semicolon) + ) + (parserTransitionStatement + (transition) + (transitionSelectionExpression + (identifier) + ) + ) + (semicolon) + ) + ) + ) + (semicolon) + ) +) + +========================= +Simple Declaration (string) +========================= +parser simple() { + state start { + string l; + transition accept; + } +}; + +--- +(p4program + (declaration + (parserDeclaration + (parserType + (parser) + (identifier) + ) + (parserStates + (parserState + (state) + (identifier) + (parserLocalElements + (parserLocalElement + (variableDeclaration + (typeRef + (baseType + (string) + ) + ) + (identifier) + ) + ) + (semicolon) + ) + (parserTransitionStatement + (transition) + (transitionSelectionExpression + (identifier) + ) + ) + (semicolon) + ) + ) + ) + (semicolon) + ) +) + +========================= +Simple Declaration (string with initial value) +========================= +parser simple() { + state start { + string l = "testing"; + transition accept; + } +}; + +--- +(p4program + (declaration + (parserDeclaration + (parserType + (parser) + (identifier) + ) + (parserStates + (parserState + (state) + (identifier) + (parserLocalElements + (parserLocalElement + (variableDeclaration + (typeRef + (baseType + (string) + ) + ) + (identifier) + (assignment) + (expression + (string_literal) + ) + ) + ) + (semicolon) + ) + (parserTransitionStatement + (transition) + (transitionSelectionExpression + (identifier) + ) + ) + (semicolon) + ) + ) + ) + (semicolon) + ) +) +