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
+14 -14
View File
@@ -47,7 +47,7 @@ import TreeSitterP4
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(x).count == 1)
}
@@ -85,7 +85,7 @@ import TreeSitterP4
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(filter).count == 2)
}
@@ -112,7 +112,7 @@ import TreeSitterP4
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(x).count == 1)
}
@@ -139,7 +139,7 @@ import TreeSitterP4
withMessage:
"{54, 63}: Error(s) parsing property list: {91, 26}: Error(s) parsing table actions: Cannot find b in lexical scope."
),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -167,7 +167,7 @@ import TreeSitterP4
withMessage:
"{54, 72}: Error(s) parsing property list: {91, 35}: Error(s) parsing table actions: Cannot find b in lexical scope."
),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -195,7 +195,7 @@ import TreeSitterP4
withMessage:
"{64, 63}: Error(s) parsing property list: {101, 26}: Error(s) parsing table actions: {101, 26}: a does not name an action"
),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -214,7 +214,7 @@ import TreeSitterP4
}
};
"""
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration)))
#expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
}
@Test func test_simple_control_declaration_with_multiple_tables() async throws {
@@ -243,7 +243,7 @@ import TreeSitterP4
Error(
withMessage: "{0, 215}: More than one table in control declaration"
),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -263,7 +263,7 @@ import TreeSitterP4
}
};
"""
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration)))
#expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
}
@Test func test_simple_control_declaration_with_action_using_parameter_wrong_type() async throws {
@@ -288,7 +288,7 @@ import TreeSitterP4
withMessage:
"{57, 10}: Failed to parse a statement element: {57, 1}: Cannot assign value with type Boolean to identifier z with type Int (width: Infinite)"
),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -320,7 +320,7 @@ import TreeSitterP4
withMessage:
"Could not compile the P4 program"
),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -347,7 +347,7 @@ import TreeSitterP4
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(x).count == 1)
}
@@ -372,7 +372,7 @@ import TreeSitterP4
ErrorWithLocation(
sourceLocation: SourceLocation(41, 23),
withError: "All parameters with direction must precede directionless parameters"),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
@@ -397,6 +397,6 @@ import TreeSitterP4
ErrorWithLocation(
sourceLocation: SourceLocation(41, 38),
withError: "All parameters with direction must precede directionless parameters"),
Program.Compile(simple_parser_declaration))
SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
)
}
+7 -7
View File
@@ -50,7 +50,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -125,7 +125,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -200,7 +200,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -274,7 +274,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -349,7 +349,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -429,7 +429,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -557,7 +557,7 @@ import TreeSitterP4
};
"""
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in