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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user