@@ -25,13 +25,13 @@ import TreeSitterP4
|
||||
let p4lang = Language(tree_sitter_p4())
|
||||
|
||||
public func ConfigureP4Parser() -> Result<SwiftTreeSitter.Parser> {
|
||||
let p = SwiftTreeSitter.Parser.init()
|
||||
let p = SwiftTreeSitter.Parser.init()
|
||||
|
||||
do {
|
||||
try p.setLanguage(p4lang)
|
||||
} catch {
|
||||
return Result.Error(Error(withMessage: "Could not configure the P4 parser"))
|
||||
}
|
||||
do {
|
||||
try p.setLanguage(p4lang)
|
||||
} catch {
|
||||
return Result.Error(Error(withMessage: "Could not configure the P4 parser"))
|
||||
}
|
||||
|
||||
return .Ok(p)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ extension TypedIdentifier: CompilableExpression {
|
||||
) -> Result<EvaluatableExpression?> {
|
||||
|
||||
let node = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(node: node, type: "identifier")
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: node, type: "identifier")
|
||||
|
||||
guard
|
||||
case Result.Ok(let type) = scopes.lookup(
|
||||
@@ -52,7 +53,8 @@ extension P4BooleanValue: CompilableExpression {
|
||||
withScopes scopes: LexicalScopes
|
||||
) -> Result<EvaluatableExpression?> {
|
||||
let node = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(node: node, type: "booleanLiteralExpression")
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: node, type: "booleanLiteralExpression")
|
||||
|
||||
if node.text == "false" {
|
||||
return .Ok(P4BooleanValue(withValue: false))
|
||||
@@ -60,7 +62,8 @@ extension P4BooleanValue: CompilableExpression {
|
||||
return .Ok(P4BooleanValue(withValue: true))
|
||||
}
|
||||
|
||||
return .Error(ErrorOnNode(node: node, withError: "Failed to parse boolean literal: \(node.text!)"))
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Failed to parse boolean literal: \(node.text!)"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +88,8 @@ extension P4StringValue: CompilableExpression {
|
||||
withScopes scopes: LexicalScopes
|
||||
) -> Result<EvaluatableExpression?> {
|
||||
let node = node.child(at: 0)!
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(node: node, type: "string_literal")
|
||||
#SkipUnlessNodeType<SwiftTreeSitter.Node, EvaluatableExpression?>(
|
||||
node: node, type: "string_literal")
|
||||
return .Ok(P4StringValue(withValue: node.text!))
|
||||
}
|
||||
}
|
||||
@@ -95,7 +99,9 @@ struct Expression {
|
||||
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<EvaluatableExpression> {
|
||||
|
||||
#RequireNodesType<Node, EvaluatableExpression>(nodes: node, type: ["expression", "keysetExpression"], msg: ["expression", "keyset expression"])
|
||||
#RequireNodesType<Node, EvaluatableExpression>(
|
||||
nodes: node, type: ["expression", "keysetExpression"],
|
||||
msg: ["expression", "keyset expression"])
|
||||
|
||||
// If the node is a keyset expression, then dig out the expression:
|
||||
let node =
|
||||
|
||||
@@ -27,7 +27,8 @@ extension ParserAssignmentStatement: CompilableStatement {
|
||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||
) -> Result<(EvaluatableStatement, LexicalScopes)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(node: node, type: "assignmentStatement", msg: "assignment statement")
|
||||
#RequireNodeType<Node, (EvaluatableStatement, LexicalScopes)>(
|
||||
node: node, type: "assignmentStatement", msg: "assignment statement")
|
||||
|
||||
guard let lvalue_node = node.child(at: 0),
|
||||
lvalue_node.nodeType == "expression"
|
||||
@@ -92,7 +93,8 @@ public struct Parser {
|
||||
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
||||
node: node,
|
||||
withError: "Unparseable statement type (\(node.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
|
||||
switch parser.Compile(node: node, inTree: tree, withScopes: scopes) {
|
||||
@@ -126,7 +128,9 @@ public struct Parser {
|
||||
guard let parser = statementParsers[statement.nodeType ?? ""] else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: statement, withError: "Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
node: statement,
|
||||
withError:
|
||||
"Unparseable statement type (\(statement.nodeType ?? "Unknown Statement Type"))"))
|
||||
}
|
||||
switch parser.Compile(node: statement, inTree: tree, withScopes: scopes) {
|
||||
case Result.Ok(let (parsed, updatedLexicalScopes)):
|
||||
|
||||
Reference in New Issue
Block a user