eff19df968
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
271 lines
6.7 KiB
Swift
271 lines
6.7 KiB
Swift
// 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
|
|
|
|
public struct LocalElements {
|
|
|
|
}
|
|
|
|
public struct LocalElement {
|
|
|
|
}
|
|
|
|
public struct ParserAssignmentStatement {
|
|
public let lvalue: TypedIdentifier
|
|
public let value: EvaluatableExpression
|
|
|
|
public init(withLValue lvalue: TypedIdentifier, withValue value: EvaluatableExpression) {
|
|
self.lvalue = lvalue
|
|
self.value = value
|
|
}
|
|
}
|
|
|
|
public struct KeysetExpression {
|
|
public let key: EvaluatableExpression
|
|
public let next_state_identifier: Identifier
|
|
public let next_state: ParserState?
|
|
|
|
public init(withKey key: EvaluatableExpression, withNextState next_state_id: Identifier) {
|
|
self.key = key
|
|
self.next_state_identifier = next_state_id
|
|
self.next_state = .none
|
|
}
|
|
public init(
|
|
withKey key: EvaluatableExpression, withNextState next_state_id: Identifier,
|
|
withNextState next_state: ParserState
|
|
) {
|
|
self.key = key
|
|
self.next_state_identifier = next_state_id
|
|
self.next_state = next_state
|
|
}
|
|
|
|
}
|
|
|
|
public struct SelectExpression {
|
|
public let selector: EvaluatableExpression
|
|
public let keyset_expressions: [KeysetExpression]
|
|
|
|
public init(
|
|
withSelector selector: EvaluatableExpression, withKeysetExpressions kses: [KeysetExpression]
|
|
) {
|
|
self.selector = selector
|
|
self.keyset_expressions = kses
|
|
}
|
|
|
|
public func append_checked_kse(kse: KeysetExpression) -> SelectExpression {
|
|
var new_kse = self.keyset_expressions
|
|
new_kse.append(kse)
|
|
return SelectExpression(
|
|
withSelector: self.selector, withKeysetExpressions: new_kse)
|
|
}
|
|
}
|
|
|
|
public struct ParserTransitionStatement {
|
|
public let next_state: Identifier?
|
|
public let transition_expression: SelectExpression?
|
|
|
|
public init() {
|
|
self.next_state = .none
|
|
self.transition_expression = .none
|
|
}
|
|
|
|
public init(withTransitionExpression transition_expression: SelectExpression) {
|
|
self.next_state = .none
|
|
self.transition_expression = transition_expression
|
|
}
|
|
|
|
public init(withNextState next_state: Identifier) {
|
|
self.next_state = next_state
|
|
self.transition_expression = .none
|
|
}
|
|
}
|
|
|
|
public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|
|
|
public private(set) var state: Identifier
|
|
public private(set) var statements: [EvaluatableStatement]
|
|
public private(set) var transition: ParserTransitionStatement?
|
|
public private(set) var next_state: ParserState?
|
|
|
|
public static func < (lhs: ParserState, rhs: ParserState) -> Bool {
|
|
// If lhs transitions to rhs, then return true. Otherwise, return false.
|
|
|
|
// TODO!!
|
|
return false
|
|
}
|
|
|
|
public var description: String {
|
|
return "Name: \(state)"
|
|
}
|
|
|
|
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
|
return lhs.state == rhs.state
|
|
}
|
|
|
|
/// Construct a ParserState
|
|
public init(
|
|
name: Identifier, withStatements stmts: [EvaluatableStatement]?,
|
|
withTransition transitionStatement: ParserTransitionStatement
|
|
) {
|
|
state = name
|
|
transition = transitionStatement
|
|
statements = stmts ?? Array()
|
|
}
|
|
|
|
public func semantic_check(states: ParserStates) -> Bool {
|
|
guard let transition = transition else {
|
|
return self == accept || self == reject
|
|
}
|
|
|
|
if let next_state = transition.next_state,
|
|
let next_state = states.find(withIdentifier: next_state)
|
|
{
|
|
self.next_state = next_state
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
/// (private) constructor (no transition)
|
|
///
|
|
/// accept and reject are the only final states and they are constructed internally.
|
|
init(name: Identifier) {
|
|
state = name
|
|
transition = .none
|
|
statements = Array()
|
|
}
|
|
|
|
public func direct_transition() -> Bool {
|
|
return
|
|
if let transition = self.transition,
|
|
transition.next_state != nil
|
|
{
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A P4 parser state type
|
|
public struct P4ParserState: P4Type {
|
|
public static func create() -> any P4Type {
|
|
return P4ParserState()
|
|
}
|
|
public var description: String {
|
|
return "Parser State"
|
|
}
|
|
public func eq(rhs: any P4Type) -> Bool {
|
|
return switch rhs {
|
|
case is P4ParserState: true
|
|
default: false
|
|
}
|
|
}
|
|
}
|
|
|
|
nonisolated(unsafe) public let accept: ParserState = ParserState(name: Identifier(name: "accept"))
|
|
nonisolated(unsafe) public let reject: ParserState = ParserState(name: Identifier(name: "reject"))
|
|
|
|
public struct ParserStates {
|
|
public var states: [ParserState] = Array()
|
|
|
|
public func count() -> Int {
|
|
return states.count
|
|
}
|
|
|
|
public func find(withIdentifier id: Identifier) -> ParserState? {
|
|
for state in states {
|
|
if state.state == id {
|
|
return .some(state)
|
|
}
|
|
}
|
|
return .none
|
|
}
|
|
|
|
public func semantic_check() -> Result<()> {
|
|
// Check whether all the states referred to in the transition statements are
|
|
// valid.
|
|
let errors = states.filter { state in
|
|
return !state.semantic_check(states: self)
|
|
}.map { state in
|
|
return Result<()>.Error(Error(withMessage: "State \(state) has invalid transition"))
|
|
}
|
|
|
|
if !errors.isEmpty {
|
|
return errors[0]
|
|
}
|
|
|
|
return .Ok(())
|
|
}
|
|
|
|
public init() {
|
|
self.states = Array()
|
|
}
|
|
|
|
private init(withStates states: [ParserState]) {
|
|
self.states = states
|
|
}
|
|
|
|
public func append(state: ParserState) -> ParserStates {
|
|
var new_states = self.states
|
|
new_states.append(state)
|
|
return ParserStates(withStates: new_states)
|
|
}
|
|
}
|
|
|
|
public struct Parser: P4Type {
|
|
public var states: ParserStates
|
|
|
|
public var name: Identifier
|
|
|
|
public init(withName name: Identifier) {
|
|
self.states = ParserStates()
|
|
self.name = name
|
|
}
|
|
|
|
public static func create() -> any P4Type {
|
|
return Parser(withName: Identifier(name: ""))
|
|
}
|
|
|
|
public func findStartState() -> ParserState? {
|
|
for state in states.states {
|
|
if state.state == Identifier(name: "start") {
|
|
return state
|
|
}
|
|
}
|
|
return .none
|
|
}
|
|
|
|
public func semantic_check() -> Result<()> {
|
|
return self.states.semantic_check()
|
|
}
|
|
|
|
public var description: String {
|
|
return "Parser"
|
|
}
|
|
|
|
public func eq(rhs: P4Type) -> Bool {
|
|
return if let parser_rhs = rhs as? Parser {
|
|
self.name == parser_rhs.name
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|