Add Support For Parsing Struct Field Access

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-16 08:30:57 -04:00
parent 75f9f68349
commit 8ee20fcf9f
2 changed files with 108 additions and 1 deletions
+3 -1
View File
@@ -88,13 +88,14 @@ export default grammar({
parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression, $._semicolon), parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression, $._semicolon),
// Expressions // 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), booleanLiteralExpression: $ => choice($.true, $.false),
selectExpression: $ => seq($.select, '(', $.expression, ')', '{', $.selectBody, '}'), // TODO: Should be expression list and not just a single expression selectExpression: $ => seq($.select, '(', $.expression, ')', '{', $.selectBody, '}'), // TODO: Should be expression list and not just a single expression
transitionSelectionExpression: $ => choice($.identifier, $.selectExpression), transitionSelectionExpression: $ => choice($.identifier, $.selectExpression),
keysetExpression: $ => $.expression, keysetExpression: $ => $.expression,
binaryOperatorExpression: $ => choice($.binaryEqualOperatorExpression), binaryOperatorExpression: $ => choice($.binaryEqualOperatorExpression),
arrayAccessExpression: $=> seq($.expression, $.open_bracket, $.expression, $.close_bracket), arrayAccessExpression: $=> seq($.expression, $.open_bracket, $.expression, $.close_bracket),
fieldAccessExpression: $=> prec.left(2, seq($.expression, $.field_access, $.identifier)),
// Binary Operations // Binary Operations
binaryEqualOperatorExpression: $ => prec.left(2, seq($.expression, $.double_equal, $.expression)), binaryEqualOperatorExpression: $ => prec.left(2, seq($.expression, $.double_equal, $.expression)),
@@ -155,6 +156,7 @@ export default grammar({
double_equal: $=> '==', double_equal: $=> '==',
open_bracket: $=> '[', open_bracket: $=> '[',
close_bracket: $=> ']', close_bracket: $=> ']',
field_access: $=> '.',
}, },
} }
); );
+105
View File
@@ -117,6 +117,7 @@ parser simple() {
) )
) )
) )
========================= =========================
Simple Array Access 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)
)
)
)
)
)
)
)