Skip to main content

fic/xta_backend/xta/
mod.rs

1//! The UPPAAL model in XTA, the language UPPAAL reads and UTAP parses: what
2//! `transpile` builds from each PD's Ccfg, printing itself as text. The network
3//! ([`Nta`] and what it holds, `nta.rs`) and the language its declarations
4//! and labels are written in (`types.rs`, `expr.rs`, `stmt.rs`, `decl.rs`).
5//! A mirror of UTAP's syntax; names and types are verifyta's to resolve.
6//!
7//! UTAP's XML reader parses each element with its own start symbol, which is
8//! what this module covers:
9//!
10//! | XML element | UTAP start symbol | Here |
11//! |---|---|---|
12//! | global `<declaration>` | `Declarations` | [`GlobalDecl`] |
13//! | template `<declaration>` | `ProcLocalDeclList` | [`ProcLocalDecl`] |
14//! | `<parameter>` | `ParameterList` | [`Param`] |
15//! | location `invariant` | `Expression` | [`Expr`] |
16//! | transition `select` | `SelectList` | [`Select`] |
17//! | transition `guard` | `Expression` | [`Expr`] |
18//! | transition `synchronisation` | `SyncExpr` | [`Sync`] |
19//! | transition `assignment` | `ExprList` | [`Expr`] (a [`Expr::Comma`] list) |
20//! | `<system>` | `XTA` | [`System`] |
21//!
22//! # The declaration language
23//!
24//! Ground truth is the UTAP parser itself, not the UPPAAL manual: the types
25//! below follow the productions of [`src/parser.y`] one for one, and the
26//! [`fmt::Display`] impls print what [`src/PrettyPrinter.cpp`] prints. Line
27//! numbers (`parser.y:494`) refer to [UPPAALModelChecker/utap] at `b5fe3b2`.
28//!
29//! [UPPAALModelChecker/utap]: https://github.com/UPPAALModelChecker/utap/tree/b5fe3b2d71f07f10c3dbd41a13d95ea179a4f0c0
30//! [`src/parser.y`]: https://github.com/UPPAALModelChecker/utap/blob/b5fe3b2d71f07f10c3dbd41a13d95ea179a4f0c0/src/parser.y
31//! [`src/PrettyPrinter.cpp`]: https://github.com/UPPAALModelChecker/utap/blob/b5fe3b2d71f07f10c3dbd41a13d95ea179a4f0c0/src/PrettyPrinter.cpp
32//!
33//! ## Three scopes, three grammars
34//!
35//! UTAP does not have one declaration list, it has three, and the local ones
36//! are strictly poorer:
37//!
38//! | Scope | UTAP production | This module |
39//! |---|---|---|
40//! | Global | `Declarations` (parser.y:494) | [`GlobalDecl`] |
41//! | Template-local | `ProcLocalDeclList` (parser.y:786) | [`ProcLocalDecl`] |
42//! | Block-local | `BlockLocalDeclList` (parser.y:1008) | [`BlockLocalDecl`] |
43//!
44//! A template admits only variables, typedefs and functions; a block only
45//! variables and typedefs. Having one enum per scope is the point of the
46//! language: the backend cannot put an instantiation inside a template, or a
47//! function inside a `for` body, because those values do not exist.
48//!
49//! Two traps the types close: [`TypePrefix`] is one slot, not a set
50//! (`urgent broadcast` is its own production, `const urgent` does not
51//! parse), and array dimensions hang off the *declarator*, not the type
52//! (`int a[4], b;` declares one array and one scalar).
53//!
54//! ## What is left out, and why
55//!
56//! - A `ProcDecl` type of its own: a [`Template`] prints as one
57//!   (`process P() { … }`). The LSC forms.
58//! - `switch` and `do`/`while` statements: nothing in the transpiler emits
59//!   them, and an unused variant is a variant nobody checks.
60//! - Queries (`.q` files are user-written), clock rates, the probabilistic
61//!   forms (branchpoints, probabilities, exponential rates), `progress` and
62//!   `gantt`.
63//! - `**`: UTAP's grammar parses it, but verifyta 5.0.0 cannot build it as
64//!   an expression; `pow(a, b)` is the call form.
65//!
66//! Everything else the global list admits is here, including the forms the
67//! transpiler does not yet build ([`ExternDecl`], [`ChanPriority`],
68//! [`DynamicDecl`], before/after update), so that the module is a faithful
69//! description of the surface rather than of today's backend.
70
71#![allow(dead_code)] // `transpile` does not build the declaration language yet
72
73use std::fmt;
74
75/// An enum whose variants print as fixed keywords or operators: the
76/// variants and their spellings as one table, each variant documented by
77/// its spelling.
78macro_rules! keywords {
79    (
80        $(#[$meta:meta])*
81        $vis:vis enum $name:ident {
82            $( $(#[$vmeta:meta])* $variant:ident => $text:literal ),+ $(,)?
83        }
84    ) => {
85        $(#[$meta])*
86        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
87        $vis enum $name {
88            $(
89                #[doc = concat!("<code>", $text, "</code>")]
90                $(#[$vmeta])*
91                $variant
92            ),+
93        }
94
95        impl fmt::Display for $name {
96            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97                f.write_str(match self {
98                    $( $name::$variant => $text ),+
99                })
100            }
101        }
102    };
103}
104
105mod decl;
106mod expr;
107mod nta;
108mod stmt;
109mod types;
110
111pub use decl::*;
112pub use expr::*;
113pub use nta::*;
114#[allow(unused_imports)] // `transpile` does not build function bodies yet
115pub use stmt::*;
116pub use types::*;
117
118/// Every word UTAP's lexer reserves (`src/keywords.cpp` at `b5fe3b2`, all
119/// syntax variants together): none may name a process, an instance or a
120/// location.
121pub const KEYWORDS: &[&str] = &[
122    "random_arcsine",
123    "random_beta",
124    "random_gamma",
125    "random_normal",
126    "random_poisson",
127    "random_tri",
128    "random_weibull",
129    "IO",
130    "Pmax",
131    "Pr",
132    "X",
133    "abs",
134    "acos",
135    "acosh",
136    "after_update",
137    "and",
138    "asin",
139    "asinh",
140    "assert",
141    "assign",
142    "atan",
143    "atanh",
144    "before_update",
145    "bool",
146    "bounds",
147    "branchpoint",
148    "broadcast",
149    "cbrt",
150    "ceil",
151    "chan",
152    "clock",
153    "commit",
154    "const",
155    "control",
156    "control_t",
157    "copysign",
158    "cos",
159    "cosh",
160    "deadlock",
161    "default",
162    "do",
163    "double",
164    "dynamic",
165    "else",
166    "erf",
167    "erfc",
168    "exists",
169    "exit",
170    "exp",
171    "fabs",
172    "false",
173    "fdim",
174    "fint",
175    "floor",
176    "fma",
177    "fmax",
178    "fmin",
179    "fmod",
180    "for",
181    "forall",
182    "foreach",
183    "fpclassify",
184    "gantt",
185    "guard",
186    "hybrid",
187    "hypot",
188    "if",
189    "ilogb",
190    "imitate",
191    "imply",
192    "import",
193    "inf",
194    "init",
195    "int",
196    "isfinite",
197    "isinf",
198    "isnan",
199    "isnormal",
200    "isunordered",
201    "ldexp",
202    "lgamma",
203    "ln",
204    "loadStrategy",
205    "location",
206    "log",
207    "logb",
208    "maxE",
209    "maxPr",
210    "meta",
211    "minE",
212    "minPr",
213    "nextafter",
214    "not",
215    "numOf",
216    "or",
217    "pow",
218    "priority",
219    "probability",
220    "process",
221    "progress",
222    "query",
223    "random",
224    "return",
225    "round",
226    "sat",
227    "saveStrategy",
228    "scalar",
229    "select",
230    "signbit",
231    "simulate",
232    "simulation",
233    "sin",
234    "sinh",
235    "spawn",
236    "sqrt",
237    "state",
238    "strategy",
239    "string",
240    "struct",
241    "sum",
242    "sup",
243    "sync",
244    "system",
245    "tan",
246    "tanh",
247    "tgamma",
248    "trans",
249    "true",
250    "trunc",
251    "typedef",
252    "under",
253    "urgent",
254    "void",
255    "while",
256    "xor",
257];
258
259/// Indent of one nesting level in a printed function body.
260const INDENT: &str = "    ";
261
262/// Write displayable items separated by `sep`, straight into the
263/// formatter — no intermediate `String` per item.
264fn joined<T: fmt::Display>(f: &mut fmt::Formatter<'_>, items: &[T], sep: &str) -> fmt::Result {
265    for (i, item) in items.iter().enumerate() {
266        if i > 0 {
267            f.write_str(sep)?;
268        }
269        write!(f, "{item}")?;
270    }
271    Ok(())
272}
273
274/// Print `// text`, with no trailing space on an empty comment.
275fn write_comment(f: &mut fmt::Formatter<'_>, text: &str) -> fmt::Result {
276    if text.is_empty() {
277        f.write_str("//")
278    } else {
279        write!(f, "// {text}")
280    }
281}