1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! Type expressions, scoped names and constant expressions.
//! Spec §2.3, §3.3.
use crate::diagnostic::DiagnosticCode;
use crate::parser::Parser;
use zerodds_idl_cst::{CstKind, TokenKind};
impl Parser<'_> {
/// A type specification that may carry annotations of its own.
///
/// Three positions in the grammar annotate a *use* of a type rather
/// than a declaration: the element of a `sequence`, the key and the
/// value of a `map`, and a union's discriminator. DDS-XTypes 1.3
/// §7.3.1.2.1.12 writes them as
/// `sequence<@try_construct(TRIM) String5, 10>` and
/// `union U switch (@try_construct T)`, and §7.3.1.2.1.12.4 is an
/// example built on the first.
///
/// The annotations are siblings of the type specification, which is
/// the shape `parse_annotated_definition` already uses for a
/// declaration and its annotations. Nothing wraps them, so the
/// positional indexing of a template argument list is unchanged and
/// the token order — and with it the round trip — is the source's.
pub(crate) fn parse_annotated_type_spec(&mut self) {
while self.current() == TokenKind::At {
self.parse_annotation();
}
self.parse_type_spec();
}
/// A type specification.
pub(crate) fn parse_type_spec(&mut self) {
match self.current() {
TokenKind::Sequence => self.parse_sequence_type(),
TokenKind::StringKw => self.parse_string_type(CstKind::StringType),
TokenKind::Wstring => self.parse_string_type(CstKind::WStringType),
TokenKind::Map => self.parse_map_type(),
TokenKind::Fixed => self.parse_fixed_type(),
TokenKind::Ident | TokenKind::ColonColon => {
self.start(CstKind::NamedType);
self.parse_scoped_name();
self.finish();
}
kind if is_primitive_keyword(kind) => self.parse_primitive_type(),
_ => {
self.diagnose(
DiagnosticCode::UnexpectedToken,
"expected a type",
"OMG IDL 4.2 §7.4.1.4",
);
self.start(CstKind::Error);
if !self.at_eof() {
self.bump();
}
self.finish();
}
}
}
/// A primitive type, which may be spelled in several keywords.
///
/// The multi-word forms IDL 4.2 §7.4.1.4.2 defines, and only those:
/// `unsigned short`, `unsigned long`, `unsigned long long`,
/// `long long`, `long double`. Anything else is one keyword, and a
/// loop that keeps eating type keywords would swallow the next
/// declaration's type.
fn parse_primitive_type(&mut self) {
self.start(CstKind::PrimitiveType);
match self.current() {
TokenKind::Unsigned => {
self.bump();
if self.eat(TokenKind::Long) {
// `unsigned long long`
let _ = self.eat(TokenKind::Long);
} else if !self.eat(TokenKind::Short) {
self.diagnose(
DiagnosticCode::MissingToken,
"expected `short` or `long` after `unsigned`",
"OMG IDL 4.2 §7.4.1.4.2",
);
}
}
TokenKind::Long => {
self.bump();
// `long long` or `long double`; a bare `long` otherwise.
if !self.eat(TokenKind::Long) {
let _ = self.eat(TokenKind::Double);
}
}
_ => self.bump(),
}
self.finish();
}
fn parse_sequence_type(&mut self) {
self.start(CstKind::SequenceType);
self.bump(); // sequence
if self.expect_type_arguments_open() {
self.start(CstKind::TypeArguments);
self.parse_annotated_type_spec();
if self.eat(TokenKind::Comma) {
self.parse_const_expr();
}
self.close_type_arguments();
self.finish();
}
self.finish();
}
fn parse_string_type(&mut self, kind: CstKind) {
self.start(kind);
self.bump(); // string or wstring
if self.current() == TokenKind::Less {
self.start(CstKind::TypeArguments);
self.bump();
self.parse_const_expr();
self.close_type_arguments();
self.finish();
}
self.finish();
}
fn parse_map_type(&mut self) {
self.start(CstKind::MapType);
self.bump(); // map
if self.expect_type_arguments_open() {
self.start(CstKind::TypeArguments);
self.parse_annotated_type_spec();
self.expect(TokenKind::Comma, "`,` between the map key and value");
self.parse_annotated_type_spec();
if self.eat(TokenKind::Comma) {
self.parse_const_expr();
}
self.close_type_arguments();
self.finish();
}
self.finish();
}
fn parse_fixed_type(&mut self) {
self.start(CstKind::FixedType);
self.bump(); // fixed
if self.current() == TokenKind::Less {
self.start(CstKind::TypeArguments);
self.bump();
self.parse_const_expr();
self.expect(TokenKind::Comma, "`,` between digits and scale");
self.parse_const_expr();
self.close_type_arguments();
self.finish();
}
self.finish();
}
fn expect_type_arguments_open(&mut self) -> bool {
if self.eat(TokenKind::Less) {
return true;
}
self.diagnose(
DiagnosticCode::MissingToken,
"expected `<` opening the type arguments",
"OMG IDL 4.2 §7.4.1.4.4",
);
false
}
/// Close a type argument list, splitting `>>` when it closes two.
///
/// `sequence<sequence<long>>` ends in one `>>` token. Closing the
/// inner list consumes the first half and leaves the second for the
/// outer one; the split is visible in the tree as two `>` nodes over
/// one source token, so the round trip still holds. Spec §2.3.
pub(crate) fn close_type_arguments(&mut self) {
if self.eat(TokenKind::Greater) {
return;
}
if self.current() == TokenKind::GreaterGreater {
self.split_shift_right();
return;
}
self.diagnose(
DiagnosticCode::MissingToken,
"expected `>` closing the type arguments",
"OMG IDL 4.2 §7.4.1.4.4",
);
}
}
/// Whether a keyword can begin a primitive type.
const fn is_primitive_keyword(kind: TokenKind) -> bool {
matches!(
kind,
TokenKind::Boolean
| TokenKind::Char
| TokenKind::Wchar
| TokenKind::Octet
| TokenKind::Short
| TokenKind::Long
| TokenKind::Float
| TokenKind::Double
| TokenKind::Unsigned
| TokenKind::Int8
| TokenKind::Int16
| TokenKind::Int32
| TokenKind::Int64
| TokenKind::Uint8
| TokenKind::Uint16
| TokenKind::Uint32
| TokenKind::Uint64
| TokenKind::Any
| TokenKind::Object
| TokenKind::Valuebase
)
}
impl Parser<'_> {
/// A scoped name, possibly rooted at `::`.
pub(crate) fn parse_scoped_name(&mut self) {
self.start(CstKind::ScopedName);
// A leading `::` is the global scope, never an operator.
let _ = self.eat(TokenKind::ColonColon);
if !self.eat(TokenKind::Ident) {
self.diagnose(
DiagnosticCode::MissingToken,
"expected a name",
"OMG IDL 4.2 §7.4.1.2",
);
self.finish();
return;
}
while self.current() == TokenKind::ColonColon {
self.bump();
if !self.eat(TokenKind::Ident) {
self.diagnose(
DiagnosticCode::MissingToken,
"expected a name after `::`",
"OMG IDL 4.2 §7.4.1.2",
);
break;
}
}
self.finish();
}
/// A constant expression, at the loosest precedence.
pub(crate) fn parse_const_expr(&mut self) {
self.parse_binary_expr(0);
}
/// Precedence-climbing over the IDL 4.2 ladder.
///
/// Loosest to tightest: `|`, `^`, `&`, shifts, additive,
/// multiplicative, unary. IDL 4.2 §7.4.1.4.2.
fn parse_binary_expr(&mut self, min_level: u8) {
let start = self.checkpoint();
self.parse_unary_expr();
loop {
let Some(level) = binary_level(self.current()) else {
return;
};
if level < min_level {
return;
}
// In an expression `>>` is a shift and is NOT split — the
// split belongs to type argument lists only. Spec §3.3.
self.bump();
// Left-associative: the right side binds one level tighter,
// so `1 - 2 - 3` groups as `(1 - 2) - 3`.
self.parse_binary_expr(level + 1);
// The left operand was already emitted, so the node is
// wrapped around it retroactively.
self.start_at(start, CstKind::BinaryExpr);
self.finish();
}
}
fn parse_unary_expr(&mut self) {
match self.current() {
TokenKind::Minus | TokenKind::Plus | TokenKind::Tilde => {
self.start(CstKind::UnaryExpr);
self.bump();
self.parse_unary_expr();
self.finish();
}
TokenKind::LParen => {
self.start(CstKind::ParenExpr);
self.bump();
self.parse_const_expr();
self.expect(TokenKind::RParen, "`)` closing the expression");
self.finish();
}
TokenKind::Ident | TokenKind::ColonColon => {
self.start(CstKind::NameExpr);
self.parse_scoped_name();
self.finish();
}
kind if kind.is_literal() => {
self.start(CstKind::LiteralExpr);
self.bump();
if matches!(kind, TokenKind::StringLiteral | TokenKind::WStringLiteral) {
self.eat_adjacent_string_literals(kind);
}
self.finish();
}
_ => {
self.diagnose(
DiagnosticCode::UnexpectedToken,
"expected a constant expression",
"OMG IDL 4.2 §7.4.1.4.2",
);
self.start(CstKind::Error);
if !self.at_eof() {
self.bump();
}
self.finish();
}
}
}
}
/// The precedence of a binary operator, tighter being higher.
const fn binary_level(kind: TokenKind) -> Option<u8> {
Some(match kind {
TokenKind::Pipe => 1,
TokenKind::Caret => 2,
TokenKind::Ampersand => 3,
TokenKind::LessLess | TokenKind::GreaterGreater => 4,
TokenKind::Plus | TokenKind::Minus => 5,
TokenKind::Star | TokenKind::Slash | TokenKind::Percent => 6,
_ => return None,
})
}