grammar,compiler: Add Support For Fixed-Width Integers
Distinguishing between signed and unsigned fixed-width integer types must still be done. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -40,9 +40,12 @@ export default grammar({
|
||||
|
||||
// Common - Types
|
||||
typeRef: $ => choice($.baseType, $.type_identifier),
|
||||
baseType: $ => choice($.bool, $.error, $.string, $.int, $.bit /* omitting "templated" types" */),
|
||||
baseType: $ => choice($.bool, $.error, $.string, $.int_type, $.bit /* omitting "templated" types" */),
|
||||
constructor_parameters: $ => seq('(', optional($.parameter_list), ')'),
|
||||
|
||||
int_type: $ => seq($.int, optional($.bit_width)),
|
||||
bit_width: $ => seq('<', $.integer, '>'),
|
||||
|
||||
// Common - Parsers
|
||||
parserType: $ => seq(optional($.annotations), $.parser, field('parser_name', $.identifier), optional($.typeParameters), $.parameters),
|
||||
|
||||
@@ -120,7 +123,7 @@ export default grammar({
|
||||
// Expressions
|
||||
expression: $ => choice($.grouped_expression, $.simple_expression),
|
||||
grouped_expression: $ => seq('(', $.expression, ')'),
|
||||
simple_expression: $ => choice($.identifier, $.integer, $.booleanLiteralExpression, $.string_literal, $.binaryOperatorExpression, $.arrayAccessExpression, $.fieldAccessExpression, $.function_call), // Very limited.
|
||||
simple_expression: $ => choice($.identifier, $.integer, $.integer_elaborated, $.booleanLiteralExpression, $.string_literal, $.binaryOperatorExpression, $.arrayAccessExpression, $.fieldAccessExpression, $.function_call), // 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),
|
||||
@@ -211,6 +214,7 @@ export default grammar({
|
||||
type_identifier: $ => /[A-Za-z_]+/,
|
||||
string_literal: $ => /"[^"]*"/,
|
||||
integer: $ => /[0-9]+/,
|
||||
integer_elaborated: $ => /[0-9]+[ws][-0-9]+/,
|
||||
annotation_literal: $ => /@[A-Za-z_]+/,
|
||||
default_keyset: $=> '_',
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ parser simple() {
|
||||
)
|
||||
|
||||
=========================
|
||||
Simple Declaration (int)
|
||||
Simple Declaration (infinite-precision int)
|
||||
=========================
|
||||
parser simple() {
|
||||
state start {
|
||||
@@ -73,7 +73,9 @@ parser simple() {
|
||||
(variableDeclaration
|
||||
(typeRef
|
||||
(baseType
|
||||
(int)
|
||||
(int_type
|
||||
(int)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
@@ -92,6 +94,59 @@ parser simple() {
|
||||
)
|
||||
)
|
||||
|
||||
=========================
|
||||
Simple Declaration (fixed-precision int)
|
||||
=========================
|
||||
parser simple() {
|
||||
state start {
|
||||
int<55> l;
|
||||
transition accept;
|
||||
}
|
||||
};
|
||||
|
||||
---
|
||||
(p4program
|
||||
(declaration
|
||||
(parserDeclaration
|
||||
(parserType
|
||||
(parser)
|
||||
(identifier)
|
||||
(parameters)
|
||||
)
|
||||
(parserStates
|
||||
(parserState
|
||||
(state)
|
||||
(identifier)
|
||||
(parserStatements
|
||||
(parserStatement
|
||||
(variableDeclaration
|
||||
(typeRef
|
||||
(baseType
|
||||
(int_type
|
||||
(int)
|
||||
(bit_width
|
||||
(integer)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
)
|
||||
)
|
||||
)
|
||||
(parserTransitionStatement
|
||||
(transition)
|
||||
(transitionSelectionExpression
|
||||
(identifier)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
=========================
|
||||
Simple Declaration (string)
|
||||
=========================
|
||||
@@ -331,7 +386,9 @@ bool functionb(bool a, int b) {
|
||||
(parameter
|
||||
(typeRef
|
||||
(baseType
|
||||
(int)
|
||||
(int_type
|
||||
(int)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
@@ -393,7 +450,9 @@ bool functionb(in bool a, out int b, inout string c) {
|
||||
)
|
||||
(typeRef
|
||||
(baseType
|
||||
(int)
|
||||
(int_type
|
||||
(int)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
@@ -468,7 +527,9 @@ extern bool functionb(in bool a, out int b, inout string c);
|
||||
)
|
||||
(typeRef
|
||||
(baseType
|
||||
(int)
|
||||
(int_type
|
||||
(int)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
|
||||
@@ -38,7 +38,9 @@ int fun() {
|
||||
(function_declaration
|
||||
(typeRef
|
||||
(baseType
|
||||
(int)
|
||||
(int_type
|
||||
(int)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
@@ -382,7 +384,9 @@ parser simple() {
|
||||
(variableDeclaration
|
||||
(typeRef
|
||||
(baseType
|
||||
(int)
|
||||
(int_type
|
||||
(int)
|
||||
)
|
||||
)
|
||||
)
|
||||
(identifier)
|
||||
|
||||
Reference in New Issue
Block a user