compiler: Flesh Out CST Visitor Framework
As a use case, use it to implement text serialization of the CST. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -24,23 +24,154 @@ public struct CSTTextSerializer {
|
||||
|
||||
public struct CSTTextSerializerContext {
|
||||
public let serialized: String
|
||||
public let indents: Int
|
||||
|
||||
public init(_ serialized: String = "") {
|
||||
public init(_ serialized: String = "", _ indents: Int = 0) {
|
||||
self.serialized = serialized
|
||||
self.indents = indents
|
||||
}
|
||||
|
||||
static func produceIndent(_ indent: Int, _ marker: String) -> String {
|
||||
return repeatElement(marker, count: indent).joined()
|
||||
}
|
||||
public func append(_ a: String) -> CSTTextSerializerContext {
|
||||
return CSTTextSerializerContext(self.serialized + a)
|
||||
return CSTTextSerializerContext(
|
||||
self.serialized + Self.produceIndent(self.indents, "\t") + a + "\n", self.indents)
|
||||
}
|
||||
|
||||
public func indent() -> CSTTextSerializerContext {
|
||||
return CSTTextSerializerContext(self.serialized, self.indents + 1)
|
||||
}
|
||||
|
||||
public func unindent() -> CSTTextSerializerContext {
|
||||
return CSTTextSerializerContext(self.serialized, self.indents - 1)
|
||||
}
|
||||
}
|
||||
|
||||
extension CSTTextSerializer: CSTVisitor<CSTTextSerializerContext> {
|
||||
public func visit(
|
||||
node: P4Parser.CST.KeysetExpression, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
var context = context.append("Keyset Expression:").indent()
|
||||
return .Ok(context.unindent())
|
||||
if case CST.KeysetExpression.Value(let x) = node {
|
||||
switch driver.visit(x, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.SelectCaseExpression, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
var context = context.append("Case Expression:").indent()
|
||||
switch driver.visit(node.key, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
context = context.append("Next State:").indent()
|
||||
switch driver.visit(node.next_state_identifier, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
context = context.unindent()
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.SelectExpression, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
var context = context.append("Select Expression:").indent()
|
||||
context = context.append("Selector:").indent()
|
||||
switch driver.visit(node.selector, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
context = context.unindent()
|
||||
context = context.append("Case Expressions:").indent()
|
||||
for ce in node.case_expressions {
|
||||
switch driver.visit(ce, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
context = context.unindent()
|
||||
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.Statements, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
var context = context
|
||||
for s in node.statements {
|
||||
switch driver.visit(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: P4Parser.CST.ExpressionStatement, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
var context = context.append("Expression Statement:").indent()
|
||||
switch driver.visit(node.expression, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.Control, driver: P4Parser.CSTVisitorDriver, context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Control Declaration"))
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.ExternDeclaration, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Extern Declaration"))
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.FunctionDeclaration, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Function Declaration"))
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.StructDeclaration, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Struct Declaration"))
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: P4Parser.CST.VariableDeclarationStatement, driver: P4Parser.CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Variable Declaration Statement"))
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: CST.BinaryOperatorExpression, driver: CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Binary Operator Expression"))
|
||||
|
||||
}
|
||||
|
||||
public func visit(
|
||||
@@ -54,28 +185,44 @@ extension CSTTextSerializer: CSTVisitor<CSTTextSerializerContext> {
|
||||
node: CST.Identifier, driver: CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("Identifier Expression"))
|
||||
return .Ok(context.append("Identifier: \(node.id)"))
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: CST.Parser, driver: CSTVisitorDriver, context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
var context = context.append("Identifier Expression")
|
||||
var context = context.append("Parser Expression")
|
||||
context = context.indent()
|
||||
for s in node.states.states {
|
||||
switch driver.visit(state: s, visitor: self, context: context) {
|
||||
switch driver.visit(s, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
return .Ok(context)
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
|
||||
public func visit(
|
||||
node: CST.ParserStateDirectTransition, driver: CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("State: Direct Transition"))
|
||||
var context = context.append("State: Direct Transition").indent()
|
||||
context = context.append("Statements:")
|
||||
context = context.indent()
|
||||
if let statements = node.statements {
|
||||
switch driver.visit(statements, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
context = context.unindent()
|
||||
context = context.append("Next State:").indent()
|
||||
switch driver.visit(node.next_state_identifier!, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
context = context.unindent()
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
|
||||
public func visit(
|
||||
@@ -89,6 +236,21 @@ extension CSTTextSerializer: CSTVisitor<CSTTextSerializerContext> {
|
||||
node: CST.ParserStateSelectTransition, driver: CSTVisitorDriver,
|
||||
context: CSTTextSerializerContext
|
||||
) -> Common.Result<CSTTextSerializerContext> {
|
||||
return .Ok(context.append("State: Direct Transition"))
|
||||
|
||||
var context = context.append("State: Select Transition").indent()
|
||||
if let statements = node.statements {
|
||||
context = context.indent()
|
||||
switch driver.visit(statements, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
context = context.unindent()
|
||||
}
|
||||
switch driver.visit(node.te, visitor: self, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
|
||||
return .Ok(context.unindent())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user