96 lines
3.0 KiB
Swift
96 lines
3.0 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
|
|
import P4Compiler
|
|
|
|
public struct TextSerializer {
|
|
public init() {}
|
|
}
|
|
|
|
public struct TextSerializerContext {
|
|
public let serialized: String
|
|
|
|
public init(_ serialized: String = "") {
|
|
self.serialized = serialized
|
|
}
|
|
|
|
public func append(_ a: String) -> TextSerializerContext {
|
|
return TextSerializerContext(self.serialized + a)
|
|
}
|
|
}
|
|
|
|
extension TextSerializer: ASTVisitor<TextSerializerContext> {
|
|
public func visit(
|
|
node: P4Compiler.AST.BinaryOperatorExpression, driver: P4Compiler.ASTVisitorDriver,
|
|
context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
return .Ok(context.append("Binary Operator Expression"))
|
|
|
|
}
|
|
|
|
public func visit(
|
|
node: P4Compiler.AST.Literal, driver: P4Compiler.ASTVisitorDriver,
|
|
context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
return .Ok(context.append("Literal Expression"))
|
|
}
|
|
|
|
public func visit(
|
|
node: P4Compiler.AST.Identifier, driver: P4Compiler.ASTVisitorDriver,
|
|
context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
return .Ok(context.append("Identifier Expression"))
|
|
}
|
|
|
|
public func visit(
|
|
node: P4Compiler.AST.Parser, driver: P4Compiler.ASTVisitorDriver, context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
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: P4Compiler.AST.ParserStateDirectTransition, driver: P4Compiler.ASTVisitorDriver,
|
|
context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
return .Ok(context.append("State: Direct Transition"))
|
|
}
|
|
|
|
public func visit(
|
|
node: P4Compiler.AST.ParserStateNoTransition, driver: P4Compiler.ASTVisitorDriver,
|
|
context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
return .Ok(context.append("State: No Transition"))
|
|
}
|
|
|
|
public func visit(
|
|
node: P4Compiler.AST.ParserStateSelectTransition, driver: P4Compiler.ASTVisitorDriver,
|
|
context: TextSerializerContext
|
|
) -> Common.Result<TextSerializerContext> {
|
|
return .Ok(context.append("State: Direct Transition"))
|
|
}
|
|
|
|
}
|