compiler: Refactor Compiler To Remove Ambiguities
Continuous Integration / Grammar Tests (push) Successful in 39s
Continuous Integration / Library Format Tests (push) Successful in 1m51s
Continuous Integration / Library Tests (push) Failing after 4m44s

There were significant overlaps in the names of data structures
between the compiler and the language that made it necessary
to litter the code with P4Lang.xxxx. This refactor removes that
requirement in most places (Parser is ambiguous wherever TreeSitter
is used -- cannot avoid that!)

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-27 12:59:29 -04:00
parent 61d8f601e8
commit 294f76acd4
39 changed files with 735 additions and 699 deletions
+18 -22
View File
@@ -140,7 +140,7 @@ extension FunctionDeclaration: CompilableDeclaration {
// Do not use the updated context returned by parsing the body
// and do not use the function_scope, either.
// And, do not update the context if we are compiling in an
// extern context.
// extern context -- the wrapping extern declaration will take care of that.
return .Ok(
(
function_declaration,
@@ -381,7 +381,7 @@ extension P4Lang.Parser: CompilableDeclaration {
}
/// TODO: Handle extern parsers.
switch Parser.Compile(
switch SpecialCompilers.Compile(
withName: parser_name!, withParameters: parameter_list, node: current_node!,
withContext: current_context)
{
@@ -393,9 +393,7 @@ extension P4Lang.Parser: CompilableDeclaration {
(
parser_declaration,
context.extern_context
? updated_context.update(
newExterns: updated_context.externs.declare(
identifier: parser.name, withValue: parser_declaration))
? context
: updated_context.update(
newTypes: updated_context.types.declare(identifier: parser.name, withValue: parser))
))
@@ -561,9 +559,7 @@ extension Control: CompilableDeclaration {
(
declared_control,
context.extern_context
? context.update(
newTypes: context.externs.declare(
identifier: control_name, withValue: declared_control))
? context
: context.update(
newTypes: context.types.declare(
identifier: control_name, withValue: control))
@@ -575,7 +571,7 @@ extension Action: Compilable {
public typealias T = Action
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.Action, CompilerContext)> {
) -> Common.Result<(Action, CompilerContext)> {
#RequireNodeType<Node, (P4Type, CompilerContext)>(
node: node, type: "action_declaration", nice_type_name: "Action Declaration")
@@ -588,7 +584,7 @@ extension Action: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error(
or: Result<(Action, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
))
@@ -605,7 +601,7 @@ extension Action: Compilable {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error(
or: Result<(Action, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
))
@@ -633,7 +629,7 @@ extension Action: Compilable {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error(
or: Result<(Action, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
))
@@ -668,7 +664,7 @@ extension TableKeyEntry: Compilable {
public typealias T = TableKeyEntry
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> {
) -> Common.Result<(TableKeyEntry, CompilerContext)> {
#RequireNodeType<Node, (P4Type, CompilerContext)>(
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
@@ -681,7 +677,7 @@ extension TableKeyEntry: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
or: Result<(TableKeyEntry, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing table key entry declaration component")))
@@ -697,7 +693,7 @@ extension TableKeyEntry: Compilable {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
or: Result<(TableKeyEntry, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing table key entry declaration component")))
@@ -716,7 +712,7 @@ extension TableKeyMatchType: Compilable {
public typealias T = TableKeyMatchType
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TableKeyMatchType, CompilerContext)> {
) -> Common.Result<(TableKeyMatchType, CompilerContext)> {
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
node: node, type: "table_key_match_type", nice_type_name: "Table Key Match Type")
@@ -734,7 +730,7 @@ extension TableKeys: Compilable {
public typealias T = TableKeys
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TableKeys, CompilerContext)> {
) -> Common.Result<(TableKeys, CompilerContext)> {
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
node: node, type: "table_keys", nice_type_name: "Table Keys")
@@ -750,7 +746,7 @@ extension TableKeys: Compilable {
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeys, CompilerContext)>.Error(
or: Result<(TableKeys, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing table keys declaration component in control declaration"))
@@ -839,7 +835,7 @@ extension TablePropertyList: Compilable {
public typealias T = TablePropertyList
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TablePropertyList, CompilerContext)> {
) -> Common.Result<(TablePropertyList, CompilerContext)> {
#RequireNodeType<Node, (P4Type, CompilerContext)>(
node: node, type: "table_property_list", nice_type_name: "Table Property List")
@@ -913,7 +909,7 @@ extension Table: Compilable {
public typealias T = Table
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.Table, CompilerContext)> {
) -> Common.Result<(Table, CompilerContext)> {
let table_declaration_node = node
#RequireNodeType<Node, (P4Type, CompilerContext)>(
@@ -928,7 +924,7 @@ extension Table: Compilable {
walker.next() // Skip the XXX?
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Table, CompilerContext)>.Error(
or: Result<(Table, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
))
@@ -946,7 +942,7 @@ extension Table: Compilable {
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Table, CompilerContext)>.Error(
or: Result<(Table, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
))