@@ -15,80 +15,84 @@
|
||||
// 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 Runtime
|
||||
import SwiftTreeSitter
|
||||
import Testing
|
||||
import TreeSitter
|
||||
import SwiftTreeSitter
|
||||
import TreeSitterP4
|
||||
import Foundation
|
||||
import Runtime
|
||||
import Common
|
||||
|
||||
import Macros
|
||||
|
||||
@testable import Parser
|
||||
|
||||
@Test func test_simple_parser() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
transition drop;
|
||||
}
|
||||
};
|
||||
"""
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
transition start;
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
|
||||
#expect(parser.states.count == 1)
|
||||
#expect(parser.states[0].state_name == "start")
|
||||
#expect(parser.states[0].statements.count == 0)
|
||||
#expect(parser.states.count() == 1)
|
||||
let state = try! #require(parser.states.find(withName: "start"))
|
||||
#expect(state.state_name == "start")
|
||||
#expect(state.statements.count == 0)
|
||||
|
||||
#expect(#RequireOkResult(parser.states.semantic_check()))
|
||||
let next_state = try! #require(state.next_state)
|
||||
#expect(next_state == state)
|
||||
}
|
||||
|
||||
@Test func test_simple_parser_syntax_error() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state
|
||||
transition drop;
|
||||
}
|
||||
};
|
||||
"""
|
||||
#expect(#RequireErrorResult(Error(withMessage: "Could not compile the P4 program"), Parser.Program(simple_parser_declaration)))
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state
|
||||
transition start;
|
||||
}
|
||||
};
|
||||
"""
|
||||
#expect(
|
||||
#RequireErrorResult(
|
||||
Error(withMessage: "Could not compile the P4 program"),
|
||||
Parser.Program(simple_parser_declaration)))
|
||||
}
|
||||
|
||||
@Test func test_simple_parser_with_statement() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
true;
|
||||
transition drop;
|
||||
}
|
||||
};
|
||||
"""
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
true;
|
||||
transition start;
|
||||
}
|
||||
};
|
||||
"""
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
|
||||
#expect(parser.states.count == 1)
|
||||
#expect(parser.states[0].state_name == "start")
|
||||
#expect(parser.states[0].statements.count == 1)
|
||||
#expect(parser.states.count() == 1)
|
||||
|
||||
let state = try! #require(parser.states.find(withName: "start"))
|
||||
#expect(state.state_name == "start")
|
||||
#expect(state.statements.count == 1)
|
||||
}
|
||||
|
||||
@Test func test_simple_parser_with_instantiation() async throws {
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
true;
|
||||
transition drop;
|
||||
}
|
||||
};
|
||||
bool() main;
|
||||
"""
|
||||
let simple_parser_declaration = """
|
||||
parser main_parser() {
|
||||
state start {
|
||||
true;
|
||||
transition start;
|
||||
}
|
||||
};
|
||||
bool() main;
|
||||
"""
|
||||
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
|
||||
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
|
||||
#expect(parser.states.count == 1)
|
||||
#expect(parser.states[0].state_name == "start")
|
||||
#expect(parser.states[0].statements.count == 1)
|
||||
let program = try #UseOkResult(Parser.Program(simple_parser_declaration))
|
||||
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
|
||||
}
|
||||
|
||||
|
||||
@@ -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