Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef6b07b54a | |||
| aa12974dd6 | |||
| d22776b018 | |||
| d7022725ed |
@@ -449,7 +449,7 @@ public enum DeriveParsableStatement: MemberMacro {
|
||||
"""
|
||||
public static func ParseStatement(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.Statement> {
|
||||
) -> Result<CST.Categories.Statement> {
|
||||
return switch Parse(node: node, withContext: context) {
|
||||
case .Ok(let res): .Ok(res)
|
||||
case .Error(let e): .Error(e)
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
+74
-69
@@ -17,31 +17,36 @@
|
||||
|
||||
import Common
|
||||
|
||||
extension P4Value: CST.AnExpression {}
|
||||
extension P4Value: CST.Categories.Expression {}
|
||||
|
||||
public struct CST {
|
||||
|
||||
public protocol AnExpression {}
|
||||
public protocol Statement {}
|
||||
public protocol AnState {}
|
||||
public struct Categories {
|
||||
public protocol LanguageElement {}
|
||||
public protocol Expression: Categories.LanguageElement {}
|
||||
public protocol Statement: Categories.LanguageElement {}
|
||||
public protocol State: Categories.LanguageElement {}
|
||||
public protocol Declaration: Categories.Statement {}
|
||||
}
|
||||
|
||||
struct Expression {}
|
||||
|
||||
public struct Statements {
|
||||
public let statements: [Statement]
|
||||
public struct Statements: Categories.Statement {
|
||||
public let statements: [Categories.Statement]
|
||||
|
||||
public init(_ s: [Statement]) {
|
||||
public init(_ s: [Categories.Statement]) {
|
||||
self.statements = s
|
||||
}
|
||||
}
|
||||
|
||||
public struct VariableDeclarationStatement: Statement {
|
||||
public var initializer: AnExpression?
|
||||
public struct VariableDeclarationStatement: Categories.Statement {
|
||||
public var initializer: Categories.Expression?
|
||||
public var identifier: CST.Identifier
|
||||
public var tipe: CST.Tipe
|
||||
|
||||
public init(
|
||||
identifier: Identifier, withType tipe: CST.Tipe, withInitializer initializer: AnExpression?
|
||||
identifier: Identifier, withType tipe: CST.Tipe,
|
||||
withInitializer initializer: Categories.Expression?
|
||||
) {
|
||||
self.identifier = identifier
|
||||
self.initializer = initializer
|
||||
@@ -49,20 +54,20 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ConditionalStatement: Statement {
|
||||
public var condition: AnExpression
|
||||
public var thenn: Statement
|
||||
public var elss: Statement?
|
||||
public struct ConditionalStatement: Categories.Statement {
|
||||
public var condition: Categories.Expression
|
||||
public var thenn: Categories.Statement
|
||||
public var elss: Categories.Statement?
|
||||
|
||||
public init(condition: AnExpression, withThen thenn: Statement) {
|
||||
public init(condition: Categories.Expression, withThen thenn: Categories.Statement) {
|
||||
self.condition = condition
|
||||
self.thenn = thenn
|
||||
self.elss = .none
|
||||
}
|
||||
|
||||
public init(
|
||||
condition: AnExpression, withThen thenn: Statement,
|
||||
andElse elss: Statement
|
||||
condition: Categories.Expression, withThen thenn: Categories.Statement,
|
||||
andElse elss: Categories.Statement
|
||||
) {
|
||||
self.condition = condition
|
||||
self.thenn = thenn
|
||||
@@ -70,7 +75,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct BlockStatement: Statement {
|
||||
public struct BlockStatement: Categories.Statement {
|
||||
public var statements: Statements
|
||||
|
||||
public init(_ statements: Statements) {
|
||||
@@ -79,15 +84,15 @@ public struct CST {
|
||||
|
||||
}
|
||||
|
||||
public struct ReturnStatement: Statement {
|
||||
public let value: AnExpression
|
||||
public struct ReturnStatement: Categories.Statement {
|
||||
public let value: Categories.Expression
|
||||
|
||||
public init(_ value: AnExpression) {
|
||||
public init(_ value: Categories.Expression) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
public struct ApplyStatement: Statement {
|
||||
public struct ApplyStatement: Categories.Statement {
|
||||
public let body: CST.BlockStatement?
|
||||
|
||||
public init() { self.body = .none }
|
||||
@@ -223,11 +228,9 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public protocol AnDeclaration: Statement {}
|
||||
|
||||
public struct Declaration {}
|
||||
|
||||
public struct Control: CustomStringConvertible, AnDeclaration {
|
||||
public struct Control: CustomStringConvertible, Categories.Declaration {
|
||||
public var description: String {
|
||||
return "Control named \(self._name) \(self.parameters) \(self.actions) \(self.table)"
|
||||
}
|
||||
@@ -272,15 +275,15 @@ public struct CST {
|
||||
|
||||
}
|
||||
|
||||
public struct ExternDeclaration: AnDeclaration {
|
||||
public let declaration: CST.AnDeclaration
|
||||
public struct ExternDeclaration: Categories.Declaration {
|
||||
public let declaration: CST.Categories.Declaration
|
||||
|
||||
public init(_ declaration: CST.AnDeclaration) {
|
||||
public init(_ declaration: CST.Categories.Declaration) {
|
||||
self.declaration = declaration
|
||||
}
|
||||
}
|
||||
|
||||
public struct FunctionDeclaration: AnDeclaration {
|
||||
public struct FunctionDeclaration: Categories.Declaration {
|
||||
public var description: String {
|
||||
return "Function named \(self.name) that returns \(self.tipe) with parameters \(self.params)"
|
||||
}
|
||||
@@ -302,7 +305,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct StructDeclaration: AnDeclaration {
|
||||
public struct StructDeclaration: Categories.Declaration {
|
||||
public let fields: [CST.VariableDeclarationStatement]
|
||||
public let identifier: CST.Identifier
|
||||
public init(_ id: CST.Identifier, _ fields: [CST.VariableDeclarationStatement]) {
|
||||
@@ -311,7 +314,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct Instantiation: Statement {
|
||||
public struct Instantiation: Categories.Statement {
|
||||
public let name: CST.Identifier
|
||||
public var tipe: CST.Identifier
|
||||
public let arguments: CST.ArgumentList
|
||||
@@ -326,34 +329,34 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ExpressionStatement: Statement {
|
||||
public let expression: AnExpression
|
||||
public struct ExpressionStatement: Categories.Statement {
|
||||
public let expression: Categories.Expression
|
||||
|
||||
public init(_ expr: AnExpression) {
|
||||
public init(_ expr: Categories.Expression) {
|
||||
self.expression = expr
|
||||
}
|
||||
}
|
||||
|
||||
public struct Identifier: AnExpression {
|
||||
public struct Identifier: Categories.Expression {
|
||||
public let id: Common.Identifier
|
||||
public init(_ id: Common.Identifier) {
|
||||
self.id = id
|
||||
}
|
||||
}
|
||||
|
||||
public struct Literal: AnExpression {
|
||||
public struct Literal: Categories.Expression {
|
||||
public let literal: P4Value
|
||||
public init(_ literal: P4Value) {
|
||||
self.literal = literal
|
||||
}
|
||||
}
|
||||
|
||||
public enum KeysetExpression: AnExpression {
|
||||
public enum KeysetExpression: Categories.Expression {
|
||||
case Default
|
||||
case Value(AnExpression)
|
||||
case Value(Categories.Expression)
|
||||
}
|
||||
|
||||
public struct SelectCaseExpression: AnExpression {
|
||||
public struct SelectCaseExpression: Categories.Expression {
|
||||
public let key: CST.KeysetExpression
|
||||
public let next_state_identifier: CST.Identifier
|
||||
|
||||
@@ -363,12 +366,12 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct SelectExpression: AnExpression {
|
||||
public let selector: AnExpression
|
||||
public struct SelectExpression: Categories.Expression {
|
||||
public let selector: Categories.Expression
|
||||
public let case_expressions: [CST.SelectCaseExpression]
|
||||
|
||||
public init(
|
||||
withSelector selector: AnExpression,
|
||||
withSelector selector: Categories.Expression,
|
||||
withSelectCaseExpressions sces: [CST.SelectCaseExpression]
|
||||
) {
|
||||
self.selector = selector
|
||||
@@ -397,15 +400,15 @@ public struct CST {
|
||||
case Or
|
||||
}
|
||||
|
||||
public struct BinaryOperatorExpression: AnExpression {
|
||||
public let left: AnExpression
|
||||
public let right: AnExpression
|
||||
public struct BinaryOperatorExpression: Categories.Expression {
|
||||
public let left: Categories.Expression
|
||||
public let right: Categories.Expression
|
||||
public let type: BinaryOperatorExpressionType
|
||||
|
||||
public init(
|
||||
withType tipe: BinaryOperatorExpressionType,
|
||||
withLhs lhs: AnExpression,
|
||||
withRhs rhs: AnExpression
|
||||
withLhs lhs: Categories.Expression,
|
||||
withRhs rhs: Categories.Expression
|
||||
) {
|
||||
self.type = tipe
|
||||
self.left = lhs
|
||||
@@ -413,30 +416,30 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public struct ArrayAccessExpression: AnExpression {
|
||||
public let indexor: AnExpression
|
||||
public let name: AnExpression
|
||||
public struct ArrayAccessExpression: Categories.Expression {
|
||||
public let indexor: Categories.Expression
|
||||
public let name: Categories.Expression
|
||||
|
||||
public init(
|
||||
withName name: AnExpression,
|
||||
withIndexor indexor: AnExpression
|
||||
withName name: Categories.Expression,
|
||||
withIndexor indexor: Categories.Expression
|
||||
) {
|
||||
self.name = name
|
||||
self.indexor = indexor
|
||||
}
|
||||
}
|
||||
|
||||
public struct FieldAccessExpression: AnExpression {
|
||||
public struct FieldAccessExpression: Categories.Expression {
|
||||
public let field: Identifier
|
||||
public let strct: AnExpression
|
||||
public let strct: Categories.Expression
|
||||
|
||||
public init(withStruct strct: AnExpression, withField field: Identifier) {
|
||||
public init(withStruct strct: Categories.Expression, withField field: Identifier) {
|
||||
self.strct = strct
|
||||
self.field = field
|
||||
}
|
||||
}
|
||||
|
||||
public struct FunctionCall: AnExpression {
|
||||
public struct FunctionCall: Categories.Expression {
|
||||
public let callee: Identifier
|
||||
public let arguments: ArgumentList
|
||||
|
||||
@@ -450,12 +453,12 @@ public struct CST {
|
||||
|
||||
public struct LocalElement {}
|
||||
|
||||
public struct ParserAssignmentStatement: Statement {
|
||||
public let lvalue: AnExpression
|
||||
public let value: AnExpression
|
||||
public struct ParserAssignmentStatement: Categories.Statement {
|
||||
public let lvalue: Categories.Expression
|
||||
public let value: Categories.Expression
|
||||
|
||||
public init(
|
||||
withLValue lvalue: AnExpression, withValue value: AnExpression
|
||||
withLValue lvalue: Categories.Expression, withValue value: Categories.Expression
|
||||
) {
|
||||
self.lvalue = lvalue
|
||||
self.value = value
|
||||
@@ -491,7 +494,7 @@ public struct CST {
|
||||
/// Only defined to define Compilable extension.
|
||||
public struct TransitionStatement {}
|
||||
|
||||
public class ParserStateDirectTransition: ParserState, AnState {
|
||||
public class ParserStateDirectTransition: ParserState, Categories.State {
|
||||
public let next_state_identifier: Identifier?
|
||||
|
||||
public init(
|
||||
@@ -503,7 +506,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateNoTransition: ParserState, AnState {
|
||||
public class ParserStateNoTransition: ParserState, Categories.State {
|
||||
/// Construct a ParserState
|
||||
public init(
|
||||
name: Identifier, withStatements stmts: CST.Statements? = .none
|
||||
@@ -512,7 +515,7 @@ public struct CST {
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserStateSelectTransition: ParserState, AnState {
|
||||
public class ParserStateSelectTransition: ParserState, Categories.State {
|
||||
|
||||
public let te: SelectExpression
|
||||
|
||||
@@ -526,17 +529,17 @@ public struct CST {
|
||||
}
|
||||
|
||||
public struct ParserStates {
|
||||
public var states: [AnState] = Array()
|
||||
public var states: [Categories.State] = Array()
|
||||
|
||||
public func count() -> Int {
|
||||
return states.count
|
||||
}
|
||||
|
||||
public init(_ states: [AnState] = Array()) {
|
||||
public init(_ states: [Categories.State] = Array()) {
|
||||
self.states = states
|
||||
}
|
||||
|
||||
public func append(state: AnState) -> ParserStates {
|
||||
public func append(state: Categories.State) -> ParserStates {
|
||||
var new_states = self.states
|
||||
new_states.append(state)
|
||||
return ParserStates(new_states)
|
||||
@@ -546,7 +549,7 @@ public struct CST {
|
||||
/// A P4 Parser
|
||||
///
|
||||
/// Note: A Parser is a type
|
||||
public struct Parser: AnDeclaration {
|
||||
public struct Parser: Categories.Declaration {
|
||||
public var states: ParserStates
|
||||
|
||||
public var name: Identifier
|
||||
@@ -632,9 +635,9 @@ public struct CST {
|
||||
|
||||
public struct Argument {
|
||||
public let index: Int
|
||||
public let argument: CST.AnExpression
|
||||
public let argument: Categories.Expression
|
||||
|
||||
public init(_ argument: CST.AnExpression, atIndex index: Int) {
|
||||
public init(_ argument: Categories.Expression, atIndex index: Int) {
|
||||
self.argument = argument
|
||||
self.index = index
|
||||
}
|
||||
@@ -646,6 +649,8 @@ public struct CST {
|
||||
self.statements = stmts
|
||||
}
|
||||
}
|
||||
|
||||
public struct Statement {}
|
||||
}
|
||||
|
||||
public struct CSTCompilerContext {
|
||||
|
||||
@@ -21,10 +21,10 @@ import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension CST.Declaration: Parsable {
|
||||
public typealias C = CST.AnDeclaration
|
||||
public typealias C = CST.Categories.Declaration
|
||||
public static func Parse(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnDeclaration> {
|
||||
) -> Result<CST.Categories.Declaration> {
|
||||
|
||||
// Be kind to our user -- if we are at a declaration node, dive into it!
|
||||
let declaration_node =
|
||||
@@ -34,7 +34,7 @@ extension CST.Declaration: Parsable {
|
||||
node
|
||||
}
|
||||
|
||||
let declaration_compilers: [String: any Parsable<CST.AnDeclaration>.Type] = [
|
||||
let declaration_compilers: [String: any Parsable<CST.Categories.Declaration>.Type] = [
|
||||
"function_declaration": CST.FunctionDeclaration.self,
|
||||
"control_declaration": CST.Control.self,
|
||||
//"type_declaration": AST.P4Struct.self,
|
||||
@@ -61,12 +61,12 @@ extension CST.Declaration: Parsable {
|
||||
extension CST.Declaration: ParsableStatement {}
|
||||
|
||||
extension CST.FunctionDeclaration: Parsable {
|
||||
public typealias C = CST.AnDeclaration
|
||||
public typealias C = CST.Categories.Declaration
|
||||
public static func Parse(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Common.Result<CST.AnDeclaration> {
|
||||
) -> Common.Result<CST.Categories.Declaration> {
|
||||
let function_declaration_node = node
|
||||
#RequireNodeType<Node, CST.AnDeclaration>(
|
||||
#RequireNodeType<Node, CST.Categories.Declaration>(
|
||||
node: function_declaration_node, type: "function_declaration",
|
||||
nice_type_name: "Function Declaration")
|
||||
|
||||
@@ -75,7 +75,7 @@ extension CST.FunctionDeclaration: Parsable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -88,7 +88,7 @@ extension CST.FunctionDeclaration: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -102,7 +102,7 @@ extension CST.FunctionDeclaration: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: function_declaration_node.toSourceLocation(),
|
||||
withError: "Missing function declaration component")))
|
||||
@@ -227,19 +227,19 @@ extension CST.StructDeclaration: Parsable {
|
||||
}
|
||||
|
||||
extension CST.Parser: Parsable {
|
||||
public typealias C = CST.AnDeclaration
|
||||
public typealias C = CST.Categories.Declaration
|
||||
public static func Parse(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnDeclaration> {
|
||||
) -> Result<CST.Categories.Declaration> {
|
||||
let parser_node = node
|
||||
#RequireNodeType<Node, Result<CST.AnDeclaration>>(
|
||||
#RequireNodeType<Node, Result<CST.Categories.Declaration>>(
|
||||
node: parser_node, type: "parserDeclaration", nice_type_name: "parser declaration")
|
||||
var walker = Walker(node: parser_node)
|
||||
var current_node: Node? = .none
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing elements of parser declaration")))
|
||||
@@ -263,7 +263,7 @@ extension CST.Parser: Parsable {
|
||||
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing elements of parser type in parser declaration")))
|
||||
@@ -279,7 +279,7 @@ extension CST.Parser: Parsable {
|
||||
type_node_walker.next()
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: type_node_child!.toSourceLocation(),
|
||||
withError: "Missing name in parser type declaration")))
|
||||
@@ -293,7 +293,7 @@ extension CST.Parser: Parsable {
|
||||
type_node_walker.next()
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: type_node_child!.toSourceLocation(),
|
||||
withError: "Missing parser parameters")))
|
||||
@@ -309,7 +309,7 @@ extension CST.Parser: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing parser declaration component")))
|
||||
@@ -317,7 +317,7 @@ extension CST.Parser: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing elements of parser declaration")))
|
||||
@@ -332,7 +332,7 @@ extension CST.Parser: Parsable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: parser_node.toSourceLocation(),
|
||||
withError: "Missing body of parser declaration")))
|
||||
@@ -379,12 +379,12 @@ extension CST.Parser: Parsable {
|
||||
}
|
||||
|
||||
extension CST.Control: Parsable {
|
||||
public typealias C = CST.AnDeclaration
|
||||
public typealias C = CST.Categories.Declaration
|
||||
public static func Parse(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Common.Result<CST.AnDeclaration> {
|
||||
) -> Common.Result<CST.Categories.Declaration> {
|
||||
|
||||
#RequireNodeType<Node, Result<CST.AnDeclaration>>(
|
||||
#RequireNodeType<Node, Result<CST.Categories.Declaration>>(
|
||||
node: node, type: "control_declaration", nice_type_name: "control declaration")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
@@ -393,7 +393,7 @@ extension CST.Control: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing control declaration component")))
|
||||
@@ -409,7 +409,7 @@ extension CST.Control: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing control declaration component")))
|
||||
@@ -426,7 +426,7 @@ extension CST.Control: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnDeclaration>.Error(
|
||||
or: Result<CST.Categories.Declaration>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing control declaration component")))
|
||||
@@ -873,10 +873,10 @@ extension CST.Table: Parsable {
|
||||
}
|
||||
|
||||
extension CST.ExternDeclaration: Parsable {
|
||||
public typealias C = CST.AnDeclaration
|
||||
public typealias C = CST.Categories.Declaration
|
||||
public static func Parse(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnDeclaration> {
|
||||
) -> Result<CST.Categories.Declaration> {
|
||||
let extern_declaration_node = node
|
||||
#RequireNodeType<Node, CST.ExternDeclaration>(
|
||||
node: extern_declaration_node, type: "extern_declaration",
|
||||
@@ -1223,12 +1223,12 @@ extension CST.ArgumentList: Parsable {
|
||||
}
|
||||
|
||||
extension CST.Argument: Parsable {
|
||||
public typealias C = CST.AnExpression
|
||||
public typealias C = CST.Categories.Expression
|
||||
public static func Parse(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Common.Result<CST.AnExpression> {
|
||||
) -> Common.Result<CST.Categories.Expression> {
|
||||
let argument_node = node
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: argument_node, type: "argument", nice_type_name: "argument")
|
||||
|
||||
let expression_node = node.child(at: 0)!
|
||||
|
||||
@@ -22,9 +22,9 @@ import TreeSitterP4
|
||||
extension CST.Identifier: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: node, type: "identifier", nice_type_name: "Identifier")
|
||||
|
||||
/// TODO: If there is a value here, then we can make this a compile-time constant!
|
||||
@@ -35,9 +35,9 @@ extension CST.Identifier: ParsableExpression {
|
||||
extension P4BooleanValue: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let node = node.child(at: 0)!
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: node, type: "booleanLiteralExpression", nice_type_name: "Boolean Literal Expression")
|
||||
|
||||
if node.text == "false" {
|
||||
@@ -56,10 +56,10 @@ extension P4BooleanValue: ParsableExpression {
|
||||
extension P4IntValue: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let node = node.child(at: 0)!
|
||||
|
||||
#RequireNodesType<Node, CST.AnExpression>(
|
||||
#RequireNodesType<Node, CST.Categories.Expression>(
|
||||
nodes: node, type: ["integer", "integer_elaborated"],
|
||||
nice_type_names: ["Integer", "Elaborated Integer"])
|
||||
|
||||
@@ -106,9 +106,9 @@ extension P4IntValue: ParsableExpression {
|
||||
extension P4StringValue: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext scopes: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let node = node.child(at: 0)!
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: node, type: "string_literal", nice_type_name: "String Literal")
|
||||
return .Ok(CST.Literal(P4Value(P4StringValue(withValue: node.text!))))
|
||||
}
|
||||
@@ -117,12 +117,12 @@ extension P4StringValue: ParsableExpression {
|
||||
extension CST.Expression: Parsable {
|
||||
public static func Parse(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: node, type: "expression", nice_type_name: "expression")
|
||||
|
||||
let expression_node = node.child(at: 0)!
|
||||
#RequireNodesType<Node, CST.AnExpression>(
|
||||
#RequireNodesType<Node, CST.Categories.Expression>(
|
||||
nodes: expression_node, type: ["grouped_expression", "simple_expression"],
|
||||
nice_type_names: ["grouped expression", "simple expression"])
|
||||
|
||||
@@ -154,10 +154,10 @@ extension CST.Expression: Parsable {
|
||||
extension CST.KeysetExpression: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let keyset_expression_node = node.child(at: 0)!
|
||||
|
||||
#RequireNodesType<Node, CST.AnExpression>(
|
||||
#RequireNodesType<Node, CST.Categories.Expression>(
|
||||
nodes: keyset_expression_node, type: ["expression", "default_keyset"],
|
||||
nice_type_names: ["expression", "default keyset"])
|
||||
|
||||
@@ -180,7 +180,7 @@ extension CST.KeysetExpression: ParsableExpression {
|
||||
extension CST.SelectExpression: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
#RequireNodeType<Node, (CST.SelectExpression, CSTCompilerContext)>(
|
||||
node: node, type: "selectExpression", nice_type_name: "parser select expression")
|
||||
|
||||
@@ -242,9 +242,9 @@ extension CST.SelectExpression: ParsableExpression {
|
||||
extension CST.SelectCaseExpression: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: node, type: "selectCase", nice_type_name: "Select Case")
|
||||
|
||||
guard let keysetexpression_node = node.child(at: 0),
|
||||
@@ -283,10 +283,10 @@ extension CST.SelectCaseExpression: ParsableExpression {
|
||||
extension CST.BinaryOperatorExpression: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let expression = node.child(at: 0)!
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: expression, type: "binaryOperatorExpression",
|
||||
nice_type_name: "Binary Operator Expression")
|
||||
let binary_operator_expression_node = expression.child(at: 0)!
|
||||
@@ -295,20 +295,20 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
|
||||
var current_node: Node? = .none
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Malformed binary operator expression"
|
||||
)))
|
||||
|
||||
/// TODO: This macro cannot handle new lines in the arrays
|
||||
// swift-format-ignore
|
||||
#RequireNodesType<Node, CST.AnExpression>(
|
||||
#RequireNodesType<Node, CST.Categories.Expression>(
|
||||
nodes: binary_operator_expression_node,
|
||||
type: ["binaryEqualOperatorExpression", "binaryLessThanOperatorExpression", "binaryLessThanEqualOperatorExpression", "binaryGreaterThanOperatorExpression", "binaryGreaterThanEqualOperatorExpression", "binaryAndOperatorExpression", "binaryOrOperatorExpression", "binaryAddOperatorExpression", "binarySubtractOperatorExpression", "binaryMultiplyOperatorExpression", "binaryDivideOperatorExpression"],
|
||||
nice_type_names: [ "binary equal operator", "binary less than operator", "binary less than or equal to operator", "binary greater than operator", "binary greater than or equal to operator", "binary and operator", "binary or operator", "binary add operator", "binary subtract operator", "binary multiply operator", "binary divide operator"])
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing LHS for binary operator expression")))
|
||||
@@ -318,7 +318,7 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing binary operator for binary operator expression")))
|
||||
@@ -326,7 +326,7 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing RHS for binary operator expression")))
|
||||
@@ -407,10 +407,10 @@ extension CST.BinaryOperatorExpression: ParsableExpression {
|
||||
extension CST.ArrayAccessExpression: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let expression = node.child(at: 0)!
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: expression, type: "arrayAccessExpression", nice_type_name: "Array Access Expression")
|
||||
let array_access_expression_node = expression
|
||||
|
||||
@@ -419,11 +419,11 @@ extension CST.ArrayAccessExpression: ParsableExpression {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Malformed array access expression")))
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: current_node!, type: "expression",
|
||||
nice_type_name: "array identifier expression")
|
||||
let array_access_identifier_node = current_node!
|
||||
@@ -431,7 +431,7 @@ extension CST.ArrayAccessExpression: ParsableExpression {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing [ for array access expression")))
|
||||
@@ -440,12 +440,12 @@ extension CST.ArrayAccessExpression: ParsableExpression {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing indexor expression for array access expression")))
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: current_node!, type: "expression",
|
||||
nice_type_name: "array indexor expression")
|
||||
|
||||
@@ -472,10 +472,10 @@ extension CST.ArrayAccessExpression: ParsableExpression {
|
||||
extension CST.FieldAccessExpression: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
let expression = node.child(at: 0)!
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: expression, type: "fieldAccessExpression", nice_type_name: "Array Access Expression")
|
||||
|
||||
let field_access_expression_node = expression
|
||||
@@ -485,11 +485,11 @@ extension CST.FieldAccessExpression: ParsableExpression {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Malformed field access expression")))
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: current_node!, type: "expression",
|
||||
nice_type_name: "struct identifier expression")
|
||||
let struct_identifier_node = current_node!
|
||||
@@ -497,7 +497,7 @@ extension CST.FieldAccessExpression: ParsableExpression {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing . for field access expression")))
|
||||
@@ -505,12 +505,12 @@ extension CST.FieldAccessExpression: ParsableExpression {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing field name for field access expression")))
|
||||
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: current_node!, type: "identifier",
|
||||
nice_type_name: "field name")
|
||||
|
||||
@@ -539,10 +539,10 @@ extension CST.FieldAccessExpression: ParsableExpression {
|
||||
extension CST.FunctionCall: ParsableExpression {
|
||||
public static func ParseExpression(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression> {
|
||||
) -> Result<CST.Categories.Expression> {
|
||||
|
||||
let expression = node.child(at: 0)!
|
||||
#RequireNodeType<Node, CST.AnExpression>(
|
||||
#RequireNodeType<Node, CST.Categories.Expression>(
|
||||
node: expression, type: "function_call", nice_type_name: "Function Call")
|
||||
|
||||
var walker = Walker(node: expression)
|
||||
@@ -550,7 +550,7 @@ extension CST.FunctionCall: ParsableExpression {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
|
||||
|
||||
@@ -563,7 +563,7 @@ extension CST.FunctionCall: ParsableExpression {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnExpression>.Error(
|
||||
or: Result<CST.Categories.Expression>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing function call component")))
|
||||
|
||||
|
||||
@@ -21,10 +21,10 @@ import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension CST.LocalElements: Parsable {
|
||||
public typealias C = CST.Statement
|
||||
public typealias C = CST.Categories.Statement
|
||||
public static func Parse(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.Statement> {
|
||||
) -> Result<CST.Categories.Statement> {
|
||||
let localElementsParsers: [String: ParsableStatement.Type] = [
|
||||
"variableDeclaration": CST.VariableDeclarationStatement.self
|
||||
]
|
||||
@@ -46,10 +46,10 @@ extension CST.LocalElements: Parsable {
|
||||
}
|
||||
|
||||
extension CST.ParserState: Parsable {
|
||||
public typealias C = CST.AnState
|
||||
public typealias C = CST.Categories.State
|
||||
public static func Parse(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnState> {
|
||||
) -> Result<CST.Categories.State> {
|
||||
var walker = Walker(node: node)
|
||||
|
||||
var current_node: Node? = .none
|
||||
@@ -65,7 +65,7 @@ extension CST.ParserState: Parsable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnState>.Error(
|
||||
or: Result<CST.Categories.State>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing elements in parser state declaration")))
|
||||
@@ -83,7 +83,7 @@ extension CST.ParserState: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnState>.Error(
|
||||
or: Result<CST.Categories.State>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing elements in parser state declaration")))
|
||||
@@ -99,7 +99,7 @@ extension CST.ParserState: Parsable {
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnState>.Error(
|
||||
or: Result<CST.Categories.State>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing body of state declaration")
|
||||
))
|
||||
@@ -130,7 +130,7 @@ extension CST.ParserState: Parsable {
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<CST.AnState>.Error(
|
||||
or: Result<CST.Categories.State>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Missing transition statement of state declaration")))
|
||||
|
||||
@@ -24,7 +24,7 @@ extension CST.Program: Parsable {
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Common.Result<CST.Program> {
|
||||
|
||||
var statements: [CST.Statement] = Array()
|
||||
var statements: [CST.Categories.Statement] = Array()
|
||||
|
||||
var errors: (any Errorable)? = .none
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
// 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 SwiftTreeSitter
|
||||
import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
public struct SpecialParsers {
|
||||
public struct Statement {}
|
||||
}
|
||||
|
||||
extension SpecialParsers.Statement: Parsable {
|
||||
public typealias C = CST.Statement
|
||||
public static func Parse(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.Statement> {
|
||||
|
||||
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
|
||||
return Result.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement")
|
||||
)
|
||||
}
|
||||
|
||||
let statement = node.child(at: 0)!
|
||||
|
||||
let statementParsers: [String: ParsableStatement.Type] = [
|
||||
"assignmentStatement": CST.ParserAssignmentStatement.self,
|
||||
"expressionStatement": CST.ExpressionStatement.self,
|
||||
"variableDeclaration": CST.VariableDeclarationStatement.self,
|
||||
"conditionalStatement": CST.ConditionalStatement.self,
|
||||
"blockStatement": CST.BlockStatement.self,
|
||||
"return_statement": CST.ReturnStatement.self,
|
||||
]
|
||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: statement.toSourceLocation(),
|
||||
withError:
|
||||
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
switch parser.ParseStatement(node: statement, withContext: context) {
|
||||
case Result.Ok(let parsed):
|
||||
return .Ok(parsed)
|
||||
case Result.Error(let e):
|
||||
return .Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Failed to parse a statement element: \(e)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,7 +132,7 @@ extension CST.ConditionalStatement: Parsable {
|
||||
}
|
||||
|
||||
guard
|
||||
case .Ok(let thenns) = SpecialParsers.Statement.Parse(
|
||||
case .Ok(let thenns) = CST.Statement.Parse(
|
||||
node: thens, withContext: context)
|
||||
else {
|
||||
return Result.Error(
|
||||
@@ -141,10 +141,10 @@ extension CST.ConditionalStatement: Parsable {
|
||||
"Could not parse the then block in a conditional statement"))
|
||||
}
|
||||
|
||||
let optional_elss: Result<CST.Statement>? =
|
||||
let optional_elss: Result<CST.Categories.Statement>? =
|
||||
if let elss = node.child(at: 6) {
|
||||
.some(
|
||||
SpecialParsers.Statement.Parse(
|
||||
CST.Statement.Parse(
|
||||
node: elss, withContext: context))
|
||||
} else {
|
||||
.none
|
||||
@@ -215,7 +215,7 @@ extension CST.VariableDeclarationStatement: Parsable {
|
||||
Error(withMessage: "Could not parse a P4 type from \(typeref.text!)"))
|
||||
}
|
||||
|
||||
var initializer: CST.AnExpression? = .none
|
||||
var initializer: CST.Categories.Expression? = .none
|
||||
|
||||
// If there is an initializer, it must be an expression.
|
||||
if let initializer_expression = maybe_rvalue {
|
||||
@@ -431,10 +431,10 @@ extension CST.Statements: Parsable {
|
||||
}
|
||||
|
||||
var errors: (any Errorable)? = .none
|
||||
var parsed_s: [CST.Statement] = Array()
|
||||
var parsed_s: [CST.Categories.Statement] = Array()
|
||||
|
||||
node.enumerateNamedChildren { node in
|
||||
switch SpecialParsers.Statement.Parse(
|
||||
switch CST.Statement.Parse(
|
||||
node: node, withContext: context)
|
||||
{
|
||||
case .Ok(let parsed_statement):
|
||||
@@ -460,7 +460,7 @@ extension CST.Statements: Parsable {
|
||||
extension CST.TransitionStatement: Parsable {
|
||||
public static func Parse(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnState> {
|
||||
) -> Result<CST.Categories.State> {
|
||||
|
||||
guard let state_identifier = context.lexical_context_name else {
|
||||
return .Error(
|
||||
@@ -522,3 +522,45 @@ extension CST.TransitionStatement: Parsable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CST.Statement: Parsable {
|
||||
public typealias C = CST.Categories.Statement
|
||||
public static func Parse(
|
||||
node: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.Categories.Statement> {
|
||||
|
||||
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
|
||||
return Result.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(), withError: "Missing expected parser statement")
|
||||
)
|
||||
}
|
||||
|
||||
let statement = node.child(at: 0)!
|
||||
|
||||
let statementParsers: [String: ParsableStatement.Type] = [
|
||||
"assignmentStatement": CST.ParserAssignmentStatement.self,
|
||||
"expressionStatement": CST.ExpressionStatement.self,
|
||||
"variableDeclaration": CST.VariableDeclarationStatement.self,
|
||||
"conditionalStatement": CST.ConditionalStatement.self,
|
||||
"blockStatement": CST.BlockStatement.self,
|
||||
"return_statement": CST.ReturnStatement.self,
|
||||
]
|
||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: statement.toSourceLocation(),
|
||||
withError:
|
||||
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
switch parser.ParseStatement(node: statement, withContext: context) {
|
||||
case Result.Ok(let parsed):
|
||||
return .Ok(parsed)
|
||||
case Result.Error(let e):
|
||||
return .Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: node.toSourceLocation(),
|
||||
withError: "Failed to parse a statement element: \(e)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,37 +23,31 @@ import TreeSitterP4
|
||||
extension P4Boolean: MaybeParsableType {
|
||||
public static func MaybeParseType(
|
||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
||||
) -> Common.Result<(CST.Tipe)?> {
|
||||
return type.text == "bool" ? .Ok(CST.Tipe(P4QualifiedType(P4Boolean()))) : .Ok(.none)
|
||||
) -> Result<(CST.Tipe)> {
|
||||
return type.nodeType == "bool"
|
||||
? .Ok(CST.Tipe(P4QualifiedType(P4Boolean())))
|
||||
: .Error(Error(withMessage: "Invalid parser selected for \(type.nodeType!)"))
|
||||
}
|
||||
}
|
||||
|
||||
extension P4Int: MaybeParsableType {
|
||||
public static func MaybeParseType(
|
||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
||||
) -> Common.Result<(CST.Tipe)?> {
|
||||
) -> Result<(CST.Tipe)> {
|
||||
#RequireNodeType<Node, CST.Tipe>(node: type, type: "int_type", nice_type_name: "Integer")
|
||||
|
||||
// Drill down, as appropriate.
|
||||
let base_type_node = type.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node>(
|
||||
node: base_type_node, type: "baseType")
|
||||
|
||||
let type_node = base_type_node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node>(
|
||||
node: type_node, type: "int_type")
|
||||
|
||||
var walker = Walker(node: type_node)
|
||||
var walker = Walker(node: type)
|
||||
|
||||
var int_node: Node? = .none
|
||||
|
||||
#MustOr(
|
||||
result: int_node, thing: walker.getNext(),
|
||||
or: Result<CST.Tipe?>.Error(
|
||||
or: Result<CST.Tipe>.Error(
|
||||
ErrorWithLocation(
|
||||
sourceLocation: type_node.toSourceLocation(),
|
||||
sourceLocation: type.toSourceLocation(),
|
||||
withError: "Missing elements in int type declaration")))
|
||||
|
||||
// Move passed the keyword.
|
||||
// Move passed the keyword -- now see whether there is a width
|
||||
walker.next()
|
||||
|
||||
if let bit_width_node = walker.getNext() {
|
||||
@@ -74,8 +68,10 @@ extension P4Int: MaybeParsableType {
|
||||
extension P4String: MaybeParsableType {
|
||||
public static func MaybeParseType(
|
||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
||||
) -> Common.Result<(CST.Tipe)?> {
|
||||
return type.text == "string" ? .Ok(CST.Tipe(P4QualifiedType(P4String()))) : .Ok(.none)
|
||||
) -> Result<(CST.Tipe)> {
|
||||
return type.nodeType == "string"
|
||||
? .Ok(CST.Tipe(P4QualifiedType(P4String())))
|
||||
: .Error(Error(withMessage: "Invalid parser selected for \(type.nodeType!)"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,13 +79,18 @@ extension CST.Types: ParsableType {
|
||||
public static func ParseType(
|
||||
type: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.Tipe> {
|
||||
let type_parsers: [MaybeParsableType.Type] = [
|
||||
P4Boolean.self, P4Int.self, P4String.self, /*P4Struct.self,*/
|
||||
]
|
||||
for type_parser in type_parsers {
|
||||
switch type_parser.MaybeParseType(type: type, withContext: context) {
|
||||
case .Ok(.some(let type)): return .Ok(type)
|
||||
case .Ok(.none): continue
|
||||
#RequireNodeType<Node, CST.Tipe>(node: type, type: "typeRef", nice_type_name: "Type Reference")
|
||||
let type = type.child(at: 0)!
|
||||
if type.nodeType == "baseType" {
|
||||
let type = type.child(at: 0)!
|
||||
let base_type_parsers: [String: MaybeParsableType.Type] = [
|
||||
"bool": P4Boolean.self, "int_type": P4Int.self, "string": P4String.self,
|
||||
]
|
||||
guard let parser = base_type_parsers[type.nodeType!] else {
|
||||
return Result.Error(Error(withMessage: "No parser for type \(type.nodeType!)"))
|
||||
}
|
||||
switch parser.MaybeParseType(type: type, withContext: context) {
|
||||
case .Ok(let type): return .Ok(type)
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public protocol CompilableValue {
|
||||
public protocol MaybeParsableType {
|
||||
static func MaybeParseType(
|
||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
||||
) -> Result<CST.Tipe?>
|
||||
) -> Result<CST.Tipe>
|
||||
}
|
||||
|
||||
public protocol ParsableType {
|
||||
@@ -40,7 +40,7 @@ public protocol ParsableType {
|
||||
public protocol ParsableExpression {
|
||||
static func ParseExpression(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.AnExpression>
|
||||
) -> Result<CST.Categories.Expression>
|
||||
}
|
||||
|
||||
public protocol Parsable<C> {
|
||||
@@ -53,16 +53,35 @@ public protocol Parsable<C> {
|
||||
public protocol ParsableStatement {
|
||||
static func ParseStatement(
|
||||
node: Node, withContext context: CSTCompilerContext
|
||||
) -> Result<CST.Statement>
|
||||
) -> Result<CST.Categories.Statement>
|
||||
}
|
||||
|
||||
public protocol CSTVisitor<T> {
|
||||
associatedtype T
|
||||
|
||||
// Declarations
|
||||
func visit(node: CST.Control, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.ExternDeclaration, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.FunctionDeclaration, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.StructDeclaration, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.Parser, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
|
||||
// Statements
|
||||
func visit(node: CST.Statements, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(
|
||||
node: CST.VariableDeclarationStatement, driver: CSTVisitorDriver, context: T
|
||||
) -> Result<T>
|
||||
func visit(node: CST.ExpressionStatement, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
|
||||
// Expressions
|
||||
func visit(node: CST.KeysetExpression, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.SelectCaseExpression, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.SelectExpression, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.BinaryOperatorExpression, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.Literal, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
func visit(node: CST.Identifier, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
|
||||
func visit(node: CST.Parser, driver: CSTVisitorDriver, context: T) -> Result<T>
|
||||
// Parser
|
||||
func visit(
|
||||
node: CST.ParserStateDirectTransition, driver: CSTVisitorDriver, context: T
|
||||
) -> Result<T>
|
||||
|
||||
@@ -21,18 +21,57 @@ public struct CSTVisitorDriver {
|
||||
public init() {}
|
||||
|
||||
public func visit<T>(
|
||||
expression: any CST.AnExpression, visitor: any CSTVisitor<T>, context: T
|
||||
_ elem: any CST.Categories.LanguageElement, visitor: any CSTVisitor<T>, context: T
|
||||
) -> Result<T> {
|
||||
return switch expression {
|
||||
case let e as CST.BinaryOperatorExpression:
|
||||
visitor.visit(node: e, driver: self, context: context)
|
||||
case let e as CST.Literal: visitor.visit(node: e, driver: self, context: context)
|
||||
default: .Error(Error(withMessage: "AST Expression Element Is Not Visitable"))
|
||||
return switch elem {
|
||||
case let elem as CST.Categories.Expression:
|
||||
visit(expression: elem, visitor: visitor, context: context)
|
||||
case let elem as CST.Categories.Statement:
|
||||
visit(statement: elem, visitor: visitor, context: context)
|
||||
case let elem as CST.Categories.State: visit(state: elem, visitor: visitor, context: context)
|
||||
case let elem as CST.Categories.Declaration:
|
||||
visit(declaration: elem, visitor: visitor, context: context)
|
||||
default: .Error(Error(withMessage: "AST Language Element (\(elem)) Is Not Visitable"))
|
||||
}
|
||||
}
|
||||
|
||||
public func visit<T>(
|
||||
state: any CST.AnState, visitor: any CSTVisitor<T>, context: T
|
||||
func visit<T>(
|
||||
declaration: any CST.Categories.Declaration, visitor: any CSTVisitor<T>, context: T
|
||||
) -> Result<T> {
|
||||
return switch declaration {
|
||||
case let elem as CST.Control: visitor.visit(node: elem, driver: self, context: context)
|
||||
case let elem as CST.ExternDeclaration:
|
||||
visitor.visit(node: elem, driver: self, context: context)
|
||||
case let elem as CST.FunctionDeclaration:
|
||||
visitor.visit(node: elem, driver: self, context: context)
|
||||
case let elem as CST.StructDeclaration:
|
||||
visitor.visit(node: elem, driver: self, context: context)
|
||||
case let elem as CST.Parser: visitor.visit(node: elem, driver: self, context: context)
|
||||
default: .Error(Error(withMessage: "AST Declaration Element (\(declaration)) Is Not Visitable"))
|
||||
}
|
||||
}
|
||||
|
||||
func visit<T>(
|
||||
expression: any CST.Categories.Expression, visitor: any CSTVisitor<T>, context: T
|
||||
) -> Result<T> {
|
||||
return switch expression {
|
||||
case let s as CST.Identifier:
|
||||
visitor.visit(node: s, driver: self, context: context)
|
||||
case let s as CST.KeysetExpression:
|
||||
visitor.visit(node: s, driver: self, context: context)
|
||||
case let s as CST.SelectCaseExpression:
|
||||
visitor.visit(node: s, driver: self, context: context)
|
||||
case let e as CST.SelectExpression:
|
||||
visitor.visit(node: e, driver: self, context: context)
|
||||
case let e as CST.BinaryOperatorExpression:
|
||||
visitor.visit(node: e, driver: self, context: context)
|
||||
case let e as CST.Literal: visitor.visit(node: e, driver: self, context: context)
|
||||
default: .Error(Error(withMessage: "AST Expression Element (\(expression)) Is Not Visitable"))
|
||||
}
|
||||
}
|
||||
|
||||
func visit<T>(
|
||||
state: any CST.Categories.State, visitor: any CSTVisitor<T>, context: T
|
||||
) -> Result<T> {
|
||||
return switch state {
|
||||
case let s as CST.ParserStateDirectTransition:
|
||||
@@ -45,25 +84,27 @@ public struct CSTVisitorDriver {
|
||||
}
|
||||
}
|
||||
|
||||
public func visit<T>(
|
||||
statement: any CST.Statement, visitor: any CSTVisitor<T>, context: T
|
||||
func visit<T>(
|
||||
statement: any CST.Categories.Statement, visitor: any CSTVisitor<T>, context: T
|
||||
) -> Result<T> {
|
||||
return switch statement {
|
||||
case let s as CST.Statements: visitor.visit(node: s, driver: self, context: context)
|
||||
case let s as CST.ExpressionStatement: visitor.visit(node: s, driver: self, context: context)
|
||||
case let s as CST.VariableDeclarationStatement:
|
||||
visitor.visit(node: s, driver: self, context: context)
|
||||
case let s as CST.Parser: visitor.visit(node: s, driver: self, context: context)
|
||||
default: .Error(Error(withMessage: "AST Statement Element Is Not Visitable"))
|
||||
default: .Error(Error(withMessage: "AST Statement Element (\(statement)) Is Not Visitable"))
|
||||
}
|
||||
}
|
||||
|
||||
public func visit<T>(
|
||||
public func start<T>(
|
||||
program: CST.Program, visitor: any CSTVisitor<T>, context: T
|
||||
) -> Result<T> {
|
||||
|
||||
var context = context
|
||||
for s in program.statements.statements {
|
||||
switch visit(statement: s, visitor: visitor, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
switch visit(statement: program.statements, visitor: visitor, context: context) {
|
||||
case .Ok(let c): context = c
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
|
||||
return .Ok(context)
|
||||
|
||||
@@ -46,5 +46,30 @@ import TreeSitterP4
|
||||
let v = CSTTextSerializer()
|
||||
let c = CSTTextSerializerContext();
|
||||
let vd = CSTVisitorDriver();
|
||||
#expect(#RequireOkResult((vd.visit(program: program, visitor: v, context: c))))
|
||||
let result = try #UseOkResult((vd.start(program: program, visitor: v, context: c)))
|
||||
|
||||
let expected = """
|
||||
Parser Expression
|
||||
State: Direct Transition
|
||||
Statements:
|
||||
Expression Statement:
|
||||
Literal Expression
|
||||
Next State:
|
||||
Identifier: accept
|
||||
State: Select Transition
|
||||
Select Expression:
|
||||
Selector:
|
||||
Literal Expression
|
||||
Case Expressions:
|
||||
Case Expression:
|
||||
Keyset Expression:
|
||||
Next State:
|
||||
Identifier: accept
|
||||
Case Expression:
|
||||
Keyset Expression:
|
||||
Next State:
|
||||
Identifier: reject
|
||||
|
||||
"""
|
||||
#expect(result.serialized == expected)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user