aa12974dd6
As a use case, use it to implement text serialization of the CST. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
91 lines
3.3 KiB
Swift
91 lines
3.3 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 SwiftTreeSitter
|
|
import TreeSitterExtensions
|
|
import TreeSitterP4
|
|
|
|
/*
|
|
public protocol CompilableValue {
|
|
static func CompileValue(withValue value: String) -> Result<P4DataValue>
|
|
}
|
|
*/
|
|
|
|
public protocol MaybeParsableType {
|
|
static func MaybeParseType(
|
|
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
|
) -> Result<CST.Tipe>
|
|
}
|
|
|
|
public protocol ParsableType {
|
|
static func ParseType(
|
|
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
|
) -> Result<CST.Tipe>
|
|
}
|
|
public protocol ParsableExpression {
|
|
static func ParseExpression(
|
|
node: Node, withContext context: CSTCompilerContext
|
|
) -> Result<CST.Categories.Expression>
|
|
}
|
|
|
|
public protocol Parsable<C> {
|
|
associatedtype C
|
|
static func Parse(
|
|
node: Node, withContext context: CSTCompilerContext
|
|
) -> Result<C>
|
|
}
|
|
|
|
public protocol ParsableStatement {
|
|
static func ParseStatement(
|
|
node: Node, withContext context: CSTCompilerContext
|
|
) -> 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>
|
|
|
|
// Parser
|
|
func visit(
|
|
node: CST.ParserStateDirectTransition, driver: CSTVisitorDriver, context: T
|
|
) -> Result<T>
|
|
func visit(node: CST.ParserStateNoTransition, driver: CSTVisitorDriver, context: T) -> Result<T>
|
|
func visit(
|
|
node: CST.ParserStateSelectTransition, driver: CSTVisitorDriver, context: T
|
|
) -> Result<T>
|
|
}
|