Refactor Parsing/Runtime

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-02-24 00:28:41 -05:00
parent f9353c683e
commit 64a0fe4255
29 changed files with 1269 additions and 974 deletions
+217
View File
@@ -0,0 +1,217 @@
// 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
extension ParserAssignmentStatement: EvaluatableStatement {
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
guard case Result.Ok(let value) = self.value.evaluate(execution: execution) else {
return execution.setError(error: Error(withMessage: "AHHHHH"))
}
let updated_scopes = execution.scopes.set(identifier: self.lvalue, withValue: 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() -> P4Lang.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() -> P4Lang.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 currrent_state.local_elements {
program = local_element.evaluate(execution: program)
}
// Then, evaluate the statements.
for statement in currrent_state.statements {
program = statement.evaluate(execution: program)
}
switch self.selector.evaluate(execution: program) {
case .Ok(let selector_value):
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)
}
}
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() -> P4Lang.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)
}
}
}
+47
View File
@@ -0,0 +1,47 @@
// 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 P4Lang
import Common
extension VariableDeclarationStatement: EvaluatableStatement {
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
guard case .Ok(let initial_value) = self.initializer.evaluate(execution: execution) else {
return execution.setError(error: Error(withMessage: "Could not evaluate \(self.initializer)"))
}
let new_scopes = execution.scopes.declare(identifier: self.identifier, withValue: initial_value)
execution.scopes = new_scopes
return execution
}
}
extension ExpressionStatement: EvaluatableStatement {
public func evaluate(execution: ProgramExecution) -> ProgramExecution {
return execution
}
}
// Variables are evaluatable because they can be looked up by identifiers.
extension TypedIdentifier: EvaluatableExpression {
public func type() -> any Common.P4Type {
return self.parsed_type
}
public func evaluate(execution: Common.ProgramExecution) -> Result<P4Value> {
return execution.scopes.lookup(identifier: self)
}
}
+47
View File
@@ -0,0 +1,47 @@
// 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
protocol EvaluatableParserTransition {
func program(execution: ProgramExecution) -> (ParserState, ProgramExecution)
}
protocol EvaluatableParserTransitionStatement {
func transition(execution: ProgramExecution) -> (ParserState, ProgramExecution)
}
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)
}
+64
View File
@@ -0,0 +1,64 @@
// 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
/// The runtime for a parser
public class ParserRuntime: CustomStringConvertible {
public var parser: ParserInstance
init(execution: ParserInstance) {
self.parser = execution
}
/// Create a parser runtime from a P4 program
public static func create(program: P4Lang.Program) -> Result<ParserRuntime> {
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)
}
case .Error(let error): .Error(error)
}
}
/// Run the P4 parser on a given packet
public func run() -> Result<(ParserState, ProgramExecution)> {
return .Ok(parser.execute())
}
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
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.current(), execution)
}
}