Refactor Parsing -> Compiling

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-02 08:25:29 -05:00
parent cfe78a9b29
commit 6dba17c97a
21 changed files with 550 additions and 489 deletions
+206
View File
@@ -0,0 +1,206 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import P4Lang
import SwiftTreeSitter
import TreeSitterP4
protocol CompilableExpression {
static func compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression?>
}
extension TypedIdentifier: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression?> {
guard
let parser_statement_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(expression (identifier) @identifier)"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let qr = parser_statement_query.execute(node: node, in: tree)
guard let result = qr.next() else {
return .Ok(.none)
}
let value_capture = result.captures(named: "identifier")
guard
case Result.Ok(let type) = scopes.lookup(
identifier: Common.Identifier(name: value_capture[0].node.text!))
else {
return .Error(Error(withMessage: "Cannot find \(result.captures[0].node.text!) in scope"))
}
return .Ok(TypedIdentifier(name: result.captures[0].node.text!, withType: type))
}
}
extension P4BooleanValue: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression?> {
guard
let true_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(expression (true))"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let true_qr = true_query.execute(node: node, in: tree)
if true_qr.next() != nil {
return .Ok(P4BooleanValue(withValue: true))
}
guard
let false_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(expression (false))"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let false_qr = false_query.execute(node: node, in: tree)
if false_qr.next() != nil {
return .Ok(P4BooleanValue(withValue: false))
}
return .Ok(.none)
}
}
extension P4IntValue: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression?> {
guard
let integer_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(expression (integer) @value)"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let integer_qr = integer_query.execute(node: node, in: tree)
guard let result = integer_qr.next() else {
return .Ok(.none)
}
let value_capture = result.captures(named: "value")
if let parsed_int = Int(value_capture[0].node.text!) {
return .Ok(P4IntValue(withValue: parsed_int))
} else {
print("HERE!!")
return .Error(Error(withMessage: "Failed to parse integer: \(result.captures[0].node.text!)"))
}
}
}
extension P4StringValue: CompilableExpression {
static func compile(
node: SwiftTreeSitter.Node, inTree tree: SwiftTreeSitter.MutableTree,
withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression?> {
guard
let string_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(expression (string_literal) @value)"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let string_qr = string_query.execute(node: node, in: tree)
guard let result = string_qr.next() else {
return .Ok(.none)
}
return .Ok(P4StringValue(withValue: result.captures[0].node.text!))
}
}
struct Expression {
public static func Compile(
node: Node, inTree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<EvaluatableExpression> {
let localElementsParsers: [CompilableExpression.Type] = [
P4BooleanValue.self, P4StringValue.self, P4IntValue.self, TypedIdentifier.self,
]
for le_parser in localElementsParsers {
switch le_parser.compile(
node: node, inTree: inTree, withScopes: scopes)
{
case .Ok(.some(let parsed)): return .Ok(parsed)
case .Error(let e): return .Error(e)
default: continue
}
}
return Result.Error(Error(withMessage: "\(node.range): Could not parse into expression"))
}
}
struct LValue {
public static func Compile(
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 Compile(
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)"))
}
}
}
+21
View File
@@ -0,0 +1,21 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
public typealias LexicalScope = Scope<P4Type>
public typealias LexicalScopes = Scopes<P4Type>
+451
View File
@@ -0,0 +1,451 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
let p4lang = Language(tree_sitter_p4())
public func ErrorOnNode(node: Node, withError error: String) -> Error {
return Error(withMessage: "\(node.range): \(error)")
}
extension ParserAssignmentStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
if node.nodeType != "assignmentStatement" {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find expected assignment statement"))
}
guard let lvalue_node = node.child(at: 0),
lvalue_node.nodeType == "expression"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Missing lvalue in assignment statement"))
}
guard let rvalue_node = node.child(at: 2),
rvalue_node.nodeType == "expression"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Missing rvalue in assignment statement"))
}
let maybe_parsed_rvalue = Expression.Compile(node: rvalue_node, inTree: tree, withScopes: scopes)
guard case Result.Ok(let rvalue) = maybe_parsed_rvalue else {
return Result.Error(maybe_parsed_rvalue.error()!)
}
let maybe_parsed_lvalue = LValue.Compile(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 {
return Result.Error(
ErrorOnNode(
node: lvalue_node,
withError: "Cannot assign to variable \(lvalue_identifier) not in scope"))
}
if rvalue.type().eq(rhs: lvalue_type) {
return Result.Ok(
(
ParserAssignmentStatement(
withLValue: TypedIdentifier(name: lvalue_node.text!, withType: lvalue_type),
withValue: rvalue
), scopes
))
} else {
return Result.Error(
ErrorOnNode(
node: node,
withError:
"Cannot assign value of type \(rvalue.type()) to \(lvalue_identifier) (with type \(lvalue_type))"
))
}
}
}
public struct Parser {
public struct LocalElements {
static func Compile(
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
let localElementsParsers: [String: CompilableStatement.Type] = [
"variableDeclaration": VariableDeclarationStatement.self
]
guard let parser = localElementsParsers[node.nodeType ?? ""] else {
return Result.Error(
ErrorOnNode(
node: node, withError: "Unparseable statement type (\(node.nodeType))"))
}
switch parser.Compile(node: node, inTree: tree, withScopes: scopes) {
case Result.Ok(let (parsed, parsed_updated_scopes)):
return Result.Ok((parsed, parsed_updated_scopes))
case Result.Error(let e):
return Result.Error(Error(withMessage: "Failed to parse local element: \(e)"))
}
}
}
public struct Statement {
static func Compile(
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
if node.nodeType != "parserStatement" && node.nodeType != "statement" {
return Result.Error(ErrorOnNode(node: node, withError: "Missing expected parser statement"))
}
let statement = node.child(at: 0)!
let statementParsers: [String: CompilableStatement.Type] = [
"assignmentStatement": ParserAssignmentStatement.self,
"expressionStatement": ExpressionStatement.self,
"variableDeclaration": VariableDeclarationStatement.self,
"conditionalStatement": ConditionalStatement.self, "blockStatement": BlockStatement.self,
]
// Iterate through statement parsers and give each one a chance.
guard let parser = statementParsers[statement.nodeType ?? ""] else {
return Result.Error(
ErrorOnNode(
node: statement, withError: "Unparseable statement type (\(statement.nodeType))"))
}
switch parser.Compile(node: statement, inTree: tree, withScopes: scopes) {
case Result.Ok(let (parsed, updatedLexicalScopes)):
return .Ok((parsed, updatedLexicalScopes))
case Result.Error(let e):
return .Error(
ErrorOnNode(node: node, withError: "Failed to parse a statement element: \(e)"))
}
}
}
public struct TransitionSelectExpressionCaseStatement {
static func Compile(
node: Node, inTree tree: MutableTree, withLexicalScopes scopes: LexicalScopes
) -> Result<(KeysetExpression, LexicalScopes)> {
if node.nodeType != "selectCase" {
return Result.Error(Error(withMessage: "Expected select case not found"))
}
guard let keysetexpression_node = node.child(at: 0),
keysetexpression_node.nodeType == "keysetExpression"
else {
return Result.Error(Error(withMessage: "Missing keyset expression in select case"))
}
guard let targetstate_node = node.child(at: 2),
targetstate_node.nodeType == "identifier"
else {
return Result.Error(Error(withMessage: "Missing target state in select case"))
}
let maybe_parsed_keysetexpression = Expression.Compile(
node: keysetexpression_node, inTree: tree, withScopes: scopes)
guard case Result.Ok(let keysetexpression) = maybe_parsed_keysetexpression else {
return Result.Error(maybe_parsed_keysetexpression.error()!)
}
let maybe_parsed_targetstate = Identifier.Compile(
node: targetstate_node, inTree: tree, withScopes: scopes)
guard case .Ok(let targetstate) = maybe_parsed_targetstate else {
return Result.Error(maybe_parsed_targetstate.error()!)
}
return .Ok(
(
KeysetExpression(
withKey: keysetexpression, withNextState: targetstate), scopes
))
}
}
public struct TransitionSelectExpression {
static func Compile(
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
) -> Result<(ParserTransitionSelectExpression, LexicalScopes)> {
guard
let transition_selection_expression_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(parserTransitionStatement (transition) (transitionSelectionExpression (selectExpression (select) (expression) @selector (selectBody) @body)))"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let qr = transition_selection_expression_query.execute(node: node, in: tree)
guard let query_result = qr.next() else {
return .Error(Error(withMessage: "Could not find transition select expression"))
}
let selector = query_result.captures(named: "selector")
let body = query_result.captures(named: "body")
if selector.isEmpty {
return .Error(
Error(withMessage: "Could not find transition select expression selector expression"))
}
let selector_node = selector[0].node
let maybe_selector = Expression.Compile(node: selector_node, inTree: tree, withScopes: scopes)
guard case .Ok(let selector) = maybe_selector else {
return .Error(
Error(
withMessage:
"Could not parse transition select expression selector expression: \(maybe_selector.error()!)"
))
}
if body.isEmpty {
return .Error(Error(withMessage: "Could not find transition select expression body"))
}
let body_node = body[0].node
var kses: [KeysetExpression] = Array()
var kses_errors: [Error] = Array()
body_node.enumerateNamedChildren { current_node in
let maybe_parsed_kse = TransitionSelectExpressionCaseStatement.Compile(
node: current_node, inTree: tree, withLexicalScopes: scopes)
if case .Ok((let parsed_kse, _)) = maybe_parsed_kse {
kses.append(parsed_kse)
} else {
kses_errors.append(Error(withMessage: "\(maybe_parsed_kse.error()!)"))
}
}
if !kses_errors.isEmpty {
return .Error(
Error(
withMessage: "Error(s) parsing select cases: "
+ (kses_errors.map { error in
return "\(error.msg)"
}.joined(separator: ";\n"))))
}
return .Ok(
(
ParserTransitionSelectExpression(withSelector: selector, withKeysetExpressions: kses),
scopes
))
}
}
public struct TransitionStatement {
static func Compile(
node: Node, inTree tree: MutableTree, withScope scopes: LexicalScopes
) -> Result<(ParserTransitionStatement, LexicalScopes)> {
guard
let next_state_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(parserTransitionStatement (transition) (transitionSelectionExpression (identifier) @next-state))"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(Error(withMessage: "Could not compile the tree sitter query"))
}
let qr = next_state_query.execute(node: node, in: tree)
if let next_state_result = qr.next() {
let transition_capture = next_state_result.captures(named: "next-state")
let maybe_parsed_next_state = Identifier.Compile(
node: transition_capture[0].node, inTree: tree, withScopes: scopes)
if case .Ok(let next_state) = maybe_parsed_next_state {
return .Ok(
(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 switch TransitionSelectExpression.Compile(node: node, inTree: tree, withScope: scopes) {
case .Ok((let tse, _)):
.Ok((ParserTransitionStatement(withTransitionExpression: tse), scopes))
case .Error(let e): .Error(e)
}
}
}
public struct Statements {
static func Compile(
node: Node, inTree tree: MutableTree, withLexicalScopes scopes: LexicalScopes
) -> Result<([EvaluatableStatement], LexicalScopes)> {
if node.nodeType != "statements" && node.nodeType != "parserStatements" {
return Result.Error(ErrorOnNode(node: node, withError: "Did not find expected statements"))
}
var parse_err: Error? = .none
var current_scopes = scopes
var parsed_s: [EvaluatableStatement] = Array()
node.enumerateNamedChildren { node in
switch Statement.Compile(
node: node, inTree: tree, withScope: current_scopes)
{
case .Ok((let parsed_statement, let updated_scopes)):
current_scopes = updated_scopes
parsed_s.append(parsed_statement)
case .Error(let e):
parse_err = e
}
}
if let parse_err = parse_err {
return Result.Error(parse_err)
}
return Result.Ok((parsed_s, current_scopes))
}
}
public struct State {
static func Compile(
node: Node, inTree tree: MutableTree, withLexicalScopes scopes: LexicalScopes
) -> Result<(ParserState, LexicalScopes)> {
var currentChildIdx = 1
var currentChildIdxSafe = 2
var currentChild: Node? = .none
guard let node_type = node.nodeType,
node_type == "parserState"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find a parser state declaration"))
}
if node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Missing state name in state declaration"))
}
currentChild = node.child(at: 1)
let maybe_state_identifier = Identifier.Compile(
node: currentChild!, inTree: tree, withScopes: scopes)
guard case Result.Ok(let state_identifier) = maybe_state_identifier else {
return Result.Error(maybe_state_identifier.error()!)
}
// Skip the '{'
currentChildIdx += 2
currentChildIdxSafe += 2
var parse_err: Error? = .none
var current_scopes: LexicalScopes = LexicalScopes()
var parsed_s: [EvaluatableStatement] = Array()
if node.childCount < currentChildIdxSafe {
return Result.Error(ErrorOnNode(node: node, withError: "Missing body of state declaration"))
}
currentChild = node.child(at: currentChildIdx)
if currentChild!.nodeType == "parserStatements" {
switch Statements.Compile(
node: currentChild!, inTree: tree, withLexicalScopes: scopes.enter())
{
case .Ok(let (state_statements, updated_scopes)):
parsed_s = state_statements
current_scopes = updated_scopes
case .Error(let error):
parse_err = error
}
currentChildIdx += 1
currentChildIdxSafe += 1
}
if let parse_err = parse_err {
return Result.Error(parse_err)
}
if node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Missing transition statement of state declaration"))
}
currentChild = node.child(at: currentChildIdx)
switch TransitionStatement.Compile(
node: currentChild!, inTree: tree, withScope: current_scopes)
{
case .Ok(let (transition_statement, new_scopes)):
return Result.Ok(
(
ParserState(
name: state_identifier, withStatements: parsed_s,
withTransition: transition_statement), new_scopes
))
case .Error(let e): return .Error(e)
}
}
}
static func Compile(
withName name: Common.Identifier, node: Node, inTree tree: MutableTree,
withLexicalScopes scopes: LexicalScopes
) -> Result<(P4Lang.Parser, LexicalScopes)> {
guard
let parser_state_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(parserStates) @parser-states"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(
Error(withMessage: "Could not compile the parser state tree sitter query"))
}
var parser = P4Lang.Parser(withName: name)
// Build a state from each one listed.
let qr = parser_state_query.execute(node: node, in: tree)
let qr_value = qr.next()!
let captures = qr_value.captures(named: "parser-states")
var error: Error? = .none
var parser_scopes = scopes
// TODO: Assert that there is only one.
captures[0].node.enumerateChildren { parser_state in
switch Parser.State.Compile(node: parser_state, inTree: tree, withLexicalScopes: scopes) {
case Result.Ok(let (state, new_parser_scopes)):
parser.states = parser.states.append(state: state)
parser_scopes = new_parser_scopes
case Result.Error(let e): error = e
}
}
if let error = error {
return .Error(error)
}
return Result.Ok((parser, parser_scopes))
}
}
+75
View File
@@ -0,0 +1,75 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
public struct Program {
public static func Compile(_ source: String) -> Result<P4Lang.Program> {
let p = SwiftTreeSitter.Parser.init()
do {
try p.setLanguage(p4lang)
} catch {
return Result.Error(Error(withMessage: "Could not configure the P4 parser"))
}
let result = p.parse(source)
guard let tree = result,
!tree.isError(lang: p4lang),
!tree.containsMissing(lang: p4lang)
else {
return Result.Error(Error(withMessage: "Could not compile the P4 program"))
}
guard
let parser_declaration_query = try? SwiftTreeSitter.Query(
language: p4lang,
data: String(
"(parserDeclaration (parserType parser_name: (identifier) @parser-name) (parserStates) @parser-states)"
).data(using: String.Encoding.utf8)!)
else {
return Result.Error(
Error(withMessage: "Could not compile the parser declaration tree sitter query"))
}
var program = P4Lang.Program()
// Set up a lexical scope for parsing.
var program_scope = LexicalScopes().enter()
let parser_qc = parser_declaration_query.execute(in: tree)
for parser_declaration in parser_qc {
switch Parser.Compile(
withName: Common.Identifier(name: parser_declaration.nodes[0].text!),
node: parser_declaration.nodes[1], inTree: tree, withLexicalScopes: program_scope)
{
case Result.Ok((let parser, let new_program_scope)):
program.types.append(parser)
program_scope = new_program_scope
case Result.Error(let error): return Result.Error(error)
}
}
return Result.Ok(program)
}
}
+37
View File
@@ -0,0 +1,37 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
public protocol CompilableStatement {
static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)>
}
public protocol CompilableValue {
static func CompileValue(withValue value: String) -> Result<P4Value>
}
public protocol CompilableType {
static func CompileType(type: String) -> Result<P4Type?>
}
+253
View File
@@ -0,0 +1,253 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
extension BlockStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
if node.nodeType != "blockStatement" {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find expected block statement"))
}
var currentChildIdx = 0
var currentChildIdxSafe = 1
var currentChild: Node? = .none
if node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Malformed block statement"))
}
currentChild = node.child(at: currentChildIdx)
if currentChild!.nodeType != "{" {
return Result.Error(
ErrorOnNode(node: currentChild!, withError: "Missing { on block statement"))
}
currentChildIdx += 1
currentChildIdxSafe += 1
var statements: [EvaluatableStatement] = Array()
var parse_err: Error? = .none
var new_scopes: LexicalScopes = LexicalScopes()
if node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Malformed block statement"))
}
currentChild = node.child(at: currentChildIdx)
if currentChild!.nodeType == "statements" {
switch Parser.Statements.Compile(node: currentChild!, inTree: tree, withLexicalScopes: scopes.enter()) {
case .Ok(let (parsed_statements, parsed_scopes)):
new_scopes = parsed_scopes
statements = parsed_statements
case .Error(let error):
parse_err = error
}
currentChildIdx += 1
currentChildIdxSafe += 1
}
if let err = parse_err {
return .Error(err)
}
if node.childCount < currentChildIdxSafe {
return Result.Error(
ErrorOnNode(node: node, withError: "Malformed block statement"))
}
currentChild = node.child(at: currentChildIdx)
if currentChild!.nodeType != "}" {
return Result.Error(
ErrorOnNode(node: currentChild!, withError: "Missing } on block statement"))
}
return .Ok((BlockStatement(statements), new_scopes))
}
}
extension ConditionalStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
guard let node_type = node.nodeType,
node_type == "conditionalStatement"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find expected conditional statement"))
}
let maybe_condition_expression = node.child(at: 2)
guard let condition_expression = maybe_condition_expression,
condition_expression.nodeType == "expression"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find condition for conditional statement"))
}
let maybe_thens = node.child(at: 4)
guard let thens = maybe_thens,
thens.nodeType == "statement"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find then statement block for conditional statement"))
}
guard
case .Ok(let condition) = Expression.Compile(
node: condition_expression, inTree: tree, withScopes: scopes)
else {
return Result.Error(
Error(withMessage: "Could not parse a conditional expression in a conditional statement"))
}
guard
case .Ok((let thenns, _)) = Parser.Statement.Compile(
node: thens, inTree: tree, withScope: scopes)
else {
return Result.Error(
Error(
withMessage:
"Could not parse the then block in a conditional statement"))
}
let optional_elss: Result<(any EvaluatableStatement, LexicalScopes)>? =
if let elss = node.child(at: 6) {
.some(
Parser.Statement.Compile(
node: elss, inTree: tree, withScope: scopes))
} else {
.none
}
if let parsed_elss = optional_elss {
guard
case .Ok((let elss, _)) = parsed_elss
else {
return Result.Error(
Error(
withMessage:
"Could not parse the else block in a conditional statement"))
}
return .Ok(
(ConditionalStatement(condition: condition, withThen: thenns, andElse: elss), scopes))
}
return .Ok((ConditionalStatement(condition: condition, withThen: thenns), scopes))
}
}
extension VariableDeclarationStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
guard let node_type = node.nodeType,
node_type == "variableDeclaration"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find expected variable declaration statement"))
}
let maybe_typeref = node.child(at: 0)
guard let typeref = maybe_typeref,
typeref.nodeType == "typeRef"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find type name for variable declaration statement"))
}
let maybe_variablename = node.child(at: 1)
guard let variablename = maybe_variablename,
variablename.nodeType == "identifier"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find identifier name for variable declaration statement"))
}
let maybe_rvalue = node.childCount > 3 ? node.child(at: 3) : .none
guard let rvalue = maybe_rvalue,
rvalue.nodeType == "expression"
else {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find initial value expression for variable declaration statement"))
}
guard
case .Ok(let parsed_variablename) = Identifier.Compile(
node: variablename, inTree: tree, withScopes: scopes.enter())
else {
return Result.Error(
Error(withMessage: "Could not parse variable name"))
}
guard
case .Ok(let parsed_rvalue) = Expression.Compile(
node: rvalue, inTree: tree, withScopes: scopes.enter())
else {
return Result.Error(
Error(
withMessage:
"Could not parse initial value expression in a variable declaration statement"))
}
guard case .Ok(let declaration_p4_type) = Types.CompileBasicType(type: typeref.text!) else {
return Result.Error(
Error(withMessage: "Could not parse a P4 type from \(typeref.text!)"))
}
if parsed_rvalue.type().eq(rhs: declaration_p4_type) {
return Result.Ok(
(
VariableDeclarationStatement(
identifier: parsed_variablename, withInitializer: parsed_rvalue),
scopes.declare(
identifier: parsed_variablename, withValue: declaration_p4_type)
))
} else {
return Result.Error(
Error(
withMessage:
"Cannot initialize \(parsed_variablename) (with type \(declaration_p4_type)) from rvalue with type \(parsed_rvalue.type())"
))
}
}
}
extension ExpressionStatement: CompilableStatement {
public static func Compile(
node: Node, inTree tree: MutableTree, withScopes scopes: LexicalScopes
) -> Result<(EvaluatableStatement, LexicalScopes)> {
if node.nodeType != "expressionStatement" {
return Result.Error(
ErrorOnNode(node: node, withError: "Did not find expected expression statement"))
}
let _ = node.child(at: 0)
return Result.Ok((ExpressionStatement(), scopes))
}
}
+54
View File
@@ -0,0 +1,54 @@
// p4rse, Copyright 2026, Will Hawkins
//
// This file is part of p4rse.
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Common
import P4Lang
import P4Runtime
import SwiftTreeSitter
import TreeSitterExtensions
import TreeSitterP4
extension P4Boolean: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "bool" ? .Ok(P4Boolean.create()) : .Ok(.none)
}
}
extension P4Int: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "int" ? .Ok(P4Int.create()) : .Ok(.none)
}
}
extension P4String: CompilableType {
public static func CompileType(type: String) -> Common.Result<(any Common.P4Type)?> {
return type == "string" ? .Ok(P4String.create()) : .Ok(.none)
}
}
public struct Types {
static func CompileBasicType(type: String) -> Result<P4Type> {
let type_parsers: [CompilableType.Type] = [P4Boolean.self, P4Int.self, P4String.self]
for type_parser in type_parsers {
switch type_parser.CompileType(type: type) {
case .Ok(.some(let type)): return .Ok(type)
case .Ok(.none): continue
case .Error(let e): return .Error(e)
}
}
return Result.Error(Error(withMessage: "Type name not recognized"))
}
}