Parse Struct Declarations

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-27 03:33:15 -04:00
parent 5abaac2816
commit cd26d1d22c
14 changed files with 426 additions and 155 deletions
+53
View File
@@ -0,0 +1,53 @@
// 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 Foundation
import Macros
import P4Runtime
import P4Lang
import SwiftTreeSitter
import Testing
import TreeSitter
import TreeSitterP4
@testable import P4Compiler
@Test func test_struct_declaration_and_field_write() async throws {
let simple_parser_declaration = """
struct Testing {
bool yesno;
int count;
};
parser main_parser() {
state start {
Testing ts;
ts.yesno = true;
bool where_to = ts.yesno;
transition select (where_to) {
true: accept;
false: reject;
};
}
};
"""
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
}
+1 -17
View File
@@ -61,22 +61,6 @@ import P4Lang
#expect(state.statements.count == 1)
}
@Test func test_simple_compilation_with_instantiation() async throws {
let simple_parser_declaration = """
parser main_parser() {
state start {
true;
false;
transition start;
}
};
bool() main;
"""
let program = try #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(#RequireOkResult(program.find_parser(withName: Identifier(name: "main_parser"))))
}
@Test func test_invalid_transition_expression_keyset_expressions() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -114,5 +98,5 @@ import P4Lang
#RequireErrorResult<(EvaluatableStatement, CompilerContext)>(
Error(withMessage: "{2, 154}: Did not find assignment statement"),
ParserAssignmentStatement.Compile( // Note: Calling ParserAssignmentStatement compilation directly.
node: result.rootNode!, withContext: CompilerContext(withNames: VarTypeScopes()))))
node: result.rootNode!, withContext: CompilerContext(withInstances: VarTypeScopes()))))
}