Refactor Parser States
Refactor the class hierarchy for parser states so that there is a parser state that acts more like a type and one that acts more like a value (the latter, then, serves as the base class for the other instantiated parser states). Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -84,7 +84,7 @@ public struct Parser {
|
||||
static func Compile(
|
||||
node: Node, forState state_identifier: Common.Identifier,
|
||||
withStatements stmts: [EvaluatableStatement], withContext context: CompilerContext
|
||||
) -> Result<(ParserState, CompilerContext)> {
|
||||
) -> Result<(InstantiatedParserState, CompilerContext)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
|
||||
node: node, type: "parserTransitionStatement", nice_type_name: "parser transition statement"
|
||||
@@ -177,7 +177,7 @@ public struct Parser {
|
||||
public struct State {
|
||||
static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(ParserState, CompilerContext)> {
|
||||
) -> Result<(InstantiatedParserState, CompilerContext)> {
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
|
||||
+91
-15
@@ -40,9 +40,11 @@ public struct ParserAssignmentStatement {
|
||||
/// A P4 Parser State
|
||||
///
|
||||
/// Note: A P4 Parser State is both a type and a value.
|
||||
/// This "bare" parser state represents the state more as a type than a value.
|
||||
public class ParserState: P4Type, P4Value, Equatable, CustomStringConvertible {
|
||||
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
||||
return lhs.state == rhs.state
|
||||
// Two "bare" parser states are always equal.
|
||||
return true
|
||||
}
|
||||
|
||||
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||
@@ -56,37 +58,106 @@ public class ParserState: P4Type, P4Value, Equatable, CustomStringConvertible {
|
||||
return self
|
||||
}
|
||||
|
||||
// Any operation between two "bare" parser states is always true.
|
||||
public func eq(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as ParserState: self.state == other.state
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lt(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as ParserState: self.state < other.state
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lte(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as ParserState: self.state <= other.state
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gt(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as ParserState: self.state > other.state
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gte(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as ParserState: self.state >= other.state
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Bare Parser State"
|
||||
}
|
||||
|
||||
/// Construct a ParserState
|
||||
public init() {}
|
||||
|
||||
public func def() -> any P4Value {
|
||||
return ParserState()
|
||||
}
|
||||
}
|
||||
|
||||
/// Instantiated Parser State
|
||||
///
|
||||
/// A parser state is both a type and a value. The Instantiated
|
||||
/// Parser State is the base class for parser states that act more
|
||||
/// as a value than a type.
|
||||
public class InstantiatedParserState: ParserState {
|
||||
public static func == (lhs: InstantiatedParserState, rhs: InstantiatedParserState) -> Bool {
|
||||
return lhs.state == rhs.state
|
||||
}
|
||||
|
||||
public override func eq(rhs: any Common.P4Type) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func type() -> any Common.P4Type {
|
||||
return self
|
||||
}
|
||||
|
||||
public override func eq(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state == other.state
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func lt(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state < other.state
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func lte(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state <= other.state
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func gt(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state > other.state
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func gte(rhs: any Common.P4Value) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state >= other.state
|
||||
default: false
|
||||
}
|
||||
}
|
||||
@@ -94,7 +165,7 @@ public class ParserState: P4Type, P4Value, Equatable, CustomStringConvertible {
|
||||
public private(set) var state: Identifier
|
||||
public private(set) var statements: [EvaluatableStatement]
|
||||
|
||||
public var description: String {
|
||||
public override var description: String {
|
||||
return "Name: \(state)"
|
||||
}
|
||||
|
||||
@@ -114,12 +185,12 @@ public class ParserState: P4Type, P4Value, Equatable, CustomStringConvertible {
|
||||
statements = Array()
|
||||
}
|
||||
|
||||
public func def() -> any P4Value {
|
||||
return ParserState(name: Identifier(name: ""))
|
||||
public override func def() -> any P4Value {
|
||||
return InstantiatedParserState(name: Identifier(name: ""))
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateDirectTransition: ParserState {
|
||||
public class ParserStateDirectTransition: InstantiatedParserState {
|
||||
|
||||
private let next_state: Identifier
|
||||
|
||||
@@ -140,7 +211,7 @@ public class ParserStateDirectTransition: ParserState {
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateNoTransition: ParserState {
|
||||
public class ParserStateNoTransition: InstantiatedParserState {
|
||||
public override init(name: Identifier, withStatements stmts: [any EvaluatableStatement]) {
|
||||
super.init(name: name, withStatements: stmts)
|
||||
}
|
||||
@@ -149,7 +220,7 @@ public class ParserStateNoTransition: ParserState {
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateSelectTransition: ParserState {
|
||||
public class ParserStateSelectTransition: InstantiatedParserState {
|
||||
|
||||
public let selectExpression: SelectExpression
|
||||
|
||||
@@ -172,7 +243,7 @@ nonisolated(unsafe) public let reject = ParserStateNoTransition(
|
||||
name: Identifier(name: "reject"), withStatements: [])
|
||||
|
||||
public struct ParserStates {
|
||||
public var states: [ParserState] = Array()
|
||||
public var states: [InstantiatedParserState] = Array()
|
||||
|
||||
public func count() -> Int {
|
||||
return states.count
|
||||
@@ -191,11 +262,11 @@ public struct ParserStates {
|
||||
self.states = Array()
|
||||
}
|
||||
|
||||
private init(withStates states: [ParserState]) {
|
||||
private init(withStates states: [InstantiatedParserState]) {
|
||||
self.states = states
|
||||
}
|
||||
|
||||
public func append(state: ParserState) -> ParserStates {
|
||||
public func append(state: InstantiatedParserState) -> ParserStates {
|
||||
var new_states = self.states
|
||||
new_states.append(state)
|
||||
return ParserStates(withStates: new_states)
|
||||
@@ -278,3 +349,8 @@ public struct Parser: P4Type, P4Value {
|
||||
return Parser(withName: Identifier(name: ""))
|
||||
}
|
||||
}
|
||||
|
||||
/// Launder a parser state into an instantiated parser state.
|
||||
public func AsInstantiatedParserState(_ state: ParserState) -> InstantiatedParserState {
|
||||
return state as! InstantiatedParserState
|
||||
}
|
||||
|
||||
@@ -24,8 +24,7 @@ extension SelectCaseExpression: EvaluatableExpression {
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
// TODO
|
||||
return reject
|
||||
return ParserState()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,9 +45,8 @@ extension SelectExpression: EvaluatableExpression {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
public func type() -> any Common.P4Type {
|
||||
return reject
|
||||
return ParserState()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -118,11 +118,11 @@ extension ParserStateSelectTransition: EvaluatableParserState {
|
||||
}
|
||||
|
||||
extension Parser: ParserExecution {
|
||||
public func execute(execution: ProgramExecution) -> (ParserState, ProgramExecution) {
|
||||
public func execute(execution: ProgramExecution) -> (InstantiatedParserState, ProgramExecution) {
|
||||
var execution = execution.enter_scope()
|
||||
|
||||
execution = execution.declare(identifier: accept.state().state, withValue: accept)
|
||||
execution = execution.declare(identifier: reject.state().state, withValue: reject)
|
||||
execution = execution.declare(identifier: AsInstantiatedParserState(accept.state()).state, withValue: accept)
|
||||
execution = execution.declare(identifier: AsInstantiatedParserState(reject.state()).state, withValue: reject)
|
||||
|
||||
// Add initial values to the global scope
|
||||
if let initial = execution.initial_values() {
|
||||
@@ -148,6 +148,6 @@ extension Parser: ParserExecution {
|
||||
while !current_state.done() && !execution.hasError() {
|
||||
(current_state, execution) = current_state.execute(program: execution)
|
||||
}
|
||||
return (current_state.state(), execution)
|
||||
return (AsInstantiatedParserState(current_state.state()), execution)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,5 +35,5 @@ public protocol EvaluatableParserState: P4Value {
|
||||
}
|
||||
|
||||
public protocol ParserExecution {
|
||||
func execute(execution: ProgramExecution) -> (ParserState, ProgramExecution)
|
||||
func execute(execution: ProgramExecution) -> (InstantiatedParserState, ProgramExecution)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user