Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into replace-madoko-spec…
Browse files Browse the repository at this point in the history
…-with-asciidoc-try4

Signed-off-by: Andy Fingerhut <[email protected]>
  • Loading branch information
jafingerhut committed Nov 6, 2024
2 parents 4c5dfed + be66b7c commit 3acbd88
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
38 changes: 38 additions & 0 deletions p4-16/spec/P4-16-spec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5479,6 +5479,7 @@ statement
| exitStatement
| returnStatement
| switchStatement
| loopStatement
;
include::grammar.adoc[tag=assignmentOrMethodCallStatement]
Expand Down Expand Up @@ -5727,6 +5728,43 @@ See "Implementing generalized P4_16 switch statements"
for possible techniques that one might
use to implement generalized switch statements.footnote:GeneralizedSwitchStatements[<https://github.com/p4lang/p4-spec/blob/master/p4-16/spec/docs/implementing-generalized-switch-statements.md>]

[#sec-loop-stmt]
=== Loop statement

A `loop` statement executes a statement zero or more times, based on a
condition or a range or collection.

[source,bison]
----
include::grammar.adoc[tag=loopStatement]
include::grammar.adoc[tag=forInitStatement]
include::grammar.adoc[tag=forUpdateStatement]
----

The basic 3-clause `for` statement is similar to a C for statement. The init statements
will be executed prior to executing the loop. The condition will be evaluated before
each iteration, to determine if the loop should exit. If the condition is false, the loop
will exit without executing any statements from the loop body or update.
The update statements will be executed after the loop body and before evaluating the
condition again for the next iteration.

The `for`-`in` statement executes the body once for each value in a range or each
element in a list expression or header stack. The list or range expression itself
will only be evaluated once, before the first iteration of the loop. All side effects
in the list or range expression will occur before the first iteration of the loop body.

A `break;` statement may be executed in a loop body to immediately exit the loop,
without executing the rest of the body or the update statements.

A `continue;` statement may be executed in a loop body to skip the rest of the current
loop body, executing the update statements and then evaluating the condition to
either execute the loop body again, or terminate the loop.

The scope of any declaration in a for statement is limited to the for statement and its body.

>>>>>>> upstream/main:p4-16/spec/P4-16-spec.mdk
[#sec-packet-parsing]
== Packet parsing

Expand Down
78 changes: 78 additions & 0 deletions p4-16/spec/grammar.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ annotationToken
| APPLY
| BOOL
| BIT
| BREAK
| CONST
| CONTINUE
| CONTROL
| DEFAULT
| ELSE
Expand All @@ -108,6 +110,7 @@ annotationToken
| EXIT
| EXTERN
| FALSE
| FOR
| HEADER
| HEADER_UNION
| IF
Expand Down Expand Up @@ -737,6 +740,18 @@ assignmentOrMethodCallStatement
;
// end::assignmentOrMethodCallStatement[]
// tag::breakStatement[]
breakStatement
: BREAK ";"
;
// end::breakStatement[]
// tag::continueStatement[]
continueStatement
: CONTINUE ";"
;
// end::continueStatement[]
// tag::emptyStatement[]
emptyStatement
: ";"
Expand Down Expand Up @@ -779,7 +794,10 @@ statement
| blockStatement
| returnStatement
| exitStatement
| breakStatement
| continueStatement
| switchStatement
| loopStatement
;
// tag::blockStatement[]
Expand Down Expand Up @@ -822,6 +840,66 @@ switchLabel
;
// end::switchLabel[]
// tag::loopStatement[]
loopStatement
: optAnnotations FOR "(" optForInitStatements ";" expression ";"
optForUpdateStatements ")" statement
| optAnnotations FOR "(" typeRef name IN forCollectionExpr ")" statement
;
// end::loopStatement[]
// tag::optForInitStatements[]
optForInitStatements
: /* empty */
| forInitStatements
;
// end::optForInitStatements[]
// tag::forInitStatements[]
forInitStatements
: forInitStatement
| forInitStatements "," forInitStatement
;
// end::forInitStatements[]
// tag::forInitStatement[]
forInitStatement
: optAnnotations typeRef name optInitializer
| lvalue "(" argumentList ")"
| lvalue "<" typeArgumentList ">" "(" argumentList ")"
| lvalue "=" expression
;
// end::forInitStatement[]
// tag::optForUpdateStatements[]
optForUpdateStatements
: /* empty */
| forUpdateStatements
;
// end::optForUpdateStatements[]
// tag::forUpdateStatements[]
forUpdateStatements
: forUpdateStatement
| forUpdateStatements "," forUpdateStatement
;
// end::forUpdateStatements[]
// tag::forUpdateStatement[]
forUpdateStatement
: lvalue "(" argumentList ")"
| lvalue "<" typeArgumentList ">" "(" argumentList ")"
| lvalue "=" expression
;
// end::forUpdateStatement[]
// tag::forCollectionExpr[]
forCollectionExpr
: expression
| expression ".." erxpression
;
// end::forCollectionExpr[]
// tag::statementOrDeclaration[]
statementOrDeclaration
: variableDeclaration
Expand Down

0 comments on commit 3acbd88

Please sign in to comment.