Start Moving Away From Queries
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
+25
-25
@@ -37,20 +37,20 @@ public struct ParserAssignmentStatement {
|
|||||||
|
|
||||||
public struct KeysetExpression {
|
public struct KeysetExpression {
|
||||||
public let key: EvaluatableExpression
|
public let key: EvaluatableExpression
|
||||||
public let next_state_name: String
|
public let next_state_identifier: Identifier
|
||||||
public let next_state: ParserState?
|
public let next_state: ParserState?
|
||||||
|
|
||||||
public init(withKey key: EvaluatableExpression, withNextStateName next_state_name: String) {
|
public init(withKey key: EvaluatableExpression, withNextState next_state_id: Identifier) {
|
||||||
self.key = key
|
self.key = key
|
||||||
self.next_state_name = next_state_name
|
self.next_state_identifier = next_state_id
|
||||||
self.next_state = .none
|
self.next_state = .none
|
||||||
}
|
}
|
||||||
public init(
|
public init(
|
||||||
withKey key: EvaluatableExpression, withNextStateName next_state_name: String,
|
withKey key: EvaluatableExpression, withNextState next_state_id: Identifier,
|
||||||
withNextState next_state: ParserState
|
withNextState next_state: ParserState
|
||||||
) {
|
) {
|
||||||
self.key = key
|
self.key = key
|
||||||
self.next_state_name = next_state_name
|
self.next_state_identifier = next_state_id
|
||||||
self.next_state = next_state
|
self.next_state = next_state
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,28 +76,28 @@ public struct ParserTransitionSelectExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public struct ParserTransitionStatement {
|
public struct ParserTransitionStatement {
|
||||||
public let next_state_name: String?
|
public let next_state: Identifier?
|
||||||
public let transition_expression: ParserTransitionSelectExpression?
|
public let transition_expression: ParserTransitionSelectExpression?
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
self.next_state_name = .none
|
self.next_state = .none
|
||||||
self.transition_expression = .none
|
self.transition_expression = .none
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(withTransitionExpression transition_expression: ParserTransitionSelectExpression) {
|
public init(withTransitionExpression transition_expression: ParserTransitionSelectExpression) {
|
||||||
self.next_state_name = .none
|
self.next_state = .none
|
||||||
self.transition_expression = transition_expression
|
self.transition_expression = transition_expression
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(withNextState next_state_name: String) {
|
public init(withNextState next_state: Identifier) {
|
||||||
self.next_state_name = next_state_name
|
self.next_state = next_state
|
||||||
self.transition_expression = .none
|
self.transition_expression = .none
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
||||||
|
|
||||||
public private(set) var state_name: String
|
public private(set) var state: Identifier
|
||||||
public private(set) var local_elements: [EvaluatableStatement]
|
public private(set) var local_elements: [EvaluatableStatement]
|
||||||
public private(set) var statements: [EvaluatableStatement]
|
public private(set) var statements: [EvaluatableStatement]
|
||||||
public private(set) var transition: ParserTransitionStatement?
|
public private(set) var transition: ParserTransitionStatement?
|
||||||
@@ -111,20 +111,20 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
return "Name: \(state_name)"
|
return "Name: \(state)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
public static func == (lhs: ParserState, rhs: ParserState) -> Bool {
|
||||||
return lhs.state_name == rhs.state_name
|
return lhs.state == rhs.state
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a ParserState
|
/// Construct a ParserState
|
||||||
public init(
|
public init(
|
||||||
name: String, withLocalElements localElements: [EvaluatableStatement]?,
|
name: Identifier, withLocalElements localElements: [EvaluatableStatement]?,
|
||||||
withStatements stmts: [EvaluatableStatement]?,
|
withStatements stmts: [EvaluatableStatement]?,
|
||||||
withTransition transitionStatement: ParserTransitionStatement
|
withTransition transitionStatement: ParserTransitionStatement
|
||||||
) {
|
) {
|
||||||
state_name = name
|
state = name
|
||||||
transition = transitionStatement
|
transition = transitionStatement
|
||||||
local_elements = localElements ?? Array()
|
local_elements = localElements ?? Array()
|
||||||
statements = stmts ?? Array()
|
statements = stmts ?? Array()
|
||||||
@@ -135,8 +135,8 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
return self == accept || self == reject
|
return self == accept || self == reject
|
||||||
}
|
}
|
||||||
|
|
||||||
if let next_state_name = transition.next_state_name,
|
if let next_state = transition.next_state,
|
||||||
let next_state = states.find(withName: next_state_name)
|
let next_state = states.find(withIdentifier: next_state)
|
||||||
{
|
{
|
||||||
self.next_state = next_state
|
self.next_state = next_state
|
||||||
return true
|
return true
|
||||||
@@ -148,8 +148,8 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
/// (private) constructor (no transition)
|
/// (private) constructor (no transition)
|
||||||
///
|
///
|
||||||
/// accept and reject are the only final states and they are constructed internally.
|
/// accept and reject are the only final states and they are constructed internally.
|
||||||
init(name: String) {
|
init(name: Identifier) {
|
||||||
state_name = name
|
state = name
|
||||||
transition = .none
|
transition = .none
|
||||||
local_elements = Array()
|
local_elements = Array()
|
||||||
statements = Array()
|
statements = Array()
|
||||||
@@ -158,7 +158,7 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
public func direct_transition() -> Bool {
|
public func direct_transition() -> Bool {
|
||||||
return
|
return
|
||||||
if let transition = self.transition,
|
if let transition = self.transition,
|
||||||
transition.next_state_name != nil
|
transition.next_state != nil
|
||||||
{
|
{
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
@@ -167,8 +167,8 @@ public class ParserState: Equatable, CustomStringConvertible, Comparable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nonisolated(unsafe) public let accept: ParserState = ParserState(name: "accept")
|
nonisolated(unsafe) public let accept: ParserState = ParserState(name: Identifier(name: "accept"))
|
||||||
nonisolated(unsafe) public let reject: ParserState = ParserState(name: "reject")
|
nonisolated(unsafe) public let reject: ParserState = ParserState(name: Identifier(name: "reject"))
|
||||||
|
|
||||||
public struct ParserStates {
|
public struct ParserStates {
|
||||||
public var states: [ParserState] = Array()
|
public var states: [ParserState] = Array()
|
||||||
@@ -177,9 +177,9 @@ public struct ParserStates {
|
|||||||
return states.count
|
return states.count
|
||||||
}
|
}
|
||||||
|
|
||||||
public func find(withName name: String) -> ParserState? {
|
public func find(withIdentifier id: Identifier) -> ParserState? {
|
||||||
for state in states {
|
for state in states {
|
||||||
if state.state_name == name {
|
if state.state == id {
|
||||||
return .some(state)
|
return .some(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,7 +233,7 @@ public struct Parser: P4Type {
|
|||||||
|
|
||||||
public func findStartState() -> ParserState? {
|
public func findStartState() -> ParserState? {
|
||||||
for state in states.states {
|
for state in states.states {
|
||||||
if state.state_name == "start" {
|
if state.state == Identifier(name:"start") {
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ extension TypedIdentifier: ParseableEvaluatableExpression {
|
|||||||
let value_capture = result.captures(named: "identifier")
|
let value_capture = result.captures(named: "identifier")
|
||||||
guard
|
guard
|
||||||
case Result.Ok(let type) = scopes.lookup(
|
case Result.Ok(let type) = scopes.lookup(
|
||||||
identifier: Identifier(name: value_capture[0].node.text!))
|
identifier: Common.Identifier(name: value_capture[0].node.text!))
|
||||||
else {
|
else {
|
||||||
return .Error(Error(withMessage: "Cannot find \(result.captures[0].node.text!) in scope"))
|
return .Error(Error(withMessage: "Cannot find \(result.captures[0].node.text!) in scope"))
|
||||||
}
|
}
|
||||||
@@ -178,3 +178,27 @@ struct Expression {
|
|||||||
return Result.Error(Error(withMessage: "Could not parse into expression."))
|
return Result.Error(Error(withMessage: "Could not parse into expression."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct LValue {
|
||||||
|
public static func Parse(
|
||||||
|
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
|
) -> Result<Common.Identifier> {
|
||||||
|
return if let node_text_value = node.text {
|
||||||
|
.Ok(Common.Identifier(name: node_text_value))
|
||||||
|
} else {
|
||||||
|
.Error(Error(withMessage: "Could not parse an identifier for an LValue"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Identifier {
|
||||||
|
public static func Parse(
|
||||||
|
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
|
) -> Result<Common.Identifier> {
|
||||||
|
return if let node_text_value = node.text {
|
||||||
|
.Ok(Common.Identifier(name: node_text_value))
|
||||||
|
} else {
|
||||||
|
.Error(Error(withMessage: "Could not parse an identifier from \(node)"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+104
-74
@@ -28,40 +28,21 @@ extension ParserAssignmentStatement: ParseableStatement {
|
|||||||
public static func Parse(
|
public static func Parse(
|
||||||
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
|
||||||
) -> Result<(EvaluatableStatement?, LexicalScopes)> {
|
) -> Result<(EvaluatableStatement?, LexicalScopes)> {
|
||||||
guard
|
|
||||||
let parser_assignment_statement_query = try? SwiftTreeSitter.Query(
|
if node.nodeType != "assignmentStatement" {
|
||||||
language: p4lang,
|
return Result.Ok((.none, scopes))
|
||||||
data: String(
|
}
|
||||||
"(assignmentStatement (expression) @lvalue (assignment) (expression) @value) @assignment"
|
|
||||||
).data(using: String.Encoding.utf8)!)
|
guard let lvalue_node = node.child(at: 0),
|
||||||
|
lvalue_node.nodeType == "expression"
|
||||||
else {
|
else {
|
||||||
return Result.Ok((.none, scopes))
|
return Result.Error(Error(withMessage: "Missing lvalue in assignment statement"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let qr = parser_assignment_statement_query.execute(node: node, in: tree)
|
guard let rvalue_node = node.child(at: 2),
|
||||||
guard let parser_assignment_statement = qr.next() else {
|
rvalue_node.nodeType == "expression"
|
||||||
return Result.Ok((.none, scopes))
|
|
||||||
}
|
|
||||||
|
|
||||||
let assignment_capture = parser_assignment_statement.captures(named: "assignment")
|
|
||||||
let lvalue_capture = parser_assignment_statement.captures(named: "lvalue")
|
|
||||||
let rvalue_capture = parser_assignment_statement.captures(named: "value")
|
|
||||||
|
|
||||||
// There must be a type name and a variable name
|
|
||||||
guard !lvalue_capture.isEmpty,
|
|
||||||
!rvalue_capture.isEmpty,
|
|
||||||
let lvalue_expression_raw = lvalue_capture[0].node.text
|
|
||||||
else {
|
else {
|
||||||
return Result.Error(
|
return Result.Error(Error(withMessage: "Missing lvalue in assignment statement"))
|
||||||
Error(withMessage: "Could not parse a parser assignment statement"))
|
|
||||||
}
|
|
||||||
|
|
||||||
let rvalue_node = rvalue_capture[0].node
|
|
||||||
let lvalue_node = lvalue_capture[0].node
|
|
||||||
let assignment_node = assignment_capture[0].node
|
|
||||||
|
|
||||||
if assignment_node.parent != node.parent {
|
|
||||||
return Result.Ok((.none, scopes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let maybe_parsed_rvalue = Expression.Parse(node: rvalue_node, inTree: tree, withScopes: scopes)
|
let maybe_parsed_rvalue = Expression.Parse(node: rvalue_node, inTree: tree, withScopes: scopes)
|
||||||
@@ -69,7 +50,10 @@ extension ParserAssignmentStatement: ParseableStatement {
|
|||||||
return Result.Error(maybe_parsed_rvalue.error()!)
|
return Result.Error(maybe_parsed_rvalue.error()!)
|
||||||
}
|
}
|
||||||
|
|
||||||
let lvalue_identifier = Identifier(name: lvalue_expression_raw)
|
let maybe_parsed_lvalue = LValue.Parse(node: lvalue_node, inTree: tree, withScopes: scopes)
|
||||||
|
guard case .Ok(let lvalue_identifier) = maybe_parsed_lvalue else {
|
||||||
|
return Result.Error(maybe_parsed_lvalue.error()!)
|
||||||
|
}
|
||||||
guard case Result.Ok(let lvalue_type) = scopes.lookup(identifier: lvalue_identifier) else {
|
guard case Result.Ok(let lvalue_type) = scopes.lookup(identifier: lvalue_identifier) else {
|
||||||
return Result.Error(
|
return Result.Error(
|
||||||
Error(withMessage: "Cannot assign to variable \(lvalue_identifier) not in scope"))
|
Error(withMessage: "Cannot assign to variable \(lvalue_identifier) not in scope"))
|
||||||
@@ -149,44 +133,47 @@ public struct Parser {
|
|||||||
public struct TransitionSelectExpressionCaseStatement {
|
public struct TransitionSelectExpressionCaseStatement {
|
||||||
static func Parse(
|
static func Parse(
|
||||||
node: Node, inTree tree: MutableTree, withLexicalScopes scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withLexicalScopes scopes: LexicalScopes
|
||||||
) -> Result<([KeysetExpression], LexicalScopes)> {
|
) -> Result<(KeysetExpression, LexicalScopes)> {
|
||||||
guard
|
if node.nodeType != "selectCase" {
|
||||||
let transition_selection_expression_query = try? SwiftTreeSitter.Query(
|
return Result.Error(Error(withMessage: "Expected select case not found"))
|
||||||
language: p4lang,
|
}
|
||||||
data: String(
|
|
||||||
"((keysetExpression (expression) @ks) (colon) (identifier) @next-state)"
|
guard let keysetexpression_node = node.child(at: 0),
|
||||||
).data(using: String.Encoding.utf8)!)
|
keysetexpression_node.nodeType == "keysetExpression"
|
||||||
else {
|
else {
|
||||||
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
|
return Result.Error(Error(withMessage: "Missing keyset expression in select case"))
|
||||||
}
|
}
|
||||||
|
|
||||||
let qr = transition_selection_expression_query.execute(node: node, in: tree)
|
guard let targetstate_node = node.child(at: 2),
|
||||||
|
targetstate_node.nodeType == "identifier"
|
||||||
|
else {
|
||||||
|
return Result.Error(Error(withMessage: "Missing target state in select case"))
|
||||||
|
}
|
||||||
|
|
||||||
var kses: [KeysetExpression] = Array()
|
let maybe_parsed_keysetexpression = Expression.Parse(
|
||||||
|
node: keysetexpression_node, inTree: tree, withScopes: scopes)
|
||||||
|
guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else {
|
||||||
|
return Result.Error(maybe_parsed_keysetexpression.error()!)
|
||||||
|
}
|
||||||
|
|
||||||
for expression in qr {
|
let maybe_parsed_targetstate = Identifier.Parse(
|
||||||
let next_state_name = expression.captures[1].node.text!
|
node: targetstate_node, inTree: tree, withScopes: scopes)
|
||||||
if case .Error(let e) = Expression.Parse(
|
guard case .Ok(let targetstate) = maybe_parsed_targetstate else {
|
||||||
node: expression.captures[0].node, inTree: tree, withScopes: scopes
|
return Result.Error(maybe_parsed_targetstate.error()!)
|
||||||
)
|
}
|
||||||
.map(block: { expression in
|
|
||||||
kses.append(
|
return .Ok(
|
||||||
|
(
|
||||||
KeysetExpression(
|
KeysetExpression(
|
||||||
withKey: expression, withNextStateName: next_state_name))
|
withKey: keysetexpression, withNextState: targetstate), scopes
|
||||||
return .Ok(expression)
|
))
|
||||||
}) {
|
|
||||||
return .Error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return .Ok((kses, scopes))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct TransitionSelectExpression {
|
public struct TransitionSelectExpression {
|
||||||
static func Parse(
|
static func Parse(
|
||||||
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
|
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
|
||||||
) -> Result<(ParserTransitionStatement?, LexicalScopes)> {
|
) -> Result<(ParserTransitionSelectExpression, LexicalScopes)> {
|
||||||
guard
|
guard
|
||||||
let transition_selection_expression_query = try? SwiftTreeSitter.Query(
|
let transition_selection_expression_query = try? SwiftTreeSitter.Query(
|
||||||
language: p4lang,
|
language: p4lang,
|
||||||
@@ -206,22 +193,50 @@ public struct Parser {
|
|||||||
let selector = query_result.captures(named: "selector")
|
let selector = query_result.captures(named: "selector")
|
||||||
let body = query_result.captures(named: "body")
|
let body = query_result.captures(named: "body")
|
||||||
|
|
||||||
return Expression.Parse(node: selector[0].node, inTree: tree, withScopes: scopes).map {
|
if selector.isEmpty {
|
||||||
expression in
|
return .Error(
|
||||||
return
|
Error(withMessage: "Could not find transition select expression selector expression"))
|
||||||
switch TransitionSelectExpressionCaseStatement.Parse(
|
}
|
||||||
node: body[0].node, inTree: tree, withLexicalScopes: scopes)
|
let selector_node = selector[0].node
|
||||||
{
|
let maybe_selector = Expression.Parse(node: selector_node, inTree: tree, withScopes: scopes)
|
||||||
case .Ok((let kse, let newLexicalScopes)):
|
guard case .Ok(let selector) = maybe_selector else {
|
||||||
Result<(ParserTransitionStatement?, LexicalScopes)>.Ok(
|
return .Error(
|
||||||
(
|
Error(
|
||||||
ParserTransitionStatement(
|
withMessage:
|
||||||
withTransitionExpression: ParserTransitionSelectExpression(
|
"Could not parse transition select expression selector expression: \(maybe_selector.error()!)"
|
||||||
withSelector: expression, withKeysetExpressions: kse)), newLexicalScopes
|
|
||||||
))
|
))
|
||||||
case .Error(let e): Result.Error(e)
|
}
|
||||||
|
|
||||||
|
if body.isEmpty {
|
||||||
|
return .Error(Error(withMessage: "Could not find transition select expression body"))
|
||||||
|
}
|
||||||
|
let body_node = body[0].node
|
||||||
|
var kses: [KeysetExpression] = Array()
|
||||||
|
for childidx in 0..<body_node.childCount {
|
||||||
|
|
||||||
|
if !childidx.isMultiple(of: 2) {
|
||||||
|
let maybe_semicolon = body_node.child(at: childidx)!
|
||||||
|
if maybe_semicolon.nodeType != "semicolon" {
|
||||||
|
return .Error(Error(withMessage: "Expected a semicolon but saw \(maybe_semicolon)"))
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let maybe_parsed_kse = TransitionSelectExpressionCaseStatement.Parse(
|
||||||
|
node: body_node.child(at: childidx)!, inTree: tree, withLexicalScopes: scopes)
|
||||||
|
if case .Ok((let parsed_kse, _)) = maybe_parsed_kse {
|
||||||
|
kses.append(parsed_kse)
|
||||||
|
} else {
|
||||||
|
return .Error(
|
||||||
|
Error(withMessage: "Error when parsing select case: \(maybe_parsed_kse.error()!)"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return .Ok(
|
||||||
|
(
|
||||||
|
ParserTransitionSelectExpression(withSelector: selector, withKeysetExpressions: kses),
|
||||||
|
scopes
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,11 +258,25 @@ public struct Parser {
|
|||||||
|
|
||||||
if let next_state_result = qr.next() {
|
if let next_state_result = qr.next() {
|
||||||
let transition_capture = next_state_result.captures(named: "next-state")
|
let transition_capture = next_state_result.captures(named: "next-state")
|
||||||
|
let maybe_parsed_next_state = Identifier.Parse(
|
||||||
|
node: transition_capture[0].node, inTree: tree, withScopes: scopes)
|
||||||
|
if case .Ok(let next_state) = maybe_parsed_next_state {
|
||||||
return .Ok(
|
return .Ok(
|
||||||
(ParserTransitionStatement(withNextState: transition_capture[0].node.text!), scopes))
|
(ParserTransitionStatement(withNextState: next_state), scopes))
|
||||||
|
} else {
|
||||||
|
return .Error(
|
||||||
|
Error(
|
||||||
|
withMessage:
|
||||||
|
"Could not parse the next state in a transition statement: \(maybe_parsed_next_state.error()!)"
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return TransitionSelectExpression.Parse(node: node, inTree: tree, withScope: scopes)
|
return switch TransitionSelectExpression.Parse(node: node, inTree: tree, withScope: scopes) {
|
||||||
|
case .Ok((let tse, _)):
|
||||||
|
.Ok((ParserTransitionStatement(withTransitionExpression: tse), scopes))
|
||||||
|
case .Error(let e): .Error(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,7 +354,8 @@ public struct Parser {
|
|||||||
// TODO: Now that scopes are involved, doing this out of order will not work!
|
// TODO: Now that scopes are involved, doing this out of order will not work!
|
||||||
guard !state_name_capture.isEmpty,
|
guard !state_name_capture.isEmpty,
|
||||||
!transition_capture.isEmpty,
|
!transition_capture.isEmpty,
|
||||||
let parsed_state_name = state_name_capture[0].node.text
|
case .Ok(let parsed_state_name) = Identifier.Parse(
|
||||||
|
node: state_name_capture[0].node, inTree: tree, withScopes: scopes)
|
||||||
else {
|
else {
|
||||||
return Result.Error(Error(withMessage: "Could not parse a parser declaration"))
|
return Result.Error(Error(withMessage: "Could not parse a parser declaration"))
|
||||||
}
|
}
|
||||||
@@ -349,7 +379,7 @@ public struct Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static func Parse(
|
static func Parse(
|
||||||
withName name: Identifier, node: Node, inTree tree: MutableTree,
|
withName name: Common.Identifier, node: Node, inTree tree: MutableTree,
|
||||||
withLexicalScopes scopes: LexicalScopes
|
withLexicalScopes scopes: LexicalScopes
|
||||||
) -> Result<(P4Lang.Parser, LexicalScopes)> {
|
) -> Result<(P4Lang.Parser, LexicalScopes)> {
|
||||||
guard
|
guard
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ public struct Program {
|
|||||||
|
|
||||||
for parser_declaration in parser_qc {
|
for parser_declaration in parser_qc {
|
||||||
switch Parser.Parse(
|
switch Parser.Parse(
|
||||||
withName: Identifier(name: parser_declaration.nodes[0].text!),
|
withName: Common.Identifier(name: parser_declaration.nodes[0].text!),
|
||||||
node: parser_declaration.nodes[1], inTree: tree, withLexicalScopes: program_scope)
|
node: parser_declaration.nodes[1], inTree: tree, withLexicalScopes: program_scope)
|
||||||
{
|
{
|
||||||
case Result.Ok((let parser, let new_program_scope)):
|
case Result.Ok((let parser, let new_program_scope)):
|
||||||
|
|||||||
@@ -207,9 +207,9 @@ extension VariableDeclarationStatement: ParseableStatement {
|
|||||||
return Result.Ok(
|
return Result.Ok(
|
||||||
(
|
(
|
||||||
VariableDeclarationStatement(
|
VariableDeclarationStatement(
|
||||||
identifier: Identifier(name: variable_name), withInitializer: rvalue),
|
identifier: Common.Identifier(name: variable_name), withInitializer: rvalue),
|
||||||
scopes.declare(
|
scopes.declare(
|
||||||
identifier: Identifier(name: variable_name), withValue: declaration_p4_type)
|
identifier: Common.Identifier(name: variable_name), withValue: declaration_p4_type)
|
||||||
))
|
))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ public struct ParserStateSelectTransition: ParserStateInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension ParserState: Compilable {
|
extension ParserState: Compilable {
|
||||||
public typealias ToCompile = (ParserState, [String: ParserStateInstance])
|
public typealias ToCompile = (ParserState, [Identifier: ParserStateInstance])
|
||||||
public typealias Compiled = ParserStateInstance
|
public typealias Compiled = ParserStateInstance
|
||||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||||
let (state, current) = parser
|
let (state, current) = parser
|
||||||
@@ -142,7 +142,7 @@ extension ParserState: Compilable {
|
|||||||
{
|
{
|
||||||
return .Ok(
|
return .Ok(
|
||||||
ParserStateDirectTransition(
|
ParserStateDirectTransition(
|
||||||
currrent_state: state, next_state: current[transition_statement.next_state_name!]!))
|
currrent_state: state, next_state: current[transition_statement.next_state!]!))
|
||||||
}
|
}
|
||||||
|
|
||||||
if let transition_select_statement = state.transition,
|
if let transition_select_statement = state.transition,
|
||||||
@@ -153,9 +153,9 @@ extension ParserState: Compilable {
|
|||||||
var states: [any ParserStateInstance] = Array()
|
var states: [any ParserStateInstance] = Array()
|
||||||
|
|
||||||
for kse in transition_select_expression.keyset_expressions {
|
for kse in transition_select_expression.keyset_expressions {
|
||||||
guard let next_state = current[kse.next_state_name] else {
|
guard let next_state = current[kse.next_state_identifier] else {
|
||||||
return .Error(
|
return .Error(
|
||||||
Error(withMessage: "Cannot find \(kse.next_state_name) as transition target"))
|
Error(withMessage: "Cannot find \(kse.next_state_identifier) as transition target"))
|
||||||
}
|
}
|
||||||
keys.append(kse.key)
|
keys.append(kse.key)
|
||||||
states.append(next_state)
|
states.append(next_state)
|
||||||
@@ -174,21 +174,21 @@ extension ParserStates: Compilable {
|
|||||||
public typealias ToCompile = ParserStates
|
public typealias ToCompile = ParserStates
|
||||||
public typealias Compiled = ParserStateInstance
|
public typealias Compiled = ParserStateInstance
|
||||||
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
public static func compile(_ parser: ToCompile) -> Result<Compiled> {
|
||||||
var compiled_states = [String: ParserStateInstance]()
|
var compiled_states = [Identifier: ParserStateInstance]()
|
||||||
|
|
||||||
compiled_states["accept"] = ParserStateNoTransition(currrent_state: accept)
|
compiled_states[Identifier(name: "accept")] = ParserStateNoTransition(currrent_state: accept)
|
||||||
compiled_states["reject"] = ParserStateNoTransition(currrent_state: reject)
|
compiled_states[Identifier(name: "reject")] = ParserStateNoTransition(currrent_state: reject)
|
||||||
|
|
||||||
// TODO: We assume that states are in transition-order!
|
// TODO: We assume that states are in transition-order!
|
||||||
for state in parser.states {
|
for state in parser.states {
|
||||||
switch ParserState.compile((state, compiled_states)) {
|
switch ParserState.compile((state, compiled_states)) {
|
||||||
case .Ok(let compiled): compiled_states[state.state_name] = compiled
|
case .Ok(let compiled): compiled_states[state.state] = compiled
|
||||||
case .Error(let e): return .Error(e)
|
case .Error(let e): return .Error(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now, find the start state:
|
// Now, find the start state:
|
||||||
if let start_state = compiled_states["start"] {
|
if let start_state = compiled_states[Identifier(name: "start")] {
|
||||||
return .Ok(start_state)
|
return .Ok(start_state)
|
||||||
} else {
|
} else {
|
||||||
return .Error(Error(withMessage: "No start state defined"))
|
return .Error(Error(withMessage: "No start state defined"))
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ import TreeSitterP4
|
|||||||
|
|
||||||
#expect(parser.states.count() == 1)
|
#expect(parser.states.count() == 1)
|
||||||
|
|
||||||
let state = try! #require(parser.states.find(withName: "start"))
|
let state = try! #require(parser.states.find(withIdentifier: Identifier(name: "start")))
|
||||||
#expect(state.state_name == "start")
|
#expect(state.state == Identifier(name: "start"))
|
||||||
#expect(state.statements.count == 1)
|
#expect(state.statements.count == 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user