Skip to content

Commit

Permalink
[fixes] updates based on tesing on Oxide p4 programs
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanlakhani committed Dec 9, 2024
1 parent 12f4d2b commit 12bbff1
Show file tree
Hide file tree
Showing 9 changed files with 14,944 additions and 12,384 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# tree-sitter-p4
# tree-sitter-p4: P4 grammar for tree-sitter

P4 grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).

## Setup

To build the parser, you need to have `tree-sitter` installed. You can install
it using most OS package managers, from source, or through `npm`:

```sh
npm install tree-sitter-cli
```

or with `cargo`:

```sh
cargo install tree-sitter-cli
```

## Contributing

This project is still in its early stages, so there are many things that can be
improved, as we aim toward capturing more of the [`P4_16` specification][p4-16-spec].

To contribute, just open a pull request!

[p4-16-spec]: https://p4.org/p4-spec/docs/P4-16-v1.0.0-spec.html
13 changes: 13 additions & 0 deletions examples/action-toplevel.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
action multicast_inbound() {
hdr.sidecar.sc_code = 4;
hdr.sidecar.sc_ingress = (bit<16>) meta.in_port;
hdr.sidecar.sc_egress = (bit<16>) ig_tm_md.ucast_egress_port;
hdr.sidecar.sc_ether_type = hdr.ethernet.ether_type;
hdr.sidecar.sc_payload = 0;
hdr.sidecar.setValid();
hdr.ethernet.ether_type = ETHERTYPE_SIDECAR;
meta.routed = false;
meta.service_routed = true;
ig_tm_md.ucast_egress_port = USER_SPACE_SERVICE_PORT;
meta.multicast = true;
}
8 changes: 8 additions & 0 deletions examples/control_counters.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
control Services(
inout sidecar_headers_t hdr,
inout sidecar_ingress_meta_t meta,
inout ingress_intrinsic_metadata_for_deparser_t ig_dprsr_md,
inout ingress_intrinsic_metadata_for_tm_t ig_tm_md)
{
Counter<bit<32>, bit<8>>(SVC_COUNTER_MAX, CounterType_t.PACKETS) service_ctr;
}
1 change: 0 additions & 1 deletion examples/list.p4
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ control ingress(inout headers_t hdr) {
hdr.udp,
});
}

}
10 changes: 10 additions & 0 deletions examples/parser_checksums.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parser IngressParser(
packet_in pkt,
out sidecar_headers_t hdr,
out sidecar_ingress_meta_t meta,
out ingress_intrinsic_metadata_t ig_intr_md)
{
Checksum() ipv4_checksum;
Checksum() icmp_checksum;
Checksum() nat_checksum;
}
68 changes: 46 additions & 22 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = grammar({
$.parser_definition,
$.control_definition,
$.package,
$.action,
$.table,
),

package: ($) =>
Expand Down Expand Up @@ -117,7 +119,10 @@ module.exports = grammar({
seq(
repeat($.annotation),
"parser",
$.method_identifier,
choice(
seq($.method_identifier, "<", $.type_identifier, ">"),
$.method_identifier,
),
"(",
repeat($.parameter),
")",
Expand Down Expand Up @@ -212,7 +217,11 @@ module.exports = grammar({

key_type: (_) => choice("range", "exact", "ternary", "lpm", "optional"),

state: ($) => seq("state", $.method_identifier, "{", repeat($.stmt), "}"),
state: ($) =>
choice(
seq($.method_identifier, "()", $.type_identifier, ";"),
seq("state", $.method_identifier, "{", repeat($.stmt), "}"),
),

stmt: ($) =>
choice(
Expand All @@ -221,19 +230,19 @@ module.exports = grammar({
$.var_decl,
seq($.transition, ";"),
seq($.call, ";"),
seq($.expr, ";"),
seq("return", ";"),
),

var_decl: ($) =>
prec(
1,
seq(
optional(choice($._type, $.type_identifier)),
$.identifier,
optional(seq("=", $.expr)),
";",
seq(
optional(choice($._type, $.type_identifier)),
$.lval,
"=",
choice(
seq("(", choice($._type, $.type_identifier), ")", $.expr),
$.expr,
),
";",
),

action: ($) =>
Expand Down Expand Up @@ -308,20 +317,32 @@ module.exports = grammar({

_else: ($) => seq("else", "{", repeat($.stmt), "}"),

_elseif: ($) => seq("else if", "(", $.expr, ")", "{", repeat($.stmt), "}"),

control_var: ($) =>
seq($.type_identifier, "(", repeat($.expr), ")", $.identifier, ";"),
choice(
seq(
$.type_identifier,
optional(seq("<", $.type_argument_list, ">")),
"(",
repeat(seq($.expr, optional(","))),
")",
$.identifier,
";",
),
seq(choice($._type, $.type_identifier), $.identifier, ";"),
),

lval: ($) => seq($.identifier, repeat(seq(".", $.identifier))),

fval: ($) =>
seq(
choice(
seq(
$.identifier,
repeat(seq(".", $.identifier)),
seq(".", $.method_identifier),
),
$.method_identifier,
choice(
seq(
$.identifier,
repeat(seq(".", $.identifier)),
seq(".", $.method_identifier),
),
$.method_identifier,
),

method: ($) =>
Expand Down Expand Up @@ -362,7 +383,6 @@ module.exports = grammar({
$.method_identifier,
seq($.method_identifier, "<", $.type_identifier, ">"),
),

$.identifier,
",",
),
Expand All @@ -383,7 +403,11 @@ module.exports = grammar({
varbit_type: ($) => seq("varbit", "<", $.number, ">"),
tuple_type: ($) => seq("tuple", "<", $.type_argument_list, ">"),

type_argument_list: ($) => seq($._type, repeat(seq(",", $._type))),
type_argument_list: ($) =>
seq(
choice($._type, $.type_identifier),
repeat(seq(",", choice($._type, $.type_identifier))),
),

type_identifier: ($) => $.identifier,

Expand Down Expand Up @@ -438,7 +462,7 @@ module.exports = grammar({
choice(
seq(
"#define",
choice($.identifier_preproc, $.identifier),
$.identifier,
choice(
$.number,
seq("{", optional($.line_continuation), repeat($.field), "}"),
Expand Down
Loading

0 comments on commit 12bbff1

Please sign in to comment.