Refactor Parsers and Parser States

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-10 03:56:18 -04:00
parent 7c7416fa64
commit 3188b52965
12 changed files with 223 additions and 381 deletions
+4 -2
View File
@@ -24,7 +24,8 @@ extension KeysetExpression: EvaluatableExpression {
}
public func type() -> any Common.P4Type {
return P4ParserState.create()
// TODO
return reject
}
}
@@ -45,8 +46,9 @@ extension SelectExpression: EvaluatableExpression {
}
}
// TODO
public func type() -> any Common.P4Type {
return P4ParserState.create()
return reject
}
}
+32 -146
View File
@@ -32,39 +32,20 @@ extension ParserAssignmentStatement: EvaluatableStatement {
}
}
public struct ParserStateDirectTransition: ParserStateInstance {
public func type() -> any Common.P4Type {
return P4ParserState.create()
}
public func eq(rhs: any Common.P4Value) -> Bool {
switch rhs {
case let state as ParserStateInstance: return currrent_state == state.state()
default: return false
}
}
public var description: String {
return "Instance of \(currrent_state)"
}
public let currrent_state: ParserState
public let next_state_identifier: Identifier
extension ParserStateDirectTransition: EvaluatableParserState {
public func execute(
program: Common.ProgramExecution
) -> (any ParserStateInstance, Common.ProgramExecution) {
) -> (any EvaluatableParserState, Common.ProgramExecution) {
var program = program.enter_scope()
for statement in currrent_state.statements {
for statement in statements {
program = statement.evaluate(execution: program)
}
let res = program.scopes.lookup(identifier: next_state_identifier)
let res = program.scopes.lookup(identifier: get_next_state())
if case .Ok(let value) = res {
if value.type().eq(rhs: P4ParserState.create()) {
return (value as! ParserStateInstance, program.exit_scope())
if value.type().eq(rhs: self) {
return (value as! EvaluatableParserState, program.exit_scope())
}
}
@@ -78,33 +59,15 @@ public struct ParserStateDirectTransition: ParserStateInstance {
}
public func state() -> P4Lang.ParserState {
return currrent_state
return self
}
}
public struct ParserStateNoTransition: ParserStateInstance {
public func type() -> any Common.P4Type {
return P4ParserState.create()
}
public func eq(rhs: any Common.P4Value) -> Bool {
switch rhs {
case let state as ParserStateInstance: return currrent_state == state.state()
default: return false
}
}
public var description: String {
return "Instance of \(currrent_state)"
}
public let currrent_state: ParserState
extension ParserStateNoTransition: EvaluatableParserState {
public func execute(
program: Common.ProgramExecution
) -> (any ParserStateInstance, Common.ProgramExecution) {
) -> (any EvaluatableParserState, Common.ProgramExecution) {
return (self, program)
}
@@ -113,41 +76,27 @@ public struct ParserStateNoTransition: ParserStateInstance {
}
public func state() -> P4Lang.ParserState {
return currrent_state
return self
}
}
public struct ParserStateSelectTransition: ParserStateInstance {
public func type() -> any Common.P4Type {
return P4ParserState.create()
}
public func eq(rhs: any Common.P4Value) -> Bool {
switch rhs {
case let state as ParserStateInstance: return currrent_state == state.state()
default: return false
}
}
public var description: String {
return "Instance of \(currrent_state)"
}
extension ParserStateSelectTransition: EvaluatableParserState {
public func execute(
program: Common.ProgramExecution
) -> (any ParserStateInstance, Common.ProgramExecution) {
) -> (any EvaluatableParserState, Common.ProgramExecution) {
var program = program.enter_scope()
// First, evaluate the statements.
for statement in currrent_state.statements {
for statement in statements {
program = statement.evaluate(execution: program)
}
let res = self.selectExpression.evaluate(execution: program)
if case .Ok(let value) = res {
if value.type().eq(rhs: P4ParserState.create()) {
return (value as! ParserStateInstance, program.exit_scope())
if value.type().eq(rhs: self) {
return (value as! EvaluatableParserState, program.exit_scope())
}
}
@@ -160,95 +109,32 @@ public struct ParserStateSelectTransition: ParserStateInstance {
}
public func state() -> P4Lang.ParserState {
return currrent_state
}
public let selectExpression: SelectExpression
public let currrent_state: ParserState
}
extension ParserState: Compilable {
public typealias ToCompile = ParserState
public typealias Compiled = ParserStateInstance
public static func compile(_ state: ToCompile) -> Result<Compiled> {
if state.direct_transition(),
let transition_statement = state.transition
{
return .Ok(
ParserStateDirectTransition(
currrent_state: state, next_state_identifier: transition_statement.next_state!))
}
if let transition_select_statement = state.transition,
let transition_select_expression = transition_select_statement.transition_expression
{
return .Ok(
ParserStateSelectTransition(
selectExpression: transition_select_expression, currrent_state: state))
}
return .Error(Error(withMessage: "Invalid parser state: No meaningful transition"))
return self
}
}
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()
extension Parser: ParserExecution {
public func execute(execution: ProgramExecution) -> (ParserState, ProgramExecution) {
var execution = execution.enter_scope()
compiled_states.append(ParserStateNoTransition(currrent_state: accept))
compiled_states.append(ParserStateNoTransition(currrent_state: reject))
execution = execution.declare(identifier: accept.state().state, withValue: accept)
execution = execution.declare(identifier: reject.state().state, withValue: reject)
var start_state: ParserStateInstance? = .none
// TODO: We assume that states are in transition-order!
for state in parser.states {
switch ParserState.compile(state) {
case .Ok(let compiled):
if compiled.state().state == Identifier(name: "start") {
start_state = compiled
}
compiled_states.append(compiled)
case .Error(let e): return .Error(e)
}
// First, add every state to the scope!
for state in self.states.states {
execution = execution.declare(identifier: state.state, withValue: state)
}
// Now, find the start state:
if let start_state = start_state {
return .Ok((start_state, compiled_states))
} else {
return .Error(Error(withMessage: "No start state defined"))
guard let _current_state = self.findStartState(),
var current_state = _current_state as? EvaluatableParserState else {
return (reject, execution.setError(error: Error(withMessage: "Could not find the start state")))
}
}
}
public class ParserInstance: ProgramExecution {
let states: [ParserStateInstance]
let start_state: ParserStateInstance
public init(start: ParserStateInstance, states: [ParserStateInstance]) {
start_state = start
self.states = states
}
public override var description: String {
return "Execution: \(super.description)\nStates: \(states)"
}
}
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, states)):
Result.Ok(ParserInstance(start: start_state, states: states))
case .Error(let e): Result.Error(e)
// Evaluate until the state is either accept or reject.
while !current_state.done() && !execution.hasError() {
(current_state, execution) = current_state.execute(program: execution)
}
return (current_state.state(), execution)
}
}
}
+4 -4
View File
@@ -19,7 +19,7 @@ import Common
import P4Lang
public protocol Execution {
func execute() -> (ParserState, ProgramExecution)
func execute(execution: ProgramExecution) -> ProgramExecution
}
public protocol Compilable {
@@ -28,12 +28,12 @@ public protocol Compilable {
static func compile(_: ToCompile) -> Result<Compiled>
}
public protocol ParserStateInstance: P4Value {
func execute(program: ProgramExecution) -> (ParserStateInstance, ProgramExecution)
public protocol EvaluatableParserState: P4Value {
func execute(program: ProgramExecution) -> (EvaluatableParserState, ProgramExecution)
func done() -> Bool
func state() -> ParserState
}
public protocol ParserExecution {
func execute() -> (ParserState, ProgramExecution)
func execute(execution: ProgramExecution) -> (ParserState, ProgramExecution)
}
+8 -31
View File
@@ -19,11 +19,11 @@ import Common
import P4Lang
/// The runtime for a parser
public class ParserRuntime: CustomStringConvertible {
public var parser: ParserInstance
public struct ParserRuntime: CustomStringConvertible {
public var parser: Parser
init(execution: ParserInstance) {
self.parser = execution
init(parser: Parser) {
self.parser = parser
}
/// Create a parser runtime from a P4 program
@@ -31,17 +31,15 @@ public class ParserRuntime: CustomStringConvertible {
return switch program.starting_parser() {
case .Ok(let parser):
switch ParserInstance.compile(parser) {
case .Ok(let execution): .Ok(P4Runtime.ParserRuntime(execution: execution))
case .Error(let error): .Error(error)
}
.Ok(P4Runtime.ParserRuntime(parser: parser))
case .Error(let error): .Error(error)
}
}
/// Run the P4 parser on a given packet
public func run() -> Result<(ParserState, ProgramExecution)> {
let (end_state, execution) = parser.execute()
let (end_state, execution) = parser.execute(execution: ProgramExecution())
if let error = execution.getError() {
return .Error(error)
}
@@ -51,25 +49,4 @@ public class ParserRuntime: CustomStringConvertible {
public var description: String {
return "Runtime:\nExecution: \(parser)"
}
}
/// Instances of parsers are executable
extension ParserInstance: ParserExecution {
public func execute() -> (ParserState, ProgramExecution) {
var execution = self as ProgramExecution
execution = execution.enter_scope()
// First, add every state to the scope!
for state in self.states {
execution = execution.declare(identifier: state.state().state, withValue: state)
}
var c = self.start_state
// Evaluate until the state is either accept or reject.
while !c.done() && !execution.hasError() {
(c, execution) = c.execute(program: execution)
}
return (c.state(), execution)
}
}
}