compiler, language, runtime: Separate Parser Type From Instances
Continuous Integration / Grammar Tests (push) Successful in 4m2s
Continuous Integration / Library Format Tests (push) Successful in 5m0s
Continuous Integration / Library Tests (push) Successful in 8m1s

In P4, parsers are considered types. Those parsers are instantiated.
The instantiated parsers are values. Previously, gp4 treated a parser
type and a parser value as identical. This PR makes that difference
clear _and_ sets the stage for the future.

TODO: Make the same distinction between control and action types and
values.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-27 05:41:23 -04:00
parent 925f20a13b
commit 61d8f601e8
36 changed files with 1058 additions and 796 deletions
@@ -40,7 +40,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -50,11 +49,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_equality_one_empty() async throws {
@@ -73,7 +72,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -83,11 +81,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_struct_equality_neither_empty() async throws {
@@ -107,7 +105,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -117,11 +114,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_equality_neither_empty2() async throws {
@@ -144,7 +141,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -154,11 +150,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_struct_equality_neither_empty3() async throws {
@@ -181,7 +177,6 @@ import TreeSitterP4
}
};
"""
let test_declarations = VarTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
@@ -191,11 +186,11 @@ import TreeSitterP4
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations, withGlobalTypes: test_types))
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(program: program))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}