Refactor Library And Start Runtime

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-01-20 07:10:58 -05:00
parent 45a8baa323
commit 4bec71dcf4
11 changed files with 472 additions and 65 deletions
+55
View File
@@ -0,0 +1,55 @@
import Testing
import TreeSitter
import SwiftTreeSitter
import TreeSitterP4
import Foundation
import P4
@testable import Parser
@Test func test_simple_parser() async throws {
let simple_parser_declaration = """
parser simple() {
state start {
transition drop;
}
}
"""
let program = try #require(Parser.Program(simple_parser_declaration))
#expect(program.parsers.count == 1)
#expect(program.parsers[0].states.count == 1)
#expect(program.parsers[0].states[0].state_name == "start")
#expect(program.parsers[0].states[0].statements.count == 0)
}
@Test func test_simple_parser_syntax_error() async throws {
let simple_parser_declaration = """
parser simple() {
state
transition drop;
}
}
"""
#expect(Parser.Program(simple_parser_declaration) == nil)
}
@Test func test_simple_parser_with_statement() async throws {
let simple_parser_declaration = """
parser simple() {
state start {
true;
transition drop;
}
}
"""
let program = try #require(Parser.Program(simple_parser_declaration))
#expect(program.parsers.count == 1)
#expect(program.parsers[0].states.count == 1)
#expect(program.parsers[0].states[0].state_name == "start")
#expect(program.parsers[0].states[0].statements.count == 1)
}
+43
View File
@@ -0,0 +1,43 @@
import Foundation
import P4
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import Parser
@Test func test_simple_runtime() async throws {
let simple_parser_declaration = """
parser simple() {
state start {
true;
transition reject;
}
}
"""
let program = try #require(Parser.Program(simple_parser_declaration))
let runtime = P4.ParserRuntime()
#expect(runtime.run(program: program.parsers[0], input: P4.Packet()) == P4.Result.Ok)
}
@Test func test_simple_runtime_no_start_state() async throws {
let simple_parser_declaration = """
parser simple() {
state tart {
true;
transition reject;
}
}
"""
let program = try #require(Parser.Program(simple_parser_declaration))
#expect(
P4.ParserRuntime().run(program: program.parsers[0], input: P4.Packet())
== Result.Error(Error(withMessage: "Could not find the start state")))
}
-35
View File
@@ -1,35 +0,0 @@
import Testing
import TreeSitter
import SwiftTreeSitter
import TreeSitterP4
import Foundation
@testable import p4lm
@Test func example() async throws {
let simple_parser_declaration = """
parser simple() {
state testing {}
}
"""
guard let tree = p4lm.parse(simple_parser_declaration) else {
assert(false, "Could not parse the simple parser declaration.")
}
let p4lang = Language(tree_sitter_p4())
let query = try! SwiftTreeSitter.Query(language: p4lang, data: String("(parserDeclaration (parserTypeDeclaration (parser) parser_name: (identifier) @parser-name))").data(using: String.Encoding.utf8)!)
let qr = query.execute(in: tree)
// TODO: Figure out how to actually determine the number of matches.
guard let parser_declaration = qr.next() else {
assert(false, "Could not parse the simple parser declaration (No parser declaration).")
}
let parser_name_capture = parser_declaration.captures(named: "parser-name")[0]
#expect(parser_name_capture.node.text == Optional<String>.some("simple"))
}