Refactor Parsing -> Compiling

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-02 08:25:29 -05:00
parent cfe78a9b29
commit 6dba17c97a
21 changed files with 550 additions and 489 deletions
+5 -5
View File
@@ -24,7 +24,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@testable import P4Compiler
@Test func test_simple_parser_syntax_error() async throws {
let simple_parser_declaration = """
@@ -37,7 +37,7 @@ import TreeSitterP4
#expect(
#RequireErrorResult(
Error(withMessage: "Could not compile the P4 program"),
Program.Parse(simple_parser_declaration)))
Program.Compile(simple_parser_declaration)))
}
@Test func test_simple_parser_with_statement() async throws {
@@ -50,7 +50,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
#expect(parser.states.count() == 1)
@@ -72,7 +72,7 @@ import TreeSitterP4
bool() main;
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
}
@@ -88,7 +88,7 @@ import TreeSitterP4
};
"""
let compilation_error = try #UseErrorResult(Program.Parse(simple_parser_declaration))
let compilation_error = try #UseErrorResult(Program.Compile(simple_parser_declaration))
#expect(compilation_error.msg.contains("asde"))
#expect(compilation_error.msg.contains("asdf"))
+8 -8
View File
@@ -25,7 +25,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@testable import P4Compiler
@Test func test_simple_runtime() async throws {
let simple_parser_declaration = """
@@ -37,7 +37,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -55,7 +55,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
// We should be in the accept state.
@@ -72,7 +72,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(
#RequireErrorResult<ParserRuntime>(
@@ -92,7 +92,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -114,7 +114,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -141,7 +141,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, exec_result) = try! #UseOkResult(runtime.run())
@@ -176,7 +176,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, exec_result) = try! #UseOkResult(runtime.run())
+48 -5
View File
@@ -25,7 +25,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@testable import P4Compiler
@Test func test_simple_local_element_variable_declaration() async throws {
let simple_parser_declaration = """
@@ -42,7 +42,7 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
@@ -84,7 +84,7 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
@@ -119,7 +119,7 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
@@ -135,6 +135,7 @@ import TreeSitterP4
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.eq(rhs: P4BooleanValue(withValue: false)))
}
@Test func test_simple_assignment() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -152,7 +153,7 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
@@ -169,4 +170,46 @@ import TreeSitterP4
#expect(where_to.eq(rhs: P4BooleanValue(withValue: false)))
let where_from = try #require(scope.lookup(identifier: Identifier(name: "where_from")))
#expect(where_from.eq(rhs: P4StringValue(withValue: "\"there\"")))
}
@Test func test_nested_declaration_assignment() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
bool where_to = true;
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;
};
}
};
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(state_result == P4Lang.reject)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 3)
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.eq(rhs: P4BooleanValue(withValue: false)))
let where_from = try #require(scope.lookup(identifier: Identifier(name: "where_from")))
#expect(where_from.eq(rhs: P4StringValue(withValue: "\"there\"")))
}
+1 -1
View File
@@ -24,7 +24,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@testable import P4Compiler
@Test func test_scope() async throws {
let s = LexicalScope()
+1 -1
View File
@@ -23,7 +23,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@testable import P4Compiler
struct NotStringConvertible {}
+10 -10
View File
@@ -24,14 +24,14 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@testable import P4Compiler
@Test func test_invalid_types() async throws {
for invalid_type_name in ["boo", "str", "in"] {
#expect(
#RequireErrorResult(
Error(withMessage: "Type name not recognized"),
Types.ParseBasicType(type: invalid_type_name)))
Types.CompileBasicType(type: invalid_type_name)))
}
}
@@ -51,9 +51,9 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"Failed to parse a statement element: Cannot assign value of type Boolean to where_to (with type String)"
"{112, 16}: Failed to parse a statement element: {112, 16}: Cannot assign value of type Boolean to where_to (with type String)"
),
Program.Parse(simple_parser_declaration)))
Program.Compile(simple_parser_declaration)))
}
@Test func test_invalid_type_in_assignment2() async throws {
@@ -72,9 +72,9 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"Failed to parse a statement element: Cannot assign value of type String to where_to (with type Boolean)"
"{114, 22}: Failed to parse a statement element: {114, 22}: Cannot assign value of type String to where_to (with type Boolean)"
),
Program.Parse(simple_parser_declaration)))
Program.Compile(simple_parser_declaration)))
}
@Test func test_invalid_type_in_declaration() async throws {
@@ -92,9 +92,9 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"Failed to parse local element: Cannot initialize where_to (with type Boolean) from rvalue with type String"
"{86, 27}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from rvalue with type String"
),
Program.Parse(simple_parser_declaration)))
Program.Compile(simple_parser_declaration)))
}
@Test func test_invalid_type_in_declaration2() async throws {
@@ -112,7 +112,7 @@ import TreeSitterP4
#RequireErrorResult(
Error(
withMessage:
"Failed to parse local element: Cannot initialize where_from (with type String) from rvalue with type Boolean"
"{77, 29}: Failed to parse a statement element: Cannot initialize where_from (with type String) from rvalue with type Boolean"
),
Program.Parse(simple_parser_declaration)))
Program.Compile(simple_parser_declaration)))
}