Semicolon Cleanup

1. Semicolons were required in the wrong spot.
2. Make semicolons "invisible" (from the tree-sitter perspective).

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-27 08:23:27 -05:00
parent 1495074459
commit cfe78a9b29
13 changed files with 73 additions and 92 deletions
+11 -11
View File
@@ -22,7 +22,7 @@ export default grammar({
name: 'p4',
rules: {
// Start symbol
p4program: $ => optional(repeat(seq(choice($.declaration, $.instantiation), $.semicolon))),
p4program: $ => optional(repeat(seq(choice($.declaration, $.instantiation), $._semicolon))),
// Common
@@ -43,14 +43,14 @@ export default grammar({
// Mark with higher precedence so that the local states are preferred when parsing!
// TODO: Test!
parserLocalElements: $ => prec(2, repeat1(seq($.parserLocalElement, $.semicolon))),
parserLocalElements: $ => prec(2, repeat1($.parserLocalElement)),
parserStates: $ => repeat1($.parserState),
parserState: $ => seq(optional($.annotations), $.state, $.identifier, '{', optional($.parserLocalElements), optional($.parserStatements), $.parserTransitionStatement, $.semicolon, '}'),
parserState: $ => seq(optional($.annotations), $.state, $.identifier, '{', optional($.parserLocalElements), optional($.parserStatements), $.parserTransitionStatement, '}'),
parserLocalElement: $ => choice($.variableDeclaration, $.todo),
selectBody: $ => repeat1(seq($.selectCase, $.semicolon)),
selectBody: $ => repeat1(seq($.selectCase, $._semicolon)),
selectCase: $ => seq($.keysetExpression, $.colon, $.identifier),
annotations: $ => repeat1($.annotation),
@@ -69,23 +69,23 @@ 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($.assignment, $.expression))),
variableDeclaration: $ => seq(optional($.annotations), $.typeRef, field('variable_name', $.identifier), optional(seq($.assignment, $.expression)), $._semicolon),
// Statements
// General statements
statements: $ => repeat1(seq($.statement, $.semicolon)),
statements: $ => repeat1($.statement),
statement: $ => choice($.conditionalStatement, $.blockStatement, $.expressionStatement, $.assignmentStatement),// Limited, so far.
blockStatement: $ => seq(optional($.annotations), '{', optional($.statements), '}'),
conditionalStatement: $ => choice(prec(1, seq($.if, '(', $.expression, ')', $.statement)), prec(2, seq($.if, '(', $.expression, ')', $.statement, $.else, $.statement))),
expressionStatement: $=> $.expression,
assignmentStatement: $=> seq($.expression, $.assignment, $.expression),
expressionStatement: $=> seq($.expression, $._semicolon),
assignmentStatement: $=> seq($.expression, $.assignment, $.expression, $._semicolon),
// Parser statements
parserStatements: $ => repeat1(seq($.parserStatement, $.semicolon)),
parserStatements: $ => repeat1($.parserStatement),
parserStatement: $ => choice($.conditionalStatement, $.parserBlockStatement, $.expressionStatement, $.assignmentStatement), // Limited, so far.
parserBlockStatement: $ => seq(optional($.annotations), '{', $.parserStatements, '}'),
parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression),
parserTransitionStatement: $ => seq($.transition, $.transitionSelectionExpression, $._semicolon),
// Expressions
expression: $ => choice($.identifier, $.integer, $.true, $.false, $.string_literal), // Very limited.
@@ -94,7 +94,7 @@ export default grammar({
keysetExpression: $ => $.expression,
// Tokens
semicolon: $ => ";",
_semicolon: $ => ";",
colon: $ => ":",
assignment: $ => "=",
todo: $ => "todo",
+3 -12
View File
@@ -1,16 +1,7 @@
parser main_parser() {
state next_state {
transition reject;
}
state not_next_state {
transition reject;
}
state start {
transition starts;
}
state starts {
transition select (se) {
true: next_state;
};
bool where_to = true;
string where_from = where_to;
transition reject;
}
};
@@ -31,7 +31,6 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -39,12 +38,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -80,7 +77,6 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -88,12 +84,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -129,7 +123,6 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -137,12 +130,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -182,7 +173,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -190,11 +180,9 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
@@ -12,6 +12,5 @@ bool() main;
)
(identifier)
)
(semicolon)
)
-4
View File
@@ -25,12 +25,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
@@ -71,10 +69,8 @@ parser imple(bool pname) {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
+4 -26
View File
@@ -4,7 +4,7 @@ Simple If Statement (No Else)
parser simple() {
state start {
if (true) {
};
}
transition accept;
}
};
@@ -33,7 +33,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -41,12 +40,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -56,7 +53,7 @@ parser simple() {
state start {
if (true) {
} else {
};
}
transition accept;
}
};
@@ -89,7 +86,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -97,12 +93,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -143,7 +137,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserStatements
(parserStatement
@@ -157,7 +150,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -165,12 +157,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -181,7 +171,7 @@ parser simple() {
bool x = true;
if (x) {
x = false;
};
}
transition accept;
}
};
@@ -213,7 +203,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserStatements
(parserStatement
@@ -236,13 +225,11 @@ parser simple() {
)
)
)
(semicolon)
)
)
)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -250,12 +237,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -269,7 +254,7 @@ parser simple() {
y = 1;
} else {
y = 2;
};
}
transition accept;
}
};
@@ -301,7 +286,6 @@ parser simple() {
)
)
)
(semicolon)
(parserLocalElement
(variableDeclaration
(typeRef
@@ -316,7 +300,6 @@ parser simple() {
)
)
)
(semicolon)
)
(parserStatements
(parserStatement
@@ -339,7 +322,6 @@ parser simple() {
)
)
)
(semicolon)
)
)
)
@@ -358,13 +340,11 @@ parser simple() {
)
)
)
(semicolon)
)
)
)
)
)
(semicolon)
)
(parserTransitionStatement
(transition)
@@ -372,11 +352,9 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
@@ -25,12 +25,10 @@ parser simple() {
(identifier)
)
)
(semicolon)
)
)
)
)
(semicolon)
)
=========================
@@ -74,15 +72,12 @@ parser simple() {
(colon)
(identifier)
)
(semicolon)
)
)
)
)
(semicolon)
)
)
)
)
(semicolon)
)