compiler: Refactor Compiler To Remove Ambiguities
Continuous Integration / Grammar Tests (push) Successful in 39s
Continuous Integration / Library Format Tests (push) Successful in 1m51s
Continuous Integration / Library Tests (push) Failing after 4m44s

There were significant overlaps in the names of data structures
between the compiler and the language that made it necessary
to litter the code with P4Lang.xxxx. This refactor removes that
requirement in most places (Parser is ambiguous wherever TreeSitter
is used -- cannot avoid that!)

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-27 12:59:29 -04:00
parent 61d8f601e8
commit 294f76acd4
39 changed files with 735 additions and 699 deletions
@@ -45,6 +45,6 @@ let flter = { (tipe: P4QualifiedType) -> Bool in
default: false default: false
} }
} }
if case .Ok(let program) = Program.Compile(p4_program_with_control_decl) { if case .Ok(let program) = SpecialCompilers.ProgramCompiler.Compile(p4_program_with_control_decl) {
print(program.InstancesWithTypes(flter)) print(program.InstancesWithTypes(flter))
} }
+1 -1
View File
@@ -38,7 +38,7 @@ let flter = { (tipe: P4Type) -> Bool in
default: false default: false
} }
} }
if case .Ok(let program) = Program.Compile(p4_program_with_struct_decl) { if case .Ok(let program) = SpecialCompilers.ProgramCompiler.Compile(p4_program_with_struct_decl) {
print(program.TypesWithTypes(flter)) print(program.TypesWithTypes(flter))
} }
+2 -2
View File
@@ -94,7 +94,7 @@ extension Cli {
return return
} }
let maybe_program = Program.Compile(source.getSource()) let maybe_program = SpecialCompilers.ProgramCompiler.Compile(source.getSource())
guard case .Ok(_) = maybe_program else { guard case .Ok(_) = maybe_program else {
let feedback = CompilationFeedback(source, [maybe_program.error()!], formatter) let feedback = CompilationFeedback(source, [maybe_program.error()!], formatter)
print(feedback.feedback) print(feedback.feedback)
@@ -134,7 +134,7 @@ extension Cli {
return return
} }
let maybe_program = Program.Compile(source.getSource()) let maybe_program = SpecialCompilers.ProgramCompiler.Compile(source.getSource())
guard case .Ok(let program) = maybe_program else { guard case .Ok(let program) = maybe_program else {
print(ErrorWithLabel("Compiler Error", maybe_source.error()!).format(formatter)) print(ErrorWithLabel("Compiler Error", maybe_source.error()!).format(formatter))
return return
-1
View File
@@ -16,7 +16,6 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common import Common
import P4Lang
import P4Runtime import P4Runtime
import SwiftTreeSitter import SwiftTreeSitter
import TreeSitterExtensions import TreeSitterExtensions
+18 -22
View File
@@ -140,7 +140,7 @@ extension FunctionDeclaration: CompilableDeclaration {
// Do not use the updated context returned by parsing the body // Do not use the updated context returned by parsing the body
// and do not use the function_scope, either. // and do not use the function_scope, either.
// And, do not update the context if we are compiling in an // And, do not update the context if we are compiling in an
// extern context. // extern context -- the wrapping extern declaration will take care of that.
return .Ok( return .Ok(
( (
function_declaration, function_declaration,
@@ -381,7 +381,7 @@ extension P4Lang.Parser: CompilableDeclaration {
} }
/// TODO: Handle extern parsers. /// TODO: Handle extern parsers.
switch Parser.Compile( switch SpecialCompilers.Compile(
withName: parser_name!, withParameters: parameter_list, node: current_node!, withName: parser_name!, withParameters: parameter_list, node: current_node!,
withContext: current_context) withContext: current_context)
{ {
@@ -393,9 +393,7 @@ extension P4Lang.Parser: CompilableDeclaration {
( (
parser_declaration, parser_declaration,
context.extern_context context.extern_context
? updated_context.update( ? context
newExterns: updated_context.externs.declare(
identifier: parser.name, withValue: parser_declaration))
: updated_context.update( : updated_context.update(
newTypes: updated_context.types.declare(identifier: parser.name, withValue: parser)) newTypes: updated_context.types.declare(identifier: parser.name, withValue: parser))
)) ))
@@ -561,9 +559,7 @@ extension Control: CompilableDeclaration {
( (
declared_control, declared_control,
context.extern_context context.extern_context
? context.update( ? context
newTypes: context.externs.declare(
identifier: control_name, withValue: declared_control))
: context.update( : context.update(
newTypes: context.types.declare( newTypes: context.types.declare(
identifier: control_name, withValue: control)) identifier: control_name, withValue: control))
@@ -575,7 +571,7 @@ extension Action: Compilable {
public typealias T = Action public typealias T = Action
public static func Compile( public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.Action, CompilerContext)> { ) -> Common.Result<(Action, CompilerContext)> {
#RequireNodeType<Node, (P4Type, CompilerContext)>( #RequireNodeType<Node, (P4Type, CompilerContext)>(
node: node, type: "action_declaration", nice_type_name: "Action Declaration") node: node, type: "action_declaration", nice_type_name: "Action Declaration")
@@ -588,7 +584,7 @@ extension Action: Compilable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error( or: Result<(Action, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component" sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
)) ))
@@ -605,7 +601,7 @@ extension Action: Compilable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error( or: Result<(Action, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component" sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
)) ))
@@ -633,7 +629,7 @@ extension Action: Compilable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Action, CompilerContext)>.Error( or: Result<(Action, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component" sourceLocation: node.toSourceLocation(), withError: "Missing action declaration component"
)) ))
@@ -668,7 +664,7 @@ extension TableKeyEntry: Compilable {
public typealias T = TableKeyEntry public typealias T = TableKeyEntry
public static func Compile( public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> { ) -> Common.Result<(TableKeyEntry, CompilerContext)> {
#RequireNodeType<Node, (P4Type, CompilerContext)>( #RequireNodeType<Node, (P4Type, CompilerContext)>(
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry") node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
@@ -681,7 +677,7 @@ extension TableKeyEntry: Compilable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error( or: Result<(TableKeyEntry, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing table key entry declaration component"))) withError: "Missing table key entry declaration component")))
@@ -697,7 +693,7 @@ extension TableKeyEntry: Compilable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error( or: Result<(TableKeyEntry, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing table key entry declaration component"))) withError: "Missing table key entry declaration component")))
@@ -716,7 +712,7 @@ extension TableKeyMatchType: Compilable {
public typealias T = TableKeyMatchType public typealias T = TableKeyMatchType
public static func Compile( public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TableKeyMatchType, CompilerContext)> { ) -> Common.Result<(TableKeyMatchType, CompilerContext)> {
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>( #RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
node: node, type: "table_key_match_type", nice_type_name: "Table Key Match Type") node: node, type: "table_key_match_type", nice_type_name: "Table Key Match Type")
@@ -734,7 +730,7 @@ extension TableKeys: Compilable {
public typealias T = TableKeys public typealias T = TableKeys
public static func Compile( public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TableKeys, CompilerContext)> { ) -> Common.Result<(TableKeys, CompilerContext)> {
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>( #RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
node: node, type: "table_keys", nice_type_name: "Table Keys") node: node, type: "table_keys", nice_type_name: "Table Keys")
@@ -750,7 +746,7 @@ extension TableKeys: Compilable {
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.TableKeys, CompilerContext)>.Error( or: Result<(TableKeys, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(),
withError: "Missing table keys declaration component in control declaration")) withError: "Missing table keys declaration component in control declaration"))
@@ -839,7 +835,7 @@ extension TablePropertyList: Compilable {
public typealias T = TablePropertyList public typealias T = TablePropertyList
public static func Compile( public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.TablePropertyList, CompilerContext)> { ) -> Common.Result<(TablePropertyList, CompilerContext)> {
#RequireNodeType<Node, (P4Type, CompilerContext)>( #RequireNodeType<Node, (P4Type, CompilerContext)>(
node: node, type: "table_property_list", nice_type_name: "Table Property List") node: node, type: "table_property_list", nice_type_name: "Table Property List")
@@ -913,7 +909,7 @@ extension Table: Compilable {
public typealias T = Table public typealias T = Table
public static func Compile( public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.Table, CompilerContext)> { ) -> Common.Result<(Table, CompilerContext)> {
let table_declaration_node = node let table_declaration_node = node
#RequireNodeType<Node, (P4Type, CompilerContext)>( #RequireNodeType<Node, (P4Type, CompilerContext)>(
@@ -928,7 +924,7 @@ extension Table: Compilable {
walker.next() // Skip the XXX? walker.next() // Skip the XXX?
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Table, CompilerContext)>.Error( or: Result<(Table, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component") sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
)) ))
@@ -946,7 +942,7 @@ extension Table: Compilable {
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
or: Result<(P4Lang.Table, CompilerContext)>.Error( or: Result<(Table, CompilerContext)>.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component") sourceLocation: node.toSourceLocation(), withError: "Missing table declaration component")
)) ))
+155 -312
View File
@@ -22,322 +22,165 @@ import SwiftTreeSitter
import TreeSitterExtensions import TreeSitterExtensions
import TreeSitterP4 import TreeSitterP4
public struct Parser { extension Statement: Compilable {
public struct LocalElements { public typealias T = EvaluatableStatement
static func Compile( public static func Compile(
node: Node, withContext context: CompilerContext node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Result<(EvaluatableStatement, CompilerContext)> { ) -> Common.Result<(EvaluatableStatement, CompilerContext)> {
let localElementsParsers: [String: CompilableStatement.Type] = [
"variableDeclaration": VariableDeclarationStatement.self
]
guard let parser = localElementsParsers[node.nodeType ?? ""] else { if node.nodeType != "parserStatement" && node.nodeType != "statement" {
return Result.Error( return Result.Error(
ErrorWithLocation( ErrorWithLocation(
sourceLocation: node.toSourceLocation(), sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement")
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
}
switch parser.Compile(node: node, withContext: context) {
case Result.Ok(let (parsed, parsed_updated_scopes)):
return Result.Ok((parsed, parsed_updated_scopes))
case Result.Error(let e):
return Result.Error(Error(withMessage: "Failed to parse local element: \(e)"))
}
}
}
public struct Statement {
static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(EvaluatableStatement, CompilerContext)> {
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
return Result.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement")
)
}
let statement = node.child(at: 0)!
let statementParsers: [String: CompilableStatement.Type] = [
"assignmentStatement": ParserAssignmentStatement.self,
"expressionStatement": ExpressionStatement.self,
"variableDeclaration": VariableDeclarationStatement.self,
"conditionalStatement": ConditionalStatement.self, "blockStatement": BlockStatement.self,
"return_statement": ReturnStatement.self,
]
guard let parser = statementParsers[statement.nodeType ?? ""] else {
return Result.Error(
ErrorWithLocation(
sourceLocation: statement.toSourceLocation(),
withError:
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
}
switch parser.Compile(node: statement, withContext: context) {
case Result.Ok(let (parsed, updated_context)):
return .Ok((parsed, updated_context))
case Result.Error(let e):
return .Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Failed to parse a statement element: \(e)"))
}
}
}
public struct TransitionStatement {
static func Compile(
node: Node, forState state_identifier: Common.Identifier,
withStatements stmts: [EvaluatableStatement], withContext context: CompilerContext
) -> Result<(ParserState, CompilerContext)> {
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement"
) )
guard let tse_node = node.child(at: 1),
tse_node.nodeType! == "transitionSelectionExpression"
else {
return .Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Could not find transition select expression"))
}
guard let next_node = tse_node.child(at: 0) else {
return .Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Could not find the next token in a transition selection expression"))
}
// If the next node is an identifier, we have the simple form ...
if next_node.nodeType == "identifier" {
let maybe_parsed_next_state_id = Identifier.Compile(
node: next_node, withContext: context)
if case .Ok(let next_state_id) = maybe_parsed_next_state_id {
if case .Ok(let next_state) = context.instances.lookup(identifier: next_state_id) {
switch next_state {
case (_, .some(let instance)):
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier,
withNextState: instance.dataValue() as! InstantiatedParserState,
withStatements: stmts), context
))
case (_, .none):
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier,
withNextStateIdentifier: next_state_id, withStatements: stmts), context
))
}
} else {
return .Error(
Error(
withMessage:
"\(next_state_id) does not name a parser state in scope"
))
}
} else {
return .Error(
Error(
withMessage:
"Could not parse the next state in a transition statement: \(maybe_parsed_next_state_id.error()!)"
))
}
}
// We know that the next node is a select expression.
return
switch SelectExpression.compile(node: next_node, withContext: context)
{
case .Ok(let tse):
.Ok(
(
ParserStateSelectTransition(
name: state_identifier, withTransitionExpression: tse as! SelectExpression,
withStatements: stmts,
), context
))
case .Error(let e): .Error(e)
}
}
}
public struct Statements {
static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<([EvaluatableStatement], CompilerContext)> {
if node.nodeType != "statements" && node.nodeType != "parserStatements" {
return Result.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements"))
}
var errors: (any Errorable)? = .none
var current_context = context
var parsed_s: [EvaluatableStatement] = Array()
node.enumerateNamedChildren { node in
switch Statement.Compile(
node: node, withContext: current_context)
{
case .Ok((let parsed_statement, let updated_context)):
current_context = updated_context
parsed_s.append(parsed_statement)
case .Error(let e):
errors =
if let errors = errors {
errors.append(error: e)
} else {
e
}
}
}
if let errors = errors {
return .Error(errors)
}
return Result.Ok((parsed_s, current_context))
}
}
public struct State {
static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(ParserState, CompilerContext)> {
var walker = Walker(node: node)
var current_node: Node? = .none
guard let node_type = node.nodeType,
node_type == "parserState"
else {
return Result.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Did not find a parser state declaration"))
}
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration")))
if current_node!.nodeType == "annotations" {
return Result.Error(
ErrorWithLocation(
sourceLocation: current_node!.toSourceLocation(),
withError: "Annotations in parser state are not yet handled."))
// Would increment here.
}
// Skip the keyword state
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration")))
let maybe_state_identifier = Identifier.Compile(
node: current_node!, withContext: context)
guard case Result.Ok(let state_identifier) = maybe_state_identifier else {
return Result.Error(maybe_state_identifier.error()!)
}
walker.next()
// Skip the '{'
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
))
var errors: (any Errorable)? = .none
var current_context = context
var parsed_s: [EvaluatableStatement] = Array()
if current_node!.nodeType == "parserStatements" {
switch Statements.Compile(
node: current_node!, withContext: current_context)
{
case .Ok(let (state_statements, updated_context)):
parsed_s = state_statements
current_context = updated_context
case .Error(let error):
errors =
if let errors = errors {
errors.append(error: error)
} else {
error
}
}
walker.next()
}
if let errors = errors {
return .Error(errors)
}
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing transition statement of state declaration")))
return TransitionStatement.Compile(
node: current_node!, forState: state_identifier, withStatements: parsed_s,
withContext: current_context)
}
}
static func Compile(
withName name: Common.Identifier, withParameters parameters: ParameterList, node: Node,
withContext context: CompilerContext
) -> Result<(P4Lang.Parser, CompilerContext)> {
var parser = P4Lang.Parser(withName: name, withParameters: parameters)
// Build a state from each one listed.
var error: (any Errorable)? = .none
var current_context = context
/// TODO: Assert that there is only one.
node.enumerateNamedChildren { parser_state in
if parser_state.nodeType != "parserState" {
return
}
// Parse a state in a nested scope.
switch Parser.State.Compile(
node: parser_state,
withContext: context.update(newInstances: current_context.instances.enter()))
{
case Result.Ok(let (state, updated_context)):
parser.states = parser.states.append(state: state)
current_context = updated_context
case Result.Error(let e): error = e
}
} }
if let error = error { let statement = node.child(at: 0)!
return .Error(error)
}
return Result.Ok((parser, current_context)) let statementParsers: [String: CompilableStatement.Type] = [
"assignmentStatement": ParserAssignmentStatement.self,
"expressionStatement": ExpressionStatement.self,
"variableDeclaration": VariableDeclarationStatement.self,
"conditionalStatement": ConditionalStatement.self, "blockStatement": BlockStatement.self,
"return_statement": ReturnStatement.self,
]
guard let parser = statementParsers[statement.nodeType ?? ""] else {
return Result.Error(
ErrorWithLocation(
sourceLocation: statement.toSourceLocation(),
withError:
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
}
switch parser.Compile(node: statement, withContext: context) {
case Result.Ok(let (parsed, updated_context)):
return .Ok((parsed, updated_context))
case Result.Error(let e):
return .Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Failed to parse a statement element: \(e)"))
}
}
}
extension LocalElements: Compilable {
public typealias T = EvaluatableStatement
public static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(EvaluatableStatement, CompilerContext)> {
let localElementsParsers: [String: CompilableStatement.Type] = [
"variableDeclaration": VariableDeclarationStatement.self
]
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
return Result.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
}
switch parser.Compile(node: node, withContext: context) {
case Result.Ok(let (parsed, parsed_updated_scopes)):
return Result.Ok((parsed, parsed_updated_scopes))
case Result.Error(let e):
return Result.Error(Error(withMessage: "Failed to parse local element: \(e)"))
}
}
}
extension ParserState: Compilable {
public typealias T = ParserState
public static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<(ParserState, CompilerContext)> {
var walker = Walker(node: node)
var current_node: Node? = .none
guard let node_type = node.nodeType,
node_type == "parserState"
else {
return Result.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Did not find a parser state declaration"))
}
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration")))
if current_node!.nodeType == "annotations" {
return Result.Error(
ErrorWithLocation(
sourceLocation: current_node!.toSourceLocation(),
withError: "Annotations in parser state are not yet handled."))
// Would increment here.
}
// Skip the keyword state
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing elements in parser state declaration")))
let maybe_state_identifier = Identifier.Compile(
node: current_node!, withContext: context)
guard case Result.Ok(let state_identifier) = maybe_state_identifier else {
return Result.Error(maybe_state_identifier.error()!)
}
walker.next()
// Skip the '{'
walker.next()
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
))
var errors: (any Errorable)? = .none
var current_context = context
var parsed_s: [EvaluatableStatement] = Array()
if current_node!.nodeType == "parserStatements" {
switch SpecialCompilers.Statements.Compile(
node: current_node!, withContext: current_context)
{
case .Ok(let (state_statements, updated_context)):
parsed_s = state_statements
current_context = updated_context
case .Error(let error):
errors =
if let errors = errors {
errors.append(error: error)
} else {
error
}
}
walker.next()
}
if let errors = errors {
return .Error(errors)
}
#MustOr(
result: current_node, thing: walker.getNext(),
or: Result<(ParserState, CompilerContext)>.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Missing transition statement of state declaration")))
return SpecialCompilers.TransitionStatement.Compile(
node: current_node!, forState: state_identifier, withStatements: parsed_s,
withContext: current_context)
} }
} }
-157
View File
@@ -1,157 +0,0 @@
// 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 P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
public struct Program {
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
// Certain names are always in scope during compilation.
var globals = StaticVarValueScopes().enter()
globals = globals.declare(
identifier: accept.state().getName(),
withValue: (P4QualifiedType(accept.type()), P4Value(accept))
)
.declare(
identifier: reject.state.getName(),
withValue: (P4QualifiedType(reject.type()), P4Value(reject)))
return Program.Compile(
source, withGlobalInstances: globals, withGlobalTypes: .none, withFFIs: [])
}
public static func Compile(
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes
) -> Result<P4Lang.Program> {
return Program.Compile(
source, withGlobalInstances: globalInstances, withGlobalTypes: .none, withFFIs: [])
}
public static func Compile(
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes?,
withGlobalTypes globalTypes: TypeTypeScopes?, withFFIs ffis: [P4FFI] = Array()
) -> Result<P4Lang.Program> {
let maybe_parser = ConfigureP4Parser()
guard case .Ok(let p) = maybe_parser else {
return .Error(maybe_parser.error()!)
}
let result = p.parse(source)
guard let tree = result,
!tree.isError(lang: p4lang),
!tree.containsMissing(lang: p4lang)
else {
return Result.Error(Error(withMessage: "Could not compile the P4 program"))
}
var program = P4Lang.Program()
// Set up a context for parsing.
var compilation_context = CompilerContext()
// Add our FFIs
compilation_context = compilation_context.update(newFFIs: ffis)
var errors: (any Errorable)? = .none
// If the caller gave any global instances, add them here.
if let globalInstances = globalInstances {
compilation_context = compilation_context.update(newInstances: globalInstances)
}
// If the caller gave any global types, add them here.
if let globalTypes = globalTypes {
compilation_context = compilation_context.update(newTypes: globalTypes)
}
// Try to parse all top-level declarations.
result?.rootNode?.enumerateNamedChildren { (declaration_node: Node) in
let specific_declaration_node = declaration_node.child(at: 0)!
let declaration_parsers: [CompilableDeclaration.Type] = [
Declaration.self, P4Lang.Parser.self,
]
var found_parser = false
for parser in declaration_parsers {
switch parser.Compile(node: specific_declaration_node, withContext: compilation_context) {
case .Ok(.none): {}()
case .Ok(.some((_, let updated_context))):
found_parser = true
compilation_context = updated_context
break
case .Error(let e):
found_parser = true
errors =
if let errors = errors {
errors.append(error: e)
} else {
e
}
break
}
}
// If none of the declaration parsers chose to parse, that's an error, too!
if !found_parser {
let no_parser_error = ErrorWithLocation(
sourceLocation: specific_declaration_node.toSourceLocation(),
withError: "Could not find parser for declaration node"
)
errors =
if let errors = errors {
errors.append(error: no_parser_error)
} else {
no_parser_error
}
}
}
if let errors = errors {
return .Error(errors)
}
// Any of the instances that are in the top-level scope should go into the program!
program.instances = Array(
compilation_context.instances.filter { (_, v) in
v.1 != nil
}.map { (_, v) in
v.1!
})
// Any of the types that are in the top-level scope should go into the program!
program.types = Array(
compilation_context.types.map { (_, v) in
v
})
// Any of the extern types that are in the top-level scope should go into the program!
program.externs = Array(
compilation_context.externs.map { (_, v) in
v
})
return Result.Ok(program)
}
}
+336
View File
@@ -0,0 +1,336 @@
// 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 P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
public struct SpecialCompilers {
public struct TransitionStatement {
static func Compile(
node: Node, forState state_identifier: Common.Identifier,
withStatements stmts: [EvaluatableStatement], withContext context: CompilerContext
) -> Result<(ParserState, CompilerContext)> {
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement"
)
guard let tse_node = node.child(at: 1),
tse_node.nodeType! == "transitionSelectionExpression"
else {
return .Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Could not find transition select expression"))
}
guard let next_node = tse_node.child(at: 0) else {
return .Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(),
withError: "Could not find the next token in a transition selection expression"))
}
// If the next node is an identifier, we have the simple form ...
if next_node.nodeType == "identifier" {
let maybe_parsed_next_state_id = Identifier.Compile(
node: next_node, withContext: context)
if case .Ok(let next_state_id) = maybe_parsed_next_state_id {
if case .Ok(let next_state) = context.instances.lookup(identifier: next_state_id) {
switch next_state {
case (_, .some(let instance)):
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier,
withNextState: instance.dataValue() as! InstantiatedParserState,
withStatements: stmts), context
))
case (_, .none):
return .Ok(
(
ParserStateDirectTransition(
name: state_identifier,
withNextStateIdentifier: next_state_id, withStatements: stmts), context
))
}
} else {
return .Error(
Error(
withMessage:
"\(next_state_id) does not name a parser state in scope"
))
}
} else {
return .Error(
Error(
withMessage:
"Could not parse the next state in a transition statement: \(maybe_parsed_next_state_id.error()!)"
))
}
}
// We know that the next node is a select expression.
return
switch SelectExpression.compile(node: next_node, withContext: context)
{
case .Ok(let tse):
.Ok(
(
ParserStateSelectTransition(
name: state_identifier, withTransitionExpression: tse as! SelectExpression,
withStatements: stmts,
), context
))
case .Error(let e): .Error(e)
}
}
}
public struct Statements {
static func Compile(
node: Node, withContext context: CompilerContext
) -> Result<([EvaluatableStatement], CompilerContext)> {
if node.nodeType != "statements" && node.nodeType != "parserStatements" {
return Result.Error(
ErrorWithLocation(
sourceLocation: node.toSourceLocation(), withError: "Did not find expected statements"))
}
var errors: (any Errorable)? = .none
var current_context = context
var parsed_s: [EvaluatableStatement] = Array()
node.enumerateNamedChildren { node in
switch Statement.Compile(
node: node, withContext: current_context)
{
case .Ok((let parsed_statement, let updated_context)):
current_context = updated_context
parsed_s.append(parsed_statement)
case .Error(let e):
errors =
if let errors = errors {
errors.append(error: e)
} else {
e
}
}
}
if let errors = errors {
return .Error(errors)
}
return Result.Ok((parsed_s, current_context))
}
}
static func Compile(
withName name: Common.Identifier, withParameters parameters: ParameterList, node: Node,
withContext context: CompilerContext
) -> Result<(P4Lang.Parser, CompilerContext)> {
var parser = P4Lang.Parser(withName: name, withParameters: parameters)
// Build a state from each one listed.
var error: (any Errorable)? = .none
var current_context = context
/// TODO: Assert that there is only one.
node.enumerateNamedChildren { parser_state in
if parser_state.nodeType != "parserState" {
return
}
// Parse a state in a nested scope.
switch ParserState.Compile(
node: parser_state,
withContext: context.update(newInstances: current_context.instances.enter()))
{
case Result.Ok(let (state, updated_context)):
parser.states = parser.states.append(state: state)
current_context = updated_context
case Result.Error(let e): error = e
}
}
if let error = error {
return .Error(error)
}
return Result.Ok((parser, current_context))
}
public struct ProgramCompiler {
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
// Certain names are always in scope during compilation.
var globals = StaticVarValueScopes().enter()
globals = globals.declare(
identifier: accept.state().getName(),
withValue: (P4QualifiedType(accept.type()), P4Value(accept))
)
.declare(
identifier: reject.state.getName(),
withValue: (P4QualifiedType(reject.type()), P4Value(reject)))
return ProgramCompiler.Compile(
source, withGlobalInstances: globals, withGlobalTypes: .none, withFFIs: [])
}
public static func Compile(
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes
) -> Result<P4Lang.Program> {
return ProgramCompiler.Compile(
source, withGlobalInstances: globalInstances, withGlobalTypes: .none, withFFIs: [])
}
public static func Compile(
_ source: String, withGlobalInstances globalInstances: StaticVarValueScopes?,
withGlobalTypes globalTypes: TypeTypeScopes?, withFFIs ffis: [P4FFI] = Array()
) -> Result<P4Lang.Program> {
let maybe_parser = ConfigureP4Parser()
guard case .Ok(let p) = maybe_parser else {
return .Error(maybe_parser.error()!)
}
let result = p.parse(source)
guard let tree = result,
!tree.isError(lang: p4lang),
!tree.containsMissing(lang: p4lang)
else {
return Result.Error(Error(withMessage: "Could not compile the P4 program"))
}
var program = P4Lang.Program()
// Set up a context for parsing.
var compilation_context = CompilerContext()
// Add our FFIs
compilation_context = compilation_context.update(newFFIs: ffis)
var errors: (any Errorable)? = .none
// If the caller gave any global instances, add them here.
if let globalInstances = globalInstances {
compilation_context = compilation_context.update(newInstances: globalInstances)
}
// If the caller gave any global types, add them here.
if let globalTypes = globalTypes {
compilation_context = compilation_context.update(newTypes: globalTypes)
}
// Try to parse all top-level declarations.
result?.rootNode?.enumerateNamedChildren { (declaration_node: Node) in
let specific_declaration_node = declaration_node.child(at: 0)!
let declaration_parsers: [CompilableDeclaration.Type] = [
Declaration.self, P4Lang.Parser.self,
]
var found_parser = false
for parser in declaration_parsers {
switch parser.Compile(node: specific_declaration_node, withContext: compilation_context) {
case .Ok(.none): {}()
case .Ok(.some((_, let updated_context))):
found_parser = true
compilation_context = updated_context
break
case .Error(let e):
found_parser = true
errors =
if let errors = errors {
errors.append(error: e)
} else {
e
}
break
}
}
// If none of the declaration parsers chose to parse, that's an error, too!
if !found_parser {
print("spec: \(declaration_node)")
// Try to see if it is an instantiation!
if declaration_node.nodeType == "instantiation" {
switch Instantiation.Compile(
node: declaration_node, withContext: compilation_context)
{
case .Ok((let x, let updated_context)):
print("instantiation: \(x)")
compilation_context = updated_context
case .Error(let e):
errors =
if let errors = errors {
errors.append(error: e)
} else {
e
}
}
} else {
let no_parser_error = ErrorWithLocation(
sourceLocation: specific_declaration_node.toSourceLocation(),
withError: "Could not find parser for declaration node"
)
errors =
if let errors = errors {
errors.append(error: no_parser_error)
} else {
no_parser_error
}
}
}
}
if let errors = errors {
return .Error(errors)
}
// Any of the instances that are in the top-level scope should go into the program!
program.instances = Array(
compilation_context.instances.filter { (_, v) in
v.1 != nil
}.map { (_, v) in
v.1!
})
// Any of the types that are in the top-level scope should go into the program!
program.types = Array(
compilation_context.types.map { (_, v) in
v
})
// Any of the extern types that are in the top-level scope should go into the program!
program.externs = Array(
compilation_context.externs.map { (_, v) in
v
})
return Result.Ok(program)
}
}
}
+3 -3
View File
@@ -57,7 +57,7 @@ extension BlockStatement: CompilableStatement {
sourceLocation: node.toSourceLocation(), withError: "Malformed block statement"))) sourceLocation: node.toSourceLocation(), withError: "Malformed block statement")))
if current_node!.nodeType == "statements" { if current_node!.nodeType == "statements" {
switch Parser.Statements.Compile( switch SpecialCompilers.Statements.Compile(
node: current_node!, withContext: current_context) node: current_node!, withContext: current_context)
{ {
case .Ok(let (parsed_statements, updated_context)): case .Ok(let (parsed_statements, updated_context)):
@@ -128,7 +128,7 @@ extension ConditionalStatement: CompilableStatement {
} }
guard guard
case .Ok((let thenns, _)) = Parser.Statement.Compile( case .Ok((let thenns, _)) = Statement.Compile(
node: thens, withContext: context) node: thens, withContext: context)
else { else {
return Result.Error( return Result.Error(
@@ -140,7 +140,7 @@ extension ConditionalStatement: CompilableStatement {
let optional_elss: Result<(any EvaluatableStatement, CompilerContext)>? = let optional_elss: Result<(any EvaluatableStatement, CompilerContext)>? =
if let elss = node.child(at: 6) { if let elss = node.child(at: 6) {
.some( .some(
Parser.Statement.Compile( Statement.Compile(
node: elss, withContext: context)) node: elss, withContext: context))
} else { } else {
.none .none
+4 -6
View File
@@ -17,13 +17,9 @@
import Common import Common
public struct LocalElements { public struct LocalElements {}
} public struct LocalElement {}
public struct LocalElement {
}
public struct ParserAssignmentStatement { public struct ParserAssignmentStatement {
public let lvalue: EvaluatableLValueExpression public let lvalue: EvaluatableLValueExpression
@@ -94,6 +90,8 @@ public class _AnyParserState: ParserState {
nonisolated(unsafe) public let AnyParserState = _AnyParserState(Identifier(name: "AnyParserState")) nonisolated(unsafe) public let AnyParserState = _AnyParserState(Identifier(name: "AnyParserState"))
public struct TransitionStatement {}
public class ParserStateDirectTransition: ParserState { public class ParserStateDirectTransition: ParserState {
public let next_state: InstantiatedParserState? public let next_state: InstantiatedParserState?
+2
View File
@@ -17,6 +17,8 @@
import Common import Common
public struct Statement {}
public struct VariableDeclarationStatement { public struct VariableDeclarationStatement {
public var initializer: EvaluatableExpression public var initializer: EvaluatableExpression
public var identifier: Identifier public var identifier: Identifier
+2 -2
View File
@@ -284,8 +284,8 @@ public struct CodeGenerator: LanguageVisitor {
return .Ok(c) return .Ok(c)
} }
public func visit( public func visit(
_ parser_state: P4Lang.InstantiatedParserState, _ c: P4Lang.VisitorContext<Generated> _ parser_state: InstantiatedParserState, _ c: VisitorContext<Generated>
) -> Common.Result<P4Lang.VisitorContext<Generated>> { ) -> Common.Result<VisitorContext<Generated>> {
return .Ok(c) return .Ok(c)
} }
} }
+4 -4
View File
@@ -66,7 +66,7 @@ extension ParserStateDirectTransitionValue: EvaluatableParserState {
return false return false
} }
public func state() -> P4Lang.ParserState { public func state() -> ParserState {
return self.state return self.state
} }
} }
@@ -82,7 +82,7 @@ extension ParserStateNoTransitionValue: EvaluatableParserState {
return true return true
} }
public func state() -> P4Lang.ParserState { public func state() -> ParserState {
return self.state return self.state
} }
} }
@@ -125,7 +125,7 @@ extension ParserStateSelectTransitionValue: EvaluatableParserState {
return false return false
} }
public func state() -> P4Lang.ParserState { public func state() -> ParserState {
return self.state return self.state
} }
} }
@@ -134,7 +134,7 @@ extension ParserValue: LibraryCallable {
public typealias T = InstantiatedParserState public typealias T = InstantiatedParserState
public func call( public func call(
execution: Common.ProgramExecution, arguments: ArgumentList execution: Common.ProgramExecution, arguments: ArgumentList
) -> (P4Lang.InstantiatedParserState, Common.ProgramExecution) { ) -> (InstantiatedParserState, Common.ProgramExecution) {
var execution = execution.enter_scope() var execution = execution.enter_scope()
execution = execution.declare( execution = execution.declare(
+3 -3
View File
@@ -36,13 +36,13 @@ public struct Runtime<U, T: LibraryCallable<U>>: CustomStringConvertible {
/// Create a parser runtime from a P4 program /// Create a parser runtime from a P4 program
public static func create( public static func create(
program: P4Lang.Program program: Program
) -> Result<Runtime<InstantiatedParserState, ParserValue>> { ) -> Result<Runtime<InstantiatedParserState, ParserValue>> {
return Runtime.create(program: program, withGlobalValues: .none) return Runtime.create(program: program, withGlobalValues: .none)
} }
public static func create( public static func create(
program: P4Lang.Program, withGlobalValues initial: VarValueScopes? program: Program, withGlobalValues initial: VarValueScopes?
) -> Result<Runtime<InstantiatedParserState, ParserValue>> { ) -> Result<Runtime<InstantiatedParserState, ParserValue>> {
return switch program.starting_parser() { return switch program.starting_parser() {
case .Ok(let parser): case .Ok(let parser):
@@ -55,7 +55,7 @@ public struct Runtime<U, T: LibraryCallable<U>>: CustomStringConvertible {
} }
public static func create( public static func create(
control: P4Lang.Control, withGlobalValues initial: VarValueScopes? control: Control, withGlobalValues initial: VarValueScopes?
) -> Result<Runtime<P4TableHitMissValue, Control>> { ) -> Result<Runtime<P4TableHitMissValue, Control>> {
return .Ok( return .Ok(
P4Runtime.Runtime<P4TableHitMissValue, Control>(callable: control, withGlobalValues: initial)) P4Runtime.Runtime<P4TableHitMissValue, Control>(callable: control, withGlobalValues: initial))
+6
View File
@@ -133,3 +133,9 @@ extension ApplyStatement: EvaluatableStatement {
return (ControlFlow.Next, execution) return (ControlFlow.Next, execution)
} }
} }
extension Instantiation: EvaluatableStatement {
public func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) {
return (ControlFlow.Next, execution)
}
}
+8 -8
View File
@@ -56,7 +56,7 @@ import TreeSitterP4
P4Value(P4IntValue(withValue: 3)), P4Value(P4IntValue(withValue: 3)),
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -87,7 +87,7 @@ import TreeSitterP4
withMessage: withMessage:
"{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type" "{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type"
), ),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
) )
} }
@@ -119,7 +119,7 @@ import TreeSitterP4
P4Value(P4IntValue(withValue: 3)), P4Value(P4IntValue(withValue: 3)),
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -155,7 +155,7 @@ import TreeSitterP4
P4Value(P4IntValue(withValue: 3)), P4Value(P4IntValue(withValue: 3)),
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -190,7 +190,7 @@ import TreeSitterP4
P4Value(P4IntValue(withValue: 3)), P4Value(P4IntValue(withValue: 3)),
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -237,7 +237,7 @@ import TreeSitterP4
withValue: [nested]))) withValue: [nested])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -276,7 +276,7 @@ import TreeSitterP4
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -325,7 +325,7 @@ import TreeSitterP4
withValue: [nested]))) withValue: [nested])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -41,7 +41,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -60,7 +60,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -79,7 +79,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -98,7 +98,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -117,7 +117,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -136,7 +136,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -155,7 +155,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -174,7 +174,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -193,7 +193,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -212,7 +212,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
+14 -14
View File
@@ -41,7 +41,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -60,7 +60,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -79,7 +79,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -98,7 +98,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -117,7 +117,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -136,7 +136,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -155,7 +155,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -174,7 +174,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -193,7 +193,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -212,7 +212,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -231,7 +231,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -250,7 +250,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -269,7 +269,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -288,7 +288,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -41,7 +41,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -60,7 +60,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -79,7 +79,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -98,7 +98,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -117,7 +117,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -136,7 +136,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -155,7 +155,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -174,7 +174,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -193,7 +193,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -212,7 +212,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -231,7 +231,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -250,7 +250,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -269,7 +269,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -288,7 +288,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -310,7 +310,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -335,7 +335,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed" "{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
@Test func test_simple_parser_binary_operator_add_non_integer2() async throws { @Test func test_simple_parser_binary_operator_add_non_integer2() async throws {
@@ -356,7 +356,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same" "{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
// Subtract Integers // Subtract Integers
@@ -373,7 +373,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -398,7 +398,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed" "{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
@Test func test_simple_parser_binary_operator_subtract_non_integer2() async throws { @Test func test_simple_parser_binary_operator_subtract_non_integer2() async throws {
@@ -419,7 +419,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same" "{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
// Multiply Integers // Multiply Integers
@@ -436,7 +436,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -461,7 +461,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed" "{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
@Test func test_simple_parser_binary_operator_multiply_non_integer2() async throws { @Test func test_simple_parser_binary_operator_multiply_non_integer2() async throws {
@@ -482,7 +482,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same" "{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
// Divide Integers // Divide Integers
@@ -499,7 +499,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -524,7 +524,7 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed" "{72, 12}: Could not parse transition select expression selector expression: Mathematical operation on operands with non-int type is not allowed"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
@Test func test_simple_parser_binary_operator_divide_non_integer2() async throws { @Test func test_simple_parser_binary_operator_divide_non_integer2() async throws {
@@ -545,5 +545,5 @@ import TreeSitterP4
withMessage: withMessage:
"{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same" "{72, 9}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
), ),
Program.Compile(simple))) SpecialCompilers.ProgramCompiler.Compile(simple)))
} }
@@ -41,7 +41,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -60,7 +60,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -79,7 +79,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -98,7 +98,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -117,7 +117,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -136,7 +136,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -155,7 +155,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -174,7 +174,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -193,7 +193,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -212,7 +212,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -49,7 +49,7 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -81,7 +81,7 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -114,7 +114,7 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -150,7 +150,7 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -186,7 +186,7 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
+2 -2
View File
@@ -41,7 +41,7 @@ func shrink(_ from: String) -> String {
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let cg = try #UseOkResult(CodeGenerator().codeGen(program)) let cg = try #UseOkResult(CodeGenerator().codeGen(program))
let expected = shrink( let expected = shrink(
@@ -73,7 +73,7 @@ func shrink(_ from: String) -> String {
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let cg = try #UseOkResult(CodeGenerator().codeGen(program)) let cg = try #UseOkResult(CodeGenerator().codeGen(program))
/// TODO: Fix this test. /// TODO: Fix this test.
+2 -2
View File
@@ -45,7 +45,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -73,7 +73,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
+14 -14
View File
@@ -47,7 +47,7 @@ import TreeSitterP4
default: false default: false
} }
} }
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(x).count == 1) #expect(program.TypesWithTypes(x).count == 1)
} }
@@ -85,7 +85,7 @@ import TreeSitterP4
default: false default: false
} }
} }
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(filter).count == 2) #expect(program.TypesWithTypes(filter).count == 2)
} }
@@ -112,7 +112,7 @@ import TreeSitterP4
default: false default: false
} }
} }
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(x).count == 1) #expect(program.TypesWithTypes(x).count == 1)
} }
@@ -139,7 +139,7 @@ import TreeSitterP4
withMessage: withMessage:
"{54, 63}: Error(s) parsing property list: {91, 26}: Error(s) parsing table actions: Cannot find b in lexical scope." "{54, 63}: Error(s) parsing property list: {91, 26}: Error(s) parsing table actions: Cannot find b in lexical scope."
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -167,7 +167,7 @@ import TreeSitterP4
withMessage: withMessage:
"{54, 72}: Error(s) parsing property list: {91, 35}: Error(s) parsing table actions: Cannot find b in lexical scope." "{54, 72}: Error(s) parsing property list: {91, 35}: Error(s) parsing table actions: Cannot find b in lexical scope."
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -195,7 +195,7 @@ import TreeSitterP4
withMessage: withMessage:
"{64, 63}: Error(s) parsing property list: {101, 26}: Error(s) parsing table actions: {101, 26}: a does not name an action" "{64, 63}: Error(s) parsing property list: {101, 26}: Error(s) parsing table actions: {101, 26}: a does not name an action"
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -214,7 +214,7 @@ import TreeSitterP4
} }
}; };
""" """
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration))) #expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_simple_control_declaration_with_multiple_tables() async throws { @Test func test_simple_control_declaration_with_multiple_tables() async throws {
@@ -243,7 +243,7 @@ import TreeSitterP4
Error( Error(
withMessage: "{0, 215}: More than one table in control declaration" withMessage: "{0, 215}: More than one table in control declaration"
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -263,7 +263,7 @@ import TreeSitterP4
} }
}; };
""" """
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration))) #expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_simple_control_declaration_with_action_using_parameter_wrong_type() async throws { @Test func test_simple_control_declaration_with_action_using_parameter_wrong_type() async throws {
@@ -288,7 +288,7 @@ import TreeSitterP4
withMessage: withMessage:
"{57, 10}: Failed to parse a statement element: {57, 1}: Cannot assign value with type Boolean to identifier z with type Int (width: Infinite)" "{57, 10}: Failed to parse a statement element: {57, 1}: Cannot assign value with type Boolean to identifier z with type Int (width: Infinite)"
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -320,7 +320,7 @@ import TreeSitterP4
withMessage: withMessage:
"Could not compile the P4 program" "Could not compile the P4 program"
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -347,7 +347,7 @@ import TreeSitterP4
default: false default: false
} }
} }
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(program.TypesWithTypes(x).count == 1) #expect(program.TypesWithTypes(x).count == 1)
} }
@@ -372,7 +372,7 @@ import TreeSitterP4
ErrorWithLocation( ErrorWithLocation(
sourceLocation: SourceLocation(41, 23), sourceLocation: SourceLocation(41, 23),
withError: "All parameters with direction must precede directionless parameters"), withError: "All parameters with direction must precede directionless parameters"),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -397,6 +397,6 @@ import TreeSitterP4
ErrorWithLocation( ErrorWithLocation(
sourceLocation: SourceLocation(41, 38), sourceLocation: SourceLocation(41, 38),
withError: "All parameters with direction must precede directionless parameters"), withError: "All parameters with direction must precede directionless parameters"),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
+7 -7
View File
@@ -50,7 +50,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -125,7 +125,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -200,7 +200,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -274,7 +274,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -349,7 +349,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -429,7 +429,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
@@ -557,7 +557,7 @@ import TreeSitterP4
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
// Pull the control out of the compiled program. // Pull the control out of the compiled program.
let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in let controls = program.TypesWithTypes { (tipe: P4Type) -> Bool in
+9 -9
View File
@@ -46,7 +46,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept) #expect(state_result == P4Lang.accept)
@@ -71,7 +71,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept) #expect(state_result == P4Lang.accept)
@@ -94,7 +94,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept) #expect(state_result == P4Lang.accept)
@@ -117,7 +117,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept) #expect(state_result == P4Lang.accept)
@@ -141,7 +141,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.reject) #expect(state_result == P4Lang.reject)
@@ -153,7 +153,7 @@ import TreeSitterP4
int count; int count;
}; };
""" """
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration))) #expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@@ -163,7 +163,7 @@ import TreeSitterP4
x = true; x = true;
}; };
""" """
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration))) #expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_function_declaration_with_parameters_and_direction() async throws { @Test func test_function_declaration_with_parameters_and_direction() async throws {
@@ -178,7 +178,7 @@ import TreeSitterP4
withMessage: withMessage:
"{63, 9}: Failed to parse a statement element: {63, 1}: Cannot assign value with type Boolean to identifier x that is in parameter" "{63, 9}: Failed to parse a statement element: {63, 1}: Cannot assign value with type Boolean to identifier x that is in parameter"
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
@@ -198,7 +198,7 @@ import TreeSitterP4
withMessage: withMessage:
"{120, 15}: Failed to parse a statement element: {120, 7}: Cannot assign to field yesno of x that is in parameter" "{120, 15}: Failed to parse a statement element: {120, 7}: Cannot assign to field yesno of x that is in parameter"
), ),
Program.Compile(simple_parser_declaration)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
) )
} }
+3 -3
View File
@@ -40,7 +40,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -60,7 +60,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -80,7 +80,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -44,7 +44,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -73,7 +73,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -101,7 +101,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -128,7 +128,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -154,7 +154,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -180,7 +180,7 @@ import TreeSitterP4
}; };
""" """
let error = try #UseErrorResult(Program.Compile(simple_parser_declaration)) let error = try #UseErrorResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(error.msg().contains("{29, 12}: Type of expression in return statement (Boolean) is not compatible with function return type (Int (width: Infinite))")) #expect(error.msg().contains("{29, 12}: Type of expression in return statement (Boolean) is not compatible with function return type (Int (width: Infinite))"))
} }
@@ -39,7 +39,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -61,7 +61,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -83,7 +83,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -105,7 +105,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -134,7 +134,7 @@ import TreeSitterP4
withMessage: withMessage:
"Error(s) parsing select cases: {81, 4}: Key expression of type Boolean is not compatible with selector type Int (width: Infinite)" "Error(s) parsing select cases: {81, 4}: Key expression of type Boolean is not compatible with selector type Int (width: Infinite)"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_select_expression_selection_order() async throws { @Test func test_select_expression_selection_order() async throws {
@@ -149,7 +149,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -171,7 +171,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([ let args = ArgumentList([
@@ -192,7 +192,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([ let args = ArgumentList([
+2 -2
View File
@@ -85,7 +85,7 @@ public struct Return6: P4FFI {
let externally = Return5() let externally = Return5()
let program = try! #UseOkResult( let program = try! #UseOkResult(
Program.Compile( SpecialCompilers.ProgramCompiler.Compile(
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none, simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none,
withFFIs: [externally])) withFFIs: [externally]))
@@ -113,7 +113,7 @@ public struct Return6: P4FFI {
let externally = Return6() let externally = Return6()
let program = try! #UseOkResult( let program = try! #UseOkResult(
Program.Compile( SpecialCompilers.ProgramCompiler.Compile(
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none, simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: .none,
withFFIs: [externally])) withFFIs: [externally]))
+2 -2
View File
@@ -46,7 +46,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
var statements_executed: [String] = Array() var statements_executed: [String] = Array()
@@ -87,7 +87,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
var expressions_evaluated: [String] = Array() var expressions_evaluated: [String] = Array()
+20 -7
View File
@@ -38,7 +38,7 @@ import P4Lang
#expect( #expect(
#RequireErrorResult( #RequireErrorResult(
Error(withMessage: "Could not compile the P4 program"), Error(withMessage: "Could not compile the P4 program"),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_simple_compilation_with_statement() async throws { @Test func test_simple_compilation_with_statement() async throws {
@@ -51,7 +51,7 @@ import P4Lang
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(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) #expect(parser.states.count() == 1)
@@ -73,7 +73,7 @@ import P4Lang
}; };
""" """
let compilation_error = try #UseErrorResult(Program.Compile(simple_parser_declaration)) let compilation_error = try #UseErrorResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect(compilation_error.msg().contains("asde")) #expect(compilation_error.msg().contains("asde"))
#expect(compilation_error.msg().contains("asdf")) #expect(compilation_error.msg().contains("asdf"))
@@ -110,7 +110,7 @@ import P4Lang
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let parser = try! #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try! #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let parameters = parser.parameters let parameters = parser.parameters
@@ -130,7 +130,7 @@ import P4Lang
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let parser = try! #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try! #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let parameters = parser.parameters let parameters = parser.parameters
@@ -153,7 +153,7 @@ import P4Lang
}; };
""" """
let program = try! #UseOkResult(Program.Compile(simple)) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple))
let parser = try! #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try! #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let parameters = parser.parameters let parameters = parser.parameters
@@ -180,5 +180,18 @@ import P4Lang
}; };
""" """
#expect(#RequireOkResult(Program.Compile(simple))) #expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple)))
}
@Test func test_simple_compiler_with_instantiation() async throws {
let simple_instantiation_program = """
parser MainParser() {
state start {
transition accept;
}
};
MainParser() mp;
"""
#expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_instantiation_program)))
} }
+7 -7
View File
@@ -37,7 +37,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -55,7 +55,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
// We should be in the accept state. // We should be in the accept state.
@@ -72,7 +72,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
#expect( #expect(
@@ -92,7 +92,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([ let args = ArgumentList([
@@ -114,7 +114,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([ let args = ArgumentList([
@@ -138,7 +138,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([ let args = ArgumentList([
@@ -162,7 +162,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let args = ArgumentList([Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 0)]) let args = ArgumentList([Argument(P4Value(P4BooleanValue(withValue: true)), atIndex: 0)])
+5 -5
View File
@@ -42,7 +42,7 @@ import TreeSitterP4
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -68,7 +68,7 @@ import TreeSitterP4
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -96,7 +96,7 @@ import TreeSitterP4
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -120,7 +120,7 @@ import TreeSitterP4
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -151,7 +151,7 @@ import TreeSitterP4
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
+13 -13
View File
@@ -60,7 +60,7 @@ import TreeSitterP4
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -91,7 +91,7 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type) test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile( SpecialCompilers.ProgramCompiler.Compile(
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types)) simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
@@ -124,7 +124,7 @@ import TreeSitterP4
identifier: Identifier(name: "Testing"), withValue: struct_type) identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
@@ -165,7 +165,7 @@ import TreeSitterP4
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -205,7 +205,7 @@ import TreeSitterP4
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -245,7 +245,7 @@ import TreeSitterP4
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -298,7 +298,7 @@ import TreeSitterP4
])) ]))
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -340,7 +340,7 @@ import TreeSitterP4
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -372,7 +372,7 @@ import TreeSitterP4
withMessage: withMessage:
"{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int (width: Infinite) to field yesno of type Boolean" "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int (width: Infinite) to field yesno of type Boolean"
), ),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
) )
} }
@@ -422,7 +422,7 @@ import TreeSitterP4
])) ]))
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -477,7 +477,7 @@ import TreeSitterP4
])) ]))
]))) ])))
let program = try #UseOkResult( let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create( P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values)) program: program, withGlobalValues: test_values))
@@ -520,7 +520,7 @@ import TreeSitterP4
withMessage: withMessage:
"{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int (width: Infinite)" "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int (width: Infinite)"
), ),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
) )
} }
@@ -551,6 +551,6 @@ import TreeSitterP4
withMessage: withMessage:
"{68, 21}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same" "{68, 21}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
), ),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
) )
} }
@@ -34,7 +34,7 @@ import TreeSitterP4
let file = FilePath.init(stringLiteral: "simple.p4") let file = FilePath.init(stringLiteral: "simple.p4")
let source = try! (#UseOkResult(prep.preprocess(file))) let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource())) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser"))))) #expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == file) #expect(source.getLocations().getPath() == file)
} }
@@ -45,7 +45,7 @@ import TreeSitterP4
let file = FilePath.init(stringLiteral: "simple.p4") let file = FilePath.init(stringLiteral: "simple.p4")
let source = try! (#UseOkResult(prep.preprocess(file))) let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource())) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser"))))) #expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == file) #expect(source.getLocations().getPath() == file)
} }
@@ -58,7 +58,7 @@ import TreeSitterP4
#expect(#RequireOkResult(prep.preprocess(file))) #expect(#RequireOkResult(prep.preprocess(file)))
let source = try! (#UseOkResult(prep.preprocess(file))) let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource())) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser"))))) #expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == file) #expect(source.getLocations().getPath() == file)
} }
@@ -71,7 +71,7 @@ import TreeSitterP4
#expect(#RequireOkResult(prep.preprocess(file))) #expect(#RequireOkResult(prep.preprocess(file)))
let source = try! (#UseOkResult(prep.preprocess(file))) let source = try! (#UseOkResult(prep.preprocess(file)))
let program = try! #UseOkResult(Program.Compile(source.getSource())) let program = try! #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(source.getSource()))
#expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser"))))) #expect(#RequireOkResult((program.find_parser(withName: Identifier(name: "main_parser")))))
#expect(source.getLocations().getPath() == file) #expect(source.getLocations().getPath() == file)
} }
+3 -3
View File
@@ -39,7 +39,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -61,7 +61,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser"))) let parser = try #UseOkResult(program.find_parser(withName: Identifier(name: "main_parser")))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -81,7 +81,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
#expect( #expect(
+2 -2
View File
@@ -38,7 +38,7 @@ import TreeSitterP4
#expect( #expect(
#RequireErrorResult( #RequireErrorResult(
Error(withMessage: "{0, 8}: Annotations in parser type are not yet handled."), Error(withMessage: "{0, 8}: Annotations in parser type are not yet handled."),
Program.Compile(simple_annotated_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_annotated_parser_declaration)))
} }
@Test func test_unsupported_annotations_state() async throws { @Test func test_unsupported_annotations_state() async throws {
@@ -52,5 +52,5 @@ import TreeSitterP4
#expect( #expect(
#RequireErrorResult( #RequireErrorResult(
Error(withMessage: "{26, 8}: Annotations in parser state are not yet handled."), Error(withMessage: "{26, 8}: Annotations in parser state are not yet handled."),
Program.Compile(simple_annotated_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_annotated_parser_declaration)))
} }
+16 -16
View File
@@ -38,7 +38,7 @@ import TreeSitterP4
}; };
""" """
let err = Program.Compile(simple_parser_declaration) let err = SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)
guard case Result.Error(let e) = err else { guard case Result.Error(let e) = err else {
assert(false, "Expected an error, but had success") assert(false, "Expected an error, but had success")
} }
@@ -66,7 +66,7 @@ import TreeSitterP4
withMessage: withMessage:
"{112, 16}: Failed to parse a statement element: {112, 8}: Cannot assign value with type Boolean to identifier where_to with type String" "{112, 16}: Failed to parse a statement element: {112, 8}: Cannot assign value with type Boolean to identifier where_to with type String"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_invalid_type_in_assignment2() async throws { @Test func test_invalid_type_in_assignment2() async throws {
@@ -87,7 +87,7 @@ import TreeSitterP4
withMessage: withMessage:
"{114, 22}: Failed to parse a statement element: {114, 8}: Cannot assign value with type String to identifier where_to with type Boolean" "{114, 22}: Failed to parse a statement element: {114, 8}: Cannot assign value with type String to identifier where_to with type Boolean"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_invalid_type_in_declaration() async throws { @Test func test_invalid_type_in_declaration() async throws {
@@ -107,7 +107,7 @@ import TreeSitterP4
withMessage: withMessage:
"{86, 27}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type String" "{86, 27}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type String"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_invalid_type_in_declaration2() async throws { @Test func test_invalid_type_in_declaration2() async throws {
@@ -127,7 +127,7 @@ import TreeSitterP4
withMessage: withMessage:
"{77, 29}: Failed to parse a statement element: Cannot initialize where_from (with type String) from expression with type Boolean" "{77, 29}: Failed to parse a statement element: Cannot initialize where_from (with type String) from expression with type Boolean"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_invalid_type_in_declaration3() async throws { @Test func test_invalid_type_in_declaration3() async throws {
@@ -141,7 +141,7 @@ import TreeSitterP4
}; };
""" """
let error = try! #UseErrorResult(Program.Compile(simple_parser_declaration)) let error = try! #UseErrorResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
#expect( #expect(
error.msg().contains( error.msg().contains(
@@ -160,7 +160,7 @@ import TreeSitterP4
}; };
""" """
#expect(#RequireOkResult(Program.Compile(simple_parser_declaration))) #expect(#RequireOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@Test func test_expression_in_declaration_initializer_specific_width_int_type() async throws { @Test func test_expression_in_declaration_initializer_specific_width_int_type() async throws {
@@ -177,7 +177,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -202,7 +202,7 @@ import TreeSitterP4
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -225,7 +225,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -248,7 +248,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -271,7 +271,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -294,7 +294,7 @@ import TreeSitterP4
} }
}; };
""" """
let program = try #UseOkResult(Program.Compile(simple_parser_declaration)) let program = try #UseOkResult(SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration))
let runtime = try #UseOkResult( let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program)) P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run()) let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -324,7 +324,7 @@ import TreeSitterP4
withMessage: withMessage:
"{49, 35}: Failed to parse a statement element: Types of values used with binary expression are not the same" "{49, 35}: Failed to parse a statement element: Types of values used with binary expression are not the same"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }
@@ -351,7 +351,7 @@ import TreeSitterP4
withMessage: withMessage:
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type Int (width: Infinite)" "{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from expression with type Int (width: Infinite)"
), ),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration, withGlobalInstances: test_declarations)))
} }
@Test func test_simple_compiler_parser_parameters_invalid_types() async throws { @Test func test_simple_compiler_parser_parameters_invalid_types() async throws {
@@ -369,5 +369,5 @@ import TreeSitterP4
withMessage: withMessage:
"{85, 9}: Failed to parse a statement element: {85, 4}: Cannot assign value with type Int (width: Infinite) to identifier pmtr with type Boolean" "{85, 9}: Failed to parse a statement element: {85, 4}: Cannot assign value with type Int (width: Infinite) to identifier pmtr with type Boolean"
), ),
Program.Compile(simple_parser_declaration))) SpecialCompilers.ProgramCompiler.Compile(simple_parser_declaration)))
} }