Will Hawkins 5abaac2816 Add Some Coding Style Guidelines
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
2026-03-27 01:42:25 -04:00
2026-02-24 01:00:32 -05:00
2026-03-27 01:42:25 -04:00
2026-03-27 01:36:38 -04:00
2026-03-23 07:56:37 -04:00
2026-01-23 23:51:49 -05:00
2026-03-06 07:47:05 -05:00
2026-03-27 01:42:25 -04:00

P4RSE: P4 Runtime In Swift Environment

P4RSE (yes, the acronym is a bit forced) is a P4 parser and runtime written in Swift. The project was started as a means to better learn Swift.

Status

Very, very alpha:

  1. Limited parts of the language can be parsed.
  2. Limited programs can be evaluated.

As an example of what can be parsed and evaluated, here is a fairly complex P4 program from our unit tests:

parser main_parser() {
    state start {
        bool where_to = ts.yesno;
        string where_from = "here";
        string where_where = "here";
        if (where_to) {
            bool where_from = true;
            if (where_from) {
                where_to = false;
            }
        }
        where_from = "there";
        transition select (where_to) {
            false: reject;
            true: accept;
        };
    }
};

(assuming hat ts is an instance of a struct with the boolean-typed field yesno)

Please check back often!

Building

Requirements And Basic Build

This project uses code item macros (CodeItemMacros) which are an experimental feature in Swift and not available in production.

Therefore, to compile this project, you must be using a non-production version of the compiler.

With that caveat noted, building can be done with swift build:

$ swift build

Contributing

We would love your help! Contributions are very welcome!

Coding Style

Here are the style guidelines that we are trying to maintain:

  • variables are in snake_case.
  • types are in CamelCase.

Of course, we want to follow the formatter, too: see below.

Testing

To run the P4RSE tests:

$ swift test

To run the parser tests, from the tree-sitter-p4 directory:

$ npx tree-sitter test

Generating Documentation

To build the documentation:

$ swift package generate-documentation

To preview the generated documentation:

$ swift package swift package --disable-sandbox preview-documentation  --target <some target>

For more information, see the documentation for the Swift-DocC plugin.

Checking Format

To check the format:

$ swift-format --recursive -i Sources/
S
Description
No description provided
Readme GPL-3.0 1.3 MiB
Languages
Swift 88.3%
JavaScript 10.7%
P4 0.9%