@@ -28,6 +28,30 @@ import TreeSitterP4
|
||||
@testable import Parser
|
||||
|
||||
@Test func test_simple_runtime() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
true;
|
||||
transition accept;
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
#expect(#RequireOkResult(Runtime.ParserRuntime.create(program: program)))
|
||||
|
||||
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
|
||||
else {
|
||||
assert(false)
|
||||
}
|
||||
|
||||
// We should be in the accept state.
|
||||
#expect(state_result == Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_simple_runtime_to_accept() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
@@ -39,8 +63,19 @@ import TreeSitterP4
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
#expect(#RequireOkResult(Runtime.ParserRuntime.create(program: program)))
|
||||
|
||||
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
|
||||
else {
|
||||
assert(false)
|
||||
}
|
||||
|
||||
// We should be in the accept state.
|
||||
#expect(state_result == Lang.reject)
|
||||
}
|
||||
|
||||
|
||||
@Test func test_simple_runtime_no_start_state() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
@@ -89,7 +124,7 @@ import TreeSitterP4
|
||||
}
|
||||
|
||||
// We should be in the accept state.
|
||||
#expect(state_result == Lang.accept)
|
||||
#expect(state_result == Lang.reject)
|
||||
|
||||
// There are two variables declared.
|
||||
#expect(scope.count == 2)
|
||||
@@ -100,3 +135,57 @@ import TreeSitterP4
|
||||
#expect(b.value_type.eq(rhs: P4BooleanValue(withValue: false)))
|
||||
#expect(s.value_type.eq(rhs: P4StringValue(withValue: "\"testing\"")))
|
||||
}
|
||||
|
||||
@Test func test_simple_parser_with_transition_select_expression() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
transition select (true) {
|
||||
false: reject;
|
||||
true: accept;
|
||||
};
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
|
||||
#expect(parser.states.count() == 1)
|
||||
|
||||
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
|
||||
else {
|
||||
assert(false)
|
||||
}
|
||||
#expect(state_result == Lang.accept)
|
||||
}
|
||||
|
||||
@Test func test_simple_parser_with_transition_select_expression_to_reject() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
transition select (false) {
|
||||
false: reject;
|
||||
true: accept;
|
||||
};
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
|
||||
#expect(parser.states.count() == 1)
|
||||
|
||||
let runtime = try #UseOkResult(Runtime.ParserRuntime.create(program: program))
|
||||
|
||||
guard case Common.Result.Ok(let (state_result, _)) = runtime.run(input: Packet())
|
||||
else {
|
||||
assert(false)
|
||||
}
|
||||
#expect(state_result == Lang.reject)
|
||||
}
|
||||
Reference in New Issue
Block a user