diff --git a/tree-sitter-p4/grammar.js b/tree-sitter-p4/grammar.js index a921c8a..2497937 100644 --- a/tree-sitter-p4/grammar.js +++ b/tree-sitter-p4/grammar.js @@ -88,13 +88,14 @@ export default grammar({ parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression, $._semicolon), // Expressions - expression: $ => choice($.identifier, $.integer, $.booleanLiteralExpression, $.string_literal, $.binaryOperatorExpression, $.arrayAccessExpression), // Very limited. + expression: $ => choice($.identifier, $.integer, $.booleanLiteralExpression, $.string_literal, $.binaryOperatorExpression, $.arrayAccessExpression, $.fieldAccessExpression), // 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($.expression, $.open_bracket, $.expression, $.close_bracket), + fieldAccessExpression: $=> prec.left(2, seq($.expression, $.field_access, $.identifier)), // Binary Operations binaryEqualOperatorExpression: $ => prec.left(2, seq($.expression, $.double_equal, $.expression)), @@ -155,6 +156,7 @@ export default grammar({ double_equal: $=> '==', open_bracket: $=> '[', close_bracket: $=> ']', + field_access: $=> '.', }, } ); diff --git a/tree-sitter-p4/test/corpus/expressions.txt b/tree-sitter-p4/test/corpus/expressions.txt index 29993f1..fb3cf34 100644 --- a/tree-sitter-p4/test/corpus/expressions.txt +++ b/tree-sitter-p4/test/corpus/expressions.txt @@ -117,6 +117,7 @@ parser simple() { ) ) ) + ========================= Simple Array Access ========================= @@ -168,3 +169,107 @@ parser simple() { ) ) ) + +========================= +Simple Struct Access +========================= +parser simple() { + state start { + abc.xyz; + transition accept; + } +}; + +--- +(p4program + (declaration + (parserDeclaration + (parserType + (parser) + (identifier) + ) + (parserStates + (parserState + (state) + (identifier) + (parserStatements + (parserStatement + (expressionStatement + (expression + (fieldAccessExpression + (expression + (identifier) + ) + (field_access) + (identifier) + ) + ) + ) + ) + ) + (parserTransitionStatement + (transition) + (transitionSelectionExpression + (identifier) + ) + ) + ) + ) + ) + ) +) + +========================= +Nested Struct Access +========================= +parser simple() { + state start { + abc.cde.xyz; + transition accept; + } +}; + +--- +(p4program + (declaration + (parserDeclaration + (parserType + (parser) + (identifier) + ) + (parserStates + (parserState + (state) + (identifier) + (parserStatements + (parserStatement + (expressionStatement + (expression + (fieldAccessExpression + (expression + (fieldAccessExpression + (expression + (identifier) + ) + (field_access) + (identifier) + ) + ) + (field_access) + (identifier) + ) + ) + ) + ) + ) + (parserTransitionStatement + (transition) + (transitionSelectionExpression + (identifier) + ) + ) + ) + ) + ) + ) +)