@@ -665,7 +665,8 @@ extension Control: CompilableDeclaration {
|
||||
// TODO: Make this error message better.
|
||||
// IDEA: Add a "compilation context" for the error message into the `CompilationContext`
|
||||
// that can be retrieved to make the error messages nicer.
|
||||
return .Error(ErrorOnNode(node: node, withError: "More than one table in control declaration"))
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "More than one table in control declaration"))
|
||||
}
|
||||
|
||||
let declared_control =
|
||||
@@ -760,10 +761,13 @@ extension Action: Compilable {
|
||||
}
|
||||
|
||||
extension TableKeyEntry: Compilable {
|
||||
public typealias T = TableKeyEntry
|
||||
public static func Compile(node: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> {
|
||||
public typealias T = TableKeyEntry
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> {
|
||||
|
||||
#RequireNodeType<Node, (P4Type, CompilerContext)>(node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
|
||||
#RequireNodeType<Node, (P4Type, CompilerContext)>(
|
||||
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
@@ -777,7 +781,8 @@ extension TableKeyEntry: Compilable {
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
let maybe_keyset_expression = KeysetExpression.compile(node: currentChild!, withContext: current_context)
|
||||
let maybe_keyset_expression = KeysetExpression.compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
guard case .Ok(let keyset_expression) = maybe_keyset_expression else {
|
||||
return Result.Error(maybe_keyset_expression.error()!)
|
||||
}
|
||||
@@ -791,7 +796,8 @@ extension TableKeyEntry: Compilable {
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
let maybe_match_type = TableKeyMatchType.Compile(node: currentChild!, withContext: current_context)
|
||||
let maybe_match_type = TableKeyMatchType.Compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
guard case .Ok((let match_type, _)) = maybe_match_type else {
|
||||
return .Error(maybe_match_type.error()!)
|
||||
}
|
||||
@@ -801,23 +807,29 @@ extension TableKeyEntry: Compilable {
|
||||
}
|
||||
|
||||
extension TableKeyMatchType: Compilable {
|
||||
public typealias T = TableKeyMatchType
|
||||
public static func Compile(node: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Common.Result<(P4Lang.TableKeyMatchType, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(node: node, type: "table_key_match_type", nice_type_name: "Table Key Match Type")
|
||||
public typealias T = TableKeyMatchType
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(P4Lang.TableKeyMatchType, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
|
||||
node: node, type: "table_key_match_type", nice_type_name: "Table Key Match Type")
|
||||
|
||||
if node.text! == "exact" {
|
||||
return .Ok((TableKeyMatchType.Exact, context))
|
||||
}
|
||||
return .Error(ErrorOnNode(node: node, withError: "\(node.text!) is not a valid match type)"))
|
||||
if node.text! == "exact" {
|
||||
return .Ok((TableKeyMatchType.Exact, context))
|
||||
}
|
||||
return .Error(ErrorOnNode(node: node, withError: "\(node.text!) is not a valid match type)"))
|
||||
}
|
||||
}
|
||||
|
||||
extension TableKeys: Compilable {
|
||||
public typealias T = TableKeys
|
||||
public static func Compile(node: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Common.Result<(P4Lang.TableKeys, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(node: node, type: "table_keys", nice_type_name: "Table Keys")
|
||||
public typealias T = TableKeys
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(P4Lang.TableKeys, CompilerContext)> {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
|
||||
node: node, type: "table_keys", nice_type_name: "Table Keys")
|
||||
|
||||
// Skip the
|
||||
// Skip the
|
||||
// keys = {
|
||||
// 0 1 2
|
||||
let currentChildIdx = 3
|
||||
@@ -827,14 +839,15 @@ extension TableKeys: Compilable {
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing table keys declaration component in control declaration"))
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing table keys declaration component in control declaration"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
var entries: [TableKeyEntry] = Array()
|
||||
var errors: [Error] = Array()
|
||||
|
||||
currentChild!.enumerateNamedChildren() { entry in
|
||||
currentChild!.enumerateNamedChildren { entry in
|
||||
switch TableKeyEntry.Compile(node: currentChild!, withContext: current_context) {
|
||||
case .Ok((let keyset_expression, let updated_context)):
|
||||
entries.append(keyset_expression)
|
||||
@@ -857,7 +870,7 @@ extension TableKeys: Compilable {
|
||||
}
|
||||
|
||||
extension TablePropertyList: Compilable {
|
||||
public typealias T = TablePropertyList
|
||||
public typealias T = TablePropertyList
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(P4Lang.TablePropertyList, CompilerContext)> {
|
||||
@@ -871,7 +884,7 @@ extension TablePropertyList: Compilable {
|
||||
var _: [Action] = Array() // Actions are not yet supported
|
||||
var errors: [Error] = Array()
|
||||
|
||||
node.enumerateNamedChildren() { child in
|
||||
node.enumerateNamedChildren { child in
|
||||
if child.nodeType == "table_keys" {
|
||||
switch TableKeys.Compile(node: child, withContext: current_context) {
|
||||
case .Ok((let table_key, let updated_context)):
|
||||
@@ -911,7 +924,7 @@ extension TablePropertyList: Compilable {
|
||||
}
|
||||
|
||||
extension Table: Compilable {
|
||||
public typealias T = Table
|
||||
public typealias T = Table
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(P4Lang.Table, CompilerContext)> {
|
||||
@@ -955,6 +968,7 @@ extension Table: Compilable {
|
||||
return Result.Error(maybe_table_property_list.error()!)
|
||||
}
|
||||
|
||||
return .Ok((Table(withName: table_name, withPropertyList: table_property_list), current_context))
|
||||
return .Ok(
|
||||
(Table(withName: table_name, withPropertyList: table_property_list), current_context))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@ import Common
|
||||
|
||||
public struct Action: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return "Action: " + "\(self.name) with parameters \(self.params) and body \(String(describing: self.body))"
|
||||
return "Action: "
|
||||
+ "\(self.name) with parameters \(self.params) and body \(String(describing: self.body))"
|
||||
}
|
||||
|
||||
public var body: EvaluatableStatement?
|
||||
@@ -44,9 +45,10 @@ public struct Actions: CustomStringConvertible {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Actions: " + actions.map() {action in
|
||||
return "\(action)"
|
||||
}.joined(separator: ";")
|
||||
return "Actions: "
|
||||
+ actions.map { action in
|
||||
return "\(action)"
|
||||
}.joined(separator: ";")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ public enum TableKeyMatchType {
|
||||
case Exact
|
||||
}
|
||||
|
||||
public struct TableKeyEntry: CustomStringConvertible {
|
||||
public struct TableKeyEntry: CustomStringConvertible {
|
||||
let key: KeysetExpression
|
||||
let match_type: TableKeyMatchType
|
||||
|
||||
@@ -68,7 +70,7 @@ public struct TableKeyEntry: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
public struct TableKeys: CustomStringConvertible {
|
||||
public struct TableKeys: CustomStringConvertible {
|
||||
let entries: [TableKeyEntry]
|
||||
|
||||
public init(withEntries entries: [TableKeyEntry]) {
|
||||
@@ -79,9 +81,10 @@ public struct TableKeys: CustomStringConvertible {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Table Keys: " + self.entries.map() { entry in
|
||||
return "\(entry)"
|
||||
}.joined(separator: ";")
|
||||
return "Table Keys: "
|
||||
+ self.entries.map { entry in
|
||||
return "\(entry)"
|
||||
}.joined(separator: ";")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +93,7 @@ public struct TableActions {
|
||||
public init() {}
|
||||
}
|
||||
|
||||
public struct TablePropertyList: CustomStringConvertible {
|
||||
public struct TablePropertyList: CustomStringConvertible {
|
||||
let actions: TableActions
|
||||
let keys: TableKeys
|
||||
public init(withActions actions: TableActions, withKeys keys: TableKeys) {
|
||||
@@ -103,7 +106,7 @@ public struct TablePropertyList: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
public struct Table: CustomStringConvertible {
|
||||
public struct Table: CustomStringConvertible {
|
||||
let properties: TablePropertyList
|
||||
let name: Identifier
|
||||
|
||||
@@ -180,18 +183,17 @@ public struct Control: P4Type, P4Value, Equatable, CustomStringConvertible {
|
||||
let _name: Identifier
|
||||
|
||||
public var parameters: ParameterList {
|
||||
get {
|
||||
_parameters
|
||||
}
|
||||
_parameters
|
||||
}
|
||||
|
||||
public var name: Identifier {
|
||||
get {
|
||||
_name
|
||||
}
|
||||
_name
|
||||
}
|
||||
|
||||
public init(named: Identifier, withParameters parameters: ParameterList, withTable table: Table, withActions actions: Actions) {
|
||||
public init(
|
||||
named: Identifier, withParameters parameters: ParameterList, withTable table: Table,
|
||||
withActions actions: Actions
|
||||
) {
|
||||
self._name = named
|
||||
self._parameters = parameters
|
||||
self.actions = actions
|
||||
|
||||
@@ -34,19 +34,19 @@ public struct Program {
|
||||
}
|
||||
|
||||
/// Retrieve global instances in the compiled P4 program.
|
||||
///
|
||||
///
|
||||
/// Use the given filter to select which of the global instances
|
||||
/// from the compiled P4 program to retrieve.
|
||||
///
|
||||
///
|
||||
/// If the compiled P4 program (from the source in the
|
||||
/// string `p4_program_with_control_decl`) has two Control
|
||||
/// instances and you only want to select the one named simple,
|
||||
/// you could use a filter like
|
||||
///
|
||||
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
|
||||
///
|
||||
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
|
||||
///
|
||||
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Type] {
|
||||
return self.instances.filter() { instance in
|
||||
return self.instances.filter { instance in
|
||||
filter(instance)
|
||||
}
|
||||
}
|
||||
@@ -57,19 +57,19 @@ public struct Program {
|
||||
}
|
||||
|
||||
/// Retrieve global types declared in the compiled P4 program.
|
||||
///
|
||||
///
|
||||
/// Use the given filter to select which of the global types
|
||||
/// declared in the compiled P4 program to retrieve.
|
||||
///
|
||||
///
|
||||
/// If the compiled P4 program (from the source in the
|
||||
/// string `p4_program_with_struct_decl`) has two structs declared and
|
||||
/// you only want to select the one named `agg`, you could
|
||||
/// use a filter like
|
||||
///
|
||||
/// @Snippet(path: "use-program-typeswithtypes", slice: "include")
|
||||
///
|
||||
/// @Snippet(path: "use-program-typeswithtypes", slice: "include")
|
||||
///
|
||||
public func TypesWithTypes(_ filter: TypeFilter) -> [P4Type] {
|
||||
return self.types.filter() { instance in
|
||||
return self.types.filter { instance in
|
||||
filter(instance)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user