Support Parameters For Parsers
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -146,11 +146,11 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(P4Type, CompilerContext)?> {
|
||||
|
||||
let parser_node = node
|
||||
if parser_node.nodeType != "parserDeclaration" {
|
||||
return .Ok(.none)
|
||||
}
|
||||
#SkipUnlessNodeType<Node, (P4Type, CompilerContext)?>(
|
||||
node: parser_node, type: "parserDeclaration")
|
||||
|
||||
var current_context = context
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
@@ -170,6 +170,9 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
let type_node = currentChild
|
||||
var parser_name: Common.Identifier? = .none
|
||||
|
||||
// Assume that the parameter list is empty!
|
||||
var parameter_list: ParameterList = ParameterList([])
|
||||
|
||||
do {
|
||||
// Parse the parser type (type_node)
|
||||
var currentChildIdx = 0
|
||||
@@ -198,25 +201,44 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
}
|
||||
currentChild = type_node?.child(at: currentChildIdx)
|
||||
|
||||
switch Identifier.Compile(node: currentChild!, withContext: context) {
|
||||
switch Identifier.Compile(node: currentChild!, withContext: current_context) {
|
||||
case .Ok(let id): parser_name = id
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
|
||||
// Now, see if there are any parameters
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if currentChildIdxSafe < type_node!.childCount {
|
||||
|
||||
// There is something that _should_ be parameters!
|
||||
|
||||
// skip the '('
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if type_node!.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: currentChild!, withError: "Missing ( before parameter list"))
|
||||
}
|
||||
|
||||
currentChild = type_node?.child(at: currentChildIdx)
|
||||
|
||||
switch ParameterList.Compile(node: currentChild!, withContext: current_context) {
|
||||
case .Ok(let (parsed_parameter_list, updated_context)):
|
||||
parameter_list = parsed_parameter_list
|
||||
current_context = updated_context
|
||||
case .Error(let e):
|
||||
return .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if parser_node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: parser_node, withError: "Missing elements of parser declaration"))
|
||||
}
|
||||
|
||||
if currentChild!.nodeType == "constructorParameters" {
|
||||
return .Error(
|
||||
ErrorOnNode(node: currentChild!, withError: "Constructor parameters are not yet handled.")
|
||||
)
|
||||
// Will increment indexes here.
|
||||
ErrorOnNode(node: parser_node, withError: "Missing parser declaration component"))
|
||||
}
|
||||
|
||||
// Skip the '{'
|
||||
@@ -242,7 +264,8 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
}
|
||||
|
||||
switch Parser.Compile(
|
||||
withName: parser_name!, node: currentChild!, withContext: context)
|
||||
withName: parser_name!, withParameters: parameter_list, node: currentChild!,
|
||||
withContext: current_context)
|
||||
{
|
||||
case Result.Ok((let parser, let updated_context)):
|
||||
// Create a new context with the name of the parser that was just compiled in scope.
|
||||
@@ -255,7 +278,156 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
))
|
||||
case Result.Error(let error): return .Error(error)
|
||||
}
|
||||
|
||||
// Assume that there is only '}' after -- the parser guaranteed that for us!
|
||||
}
|
||||
}
|
||||
|
||||
extension ParameterList: Compilable {
|
||||
public typealias T = ParameterList
|
||||
public static func Compile(
|
||||
node: SwiftTreeSitter.Node, withContext context: CompilerContext
|
||||
) -> Common.Result<(ParameterList, CompilerContext)> {
|
||||
|
||||
if node.text == ")" {
|
||||
// There are no parameters!
|
||||
return .Ok((ParameterList([]), context))
|
||||
}
|
||||
|
||||
#RequireNodeType<Node, (ParameterList, CompilerContext)>(
|
||||
node: node, type: "parameter_list", nice_type_name: "Parameter List")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var currentChild: Node? = .none
|
||||
var parameters: ParameterList = ParameterList([])
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing parameter list component"))
|
||||
}
|
||||
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
if currentChild?.nodeType == "parameter_list" {
|
||||
switch ParameterList.Compile(node: currentChild!, withContext: context) {
|
||||
case .Ok(let (ps, _)):
|
||||
parameters = ps
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
|
||||
print("Back here!")
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
}
|
||||
|
||||
// We may have moved nodes, check/reset currentChild.
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing parameter list component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
// If this is a ')', we are done.
|
||||
if currentChild?.text == ")" {
|
||||
return .Ok((parameters, context))
|
||||
}
|
||||
|
||||
// If this is a comma, we skip it!
|
||||
if currentChild?.text == "," {
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
}
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing parameter list component"))
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
// Otherwise, there should be one parameter left!
|
||||
switch Parameter.Compile(node: currentChild!, withContext: context) {
|
||||
case .Ok(let (vds, updated_context)):
|
||||
return .Ok((parameters.addParameter(vds), updated_context))
|
||||
case .Error(let e): return .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Parameter: Compilable {
|
||||
public typealias T = Parameter
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Parameter, CompilerContext)> {
|
||||
|
||||
#RequireNodeType<Node, (EvaluatableStatement, CompilerContext)>(
|
||||
node: node, type: "parameter", nice_type_name: "parameter")
|
||||
|
||||
var currentChildIdx = 0
|
||||
var currentChildIdxSafe = 1
|
||||
var currentChild: Node? = .none
|
||||
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing parameter declaration component"))
|
||||
}
|
||||
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
// Annotation?
|
||||
if currentChild!.nodeType == "annotations" {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: currentChild!,
|
||||
withError: "Annotations in parameter declarations are not yet handled"))
|
||||
// Will increment indexes here.
|
||||
}
|
||||
|
||||
// Direction?
|
||||
if currentChild!.nodeType == "direction" {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: currentChild!, withError: "Direction in parameter declarations are not yet handled"
|
||||
))
|
||||
// Will increment indexes here.
|
||||
}
|
||||
|
||||
if currentChild!.nodeType != "typeRef" {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Did not find type name for parameter declaration"))
|
||||
}
|
||||
|
||||
guard
|
||||
case .Ok(let parameter_type) = Types.CompileType(type: currentChild!, withContext: context)
|
||||
else {
|
||||
return Result.Error(
|
||||
Error(withMessage: "Could not parse a P4 type from \(currentChild!.text!)"))
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
if node.childCount < currentChildIdxSafe {
|
||||
return .Error(
|
||||
ErrorOnNode(node: node, withError: "Missing parameter declaration component"))
|
||||
}
|
||||
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
if currentChild!.nodeType != "identifier" {
|
||||
return Result.Error(
|
||||
ErrorOnNode(
|
||||
node: node, withError: "Did not find identifier for parameter statement"))
|
||||
}
|
||||
|
||||
guard
|
||||
case .Ok(let parameter_name) = Identifier.Compile(node: currentChild!, withContext: context)
|
||||
else {
|
||||
return Result.Error(
|
||||
Error(withMessage: "Could not parse a parameter name from \(currentChild!.text!)"))
|
||||
}
|
||||
|
||||
return Result.Ok(
|
||||
(
|
||||
Parameter(
|
||||
identifier: parameter_name, withType: parameter_type),
|
||||
context
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,11 +266,11 @@ public struct Parser {
|
||||
}
|
||||
|
||||
static func Compile(
|
||||
withName name: Common.Identifier, node: Node,
|
||||
withName name: Common.Identifier, withParameters parameters: ParameterList, node: Node,
|
||||
withContext context: CompilerContext
|
||||
) -> Result<(P4Lang.Parser, CompilerContext)> {
|
||||
|
||||
var parser = P4Lang.Parser(withName: name)
|
||||
var parser = P4Lang.Parser(withName: name, withParameters: parameters)
|
||||
|
||||
// Build a state from each one listed.
|
||||
var error: Error? = .none
|
||||
|
||||
@@ -43,3 +43,10 @@ public protocol CompilableDeclaration {
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(P4Type, CompilerContext)?>
|
||||
}
|
||||
|
||||
public protocol Compilable<T> {
|
||||
associatedtype T
|
||||
static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(T, CompilerContext)>
|
||||
}
|
||||
|
||||
@@ -18,3 +18,38 @@
|
||||
import Common
|
||||
|
||||
public struct Declaration {}
|
||||
|
||||
public struct Parameter: CustomStringConvertible {
|
||||
public var name: Identifier
|
||||
public var type: P4Type
|
||||
|
||||
public init(
|
||||
identifier: Identifier, withType type: P4Type
|
||||
) {
|
||||
self.name = identifier
|
||||
self.type = type
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Parameter: \(self.name) with type \(self.type)"
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParameterList: CustomStringConvertible {
|
||||
public var parameters: [Parameter]
|
||||
|
||||
public init(_ parameters: [Parameter]) {
|
||||
self.parameters = parameters
|
||||
}
|
||||
|
||||
public func addParameter(_ parameter: Parameter) -> ParameterList {
|
||||
return ParameterList(self.parameters + [parameter])
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
let parameters = self.parameters.map { parameter in
|
||||
parameter.description
|
||||
}.joined(separator: ";")
|
||||
return "Parameter list: \(parameters)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,12 +319,19 @@ public struct Parser: P4Type, P4Value {
|
||||
public var states: ParserStates
|
||||
|
||||
public var name: Identifier
|
||||
public var parameters: ParameterList?
|
||||
|
||||
public init(withName name: Identifier) {
|
||||
self.states = ParserStates()
|
||||
self.name = name
|
||||
}
|
||||
|
||||
public init(withName name: Identifier, withParameters parameters: ParameterList) {
|
||||
self.states = ParserStates()
|
||||
self.parameters = parameters
|
||||
self.name = name
|
||||
}
|
||||
|
||||
public func findStartState() -> ParserState? {
|
||||
for state in states.states {
|
||||
if state.state == Identifier(name: "start") {
|
||||
@@ -342,7 +349,8 @@ public struct Parser: P4Type, P4Value {
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Parser"
|
||||
let parameters = self.parameters?.description ?? "N/A"
|
||||
return "Parser \(self.name) with parameters: \(parameters) and states: \(self.states)"
|
||||
}
|
||||
|
||||
public func def() -> any P4Value {
|
||||
|
||||
Reference in New Issue
Block a user