+181
-32
@@ -18,48 +18,197 @@
|
||||
import Common
|
||||
import Lang
|
||||
|
||||
extension ParserState: EvaluatableParserTransition {
|
||||
public func evaluate(execution: ProgramExecution) -> (ParserState, ProgramExecution) {
|
||||
var currentExecution = execution
|
||||
extension ParserAssignmentStatement: EvaluatableParserStatement {
|
||||
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
|
||||
let updated_scopes = execution.scopes.set(identifier: self.lvalue, value: self.value)
|
||||
|
||||
execution.scopes = updated_scopes
|
||||
|
||||
return execution
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParserStateDirectTransition: ParserStateInstance {
|
||||
public let currrent_state: ParserState
|
||||
public let next_state: ParserStateInstance
|
||||
|
||||
public func execute(
|
||||
program: Common.ProgramExecution
|
||||
) -> (any ParserStateInstance, Common.ProgramExecution) {
|
||||
var program = program.exit_scope()
|
||||
program = program.enter_scope()
|
||||
|
||||
for local_element in currrent_state.local_elements {
|
||||
program = local_element.evaluate(execution: program)
|
||||
}
|
||||
|
||||
for statement in currrent_state.statements {
|
||||
program = statement.evaluate(execution: program)
|
||||
}
|
||||
|
||||
return (self.next_state, program)
|
||||
}
|
||||
|
||||
public func done() -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
public func current() -> Lang.ParserState {
|
||||
return currrent_state
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct ParserStateNoTransition: ParserStateInstance {
|
||||
public let currrent_state: ParserState
|
||||
public func execute(
|
||||
program: Common.ProgramExecution
|
||||
) -> (any ParserStateInstance, Common.ProgramExecution) {
|
||||
let program = program.exit_scope()
|
||||
return (self, program)
|
||||
}
|
||||
|
||||
public func done() -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
public func current() -> Lang.ParserState {
|
||||
return currrent_state
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParserStateSelectTransition: ParserStateInstance {
|
||||
public func execute(
|
||||
program: Common.ProgramExecution
|
||||
) -> (any ParserStateInstance, Common.ProgramExecution) {
|
||||
// Otherwise, we exit the scope from the previous state and enter a new one!
|
||||
var program = program.exit_scope()
|
||||
program = program.enter_scope()
|
||||
|
||||
// First, evaluate the local elements.
|
||||
for local_element in local_elements {
|
||||
currentExecution = local_element.evaluate(execution: currentExecution)
|
||||
for local_element in currrent_state.local_elements {
|
||||
program = local_element.evaluate(execution: program)
|
||||
}
|
||||
|
||||
// Then, evaluate the statements.
|
||||
for statement in statements {
|
||||
currentExecution = statement.evaluate(execution: currentExecution)
|
||||
for statement in currrent_state.statements {
|
||||
program = statement.evaluate(execution: program)
|
||||
}
|
||||
|
||||
if direct_transition() {
|
||||
return (next_state!, currentExecution)
|
||||
}
|
||||
|
||||
if let transition_expression = self.transition,
|
||||
let transition_select_expression = transition_expression.transition_expression
|
||||
{
|
||||
return transition_select_expression.evaluate(execution: currentExecution)
|
||||
}
|
||||
return (reject, currentExecution)
|
||||
}
|
||||
}
|
||||
|
||||
extension ParserTransitionSelectExpression: EvaluatableParserTransition {
|
||||
func evaluate(execution: Common.ProgramExecution) -> (Lang.ParserState, Common.ProgramExecution) {
|
||||
// First, evaluate the selector.
|
||||
|
||||
switch self.selector.evaluate(execution: execution) {
|
||||
switch self.selector.evaluate(execution: program) {
|
||||
case .Ok(let selector_value):
|
||||
for kse in self.keyset_expressions {
|
||||
if case .Ok(let kse_key) = kse.key.evaluate(execution: execution),
|
||||
kse_key.eq(rhs: selector_value)
|
||||
{
|
||||
return (kse.next_state!, execution)
|
||||
}
|
||||
for (key, target) in zip(self.keys, self.states) {
|
||||
if case .Ok(let kse_key) = key.evaluate(execution: program),
|
||||
kse_key.eq(rhs: selector_value)
|
||||
{
|
||||
return (target, program)
|
||||
}
|
||||
case .Error(let e): return (reject, execution.setError(error: e))
|
||||
}
|
||||
return (
|
||||
self, program.setError(error: Error(withMessage: "No selector key matched")).setDone()
|
||||
)
|
||||
case .Error(let e): return (self, program.setError(error: e))
|
||||
}
|
||||
}
|
||||
|
||||
public func done() -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
public func current() -> Lang.ParserState {
|
||||
return currrent_state
|
||||
}
|
||||
|
||||
public let keys: [any EvaluatableExpression]
|
||||
public let states: [ParserStateInstance]
|
||||
public let selector: any EvaluatableExpression
|
||||
public let currrent_state: ParserState
|
||||
}
|
||||
|
||||
extension ParserState: Compilable {
|
||||
public typealias ToCompile = (ParserState, [String: ParserStateInstance])
|
||||
public typealias Compiled = ParserStateInstance
|
||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||
let (state, current) = parser
|
||||
if state == accept || state == reject {
|
||||
return .Ok(ParserStateNoTransition(currrent_state: state))
|
||||
}
|
||||
|
||||
if state.direct_transition(),
|
||||
let transition_statement = state.transition {
|
||||
return .Ok(
|
||||
ParserStateDirectTransition(
|
||||
currrent_state: state, next_state: current[transition_statement.next_state_name!]!))
|
||||
}
|
||||
|
||||
if let transition_select_statement = state.transition,
|
||||
let transition_select_expression = transition_select_statement.transition_expression {
|
||||
|
||||
var keys: Array<any EvaluatableExpression> = Array()
|
||||
var states: Array<any ParserStateInstance> = Array()
|
||||
|
||||
for kse in transition_select_expression.keyset_expressions {
|
||||
guard let next_state = current[kse.next_state_name] else {
|
||||
return .Error(Error(withMessage: "Cannot find \(kse.next_state_name) as transition target"))
|
||||
}
|
||||
keys.append(kse.key)
|
||||
states.append(next_state)
|
||||
}
|
||||
return .Ok(ParserStateSelectTransition(keys: keys, states: states, selector: transition_select_expression.selector, currrent_state: state))
|
||||
}
|
||||
|
||||
return .Error(Error(withMessage: "Invalid parser state: No meaningful transition"))
|
||||
}
|
||||
}
|
||||
|
||||
extension ParserStates: Compilable {
|
||||
public typealias ToCompile = ParserStates
|
||||
public typealias Compiled = ParserStateInstance
|
||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||
var compiled_states = [String: ParserStateInstance]()
|
||||
|
||||
compiled_states["accept"] = ParserStateNoTransition(currrent_state: accept)
|
||||
compiled_states["reject"] = ParserStateNoTransition(currrent_state: reject)
|
||||
|
||||
// TODO: We assume that states are in transition-order!
|
||||
for state in parser.states {
|
||||
switch ParserState.compile((state, compiled_states)) {
|
||||
case .Ok(let compiled): compiled_states[state.state_name] = compiled
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
// Now, find the start state:
|
||||
if let start_state = compiled_states["start"] {
|
||||
return .Ok(start_state)
|
||||
} else {
|
||||
return .Error(Error(withMessage: "No start state defined"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserInstance: ProgramExecution {
|
||||
|
||||
let start_state: ParserStateInstance
|
||||
|
||||
public init(_ _start_state: ParserStateInstance) {
|
||||
self.start_state = _start_state
|
||||
}
|
||||
|
||||
public override var description: String {
|
||||
return "Execution: \(super.description)\nStart State: \(start_state)"
|
||||
}
|
||||
}
|
||||
|
||||
extension ParserInstance: Compilable {
|
||||
public typealias ToCompile = Parser
|
||||
public typealias Compiled = ParserInstance
|
||||
|
||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||
return switch ParserStates.compile(parser.states) {
|
||||
case .Ok(let start_state): Result.Ok(ParserInstance(start_state))
|
||||
case .Error(let e): Result.Error(e)
|
||||
}
|
||||
return (reject, execution)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import Common
|
||||
import Lang
|
||||
|
||||
protocol EvaluatableParserTransition {
|
||||
func evaluate(execution: ProgramExecution) -> (ParserState, ProgramExecution)
|
||||
func program(execution: ProgramExecution) -> (ParserState, ProgramExecution)
|
||||
}
|
||||
|
||||
protocol EvaluatableParserTransitionStatement {
|
||||
@@ -28,4 +28,20 @@ protocol EvaluatableParserTransitionStatement {
|
||||
|
||||
public protocol Execution {
|
||||
func execute() -> (ParserState, ProgramExecution)
|
||||
}
|
||||
|
||||
public protocol Compilable {
|
||||
associatedtype ToCompile
|
||||
associatedtype Compiled
|
||||
static func compile(_: ToCompile) -> Result<Compiled>
|
||||
}
|
||||
|
||||
public protocol ParserStateInstance {
|
||||
func execute(program: ProgramExecution) -> (ParserStateInstance, ProgramExecution)
|
||||
func done() -> Bool
|
||||
func current() -> ParserState
|
||||
}
|
||||
|
||||
public protocol ParserExecution {
|
||||
func execute() -> (ParserState, ProgramExecution)
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import Lang
|
||||
|
||||
/// The runtime for a parser
|
||||
public class ParserRuntime: CustomStringConvertible {
|
||||
var parser: ParserInstance
|
||||
public var parser: ParserInstance
|
||||
|
||||
init(execution: ParserInstance) {
|
||||
self.parser = execution
|
||||
@@ -31,7 +31,7 @@ public class ParserRuntime: CustomStringConvertible {
|
||||
|
||||
return switch program.starting_parser() {
|
||||
case .Ok(let parser):
|
||||
switch ParserInstance.create(parser) {
|
||||
switch ParserInstance.compile(parser) {
|
||||
case .Ok(let execution): .Ok(Runtime.ParserRuntime(execution: execution))
|
||||
case .Error(let error): .Error(error)
|
||||
}
|
||||
@@ -40,8 +40,7 @@ public class ParserRuntime: CustomStringConvertible {
|
||||
}
|
||||
|
||||
/// Run the P4 parser on a given packet
|
||||
public func run(input: Packet) -> Result<(ParserState, ProgramExecution)> {
|
||||
parser.scopes.enter()
|
||||
public func run() -> Result<(ParserState, ProgramExecution)> {
|
||||
return .Ok(parser.execute())
|
||||
}
|
||||
|
||||
@@ -51,15 +50,15 @@ public class ParserRuntime: CustomStringConvertible {
|
||||
}
|
||||
|
||||
/// Instances of parsers are executable
|
||||
extension ParserInstance: Execution {
|
||||
extension ParserInstance: ParserExecution {
|
||||
public func execute() -> (ParserState, ProgramExecution) {
|
||||
var execution = self as ProgramExecution
|
||||
var state = self.state
|
||||
var c = self.start_state
|
||||
|
||||
// Evaluate until the state is either accept or reject.
|
||||
while state != accept && state != reject {
|
||||
(state, execution) = state.evaluate(execution: execution)
|
||||
while !c.done() && !execution.hasError() {
|
||||
(c, execution) = c.execute(program: execution)
|
||||
}
|
||||
return (state, execution)
|
||||
return (c.current(), execution)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user