@@ -62,8 +62,8 @@ extension TypedIdentifier: CompilableLValueExpression {
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: expression, type: "identifier")
|
||||
|
||||
let maybe_parsed_expression = TypedIdentifier.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_typed_identifier) = maybe_parsed_expression else {
|
||||
let maybe_parsed_expression = TypedIdentifier.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_typed_identifier) = maybe_parsed_expression else {
|
||||
return .Error(maybe_parsed_expression.error()!)
|
||||
}
|
||||
|
||||
@@ -525,8 +525,8 @@ extension FieldAccessExpression: CompilableLValueExpression {
|
||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||
node: expression, type: "fieldAccessExpression")
|
||||
|
||||
let maybe_parsed_expression = FieldAccessExpression.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_field_access_expression) = maybe_parsed_expression else {
|
||||
let maybe_parsed_expression = FieldAccessExpression.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_field_access_expression) = maybe_parsed_expression else {
|
||||
return .Error(maybe_parsed_expression.error()!)
|
||||
}
|
||||
|
||||
@@ -544,8 +544,8 @@ extension ArrayAccessExpression: CompilableLValueExpression {
|
||||
#SkipUnlessNodeType<Node, EvaluatableExpression?>(
|
||||
node: expression, type: "arrayAccessExpression")
|
||||
|
||||
let maybe_parsed_expression = ArrayAccessExpression.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_array_access_expression) = maybe_parsed_expression else {
|
||||
let maybe_parsed_expression = ArrayAccessExpression.compile(node: node, withContext: context)
|
||||
guard case .Ok(let maybe_array_access_expression) = maybe_parsed_expression else {
|
||||
return .Error(maybe_parsed_expression.error()!)
|
||||
}
|
||||
|
||||
@@ -553,4 +553,4 @@ extension ArrayAccessExpression: CompilableLValueExpression {
|
||||
|
||||
return Result.Ok(array_access_expression)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import SwiftTreeSitter
|
||||
import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
|
||||
public struct Parser {
|
||||
public struct LocalElements {
|
||||
static func Compile(
|
||||
@@ -165,9 +164,11 @@ public struct Parser {
|
||||
}
|
||||
|
||||
if !parse_errs.isEmpty {
|
||||
return Result.Error(Error(withMessage: parse_errs.map() { err in
|
||||
return String(err.msg)
|
||||
}.joined(separator: ";")))
|
||||
return Result.Error(
|
||||
Error(
|
||||
withMessage: parse_errs.map { err in
|
||||
return String(err.msg)
|
||||
}.joined(separator: ";")))
|
||||
}
|
||||
return Result.Ok((parsed_s, current_context))
|
||||
}
|
||||
@@ -246,9 +247,11 @@ public struct Parser {
|
||||
}
|
||||
|
||||
if !parse_errs.isEmpty {
|
||||
return Result.Error(Error(withMessage: parse_errs.map() { err in
|
||||
return String(err.msg)
|
||||
}.joined(separator: ";")))
|
||||
return Result.Error(
|
||||
Error(
|
||||
withMessage: parse_errs.map { err in
|
||||
return String(err.msg)
|
||||
}.joined(separator: ";")))
|
||||
}
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
|
||||
@@ -27,12 +27,15 @@ public struct Program {
|
||||
return Program.Compile(source, withGlobalInstances: .none, withGlobalTypes: .none)
|
||||
}
|
||||
|
||||
public static func Compile(_ source: String, withGlobalInstances globalInstances: VarTypeScopes) -> Result<P4Lang.Program> {
|
||||
public static func Compile(
|
||||
_ source: String, withGlobalInstances globalInstances: VarTypeScopes
|
||||
) -> Result<P4Lang.Program> {
|
||||
return Program.Compile(source, withGlobalInstances: globalInstances, withGlobalTypes: .none)
|
||||
}
|
||||
|
||||
public static func Compile(
|
||||
_ source: String, withGlobalInstances globalInstances: VarTypeScopes?, withGlobalTypes globalTypes: TypeTypeScopes?
|
||||
_ source: String, withGlobalInstances globalInstances: VarTypeScopes?,
|
||||
withGlobalTypes globalTypes: TypeTypeScopes?
|
||||
) -> Result<P4Lang.Program> {
|
||||
|
||||
let maybe_parser = ConfigureP4Parser()
|
||||
|
||||
@@ -33,5 +33,7 @@ public protocol CompilableValue {
|
||||
}
|
||||
|
||||
public protocol CompilableType {
|
||||
static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Result<P4Type?>
|
||||
static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||
) -> Result<P4Type?>
|
||||
}
|
||||
|
||||
@@ -191,7 +191,8 @@ extension VariableDeclarationStatement: CompilableStatement {
|
||||
Error(withMessage: "Could not parse variable name"))
|
||||
}
|
||||
|
||||
guard case .Ok(let declaration_p4_type) = Types.CompileType(type: typeref, withContext: context) else {
|
||||
guard case .Ok(let declaration_p4_type) = Types.CompileType(type: typeref, withContext: context)
|
||||
else {
|
||||
return Result.Error(
|
||||
Error(withMessage: "Could not parse a P4 type from \(typeref.text!)"))
|
||||
}
|
||||
|
||||
@@ -23,31 +23,40 @@ import TreeSitterExtensions
|
||||
import TreeSitterP4
|
||||
|
||||
extension P4Boolean: CompilableType {
|
||||
public static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Common.Result<(any Common.P4Type)?> {
|
||||
public static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||
) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type.text == "bool" ? .Ok(P4Boolean()) : .Ok(.none)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4Int: CompilableType {
|
||||
public static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Common.Result<(any Common.P4Type)?> {
|
||||
public static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||
) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type.text == "int" ? .Ok(P4Int()) : .Ok(.none)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4String: CompilableType {
|
||||
public static func CompileType(type: SwiftTreeSitter.Node, withContext: CompilerContext) -> Common.Result<(any Common.P4Type)?> {
|
||||
public static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext: CompilerContext
|
||||
) -> Common.Result<(any Common.P4Type)?> {
|
||||
return type.text == "string" ? .Ok(P4String()) : .Ok(.none)
|
||||
}
|
||||
}
|
||||
|
||||
extension P4Struct: CompilableType {
|
||||
public static func CompileType(type: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Common.Result<(any Common.P4Type)?> {
|
||||
public static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(any Common.P4Type)?> {
|
||||
let maybe_parsed_type_id = Identifier.Compile(node: type, withContext: context)
|
||||
guard case .Ok(let parsed_type_id) = maybe_parsed_type_id else {
|
||||
return .Error(maybe_parsed_type_id.error()!)
|
||||
}
|
||||
if case .Ok(let found_type) = context.types.lookup(identifier: parsed_type_id),
|
||||
let found_struct_type = found_type as? P4Struct {
|
||||
let found_struct_type = found_type as? P4Struct
|
||||
{
|
||||
return .Ok(found_struct_type)
|
||||
}
|
||||
return .Ok(.none)
|
||||
@@ -55,8 +64,12 @@ extension P4Struct: CompilableType {
|
||||
}
|
||||
|
||||
public struct Types {
|
||||
static func CompileType(type: SwiftTreeSitter.Node, withContext context: CompilerContext) -> Result<P4Type> {
|
||||
let type_parsers: [CompilableType.Type] = [P4Boolean.self, P4Int.self, P4String.self, P4Struct.self]
|
||||
static func CompileType(
|
||||
type: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Result<P4Type> {
|
||||
let type_parsers: [CompilableType.Type] = [
|
||||
P4Boolean.self, P4Int.self, P4String.self, P4Struct.self,
|
||||
]
|
||||
for type_parser in type_parsers {
|
||||
switch type_parser.CompileType(type: type, withContext: context) {
|
||||
case .Ok(.some(let type)): return .Ok(type)
|
||||
|
||||
Reference in New Issue
Block a user