@@ -72,7 +72,6 @@ public class Variable: TypedIdentifier {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// The type for a P4 struct
|
||||
public struct P4Struct: P4Type {
|
||||
|
||||
@@ -247,7 +246,6 @@ public class P4StringValue: P4Value {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class Packet {
|
||||
public init() {}
|
||||
}
|
||||
|
||||
@@ -141,7 +141,9 @@ extension Result: CustomStringConvertible {
|
||||
#externalMacro(module: "Macros", type: "UseOkResult")
|
||||
@freestanding(expression) public macro UseErrorResult<T>(_: Result<T>) -> Error =
|
||||
#externalMacro(module: "Macros", type: "UseErrorResult")
|
||||
@freestanding(codeItem) public macro RequireNodeType<N, T>(node: N, type: String, nice_type_name: String) =
|
||||
@freestanding(codeItem) public macro RequireNodeType<N, T>(
|
||||
node: N, type: String, nice_type_name: String
|
||||
) =
|
||||
#externalMacro(module: "Macros", type: "RequireNodeType")
|
||||
@freestanding(codeItem) public macro RequireNodesType<N, T>(
|
||||
nodes: N, type: [String], nice_type_names: [String]
|
||||
|
||||
@@ -184,8 +184,9 @@ extension SelectExpression: CompilableExpression {
|
||||
var kses: [KeysetExpression] = Array()
|
||||
var kses_errors: [Error] = Array()
|
||||
|
||||
select_body_node.enumerateNamedChildren() { current_node in
|
||||
let maybe_parsed_kse = KeysetExpression.compile(node: current_node, inTree: tree, withScopes: scopes)
|
||||
select_body_node.enumerateNamedChildren { current_node in
|
||||
let maybe_parsed_kse = KeysetExpression.compile(
|
||||
node: current_node, inTree: tree, withScopes: scopes)
|
||||
if case .Ok(let parsed_kse) = maybe_parsed_kse {
|
||||
kses.append(parsed_kse as! KeysetExpression)
|
||||
} else {
|
||||
@@ -244,5 +245,4 @@ extension KeysetExpression: CompilableExpression {
|
||||
withKey: keysetexpression, withNextState: targetstate)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -147,15 +147,22 @@ public struct Parser {
|
||||
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
|
||||
) -> Result<(ParserTransitionStatement, LexicalScopes)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
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(ErrorOnNode(node: node, withError: "Could not find transition select expression"))
|
||||
tse_node.nodeType! == "transitionSelectionExpression"
|
||||
else {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Could not find transition select expression"))
|
||||
}
|
||||
|
||||
guard let next_node = tse_node.child(at: 0) else {
|
||||
return .Error(ErrorOnNode(node: node, withError: "Could not find the next token in a transition selection expression"))
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: node,
|
||||
withError: "Could not find the next token in a transition selection expression"))
|
||||
}
|
||||
|
||||
// If the next node is an identifier, we have the simple form ...
|
||||
@@ -179,7 +186,8 @@ public struct Parser {
|
||||
switch SelectExpression.compile(node: next_node, inTree: tree, withScopes: scopes)
|
||||
{
|
||||
case .Ok(let tse):
|
||||
.Ok((ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes))
|
||||
.Ok(
|
||||
(ParserTransitionStatement(withTransitionExpression: tse! as! SelectExpression), scopes))
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,8 @@ extension BlockStatement: CompilableStatement {
|
||||
public static func Compile(
|
||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "blockStatement", nice_type_name: "block statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
node: node, type: "blockStatement", nice_type_name: "block statement")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
@@ -91,7 +92,8 @@ extension ConditionalStatement: CompilableStatement {
|
||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "conditionalStatement", nice_type_name: "conditional statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
node: node, type: "conditionalStatement", nice_type_name: "conditional statement")
|
||||
|
||||
let maybe_condition_expression = node.child(at: 2)
|
||||
guard let condition_expression = maybe_condition_expression,
|
||||
@@ -158,7 +160,8 @@ extension VariableDeclarationStatement: CompilableStatement {
|
||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
node: node, type: "variableDeclaration", nice_type_name: "variable declaration statement")
|
||||
|
||||
let maybe_typeref = node.child(at: 0)
|
||||
guard let typeref = maybe_typeref,
|
||||
@@ -235,7 +238,8 @@ extension ExpressionStatement: CompilableStatement {
|
||||
public static func Compile(
|
||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "expressionStatement", nice_type_name: "expression statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
node: node, type: "expressionStatement", nice_type_name: "expression statement")
|
||||
|
||||
let _ = node.child(at: 0)
|
||||
|
||||
|
||||
@@ -46,10 +46,8 @@ public struct ParserStateDirectTransition: ParserStateInstance {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
get {
|
||||
return "Instance of \(currrent_state)"
|
||||
}
|
||||
}
|
||||
|
||||
public let currrent_state: ParserState
|
||||
public let next_state_identifier: Identifier
|
||||
@@ -99,10 +97,8 @@ public struct ParserStateNoTransition: ParserStateInstance {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
get {
|
||||
return "Instance of \(currrent_state)"
|
||||
}
|
||||
}
|
||||
|
||||
public let currrent_state: ParserState
|
||||
|
||||
@@ -134,10 +130,8 @@ public struct ParserStateSelectTransition: ParserStateInstance {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
get {
|
||||
return "Instance of \(currrent_state)"
|
||||
}
|
||||
}
|
||||
|
||||
public func execute(
|
||||
program: Common.ProgramExecution
|
||||
@@ -203,7 +197,7 @@ extension ParserStates: Compilable {
|
||||
public typealias ToCompile = ParserStates
|
||||
public typealias Compiled = (ParserStateInstance, [ParserStateInstance])
|
||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||
var compiled_states:[ParserStateInstance] = Array()
|
||||
var compiled_states: [ParserStateInstance] = Array()
|
||||
|
||||
compiled_states.append(ParserStateNoTransition(currrent_state: accept))
|
||||
compiled_states.append(ParserStateNoTransition(currrent_state: reject))
|
||||
@@ -252,7 +246,8 @@ extension ParserInstance: Compilable {
|
||||
|
||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||
return switch ParserStates.compile(parser.states) {
|
||||
case .Ok(let (start_state, states)): Result.Ok(ParserInstance(start: start_state, states: states))
|
||||
case .Ok(let (start_state, states)):
|
||||
Result.Ok(ParserInstance(start: start_state, states: states))
|
||||
case .Error(let e): Result.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user