tla-syntax 0.2.0

Parser, AST and printer for TLA+, with no dependencies
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kw {
    Module,
    Extends,
    Constant,
    Variable,
    Let,
    In,
    If,
    Then,
    Else,
    Choose,
    Case,
    Other,
    Assume,
    Theorem,
    Instance,
    With,
    Local,
    Recursive,
    Lambda,
    /// A keyword that only appears inside a TLAPS proof.
    Proof,
    Except,
    True,
    False,
    Domain,
    Subset,
    Union,
    Enabled,
    Unchanged,
}

impl Kw {
    /// How the keyword is spelled, for the rare specification that defines
    /// something by that name. `Proof` stands for a dozen words at once and so
    /// has none.
    pub fn text(self) -> Option<&'static str> {
        Some(match self {
            Self::Module => "MODULE",
            Self::Extends => "EXTENDS",
            Self::Constant => "CONSTANT",
            Self::Variable => "VARIABLE",
            Self::Let => "LET",
            Self::In => "IN",
            Self::If => "IF",
            Self::Then => "THEN",
            Self::Else => "ELSE",
            Self::Choose => "CHOOSE",
            Self::Case => "CASE",
            Self::Other => "OTHER",
            Self::Assume => "ASSUME",
            Self::Theorem => "THEOREM",
            Self::Instance => "INSTANCE",
            Self::With => "WITH",
            Self::Local => "LOCAL",
            Self::Recursive => "RECURSIVE",
            Self::Lambda => "LAMBDA",
            Self::Except => "EXCEPT",
            Self::True => "TRUE",
            Self::False => "FALSE",
            Self::Domain => "DOMAIN",
            Self::Subset => "SUBSET",
            Self::Union => "UNION",
            Self::Enabled => "ENABLED",
            Self::Unchanged => "UNCHANGED",
            Self::Proof => return None,
        })
    }

    pub fn lookup(word: &str) -> Option<Self> {
        Some(match word {
            "MODULE" => Self::Module,
            "EXTENDS" => Self::Extends,
            "CONSTANT" | "CONSTANTS" => Self::Constant,
            "VARIABLE" | "VARIABLES" => Self::Variable,
            "LET" => Self::Let,
            "IN" => Self::In,
            "IF" => Self::If,
            "THEN" => Self::Then,
            "ELSE" => Self::Else,
            "CHOOSE" => Self::Choose,
            "CASE" => Self::Case,
            "OTHER" => Self::Other,
            "ASSUME" | "ASSUMPTION" => Self::Assume,
            "INSTANCE" => Self::Instance,
            "WITH" => Self::With,
            "LOCAL" => Self::Local,
            "RECURSIVE" => Self::Recursive,
            "LAMBDA" => Self::Lambda,
            "THEOREM" | "LEMMA" | "COROLLARY" | "PROPOSITION" | "AXIOM" => Self::Theorem,
            "PROOF" | "BY" | "OBVIOUS" | "OMITTED" | "QED" | "DEF" | "DEFS" | "DEFINE"
            | "SUFFICES" | "PICK" | "WITNESS" | "HAVE" | "TAKE" | "USE" | "HIDE" | "PROVE"
            | "NEW" | "ONLY" => Self::Proof,
            "EXCEPT" => Self::Except,
            "TRUE" => Self::True,
            "FALSE" => Self::False,
            "DOMAIN" => Self::Domain,
            "SUBSET" => Self::Subset,
            "UNION" => Self::Union,
            "ENABLED" => Self::Enabled,
            "UNCHANGED" => Self::Unchanged,
            _ => return None,
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Op {
    Implies,
    Equiv,
    Or,
    And,
    Eq,
    Neq,
    Lt,
    Gt,
    Le,
    Ge,
    In,
    NotIn,
    Subseteq,
    Supseteq,
    AtAt,
    OneTo,
    Cup,
    Cap,
    SetMinus,
    DotDot,
    Plus,
    Minus,
    Times,
    Div,
    Mod,
    Cartesian,
    Concat,
    Pow,
    Not,
    Always,
    Eventually,
    Forall,
    Exists,
    Domain,
    Subset,
    BigUnion,
    Enabled,
    Unchanged,
    /// `P ~> Q`: temporal leads-to.
    LeadsTo,
    /// `\AA x : F` and `\EE x : F`: quantification over a hidden variable.
    TemporalForall,
    TemporalExists,
    /// An operator the language reserves a symbol and a precedence for but
    /// gives no meaning to. Every one of these exists to be defined by a
    /// specification; `\prec`, `\oplus` and `&` are all of them.
    User(&'static str),
}

/// The symbols TLA+ sets aside for specifications to define, with the
/// precedence the language fixes for each. Taken from the operator table in
/// *Specifying Systems*; the left binding power is used, which is what matters
/// for reading an expression back the way it was written.
pub(crate) const USER_OPERATORS: &[(&str, u8)] = &[
    ("\\prec", 5),
    ("\\preceq", 5),
    ("\\succ", 5),
    ("\\succeq", 5),
    ("\\sqsubset", 5),
    ("\\sqsubseteq", 5),
    ("\\sqsupset", 5),
    ("\\sqsupseteq", 5),
    ("\\subset", 5),
    ("\\supset", 5),
    ("\\ll", 5),
    ("\\gg", 5),
    ("\\sim", 5),
    ("\\simeq", 5),
    ("\\approx", 5),
    ("\\asymp", 5),
    ("\\cong", 5),
    ("\\doteq", 5),
    ("\\propto", 5),
    ("\\cdot", 5),
    ("\\mod", 11),
    ("...", 9),
    ("--", 11),
    ("\\times", 11),
    ("|-", 5),
    ("-|", 5),
    ("|=", 5),
    ("=|", 5),
    ("::=", 5),
    ("<:", 7),
    (":=", 5),
    ("\\sqcap", 9),
    ("\\sqcup", 9),
    ("\\uplus", 9),
    ("\\oplus", 10),
    ("\\ominus", 11),
    ("(+)", 10),
    ("(-)", 11),
    ("(.)", 13),
    ("(/)", 13),
    ("(\\X)", 13),
    ("\\odot", 13),
    ("\\oslash", 13),
    ("\\otimes", 13),
    ("\\star", 13),
    ("\\bullet", 13),
    ("\\bigcirc", 13),
    ("\\wr", 14),
    ("&", 13),
    ("&&", 13),
    ("|", 10),
    ("||", 10),
    ("$", 9),
    ("$$", 9),
    ("??", 9),
    ("%%", 11),
    ("##", 9),
    ("!!", 9),
    ("^^", 14),
    ("++", 10),
    ("**", 13),
    ("//", 13),
    ("/", 13),
    ("^+", 15),
    ("^*", 15),
    ("^#", 15),
    ("-+->", 2),
];

pub(crate) fn user_operator(symbol: &str) -> Option<Op> {
    USER_OPERATORS
        .iter()
        .find(|(name, _)| *name == symbol)
        .map(|(name, _)| Op::User(name))
}

impl Op {
    /// Binding power as an infix operator; `None` for prefix-only operators.
    ///
    /// Ordering follows TLA+'s table where it matters for these specs: `\cup`
    /// binds tighter than `=`, and `:>` tighter than `@@`, so `a \cup {b} = c`
    /// and `("k" :> v) @@ rest` parse without parentheses.
    pub fn infix_prec(self) -> Option<u8> {
        Some(match self {
            Self::Implies => 1,
            Self::Equiv | Self::LeadsTo => 2,
            Self::Or => 3,
            Self::And => 4,
            Self::Eq
            | Self::Neq
            | Self::Lt
            | Self::Gt
            | Self::Le
            | Self::Ge
            | Self::In
            | Self::NotIn
            | Self::Subseteq
            | Self::Supseteq => 5,
            Self::AtAt => 6,
            Self::OneTo => 7,
            Self::Cup | Self::Cap | Self::SetMinus => 8,
            Self::DotDot => 9,
            Self::Plus | Self::Minus => 10,
            Self::Times | Self::Div | Self::Mod | Self::Cartesian => 11,
            Self::Concat => 12,
            Self::Pow => 13,
            Self::User(_) if self.is_postfix() => return None,
            Self::User(symbol) => {
                return USER_OPERATORS
                    .iter()
                    .find(|(name, _)| *name == symbol)
                    .map(|(_, prec)| *prec);
            }
            _ => return None,
        })
    }

    /// `s^+`, `s^*` and `s^#` follow their operand rather than sitting
    /// between two, so they are never infix.
    pub fn is_postfix(self) -> bool {
        matches!(self, Self::User("^+" | "^*" | "^#"))
    }

    pub fn is_right_assoc(self) -> bool {
        matches!(self, Self::Implies | Self::Pow)
    }

    /// How the operator is written. Where TLA+ offers several spellings the
    /// ASCII one is chosen, so printed output can be re-read by the parser.
    pub fn symbol(self) -> &'static str {
        match self {
            Self::Implies => "=>",
            Self::Equiv => "<=>",
            Self::Or => "\\/",
            Self::And => "/\\",
            Self::Eq => "=",
            Self::Neq => "#",
            Self::Lt => "<",
            Self::Gt => ">",
            Self::Le => "<=",
            Self::Ge => ">=",
            Self::In => "\\in",
            Self::NotIn => "\\notin",
            Self::Subseteq => "\\subseteq",
            Self::Supseteq => "\\supseteq",
            Self::AtAt => "@@",
            Self::OneTo => ":>",
            Self::Cup => "\\cup",
            Self::Cap => "\\cap",
            Self::SetMinus => "\\",
            Self::DotDot => "..",
            Self::Plus => "+",
            Self::Minus => "-",
            Self::Times => "*",
            Self::Div => "\\div",
            Self::Mod => "%",
            Self::Cartesian => "\\X",
            Self::Concat => "\\o",
            Self::Pow => "^",
            Self::Not => "~",
            Self::Always => "[]",
            Self::Eventually => "<>",
            Self::Forall => "\\A",
            Self::Exists => "\\E",
            Self::Domain => "DOMAIN",
            Self::Subset => "SUBSET",
            Self::BigUnion => "UNION",
            Self::Enabled => "ENABLED",
            Self::Unchanged => "UNCHANGED",
            Self::LeadsTo => "~>",
            Self::TemporalForall => "\\AA",
            Self::TemporalExists => "\\EE",
            Self::User(symbol) => symbol,
        }
    }

    /// How tightly the operator holds its operand when used as a prefix, and
    /// so how tightly it binds as a node in printed output.
    pub fn prefix_prec(self) -> u8 {
        match self {
            Self::Minus => 11,
            Self::Domain | Self::Subset | Self::BigUnion => 9,
            _ => 5,
        }
    }

    /// True for the word-shaped prefix operators, which need a space before
    /// their operand where the symbolic ones do not.
    pub fn is_word(self) -> bool {
        matches!(
            self,
            Self::Domain | Self::Subset | Self::BigUnion | Self::Enabled | Self::Unchanged
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Tok {
    Ident(String),
    Num(i64),
    /// `123.456`, kept as written: TLA+ decimals are exact, and no binary
    /// floating-point type could hold one faithfully.
    Decimal(String),
    Str(String),
    Kw(Kw),
    Op(Op),
    /// `WF_vars` / `SF_vars`, which lex as one word but mean an operator
    /// applied to a subscript.
    Fair {
        strong: bool,
        subscript: String,
    },
    ModuleEnd,
    Separator,
    DefEq,
    LParen,
    RParen,
    LBrack,
    RBrack,
    LBrace,
    RBrace,
    LTup,
    RTup,
    Comma,
    Colon,
    /// The `::` of a labelled expression.
    ColonColon,
    Dot,
    Bang,
    /// The `@` of an `EXCEPT` update, standing for the value being replaced.
    At,
    Underscore,
    Prime,
    MapsTo,
    Arrow,
    Gets,
    Eof,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
    pub tok: Tok,
    pub line: u32,
    pub col: u32,
}