From 16692de7b205f8392a47ea637e29cb2f7811708c Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Fri, 13 Mar 2026 08:26:14 -0400 Subject: [PATCH] Add Grammar Support for == And Array Access Signed-off-by: Will Hawkins --- tree-sitter-p4/grammar.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tree-sitter-p4/grammar.js b/tree-sitter-p4/grammar.js index 944fdf9..a7370ce 100644 --- a/tree-sitter-p4/grammar.js +++ b/tree-sitter-p4/grammar.js @@ -88,11 +88,16 @@ export default grammar({ parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression, $._semicolon), // Expressions - expression: $ => choice($.identifier, $.integer, $.booleanLiteralExpression, $.string_literal), // Very limited. + expression: $ => choice($.identifier, $.integer, $.booleanLiteralExpression, $.string_literal, $.binaryOperatorExpression, $.arrayAccessExpression), // Very limited. booleanLiteralExpression: $ => choice($.true, $.false), selectExpression: $ => seq($.select, '(', $.expression, ')', '{', $.selectBody, '}'), // TODO: Should be expression list and not just a single expression transitionSelectionExpression: $ => choice($.identifier, $.selectExpression), keysetExpression: $ => $.expression, + binaryOperatorExpression: $ => choice($.binaryEqualOperatorExpression), + arrayAccessExpression: $=> seq($.identifier, $.open_bracket, $.expression, $.close_bracket), + + // Binary Operations + binaryEqualOperatorExpression: $ => prec.left(2, seq($.expression, $.double_equal, $.expression)), // Tokens _semicolon: $ => ";", @@ -147,6 +152,9 @@ export default grammar({ string_literal: $ => /".*"/, integer: $ => /[0-9]+/, annotation_literal: $ => /@[a-z_]+/, + double_equal: $=> '==', + open_bracket: $=> '[', + close_bracket: $=> ']', }, } );