Refactor Parsing/Runtime

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-24 00:28:41 -05:00
parent f9353c683e
commit 64a0fe4255
29 changed files with 1269 additions and 974 deletions
+4 -4
View File
@@ -24,7 +24,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import Parser
@testable import P4Parser
@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"),
Parser.Program(simple_parser_declaration)))
Program.Parse(simple_parser_declaration)))
}
@Test func test_simple_parser_with_statement() async throws {
@@ -50,7 +50,7 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
#expect(parser.states.count() == 1)
@@ -72,6 +72,6 @@ import TreeSitterP4
bool() main;
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
}
+17 -17
View File
@@ -17,15 +17,15 @@
import Common
import Foundation
import Lang
import P4Lang
import Macros
import Runtime
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import Parser
@testable import P4Parser
@Test func test_simple_runtime() async throws {
let simple_parser_declaration = """
@@ -37,12 +37,12 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let program = try #UseOkResult(Program.Parse(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.
#expect(state_result == Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_runtime_to_accept() async throws {
@@ -55,11 +55,11 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let program = try #UseOkResult(Program.Parse(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.
#expect(state_result == Lang.reject)
#expect(state_result == P4Lang.reject)
}
@@ -73,12 +73,12 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
#expect(
#RequireErrorResult<ParserRuntime>(
Error(withMessage: "No start state defined"),
Runtime.ParserRuntime.create(program: program)))
P4Runtime.ParserRuntime.create(program: program)))
}
@Test func test_simple_parser_with_transition_select_expression() async throws {
@@ -94,14 +94,14 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(state_result == Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_simple_parser_with_transition_select_expression_to_reject() async throws {
@@ -116,11 +116,11 @@ import TreeSitterP4
};
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 1)
#expect(state_result == Lang.reject)
#expect(state_result == P4Lang.reject)
}
+22 -23
View File
@@ -17,15 +17,15 @@
import Common
import Foundation
import Lang
import P4Lang
import Macros
import Runtime
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import Parser
@testable import P4Parser
@Test func test_simple_local_element_variable_declaration() async throws {
let simple_parser_declaration = """
@@ -42,8 +42,8 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(execution_result.scopes.count == 1)
@@ -53,7 +53,7 @@ import TreeSitterP4
}
// We should be in the accept state.
#expect(state_result == Lang.reject)
#expect(state_result == P4Lang.reject)
// There are two variables declared.
#expect(scope.count == 2)
@@ -61,8 +61,8 @@ import TreeSitterP4
// Check the names/values of the variables in scope.
let b = try #require(scope.lookup(identifier: Identifier(name: "b")))
let s = try #require(scope.lookup(identifier: Identifier(name: "s")))
#expect(b.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(s.value_type.eq(rhs: P4StringValue(withValue: "\"testing\"")))
#expect(b.eq(rhs: P4BooleanValue(withValue: false)))
#expect(s.eq(rhs: P4StringValue(withValue: "\"testing\"")))
}
@Test func test_simple_scope() async throws {
@@ -84,19 +84,19 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(state_result == Lang.accept)
#expect(state_result == P4Lang.accept)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 2)
let va = try #require(scope.lookup(identifier: Identifier(name: "va")))
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(va.value_type.eq(rhs: P4IntValue(withValue: 5)))
#expect(where_to.eq(rhs: P4BooleanValue(withValue: false)))
#expect(va.eq(rhs: P4IntValue(withValue: 5)))
}
@Test func test_simple_scope2() async throws {
@@ -119,23 +119,22 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, execution_result) = try! #UseOkResult(runtime.run())
#expect(parser.states.count() == 2)
#expect(state_result == Lang.reject)
#expect(state_result == P4Lang.reject)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 1)
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(where_to.eq(rhs: P4BooleanValue(withValue: false)))
}
@Test func test_simple_assignment() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -153,21 +152,21 @@ import TreeSitterP4
"""
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
let program = try #UseOkResult(Program.Parse(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
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 == Lang.accept)
#expect(state_result == P4Lang.accept)
#expect(execution_result.scopes.count == 1)
let scope = try! #require(execution_result.scopes.current)
#expect(scope.count == 2)
let where_to = try #require(scope.lookup(identifier: Identifier(name: "where_to")))
#expect(where_to.value_type.eq(rhs: P4BooleanValue(withValue: false)))
#expect(where_to.eq(rhs: P4BooleanValue(withValue: false)))
let where_from = try #require(scope.lookup(identifier: Identifier(name: "where_from")))
#expect(where_from.value_type.eq(rhs: P4StringValue(withValue: "\"there\"")))
#expect(where_from.eq(rhs: P4StringValue(withValue: "\"there\"")))
}
+66
View File
@@ -0,0 +1,66 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import Foundation
import Macros
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@Test func test_scope() async throws {
let s = LexicalScope()
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Int.create())
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
#expect(found_first.eq(rhs: P4Int.create()))
#expect(s2.count == 1)
}
@Test func test_scope_no_set() async throws {
var ss = LexicalScopes().enter()
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Int.create())
ss = ss.enter()
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean.create())
let found_first = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "first")))
let found_second = try! #UseOkResult(ss.lookup(identifier: Identifier(name: "second")))
#expect(found_first.eq(rhs: P4Int.create()))
#expect(found_second.eq(rhs: P4Boolean.create()))
}
@Test func test_scope_set() async throws {
var ss = LexicalScopes().enter()
let id = Identifier(name: "first")
let id_type = P4Int.create()
ss = ss.declare(identifier: id, withValue: id_type)
ss = ss.enter()
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean.create())
// Change the value of `first`.
ss = ss.set(identifier: id, withValue: P4String.create())
// Verify the change!
let found = try! #UseOkResult(ss.lookup(identifier: id))
#expect(found.eq(rhs: P4String.create()))
}
+1 -1
View File
@@ -23,7 +23,7 @@ import Testing
import TreeSitter
import TreeSitterP4
@testable import Parser
@testable import P4Parser
struct NotStringConvertible {}
+117
View File
@@ -0,0 +1,117 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import Foundation
import Macros
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Parser
@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)))
}
}
@Test func test_invalid_type_in_assignment() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
string where_to = "Testing";
where_to = true;
transition reject;
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"Failed to parse a statement element: Cannot assign value of type Boolean to where_to (with type String)"
),
Program.Parse(simple_parser_declaration)))
}
@Test func test_invalid_type_in_assignment2() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
bool where_to = true;
string where_from = "testing";
where_to = where_from;
transition reject;
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"Failed to parse a statement element: Cannot assign value of type String to where_to (with type Boolean)"
),
Program.Parse(simple_parser_declaration)))
}
@Test func test_invalid_type_in_declaration() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
string where_from = "testing";
bool where_to = where_from;
transition reject;
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"Failed to parse local element: Cannot initialize where_to (with type Boolean) from rvalue with type String"
),
Program.Parse(simple_parser_declaration)))
}
@Test func test_invalid_type_in_declaration2() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
bool where_to = true;
string where_from = where_to;
transition reject;
}
};
"""
#expect(
#RequireErrorResult(
Error(
withMessage:
"Failed to parse local element: Cannot initialize where_from (with type String) from rvalue with type Boolean"
),
Program.Parse(simple_parser_declaration)))
}