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
+170 -108
View File
@@ -18,8 +18,8 @@
import Common
import Foundation
import Macros
import P4Runtime
import P4Lang
import P4Runtime
import SwiftTreeSitter
import Testing
import TreeSitter
@@ -39,27 +39,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
])))
withValue: P4Value(
P4StructValue(
withType: struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_access_declared() async throws {
@@ -84,11 +90,13 @@ import TreeSitterP4
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let program = try #UseOkResult(
Program.Compile(
simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
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_field_access_declared2() async throws {
@@ -106,19 +114,22 @@ import TreeSitterP4
}
};
"""
var test_types = TypeTypeScopes().enter()
var test_declarations = TypeTypeScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_types = test_types.declare(identifier: Identifier(name: "Testing"), withValue: struct_type)
test_declarations = test_declarations.declare(
identifier: Identifier(name: "Testing"), withValue: struct_type)
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_types))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: .none, withGlobalTypes: test_declarations))
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_field_access_opp() async throws {
@@ -133,30 +144,35 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: false)),
P4Value(P4IntValue(withValue: 5)),
])))
withValue: P4Value(
P4StructValue(
withType: struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: false)),
P4Value(P4IntValue(withValue: 5)),
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_field_access2() async throws {
let simple_parser_declaration = """
parser main_parser() {
@@ -168,27 +184,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
])))
withValue: P4Value(
P4StructValue(
withType: struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_access2_opp() async throws {
@@ -202,27 +224,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 8)),
])))
withValue: P4Value(
P4StructValue(
withType: struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 8)),
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.reject)
#expect(state_result == P4Lang.reject)
}
@Test func test_field_access_nested() async throws {
@@ -237,7 +265,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -245,30 +273,37 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_fields = P4StructFields([
P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))
])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
]))
])))
withValue: P4Value(
P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(
P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 5)),
]))
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write() async throws {
@@ -284,27 +319,33 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(withType: struct_type, andInitializers: [
P4Value(P4BooleanValue(withValue: false)),
P4Value(P4IntValue(withValue: 5)),
])))
withValue: P4Value(
P4StructValue(
withType: struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: false)),
P4Value(P4IntValue(withValue: 5)),
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write_invalid_type() async throws {
@@ -316,18 +357,20 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int (width: Infinite) to field yesno of type Boolean"
withMessage:
"{49, 13}: Failed to parse a statement element: {49, 8}: Cannot assign value of type Int (width: Infinite) to field yesno of type Boolean"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
@@ -346,7 +389,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -354,30 +397,37 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_fields = P4StructFields([
P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))
])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 7)),
]))
])))
withValue: P4Value(
P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(
P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 7)),
]))
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write_nested2() async throws {
@@ -394,7 +444,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -402,30 +452,37 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_fields = P4StructFields([
P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))
])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
var test_values = VarValueScopes().enter()
test_values = test_values.declare(
identifier: Identifier(name: "ts"),
withValue: P4Value(P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 7)),
]))
])))
withValue: P4Value(
P4StructValue(
withType: ts_struct_type,
andInitializers: [
P4Value(
P4StructValue(
withType: ty_struct_type,
andInitializers: [
P4Value(P4BooleanValue(withValue: true)),
P4Value(P4IntValue(withValue: 7)),
]))
])))
let program = try #UseOkResult(
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
let runtime = try #UseOkResult(P4Runtime.Runtime<InstantiatedParserState, P4Lang.Parser>.create(program: program, withGlobalValues: test_values))
let runtime = try #UseOkResult(
P4Runtime.Runtime<InstantiatedParserState, P4Lang.ParserValue>.create(
program: program, withGlobalValues: test_values))
let (state_result, _) = try! #UseOkResult(runtime.run())
#expect(AsInstantiatedParserState(state_result) == P4Lang.accept)
#expect(state_result == P4Lang.accept)
}
@Test func test_field_write_nested_invalid_type() async throws {
@@ -441,7 +498,7 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let ty_fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
@@ -449,15 +506,19 @@ import TreeSitterP4
])
let ty_struct_type = P4Struct(withName: Identifier(name: "nested"), andFields: ty_fields)
let ts_fields = P4StructFields([P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))])
let ts_fields = P4StructFields([
P4StructFieldIdentifier(name: "ty", withType: P4QualifiedType(ty_struct_type))
])
let ts_struct_type = P4Struct(withName: Identifier(name: "outer"), andFields: ts_fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(ts_struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(ts_struct_type), .none))
#expect(
#RequireErrorResult(
Error(
withMessage: "{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int (width: Infinite)"
withMessage:
"{49, 20}: Failed to parse a statement element: {49, 11}: Cannot assign value of type Boolean to field count of type Int (width: Infinite)"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
@@ -475,20 +536,21 @@ import TreeSitterP4
}
};
"""
var test_declarations = VarTypeScopes().enter()
var test_declarations = StaticVarValueScopes().enter()
let fields = P4StructFields([
P4StructFieldIdentifier(name: "yesno", withType: P4QualifiedType(P4Boolean())),
P4StructFieldIdentifier(name: "count", withType: P4QualifiedType(P4Int())),
])
let struct_type = P4Struct(withName: Identifier(name: "Testing"), andFields: fields)
test_declarations = test_declarations.declare(identifier: Identifier(name: "ts"), withValue: P4QualifiedType(struct_type))
test_declarations = test_declarations.declare(
identifier: Identifier(name: "ts"), withValue: (P4QualifiedType(struct_type), .none))
#expect(
#RequireErrorResult(
Error(
withMessage: "{68, 21}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
withMessage:
"{68, 21}: Could not parse transition select expression selector expression: Types of values used with binary expression are not the same"
),
Program.Compile(simple_parser_declaration, withGlobalInstances: test_declarations))
)
}