compiler, testing: Build System for Compilation
The compilation code was written as a precursor for implementation with macros. The updates in this commit make the switch. There is still plenty to do: 1. Comment Walker. 2. Comment Macros. Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -51,46 +51,42 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
node: function_declaration_node, type: "function_declaration",
|
||||
nice_type_name: "Function Declaration")
|
||||
|
||||
var walker = Walker(node: function_declaration_node)
|
||||
var context = context
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var currentChild: Node? = .none
|
||||
if function_declaration_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: function_declaration_node, withError: "Missing function declaration component"))
|
||||
}
|
||||
currentChild = function_declaration_node.child(at: currentChildIdx)
|
||||
|
||||
let maybe_function_type = Types.CompileType(type: currentChild!, withContext: context)
|
||||
var current_node: Node? = .none
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: function_declaration_node, withError: "Missing function declaration component")))
|
||||
|
||||
let maybe_function_type = Types.CompileType(type: current_node!, withContext: context)
|
||||
guard case .Ok(let function_type) = maybe_function_type else {
|
||||
return .Error(maybe_function_type.error()!)
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if function_declaration_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: function_declaration_node, withError: "Missing function declaration component"))
|
||||
}
|
||||
currentChild = function_declaration_node.child(at: currentChildIdx)
|
||||
node: function_declaration_node, withError: "Missing function declaration component")))
|
||||
|
||||
let maybe_function_name = Identifier.Compile(node: currentChild!, withContext: context)
|
||||
let maybe_function_name = Identifier.Compile(node: current_node!, withContext: context)
|
||||
guard case .Ok(let function_name) = maybe_function_name else {
|
||||
return .Error(maybe_function_name.error()!)
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if function_declaration_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: function_declaration_node, withError: "Missing function declaration component"))
|
||||
}
|
||||
currentChild = function_declaration_node.child(at: currentChildIdx)
|
||||
node: function_declaration_node, withError: "Missing function declaration component")))
|
||||
|
||||
let maybe_function_parameters = ParameterList.Compile(node: currentChild!, withContext: context)
|
||||
let maybe_function_parameters = ParameterList.Compile(node: current_node!, withContext: context)
|
||||
guard case .Ok((let function_parameters, let updated_context)) = maybe_function_parameters
|
||||
else {
|
||||
return .Error(maybe_function_parameters.error()!)
|
||||
@@ -99,11 +95,8 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
|
||||
var function_body: BlockStatement? = .none
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if currentChildIdxSafe <= function_declaration_node.childCount {
|
||||
currentChild = function_declaration_node.child(at: currentChildIdx)
|
||||
|
||||
walker.next()
|
||||
if let body = walker.getNext() {
|
||||
// Add the parameters into scope.
|
||||
var function_scope = context.instances.enter()
|
||||
for parameter in function_parameters.parameters {
|
||||
@@ -112,7 +105,7 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
}
|
||||
|
||||
let maybe_function_body = BlockStatement.Compile(
|
||||
node: currentChild!,
|
||||
node: body,
|
||||
withContext: context.update(newInstances: function_scope).update(
|
||||
newExpectation: function_type))
|
||||
|
||||
@@ -160,59 +153,38 @@ extension P4Struct: CompilableDeclaration {
|
||||
static public func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Declaration, CompilerContext)?> {
|
||||
|
||||
let struct_declaration_node = node.child(at: 0)!
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var walker = Walker(node: struct_declaration_node)
|
||||
var currentNode: Node? = .none
|
||||
|
||||
var currentChild: Node? = .none
|
||||
|
||||
guard let node_type = struct_declaration_node.nodeType,
|
||||
node_type == "struct_declaration"
|
||||
else {
|
||||
return Result.Error(
|
||||
ErrorOnNode(node: struct_declaration_node, withError: "Did not find a struct declaration"))
|
||||
}
|
||||
|
||||
if struct_declaration_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: struct_declaration_node, withError: "Missing elements in struct declaration"))
|
||||
}
|
||||
#SkipUnlessNodeType(node: struct_declaration_node, type: "struct_declaration")
|
||||
|
||||
// Skip the keyword struct
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if struct_declaration_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: currentNode, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: struct_declaration_node, withError: "Missing elements in struct declaration"))
|
||||
}
|
||||
node: struct_declaration_node, withError: "Missing function declaration component")))
|
||||
|
||||
// The name of the struct type.
|
||||
currentChild = struct_declaration_node.child(at: currentChildIdx)
|
||||
let maybe_struct_identifier = Identifier.Compile(
|
||||
node: currentChild!, withContext: context)
|
||||
node: currentNode!, withContext: context)
|
||||
guard case Result.Ok(let struct_identifier) = maybe_struct_identifier else {
|
||||
return Result.Error(maybe_struct_identifier.error()!)
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
walker.next()
|
||||
|
||||
// Skip the '{'
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if struct_declaration_node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: currentNode, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: struct_declaration_node, withError: "Missing element of struct declaration"))
|
||||
}
|
||||
|
||||
currentChild = struct_declaration_node.child(at: currentChildIdx)
|
||||
node: struct_declaration_node, withError: "Missing function declaration component")))
|
||||
|
||||
// If there are no fields, it will be a "}"
|
||||
if currentChild!.nodeType == "}" {
|
||||
if currentNode!.nodeType == "}" {
|
||||
let struc = Declaration(
|
||||
TypedIdentifier(
|
||||
id: struct_identifier,
|
||||
@@ -232,8 +204,8 @@ extension P4Struct: CompilableDeclaration {
|
||||
var current_context = context
|
||||
var parsed_fields: [P4StructFieldIdentifier] = Array()
|
||||
|
||||
if currentChild!.nodeType == "struct_declaration_fields" {
|
||||
currentChild!.enumerateNamedChildren { declaration_field in
|
||||
if currentNode!.nodeType == "struct_declaration_fields" {
|
||||
currentNode!.enumerateNamedChildren { declaration_field in
|
||||
switch VariableDeclarationStatement.Compile(
|
||||
node: declaration_field, withContext: current_context)
|
||||
{
|
||||
@@ -281,75 +253,67 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Declaration, CompilerContext)?> {
|
||||
let parser_node = node
|
||||
#SkipUnlessNodeType<Node, (P4DataType, CompilerContext)?>(
|
||||
node: parser_node, type: "parserDeclaration")
|
||||
|
||||
#SkipUnlessNodeType<Node>(node: parser_node, type: "parserDeclaration")
|
||||
var current_context = context
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var currentChild: Node? = .none
|
||||
var walker = Walker(node: parser_node)
|
||||
var current_node: Node? = .none
|
||||
|
||||
if parser_node.childCount < currentChildIdxSafe {
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: parser_node, withError: "Missing elements of parser declaration")))
|
||||
|
||||
if current_node!.nodeType != "parserType" {
|
||||
return .Error(
|
||||
ErrorOnNode(node: parser_node, withError: "Missing elements of parser declaration"))
|
||||
ErrorOnNode(node: current_node!, withError: "Missing type for parser declaration"))
|
||||
}
|
||||
|
||||
currentChild = parser_node.child(at: currentChildIdx)
|
||||
if currentChild!.nodeType != "parserType" {
|
||||
return .Error(
|
||||
ErrorOnNode(node: currentChild!, withError: "Missing type for parser declaration"))
|
||||
}
|
||||
|
||||
let type_node = currentChild
|
||||
let type_node = current_node
|
||||
var parser_name: Common.Identifier? = .none
|
||||
|
||||
// TODO: Handle parser parameter lists.
|
||||
var parameter_list = ParameterList()
|
||||
|
||||
do {
|
||||
// Parse the parser type (type_node)
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var type_node_walker = Walker(node: type_node!)
|
||||
var type_node_child: Node? = .none
|
||||
|
||||
if type_node!.childCount < currentChildIdxSafe {
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: parser_node, withError: "Missing elements of parser type in parser declaration")))
|
||||
|
||||
if type_node_child!.nodeType == "annotations" {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: parser_node, withError: "Missing elements of parser type in parser declaration"))
|
||||
}
|
||||
|
||||
var currentChild = type_node!.child(at: currentChildIdx)
|
||||
if currentChild!.nodeType == "annotations" {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: currentChild!, withError: "Annotations in parser type are not yet handled."))
|
||||
node: type_node_child!, withError: "Annotations in parser type are not yet handled."))
|
||||
// Will increment indexes here.
|
||||
}
|
||||
|
||||
// Skip the parser keyword
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if type_node!.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: type_node!, withError: "Missing name in parser type declaration"))
|
||||
}
|
||||
currentChild = type_node?.child(at: currentChildIdx)
|
||||
type_node_walker.next()
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: type_node_child!, withError: "Missing name in parser type declaration")))
|
||||
|
||||
switch Identifier.Compile(node: currentChild!, withContext: current_context) {
|
||||
switch Identifier.Compile(node: type_node_child!, withContext: current_context) {
|
||||
case .Ok(let id): parser_name = id
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if type_node!.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: type_node!, withError: "Missing parser parameters"))
|
||||
}
|
||||
type_node_walker.next()
|
||||
#MustOr(
|
||||
result: type_node_child, thing: type_node_walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: type_node_child!, withError: "Missing parser parameters")))
|
||||
|
||||
currentChild = type_node?.child(at: currentChildIdx)
|
||||
switch ParameterList.Compile(node: currentChild!, withContext: current_context) {
|
||||
switch ParameterList.Compile(node: type_node_child!, withContext: current_context) {
|
||||
case .Ok(let (parsed_parameter_list, updated_context)):
|
||||
parameter_list = parsed_parameter_list
|
||||
current_context = updated_context
|
||||
@@ -365,37 +329,38 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
identifier: parameter.name, withValue: parameter.type))
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if parser_node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: parser_node, withError: "Missing parser declaration component"))
|
||||
}
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: parser_node, withError: "Missing parser declaration component")))
|
||||
|
||||
// Skip the '{'
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if parser_node.childCount < currentChildIdxSafe {
|
||||
return .Error((Error(withMessage: "Missing body of parser declaration")))
|
||||
}
|
||||
currentChild = parser_node.child(at: currentChildIdx)
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: parser_node, withError: "Missing elements of parser declaration")))
|
||||
|
||||
if currentChild!.nodeType == "parserLocalElements" {
|
||||
if current_node!.nodeType == "parserLocalElements" {
|
||||
return .Error(
|
||||
ErrorOnNode(node: currentChild!, withError: "Parser Local Elements are not yet handled."))
|
||||
ErrorOnNode(node: current_node!, withError: "Parser Local Elements are not yet handled."))
|
||||
// Will increment indexes here.
|
||||
}
|
||||
|
||||
if parser_node.childCount < currentChildIdxSafe {
|
||||
return .Error((Error(withMessage: "Missing body of parser declaration")))
|
||||
}
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: parser_node, withError: "Missing body of parser declaration")))
|
||||
|
||||
if currentChild!.nodeType != "parserStates" {
|
||||
if current_node!.nodeType != "parserStates" {
|
||||
return .Error(Error(withMessage: "Missing parser states in parser declaration"))
|
||||
}
|
||||
|
||||
switch Parser.Compile(
|
||||
withName: parser_name!, withParameters: parameter_list, node: currentChild!,
|
||||
withName: parser_name!, withParameters: parameter_list, node: current_node!,
|
||||
withContext: current_context)
|
||||
{
|
||||
case Result.Ok((let parser, let updated_context)):
|
||||
@@ -421,47 +386,37 @@ extension Control: CompilableDeclaration {
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(Declaration, CompilerContext)?> {
|
||||
|
||||
#SkipUnlessNodeType<Node, (P4DataType, CompilerContext)?>(
|
||||
node: node, type: "control_declaration")
|
||||
#SkipUnlessNodeType<Node>(node: node, type: "control_declaration")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var currentChild: Node? = .none
|
||||
var walker = Walker(node: node)
|
||||
var current_node: Node? = .none
|
||||
|
||||
var local_context = context
|
||||
|
||||
// Skip control keyword
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing control declaration component"))
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing control declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing control declaration component")))
|
||||
|
||||
guard
|
||||
case .Ok(let control_name) = Identifier.Compile(
|
||||
node: currentChild!, withContext: local_context)
|
||||
node: current_node!, withContext: local_context)
|
||||
else {
|
||||
return Result.Error(
|
||||
Error(withMessage: "Could not parse a parameter name from \(currentChild!.text!)"))
|
||||
Error(withMessage: "Could not parse a parameter name from \(current_node!.text!)"))
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing control declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing control declaration component")))
|
||||
|
||||
let maybe_control_parameters = ParameterList.Compile(
|
||||
node: currentChild!, withContext: local_context)
|
||||
node: current_node!, withContext: local_context)
|
||||
guard case .Ok((let control_parameters, let updated_context)) = maybe_control_parameters
|
||||
else {
|
||||
return .Error(maybe_control_parameters.error()!)
|
||||
@@ -476,26 +431,26 @@ extension Control: CompilableDeclaration {
|
||||
}
|
||||
local_context = local_context.update(newInstances: control_scope)
|
||||
|
||||
walker.next()
|
||||
// Skip the '{'
|
||||
currentChildIdx += 2
|
||||
currentChildIdxSafe += 2
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing control declaration component"))
|
||||
}
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(Declaration, CompilerContext)?>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing control declaration component")))
|
||||
|
||||
var actions: [Action] = Array()
|
||||
var tables: [Table] = Array()
|
||||
var apply: ApplyStatement? = .none
|
||||
|
||||
// Because the final child
|
||||
// is the '}'.
|
||||
// \/\/
|
||||
for currentChildIdx in currentChildIdx..<(node.childCount - 1) {
|
||||
let currentChild = node.child(at: currentChildIdx)!
|
||||
if currentChild.nodeType == "action_declaration" {
|
||||
// Because the final child
|
||||
// is the '}'.
|
||||
// \/\/
|
||||
let body_parse_results = walker.overUntil(n: node.childCount - 1) { current_node in
|
||||
if current_node.nodeType == "action_declaration" {
|
||||
let maybe_action_declaration = Action.Compile(
|
||||
node: currentChild, withContext: local_context)
|
||||
node: current_node, withContext: local_context)
|
||||
guard
|
||||
case .Ok((let action_declaration, let updated_context)) = maybe_action_declaration
|
||||
else {
|
||||
@@ -503,9 +458,9 @@ extension Control: CompilableDeclaration {
|
||||
}
|
||||
actions.append(action_declaration)
|
||||
local_context = updated_context
|
||||
} else if currentChild.nodeType == "table_declaration" {
|
||||
} else if current_node.nodeType == "table_declaration" {
|
||||
let maybe_table_declaration = Table.Compile(
|
||||
node: currentChild, withContext: local_context)
|
||||
node: current_node, withContext: local_context)
|
||||
guard
|
||||
case .Ok((let table_declaration, let updated_context)) = maybe_table_declaration
|
||||
else {
|
||||
@@ -513,10 +468,10 @@ extension Control: CompilableDeclaration {
|
||||
}
|
||||
tables.append(table_declaration)
|
||||
local_context = updated_context
|
||||
} else if currentChild.nodeType == "apply_statement" {
|
||||
} else if current_node.nodeType == "apply_statement" {
|
||||
// When we see an apply, that is it for the actions and the tables.
|
||||
let maybe_apply_statement = ApplyStatement.Compile(
|
||||
node: currentChild, withContext: local_context)
|
||||
node: current_node, withContext: local_context)
|
||||
guard
|
||||
case .Ok((let apply_statement, let updated_context)) = maybe_apply_statement
|
||||
else {
|
||||
@@ -526,18 +481,23 @@ extension Control: CompilableDeclaration {
|
||||
apply = (apply_statement as! ApplyStatement)
|
||||
|
||||
// The apply is the last thing in a control declaration.
|
||||
break
|
||||
// But, that is handled by the compiler.
|
||||
} else {
|
||||
return .Error(
|
||||
ErrorOnNode(node: currentChild, withError: "Uknown node type in control declaration"))
|
||||
ErrorOnNode(node: current_node, withError: "Uknown node type in control declaration"))
|
||||
}
|
||||
return .Ok(())
|
||||
}
|
||||
|
||||
if case .Error(let e) = body_parse_results {
|
||||
return .Error(e)
|
||||
}
|
||||
|
||||
// There should only be a single table!
|
||||
// TODO: Check the semantics here.
|
||||
/// TODO: Check the semantics here.
|
||||
if tables.count > 1 {
|
||||
// TODO: Make this error message better.
|
||||
// IDEA: Add a "compilation context" for the error message into the `CompilationContext`
|
||||
/// TODO: Make this error message better.
|
||||
/// IDEA: Add a "compilation context" for the error message into the `CompilationContext`
|
||||
// that can be retrieved to make the error messages nicer.
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "More than one table in control declaration"))
|
||||
@@ -580,50 +540,48 @@ extension Action: Compilable {
|
||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
||||
node: node, type: "action_declaration", nice_type_name: "Action Declaration")
|
||||
|
||||
var currentChildIdx = 1
|
||||
var currentChildIdxSafe = 2
|
||||
var currentChild: Node? = .none
|
||||
var walker = Walker(node: node)
|
||||
var current_node: Node? = .none
|
||||
var current_context = context
|
||||
|
||||
// Skip action keyword
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
walker.next()
|
||||
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing action declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
)
|
||||
|
||||
guard
|
||||
case .Ok(let action_name) = Identifier.Compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
node: current_node!, withContext: current_context)
|
||||
else {
|
||||
return Result.Error(
|
||||
Error(withMessage: "Could not parse an action name from \(currentChild!.text!)"))
|
||||
Error(withMessage: "Could not parse an action name from \(current_node!.text!)"))
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing action declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
)
|
||||
|
||||
let maybe_action_parameters = ParameterList.Compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok((let action_parameters, let updated_context)) = maybe_action_parameters
|
||||
else {
|
||||
return .Error(maybe_action_parameters.error()!)
|
||||
}
|
||||
current_context = updated_context
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing action declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.Action, CompilerContext)>.Error(
|
||||
ErrorOnNode(node: node, withError: "Missing action declaration component"))
|
||||
)
|
||||
|
||||
// Add the parameters into scope.
|
||||
var function_scope = context.instances.enter()
|
||||
@@ -633,12 +591,12 @@ extension Action: Compilable {
|
||||
}
|
||||
|
||||
let maybe_action_body = BlockStatement.Compile(
|
||||
node: currentChild!, withContext: context.update(newInstances: function_scope))
|
||||
node: current_node!, withContext: context.update(newInstances: function_scope))
|
||||
guard case .Ok((let action_body, _)) = maybe_action_body else {
|
||||
return .Error(maybe_action_body.error()!)
|
||||
}
|
||||
|
||||
// TODO: Actions cannot contain switches!
|
||||
/// TODO: Actions cannot contain switches!
|
||||
|
||||
return .Ok(
|
||||
(
|
||||
@@ -659,35 +617,35 @@ extension TableKeyEntry: Compilable {
|
||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
||||
node: node, type: "table_key_entry", nice_type_name: "Table Key Entry")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var currentChild: Node? = .none
|
||||
var walker = Walker(node: node)
|
||||
|
||||
var current_node: Node? = .none
|
||||
|
||||
let current_context = context
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing table key entry declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing table key entry declaration component")))
|
||||
|
||||
let maybe_keyset_expression = KeysetExpression.compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok(let keyset_expression) = maybe_keyset_expression else {
|
||||
return Result.Error(maybe_keyset_expression.error()!)
|
||||
}
|
||||
|
||||
walker.next()
|
||||
// Skip the ':'
|
||||
currentChildIdx += 2
|
||||
currentChildIdxSafe += 2
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing table key entry declaration component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.TableKeyEntry, CompilerContext)>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing table key entry declaration component")))
|
||||
|
||||
let maybe_match_type = TableKeyMatchType.Compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok((let match_type, _)) = maybe_match_type else {
|
||||
return .Error(maybe_match_type.error()!)
|
||||
}
|
||||
@@ -719,26 +677,29 @@ extension TableKeys: Compilable {
|
||||
#RequireNodeType<Node, (TableKeyMatchType, CompilerContext)>(
|
||||
node: node, type: "table_keys", nice_type_name: "Table Keys")
|
||||
|
||||
var walker = Walker(node: node)
|
||||
// Skip the
|
||||
// keys = {
|
||||
// 0 1 2
|
||||
let currentChildIdx = 3
|
||||
let currentChildIdxSafe = 4
|
||||
var currentChild: Node? = .none
|
||||
// 1 2 3
|
||||
walker.next() // 1
|
||||
walker.next() // 2
|
||||
walker.next() // 3
|
||||
|
||||
var current_node: Node? = .none
|
||||
var current_context = context
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.TableKeys, CompilerContext)>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing table keys declaration component in control declaration"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
)
|
||||
|
||||
var entries: [TableKeyEntry] = Array()
|
||||
var errors: [Error] = Array()
|
||||
|
||||
currentChild!.enumerateNamedChildren { entry in
|
||||
switch TableKeyEntry.Compile(node: currentChild!, withContext: current_context) {
|
||||
current_node!.enumerateNamedChildren { entry in
|
||||
switch TableKeyEntry.Compile(node: current_node!, withContext: current_context) {
|
||||
case .Ok((let keyset_expression, let updated_context)):
|
||||
entries.append(keyset_expression)
|
||||
current_context = updated_context
|
||||
@@ -823,37 +784,38 @@ extension Table: Compilable {
|
||||
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
|
||||
node: table_declaration_node, type: "table_declaration", nice_type_name: "Table Declaration")
|
||||
|
||||
var currentChildIdx = 1
|
||||
var currentChildIdxSafe = 2
|
||||
var currentChild: Node? = .none
|
||||
var walker = Walker(node: table_declaration_node)
|
||||
|
||||
var current_node: Node? = .none
|
||||
|
||||
let current_context = context
|
||||
|
||||
if table_declaration_node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: table_declaration_node, withError: "Missing table declaration component"))
|
||||
}
|
||||
currentChild = table_declaration_node.child(at: currentChildIdx)
|
||||
walker.next() // Skip the XXX?
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.Table, CompilerContext)>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing table declaration component")))
|
||||
|
||||
guard
|
||||
case .Ok(let table_name) = Identifier.Compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
node: current_node!, withContext: current_context)
|
||||
else {
|
||||
return Result.Error(
|
||||
Error(withMessage: "Could not parse a table name from \(currentChild!.text!)"))
|
||||
Error(withMessage: "Could not parse a table name from \(current_node!.text!)"))
|
||||
}
|
||||
|
||||
walker.next()
|
||||
// Skip the '{'
|
||||
currentChildIdx += 2
|
||||
currentChildIdxSafe += 2
|
||||
if table_declaration_node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: table_declaration_node, withError: "Missing table declaration component"))
|
||||
}
|
||||
currentChild = table_declaration_node.child(at: currentChildIdx)
|
||||
walker.next()
|
||||
#MustOr(
|
||||
result: current_node, thing: walker.getNext(),
|
||||
or: Result<(P4Lang.Table, CompilerContext)>.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Missing table declaration component")))
|
||||
|
||||
let maybe_table_property_list = TablePropertyList.Compile(
|
||||
node: currentChild!, withContext: current_context)
|
||||
node: current_node!, withContext: current_context)
|
||||
guard case .Ok((let table_property_list, _)) = maybe_table_property_list else {
|
||||
return Result.Error(maybe_table_property_list.error()!)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user