Refactor Names of Scope Types

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-03-20 04:23:53 -04:00
parent 6a9a138afe
commit f0d816b99f
12 changed files with 144 additions and 163 deletions
+31 -31
View File
@@ -39,16 +39,16 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4ArrayValue(withType: P4Int(), withValue: [
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -67,14 +67,14 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Int())
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Int())
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 22}: Failed to parse a statement element: {65, 2}: ta does not name an array type"
),
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
}
@@ -90,16 +90,16 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4ArrayValue(withType: P4Int(), withValue: [
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -117,16 +117,16 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4ArrayValue(withType: P4Int(), withValue: [
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -144,16 +144,16 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4ArrayValue(withType: P4Int(), withValue: [
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -172,9 +172,9 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
var test_values = VarValueScopes().enter()
let nested = P4ArrayValue(
withType: P4Int(),
@@ -185,7 +185,7 @@ import TreeSitterP4
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -205,16 +205,16 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ta"),
withValue: P4ArrayValue(withType: P4Int(), withValue: [
P4IntValue(withValue: 1), P4IntValue(withValue: 2), P4IntValue(withValue: 3),
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
@@ -234,9 +234,9 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
var test_values = ValueScopes().enter()
var test_declarations = VarTypeScopes().enter()
test_declarations = test_declarations.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Array(withValueType: P4Int())))
var test_values = VarValueScopes().enter()
let nested = P4ArrayValue(
withType: P4Int(),
@@ -247,7 +247,7 @@ import TreeSitterP4
withValue: P4ArrayValue(withType: P4Array(withValueType: P4Int()), withValue: [nested]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
+1 -1
View File
@@ -114,5 +114,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: LexicalScopes()))))
node: result.rootNode!, withContext: CompilerContext(withNames: VarTypeScopes()))))
}
+3 -3
View File
@@ -27,7 +27,7 @@ import TreeSitterP4
@testable import P4Compiler
@Test func test_scope() async throws {
let s = LexicalScope()
let s = VarTypeScope()
let s2 = s.declare(identifier: Identifier(name: "first"), withValue: P4Int())
let found_first = try! #require(s2.lookup(identifier: Identifier(name: "first")))
@@ -36,7 +36,7 @@ import TreeSitterP4
}
@Test func test_scope_no_set() async throws {
var ss = LexicalScopes().enter()
var ss = VarTypeScopes().enter()
ss = ss.declare(identifier: Identifier(name: "first"), withValue: P4Int())
ss = ss.enter()
ss = ss.declare(identifier: Identifier(name: "second"), withValue: P4Boolean())
@@ -49,7 +49,7 @@ import TreeSitterP4
}
@Test func test_scope_set() async throws {
var ss = LexicalScopes().enter()
var ss = VarTypeScopes().enter()
let id = Identifier(name: "first")
let id_type = P4Int()
+38 -38
View File
@@ -39,15 +39,15 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
P4StructFieldIdentifier(name: "count", withType: P4Int()),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4StructValue(withType: struct_type, andInitializers: [
@@ -56,7 +56,7 @@ import TreeSitterP4
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
@@ -74,15 +74,15 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
P4StructFieldIdentifier(name: "count", withType: P4Int()),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4StructValue(withType: struct_type, andInitializers: [
@@ -91,7 +91,7 @@ import TreeSitterP4
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.reject)
@@ -109,15 +109,15 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
P4StructFieldIdentifier(name: "count", withType: P4Int()),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4StructValue(withType: struct_type, andInitializers: [
@@ -126,7 +126,7 @@ import TreeSitterP4
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
@@ -143,15 +143,15 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
P4StructFieldIdentifier(name: "count", withType: P4Int()),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4StructValue(withType: struct_type, andInitializers: [
@@ -160,7 +160,7 @@ import TreeSitterP4
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.reject)
@@ -178,7 +178,7 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
@@ -189,9 +189,9 @@ import TreeSitterP4
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
@@ -206,7 +206,7 @@ import TreeSitterP4
])
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
@@ -225,15 +225,15 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
P4StructFieldIdentifier(name: "count", withType: P4Int()),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4StructValue(withType: struct_type, andInitializers: [
@@ -242,7 +242,7 @@ import TreeSitterP4
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
@@ -257,20 +257,20 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
P4StructFieldIdentifier(name: "count", withType: P4Int()),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: struct_type)
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int to field with type Boolean"
),
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
}
@@ -287,7 +287,7 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
@@ -298,9 +298,9 @@ import TreeSitterP4
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
@@ -315,7 +315,7 @@ import TreeSitterP4
])
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
@@ -335,7 +335,7 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
@@ -346,9 +346,9 @@ import TreeSitterP4
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
var test_values = ValueScopes().enter()
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
@@ -363,7 +363,7 @@ import TreeSitterP4
])
]))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.ParserRuntime.create(program: program, withInitialValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(state_result == P4Lang.accept)
@@ -382,7 +382,7 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_declarations = VarTypeScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4Boolean()),
@@ -393,14 +393,14 @@ import TreeSitterP4
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: ty_struct_type)])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_types = test_types.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: ts_struct_type)
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field with type Int"
),
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
}
+16 -6
View File
@@ -29,10 +29,20 @@ import TreeSitterP4
@Test func test_invalid_types() async throws {
for invalid_type_name in ["boo", "str", "in"] {
#expect(
#RequireErrorResult(
Error(withMessage: "Type name not recognized"),
Types.CompileBasicType(type: invalid_type_name)))
let simple_parser_declaration = """
parser main_parser() {
state start {
\(invalid_type_name) v = 1;
transition reject;
}
};
""";
let err = Program.Compile(simple_parser_declaration)
guard case Result.Error(let e) = err else {
assert(false, "Expected an error, but had success")
}
#expect(e.msg.contains("Failed to parse a statement element: Could not parse a P4 type from \(invalid_type_name)"))
}
}
@@ -242,7 +252,7 @@ import TreeSitterP4
}
};
"""
var test_types = LexicalScopes().enter()
var test_types = VarTypeScopes().enter()
test_types = test_types.declare(identifier: Identifier(name: "ta"), withValue: P4Array(withValueType: P4Int()))
#expect(
#RequireErrorResult(
@@ -250,5 +260,5 @@ import TreeSitterP4
withMessage:
"{49, 22}: Failed to parse a statement element: Cannot initialize where_to (with type Boolean) from rvalue with type Int"
),
Program.Compile(simple_parser_declaration, withGlobalTypes: test_types)))
Program.Compile(simple_parser_declaration, withGlobalInstances: test_types)))
}