Add support for directions on parameters and attributed types. The latter is necessary to support the former, but is not limited to the direction use case. An attributed type is a P4 type with attributes (like its direction, whether it is read only, etc.) Other attributes will be added in the future. Changes necessary to support attributed types include: 1. Global instances tracked during compilation are not attributed types and not simply types. 2. (future) Type checking will have to make sure that a types attributes do not affect type compatibility. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
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:
- Limited parts of the language can be parsed.
- Limited programs can be evaluated.
As an example of what can be parsed and evaluated, here is a fairly complex P4 program cobbled together from our unit tests:
control simple(bool x, bool y) {
action a(int z) {
z = false;
}
table t {
key = {
x: exact;
y: exact;
}
}
};
struct Testing {
bool yesno;
int count;
};
parser main_parser() {
state start {
Testing ts;
ts.count = 1;
transition select (ts.count) {
0: accept;
_: reject;
};
}
};
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.
Commit Messages
We will try to maintain the following headline format for commit messages:
<component>: <subcomponent>: <change>
where <component> is one of:
grammar: For the tree-sitter-based grammar.compiler: For the Swift-based P4 compiler of tree-sitter-based-parser parsed programs into AST.runtime: For the Swift-based P4 interpreter.common: For any Swift-based components common to the entire project (and macros).documentation: For any documentation updates.testing: For Swift-based tests.
where <subcomponent> can be more free-form and <change> is a pithy description of the changes in the commit.
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/