compiler: Refactor Type Parsers
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -23,37 +23,31 @@ import TreeSitterP4
|
|||||||
extension P4Boolean: MaybeParsableType {
|
extension P4Boolean: MaybeParsableType {
|
||||||
public static func MaybeParseType(
|
public static func MaybeParseType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
||||||
) -> Common.Result<(CST.Tipe)?> {
|
) -> Result<(CST.Tipe)> {
|
||||||
return type.text == "bool" ? .Ok(CST.Tipe(P4QualifiedType(P4Boolean()))) : .Ok(.none)
|
return type.nodeType == "bool"
|
||||||
|
? .Ok(CST.Tipe(P4QualifiedType(P4Boolean())))
|
||||||
|
: .Error(Error(withMessage: "Invalid parser selected for \(type.nodeType!)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension P4Int: MaybeParsableType {
|
extension P4Int: MaybeParsableType {
|
||||||
public static func MaybeParseType(
|
public static func MaybeParseType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
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.
|
var walker = Walker(node: type)
|
||||||
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 int_node: Node? = .none
|
var int_node: Node? = .none
|
||||||
|
|
||||||
#MustOr(
|
#MustOr(
|
||||||
result: int_node, thing: walker.getNext(),
|
result: int_node, thing: walker.getNext(),
|
||||||
or: Result<CST.Tipe?>.Error(
|
or: Result<CST.Tipe>.Error(
|
||||||
ErrorWithLocation(
|
ErrorWithLocation(
|
||||||
sourceLocation: type_node.toSourceLocation(),
|
sourceLocation: type.toSourceLocation(),
|
||||||
withError: "Missing elements in int type declaration")))
|
withError: "Missing elements in int type declaration")))
|
||||||
|
|
||||||
// Move passed the keyword.
|
// Move passed the keyword -- now see whether there is a width
|
||||||
walker.next()
|
walker.next()
|
||||||
|
|
||||||
if let bit_width_node = walker.getNext() {
|
if let bit_width_node = walker.getNext() {
|
||||||
@@ -74,8 +68,10 @@ extension P4Int: MaybeParsableType {
|
|||||||
extension P4String: MaybeParsableType {
|
extension P4String: MaybeParsableType {
|
||||||
public static func MaybeParseType(
|
public static func MaybeParseType(
|
||||||
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
type: SwiftTreeSitter.Node, withContext: CSTCompilerContext
|
||||||
) -> Common.Result<(CST.Tipe)?> {
|
) -> Result<(CST.Tipe)> {
|
||||||
return type.text == "string" ? .Ok(CST.Tipe(P4QualifiedType(P4String()))) : .Ok(.none)
|
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(
|
public static func ParseType(
|
||||||
type: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
type: SwiftTreeSitter.Node, withContext context: CSTCompilerContext
|
||||||
) -> Result<CST.Tipe> {
|
) -> Result<CST.Tipe> {
|
||||||
let type_parsers: [MaybeParsableType.Type] = [
|
#RequireNodeType<Node, CST.Tipe>(node: type, type: "typeRef", nice_type_name: "Type Reference")
|
||||||
P4Boolean.self, P4Int.self, P4String.self, /*P4Struct.self,*/
|
let type = type.child(at: 0)!
|
||||||
]
|
if type.nodeType == "baseType" {
|
||||||
for type_parser in type_parsers {
|
let type = type.child(at: 0)!
|
||||||
switch type_parser.MaybeParseType(type: type, withContext: context) {
|
let base_type_parsers: [String: MaybeParsableType.Type] = [
|
||||||
case .Ok(.some(let type)): return .Ok(type)
|
"bool": P4Boolean.self, "int_type": P4Int.self, "string": P4String.self,
|
||||||
case .Ok(.none): continue
|
]
|
||||||
|
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)
|
case .Error(let e): return .Error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user