// 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 .
import Common
import P4Parser
public struct CSTTextSerializer {
public init() {}
}
public struct CSTTextSerializerContext {
public let serialized: String
public init(_ serialized: String = "") {
self.serialized = serialized
}
public func append(_ a: String) -> CSTTextSerializerContext {
return CSTTextSerializerContext(self.serialized + a)
}
}
extension CSTTextSerializer: CSTVisitor {
public func visit(
node: CST.BinaryOperatorExpression, driver: CSTVisitorDriver,
context: CSTTextSerializerContext
) -> Common.Result {
return .Ok(context.append("Binary Operator Expression"))
}
public func visit(
node: CST.Literal, driver: CSTVisitorDriver,
context: CSTTextSerializerContext
) -> Common.Result {
return .Ok(context.append("Literal Expression"))
}
public func visit(
node: CST.Identifier, driver: CSTVisitorDriver,
context: CSTTextSerializerContext
) -> Common.Result {
return .Ok(context.append("Identifier Expression"))
}
public func visit(
node: CST.Parser, driver: CSTVisitorDriver, context: CSTTextSerializerContext
) -> Common.Result {
var context = context.append("Identifier Expression")
for s in node.states.states {
switch driver.visit(state: s, visitor: self, context: context) {
case .Ok(let c): context = c
case .Error(let e): return .Error(e)
}
}
return .Ok(context)
}
public func visit(
node: CST.ParserStateDirectTransition, driver: CSTVisitorDriver,
context: CSTTextSerializerContext
) -> Common.Result {
return .Ok(context.append("State: Direct Transition"))
}
public func visit(
node: CST.ParserStateNoTransition, driver: CSTVisitorDriver,
context: CSTTextSerializerContext
) -> Common.Result {
return .Ok(context.append("State: No Transition"))
}
public func visit(
node: CST.ParserStateSelectTransition, driver: CSTVisitorDriver,
context: CSTTextSerializerContext
) -> Common.Result {
return .Ok(context.append("State: Direct Transition"))
}
}