compiler, language, runtime: Separate Parser Type From Instances
In P4, parsers are considered types. Those parsers are instantiated. The instantiated parsers are values. Previously, gp4 treated a parser type and a parser value as identical. This PR makes that difference clear _and_ sets the stage for the future. TODO: Make the same distinction between control and action types and values. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
+229
-193
@@ -41,209 +41,234 @@ public struct ParserAssignmentStatement {
|
||||
///
|
||||
/// 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, P4DataValue, Equatable, CustomStringConvertible {
|
||||
public class ParserState: P4Type, Equatable, CustomStringConvertible {
|
||||
let name: Identifier
|
||||
public let statements: [EvaluatableStatement]
|
||||
|
||||
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
||||
// Two "bare" parser states are always equal.
|
||||
return true
|
||||
return lhs.eq(rhs: rhs)
|
||||
}
|
||||
|
||||
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
return self
|
||||
}
|
||||
|
||||
// Any operation between two "bare" parser states is always true.
|
||||
public func eq(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case is ParserState: true
|
||||
case let rrhs as ParserState: self.name == rrhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Bare Parser State"
|
||||
return "Parser State named \(self.name)"
|
||||
}
|
||||
|
||||
public func getName() -> Identifier {
|
||||
return self.name
|
||||
}
|
||||
|
||||
public func getStatements() -> [EvaluatableStatement] {
|
||||
return self.statements
|
||||
}
|
||||
|
||||
/// Construct a ParserState
|
||||
public init() {}
|
||||
public init(_ name: Identifier, _ statements: [EvaluatableStatement] = Array()) {
|
||||
self.name = name
|
||||
self.statements = statements
|
||||
}
|
||||
|
||||
public func def() -> P4DataValue? {
|
||||
return .none
|
||||
}
|
||||
|
||||
public func instantiate(_ name: Identifier) -> InstantiatedParserState? {
|
||||
return .none
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 class _AnyParserState: ParserState {
|
||||
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
|
||||
nonisolated(unsafe) public let AnyParserState = _AnyParserState(Identifier(name: "AnyParserState"))
|
||||
|
||||
public class ParserStateDirectTransition: ParserState {
|
||||
|
||||
public let next_state: InstantiatedParserState?
|
||||
public let next_state_identifier: Identifier?
|
||||
|
||||
/// Construct a ParserState
|
||||
public init(
|
||||
name: Identifier, withNextState next_state: InstantiatedParserState,
|
||||
withStatements stmts: [EvaluatableStatement] = Array(),
|
||||
) {
|
||||
self.next_state = next_state
|
||||
self.next_state_identifier = .none
|
||||
super.init(name, stmts)
|
||||
}
|
||||
|
||||
public override func eq(rhs: any Common.P4DataValue) -> Bool {
|
||||
public init(
|
||||
name: Identifier, withNextStateIdentifier next_state_id: Identifier,
|
||||
withStatements stmts: [EvaluatableStatement] = Array(),
|
||||
) {
|
||||
self.next_state = .none
|
||||
self.next_state_identifier = next_state_id
|
||||
super.init(name, stmts)
|
||||
}
|
||||
|
||||
public override func instantiate(_ name: Identifier) -> InstantiatedParserState? {
|
||||
return if let next_state = self.next_state {
|
||||
ParserStateDirectTransitionValue(name: name, withState: self, withNextState: next_state)
|
||||
} else {
|
||||
ParserStateDirectTransitionValue(
|
||||
name: name, withState: self, withNextStateIdentifier: self.next_state_identifier!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateNoTransition: ParserState {
|
||||
/// Construct a ParserState
|
||||
public init(
|
||||
name: Identifier, withStatements stmts: [EvaluatableStatement] = Array(),
|
||||
) {
|
||||
super.init(name, stmts)
|
||||
}
|
||||
|
||||
public override func instantiate(_ name: Identifier) -> InstantiatedParserState? {
|
||||
return ParserStateNoTransitionValue(name: name, withState: self)
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateSelectTransition: ParserState {
|
||||
|
||||
public let te: SelectExpression
|
||||
|
||||
public init(
|
||||
name: Identifier, withTransitionExpression te: SelectExpression,
|
||||
withStatements stmts: [EvaluatableStatement] = Array()
|
||||
) {
|
||||
self.te = te
|
||||
super.init(name, stmts)
|
||||
}
|
||||
|
||||
public override func instantiate(_ name: Identifier) -> InstantiatedParserState? {
|
||||
return ParserStateSelectTransitionValue(name: name, withState: self, withSelectExpression: te)
|
||||
}
|
||||
}
|
||||
|
||||
/// Instantiated Parser State
|
||||
///
|
||||
public class InstantiatedParserState: P4DataValue {
|
||||
public static func == (lhs: InstantiatedParserState, rhs: InstantiatedParserState) -> Bool {
|
||||
return lhs.state == rhs.state
|
||||
}
|
||||
|
||||
public func type() -> any Common.P4Type {
|
||||
return self.state
|
||||
}
|
||||
|
||||
public func eq(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state == other.state
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func lt(rhs: any Common.P4DataValue) -> Bool {
|
||||
public func lt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state < other.state
|
||||
case let other as InstantiatedParserState: self.state.getName() < other.state.getName()
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func lte(rhs: any Common.P4DataValue) -> Bool {
|
||||
public func lte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state <= other.state
|
||||
case let other as InstantiatedParserState: self.state.getName() <= other.state.getName()
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func gt(rhs: any Common.P4DataValue) -> Bool {
|
||||
public func gt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state > other.state
|
||||
case let other as InstantiatedParserState: self.state.getName() > other.state.getName()
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public override func gte(rhs: any Common.P4DataValue) -> Bool {
|
||||
public func gte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as InstantiatedParserState: self.state >= other.state
|
||||
case let other as InstantiatedParserState: self.state.getName() >= other.state.getName()
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public private(set) var state: Identifier
|
||||
public private(set) var statements: [EvaluatableStatement]
|
||||
public private(set) var state: ParserState
|
||||
public private(set) var name: Identifier
|
||||
|
||||
public override var description: String {
|
||||
return "Name: \(state)"
|
||||
public var description: String {
|
||||
return "Instance of state of type \(state) named \(name)"
|
||||
}
|
||||
|
||||
/// Construct a ParserState
|
||||
public init(
|
||||
name: Identifier, withStatements stmts: [EvaluatableStatement],
|
||||
) {
|
||||
state = name
|
||||
statements = stmts
|
||||
}
|
||||
|
||||
/// (private) constructor (no transition)
|
||||
///
|
||||
/// accept and reject are the only final states and they are constructed internally.
|
||||
private init(name: Identifier) {
|
||||
state = name
|
||||
statements = Array()
|
||||
}
|
||||
|
||||
public override func def() -> any P4DataValue {
|
||||
return InstantiatedParserState(name: Identifier(name: ""))
|
||||
public init(_ name: Identifier, _ state: ParserState) {
|
||||
self.name = name
|
||||
self.state = state
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateDirectTransition: InstantiatedParserState {
|
||||
|
||||
private let next_state: Identifier
|
||||
public class ParserStateDirectTransitionValue: InstantiatedParserState {
|
||||
public let next_state: InstantiatedParserState?
|
||||
public let next_state_identifier: Identifier?
|
||||
|
||||
public init(
|
||||
name: Identifier, withStatements stmts: [EvaluatableStatement],
|
||||
withNextState next_state: Identifier
|
||||
name: Identifier, withState state: ParserStateDirectTransition,
|
||||
withNextState next_state: InstantiatedParserState
|
||||
) {
|
||||
self.next_state = next_state
|
||||
super.init(name: name, withStatements: stmts)
|
||||
}
|
||||
|
||||
public override var description: String {
|
||||
return "State (Name: \(super.state) (direct transition))"
|
||||
}
|
||||
|
||||
public func get_next_state() -> Identifier {
|
||||
return self.next_state
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateNoTransition: InstantiatedParserState {
|
||||
public override init(name: Identifier, withStatements stmts: [any EvaluatableStatement]) {
|
||||
super.init(name: name, withStatements: stmts)
|
||||
}
|
||||
public override var description: String {
|
||||
return "State (Name: \(super.state) (no transition))"
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateSelectTransition: InstantiatedParserState {
|
||||
|
||||
public let selectExpression: SelectExpression
|
||||
|
||||
public override var description: String {
|
||||
return "State (Name: \(super.state) (select transition))"
|
||||
self.next_state_identifier = .none
|
||||
super.init(name, state)
|
||||
}
|
||||
|
||||
public init(
|
||||
name: Identifier, withStatements stmts: [any EvaluatableStatement],
|
||||
withTransitioniExpression te: SelectExpression
|
||||
name: Identifier, withState state: ParserStateDirectTransition,
|
||||
withNextStateIdentifier next_state_id: Identifier
|
||||
) {
|
||||
self.selectExpression = te
|
||||
super.init(name: name, withStatements: stmts)
|
||||
self.next_state = .none
|
||||
self.next_state_identifier = next_state_id
|
||||
super.init(name, state)
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated(unsafe) public let accept = ParserStateNoTransition(
|
||||
public class ParserStateNoTransitionValue: InstantiatedParserState {
|
||||
public init(name: Identifier, withState state: ParserStateNoTransition) {
|
||||
super.init(name, state)
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateSelectTransitionValue: InstantiatedParserState {
|
||||
public let te: SelectExpression
|
||||
public init(
|
||||
name: Identifier, withState state: ParserStateSelectTransition,
|
||||
withSelectExpression te: SelectExpression
|
||||
) {
|
||||
self.te = te
|
||||
super.init(name, state)
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated(unsafe) private let accept_type = ParserStateNoTransition(
|
||||
name: Identifier(name: "accept"), withStatements: [])
|
||||
nonisolated(unsafe) public let reject = ParserStateNoTransition(
|
||||
nonisolated(unsafe) private let reject_type = ParserStateNoTransition(
|
||||
name: Identifier(name: "reject"), withStatements: [])
|
||||
|
||||
nonisolated(unsafe) public let accept = ParserStateNoTransitionValue(
|
||||
name: Identifier(name: "accept"), withState: accept_type)
|
||||
nonisolated(unsafe) public let reject = ParserStateNoTransitionValue(
|
||||
name: Identifier(name: "reject"), withState: reject_type)
|
||||
|
||||
public struct ParserStates {
|
||||
public var states: [InstantiatedParserState] = Array()
|
||||
public var states: [ParserState] = Array()
|
||||
|
||||
public func count() -> Int {
|
||||
return states.count
|
||||
@@ -251,71 +276,28 @@ public struct ParserStates {
|
||||
|
||||
public func find(withIdentifier id: Identifier) -> ParserState? {
|
||||
for state in states {
|
||||
if state.state == id {
|
||||
if state.getName() == id {
|
||||
return .some(state)
|
||||
}
|
||||
}
|
||||
return .none
|
||||
}
|
||||
|
||||
public init() {
|
||||
self.states = Array()
|
||||
}
|
||||
|
||||
private init(withStates states: [InstantiatedParserState]) {
|
||||
public init(_ states: [ParserState] = Array()) {
|
||||
self.states = states
|
||||
}
|
||||
|
||||
public func append(state: InstantiatedParserState) -> ParserStates {
|
||||
public func append(state: ParserState) -> ParserStates {
|
||||
var new_states = self.states
|
||||
new_states.append(state)
|
||||
return ParserStates(withStates: new_states)
|
||||
return ParserStates(new_states)
|
||||
}
|
||||
}
|
||||
|
||||
/// A P4 Parser
|
||||
///
|
||||
/// Note: A Parser is both a type _and_ a value.
|
||||
public struct Parser: P4Type, P4DataValue {
|
||||
public func type() -> any Common.P4Type {
|
||||
return self
|
||||
}
|
||||
|
||||
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||
return switch rhs {
|
||||
case let parser_rhs as Parser: self.name == parser_rhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let parser_rhs as Parser: self.name < parser_rhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let parser_rhs as Parser: self.name <= parser_rhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let parser_rhs as Parser: self.name > parser_rhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let parser_rhs as Parser: self.name >= parser_rhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
/// Note: A Parser is a type
|
||||
public struct Parser: P4Type {
|
||||
public var states: ParserStates
|
||||
|
||||
public var name: Identifier
|
||||
@@ -334,19 +316,7 @@ public struct Parser: P4Type, P4DataValue {
|
||||
}
|
||||
|
||||
public func findStartState() -> ParserState? {
|
||||
for state in states.states {
|
||||
if state.state == Identifier(name: "start") {
|
||||
return state
|
||||
}
|
||||
}
|
||||
return .none
|
||||
}
|
||||
|
||||
public func eq(rhs: any P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let other as Parser: self.name == other.name
|
||||
default: false
|
||||
}
|
||||
return self.states.find(withIdentifier: Identifier(name: "start"))
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
@@ -356,9 +326,75 @@ public struct Parser: P4Type, P4DataValue {
|
||||
public func def() -> P4DataValue? {
|
||||
return .none
|
||||
}
|
||||
|
||||
public func eq(rhs: any Common.P4Type) -> Bool {
|
||||
return switch rhs {
|
||||
case let parser_rhs as Parser: self.name == parser_rhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func instantiate(_ name: Identifier) -> ParserValue {
|
||||
return ParserValue(self, name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Launder a parser state into an instantiated parser state.
|
||||
public func AsInstantiatedParserState(_ state: ParserState) -> InstantiatedParserState {
|
||||
return state as! InstantiatedParserState
|
||||
/// A instance of a P4 Parser
|
||||
///
|
||||
public struct ParserValue: P4DataValue {
|
||||
public func type() -> P4Type {
|
||||
return self.tipe
|
||||
}
|
||||
|
||||
public func eq(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name == rrhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name < rrhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func lte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name <= rrhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gt(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name > rrhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public func gte(rhs: any Common.P4DataValue) -> Bool {
|
||||
return switch rhs {
|
||||
case let rrhs as ParserValue: rrhs.type().eq(rhs: self.type()) && self.name >= rrhs.name
|
||||
default: false
|
||||
}
|
||||
}
|
||||
|
||||
public var tipe: Parser
|
||||
public var name: Identifier
|
||||
|
||||
public init(_ tipe: Parser, _ name: Identifier) {
|
||||
self.tipe = tipe
|
||||
self.name = name
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Parser \(self.name) of type \(self.tipe)"
|
||||
}
|
||||
|
||||
public func def() -> P4DataValue? {
|
||||
return self.tipe.def()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public struct ExpressionStatement {
|
||||
public struct Program {
|
||||
public var types: [P4Type] = Array()
|
||||
public var externs: [P4Type] = Array()
|
||||
public var instances: [P4QualifiedType] = Array()
|
||||
public var instances: [P4Value] = Array()
|
||||
|
||||
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)``
|
||||
public typealias TypeFilter = (P4QualifiedType) -> Bool
|
||||
@@ -36,7 +36,7 @@ public struct Program {
|
||||
public typealias DataTypeFilter = (P4Type) -> Bool
|
||||
|
||||
/// Retrieve global instances in the compiled P4 program.
|
||||
public func InstancesWithTypes() -> [P4QualifiedType] {
|
||||
public func InstancesWithTypes() -> [P4Value] {
|
||||
return self.instances
|
||||
}
|
||||
|
||||
@@ -52,9 +52,9 @@ public struct Program {
|
||||
///
|
||||
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
|
||||
///
|
||||
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4QualifiedType] {
|
||||
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Value] {
|
||||
return self.instances.filter { instance in
|
||||
filter(instance)
|
||||
filter(instance.type())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,8 +112,8 @@ public struct Program {
|
||||
}
|
||||
|
||||
public func find_parser(withName name: Identifier) -> Result<Parser> {
|
||||
for instance in self.instances {
|
||||
guard let parser = instance.baseType() as? Parser else {
|
||||
for instance in self.types {
|
||||
guard let parser = instance as? Parser else {
|
||||
continue
|
||||
}
|
||||
if parser.name == name {
|
||||
|
||||
Reference in New Issue
Block a user