smc_scan_scxml 0.1.0

SCXML frontend for the Scan model checker.
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
409
410
411
412
413
414
415
416
//! Parser for SCAN's XML specification format.

mod fsm;
mod omg_types;
mod property;
mod rye;
mod vocabulary;

pub use self::fsm::*;
pub use self::omg_types::*;
pub use self::property::*;
pub use self::vocabulary::*;
use anyhow::{Context, anyhow, bail};
use boa_ast::Expression;
use boa_ast::StatementListItem;
use boa_ast::scope::Scope;
use boa_interner::Interner;
use log::warn;
use log::{error, info, trace};
use quick_xml::Reader;
use quick_xml::events::Event;
use std::collections::HashMap;
use std::io::BufRead;
use std::io::Read;
use std::io::Seek;
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ParserError {
    #[error("error parsing tag `{0}`")]
    Tag(String),
    #[error("unknown or unexpected empty tag `{0}`")]
    UnexpectedTag(String),
    #[error("unknown or unexpected start tag `{0}`")]
    UnexpectedStartTag(String),
    #[error("unknown or unexpected end tag `{0}`")]
    UnexpectedEndTag(String),
    #[error("missing required attribute `{0}`")]
    MissingAttr(String),
    #[error("unknown or unexpected attribute key `{0}`")]
    UnknownAttrKey(String),
    #[error("open tags have not been closed")]
    UnclosedTags,
    #[error("type annotation missing")]
    NoTypeAnnotation,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ConvinceTag {
    Specification,
    Model,
    ProcessList,
    DataTypeList,
    Enumeration(String),
    Structure(String),
}

impl From<ConvinceTag> for &'static str {
    fn from(value: ConvinceTag) -> Self {
        match value {
            ConvinceTag::Specification => TAG_SPECIFICATION,
            ConvinceTag::Model => TAG_MODEL,
            ConvinceTag::ProcessList => TAG_PROCESS_LIST,
            ConvinceTag::DataTypeList => TAG_DATA_TYPE_LIST,
            ConvinceTag::Enumeration(_) => TAG_ENUMERATION,
            ConvinceTag::Structure(_) => TAG_STRUCT,
        }
    }
}

fn attrs(
    tag: quick_xml::events::BytesStart<'_>,
    keys: &[&str],
    opt_keys: &[&str],
) -> anyhow::Result<HashMap<String, String>> {
    let mut attrs = HashMap::new();
    for attr in tag.attributes() {
        let attr = attr?;
        let key = str::from_utf8(attr.key.into_inner())?;
        if keys.contains(&key) || opt_keys.contains(&key) {
            let val = attr.unescape_value()?.to_string();
            attrs.insert(key.to_string(), val);
        } else {
            error!(target: "parser", "found unknown attribute '{key}'");
            bail!(ParserError::UnknownAttrKey(key.to_string()));
        }
    }
    for key in keys {
        if !attrs.contains_key(*key) {
            error!(target: "parser", "missing required attribute '{key}'");
            bail!(ParserError::MissingAttr(key.to_string()));
        }
    }
    Ok(attrs)
}

fn count_lines<R: BufRead + Seek>(mut reader: Reader<R>) -> usize {
    let end_pos = reader.buffer_position();
    reader.get_mut().rewind().unwrap();
    reader.into_inner().take(end_pos).lines().count()
}

fn ecmascript(code: &str, scope: &Scope, interner: &mut Interner) -> anyhow::Result<Expression> {
    let script = boa_parser::Parser::new(boa_parser::Source::from_bytes(&code))
        .parse_script(scope, interner)
        .map_err(|err| anyhow!(err))
        .context("ECMAScript parser error")?;
    if script.statements().len() == 1 {
        let statement = script
            .statements()
            .first()
            .ok_or_else(|| anyhow!("expression {code} is not a statement"))?
            .to_owned();
        match statement {
            StatementListItem::Statement(statement) => match *statement {
                boa_ast::Statement::Expression(expression) => Ok(expression),
                _ => Err(anyhow!("{statement:?} assignment is not an expression")),
            },
            _ => Err(anyhow!("{statement:?} assignment is not an expression")),
        }
    } else {
        Err(anyhow!("code must be made by a single statement"))
    }
}

/// Represents a model specified in the CONVINCE-XML format.
#[derive(Debug)]
pub struct Parser {
    pub(crate) processes: HashMap<String, Scxml>,
    pub(crate) types: OmgTypes,
    pub(crate) properties: Properties,
    pub(crate) interner: Interner,
}

impl Parser {
    /// Builds a [`Parser`] representation by parsing the given main file of a model specification in the CONVINCE-XML format,
    /// or a folder containing the required source files.
    ///
    /// Fails if the parsed content contains syntactic errors.
    pub fn parse(path: &Path) -> anyhow::Result<Self> {
        info!(target: "parser", "creating parser");
        let mut parser = Parser {
            processes: HashMap::new(),
            types: OmgTypes::new(),
            properties: Properties::new(),
            interner: Interner::new(),
        };
        if path.is_dir() {
            info!(target: "parser", "parsing directory '{}'", path.display());
            parser.parse_directory(path)?;
        } else {
            info!(target: "parser", "parsing main model file '{}'", path.display());
            let mut reader = Reader::from_file(path).with_context(|| {
                format!("failed to create reader from file '{}'", path.display())
            })?;
            let parent = path.parent().ok_or(anyhow!(
                "failed to take parent directory of '{}'",
                path.display()
            ))?;
            parser.parse_main(&mut reader, parent).with_context(|| {
                format!(
                    "failed to parse model specification at line {} in '{}'",
                    count_lines(reader),
                    path.display(),
                )
            })?;
        }
        Ok(parser)
    }

    fn parse_directory(&mut self, path: &Path) -> anyhow::Result<()> {
        let model_found = self.parse_directory_check(path)?;
        if model_found {
            Ok(())
        } else {
            bail!("No SCXML model found in folder `{}`", path.display())
        }
    }

    fn parse_directory_check(&mut self, path: &Path) -> anyhow::Result<bool> {
        let mut model_found = false;
        for entry in std::fs::read_dir(path)
            .with_context(|| format!("failed to read directory '{}'", path.display()))?
        {
            let path = entry.context("failed to read directory entry")?.path();
            if path.is_dir() {
                model_found |= self.parse_directory_check(&path)?;
            } else {
                self.parse_file(&path)?;
                model_found = true;
            }
        }
        Ok(model_found)
    }

    fn parse_file(&mut self, path: &Path) -> anyhow::Result<()> {
        if path.is_dir() {
            bail!("path '{}' is a directory", path.display());
        } else if let Some(ext) = path.extension() {
            let ext = ext
                .to_str()
                .ok_or(anyhow!("failed file extension conversion to string"))?;
            match ext {
                "scxml" => {
                    info!("creating reader from file '{}'", path.display());
                    let mut reader = Reader::from_file(path).with_context(|| {
                        format!("failed to create reader from file '{}'", path.display())
                    })?;
                    let fsm = fsm::parse(&mut reader, &mut self.interner, &self.types)
                        .with_context(|| {
                            format!(
                                "failed to parse fsm at line {} in '{}'",
                                count_lines(reader),
                                path.display(),
                            )
                        })?;
                    self.processes.insert(fsm.name.to_owned(), fsm);
                }
                "xml" => {
                    info!("creating reader from file '{}'", path.display());
                    let mut reader = Reader::from_file(path).with_context(|| {
                        format!("failed to create reader from file '{}'", path.display())
                    })?;
                    self.properties
                        .parse(&mut reader, &mut self.interner, &self.types)
                        .with_context(|| {
                            format!(
                                "failed to parse properties at line {} in '{}'",
                                count_lines(reader),
                                path.display(),
                            )
                        })?;
                }
                _ => {
                    warn!(target: "parser", "unknown file extension '{ext}'");
                }
            }
        }
        Ok(())
    }

    fn parse_main<R: BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        parent: &Path,
    ) -> anyhow::Result<()> {
        let mut buf = Vec::new();
        let mut stack = Vec::new();
        loop {
            match reader
                .read_event_into(&mut buf)
                .context("failed reading event")?
            {
                Event::Start(tag) => {
                    let tag_name = &*reader.decoder().decode(tag.name().into_inner())?;
                    trace!(target: "parser", "start tag '{tag_name}'");
                    let new_tag = match tag_name {
                        TAG_SPECIFICATION if stack.is_empty() => ConvinceTag::Specification,
                        TAG_MODEL
                            if stack
                                .last()
                                .is_some_and(|e| *e == ConvinceTag::Specification) =>
                        {
                            ConvinceTag::Model
                        }
                        TAG_PROCESS_LIST
                            if stack.last().is_some_and(|e| *e == ConvinceTag::Model) =>
                        {
                            ConvinceTag::ProcessList
                        }
                        _ => {
                            error!(target: "parser", "unknown or unexpected start tag '{tag_name}'");
                            bail!(ParserError::UnexpectedStartTag(tag_name.to_string()));
                        }
                    };
                    stack.push(new_tag);
                }
                Event::End(tag) => {
                    let tag_name = &*reader.decoder().decode(tag.name().into_inner())?;
                    if stack
                        .pop()
                        .is_some_and(|state| Into::<&str>::into(state) == tag_name)
                    {
                        trace!(target: "parser", "end tag '{tag_name}'");
                    } else {
                        error!(target: "parser", "unknown or unexpected end tag '{tag_name}'");
                        bail!(ParserError::UnexpectedEndTag(tag_name.to_string()));
                    }
                }
                Event::Empty(tag) => {
                    let tag_name = &*reader.decoder().decode(tag.name().into_inner())?;
                    trace!(target: "parser", "empty tag '{tag_name}'");
                    match tag_name {
                        TAG_TYPES
                            if stack
                                .last()
                                .is_some_and(|e| *e == ConvinceTag::Specification) =>
                        {
                            let attrs = attrs(tag, &[ATTR_PATH], &[])
                                .context("failed to parse 'types' tag attributes")?;
                            let mut path = parent.to_owned();
                            path.extend(&PathBuf::from(attrs.get(ATTR_PATH).unwrap()));
                            info!("creating reader from file '{}'", path.display());
                            let mut reader = Reader::from_file(path.clone())?;
                            self.types.parse(&mut reader).with_context(|| {
                                format!(
                                    "failed to parse types specification at line {} in '{}'",
                                    count_lines(reader),
                                    path.display()
                                )
                            })?;
                        }
                        TAG_PROPERTIES
                            if stack
                                .last()
                                .is_some_and(|e| *e == ConvinceTag::Specification) =>
                        {
                            let attrs = attrs(tag, &[ATTR_PATH], &[])
                                .context("failed to parse 'properties' tag attributes")?;
                            let mut path = parent.to_owned();
                            path.extend(&PathBuf::from(attrs.get(ATTR_PATH).unwrap()));
                            info!(target: "parser", "creating reader from file '{}'", path.display());
                            let mut reader = Reader::from_file(&path).with_context(|| {
                                format!("failed to create reader from file '{}'", path.display())
                            })?;
                            self.properties
                                .parse(&mut reader, &mut self.interner, &self.types)
                                .with_context(|| {
                                    format!(
                                        "failed to parse properties at line {} in '{}'",
                                        count_lines(reader),
                                        path.display(),
                                    )
                                })?;
                        }
                        TAG_PROCESS
                            if stack.last().is_some_and(|e| *e == ConvinceTag::ProcessList) =>
                        {
                            let attrs = attrs(tag, &[ATTR_ID, ATTR_PATH], &[ATTR_MOC])
                                .context("failed to parse 'process' tag attributes")?;
                            if let Some(moc) = attrs.get(ATTR_MOC)
                                && moc != "fsm"
                            {
                                bail!("unknown moc {moc}");
                            }
                            let process_id = attrs.get(ATTR_ID).unwrap().clone();
                            if self.processes.contains_key(&process_id) {
                                bail!("process '{process_id}' declared multiple times");
                            }
                            let mut path = parent.to_owned();
                            path.extend(&PathBuf::from(attrs.get(ATTR_PATH).unwrap()));
                            info!(target: "parser",
                                "creating reader from file '{}' for fsm '{process_id}'",
                                path.display()
                            );
                            let mut reader = Reader::from_file(path.clone())?;
                            let fsm = fsm::parse(&mut reader, &mut self.interner, &self.types)
                                .with_context(|| {
                                    format!(
                                        "failed to parse fsm at line {} in '{}'",
                                        count_lines(reader),
                                        path.display()
                                    )
                                })?;
                            // Add process to list and check that no process was already in the list under the same name
                            if self.processes.insert(process_id.clone(), fsm).is_some() {
                                panic!("process added to list multiple times");
                            }
                        }
                        _ => {
                            error!(target: "parser", "unknown or unexpected empty tag '{tag_name}'");
                            bail!(ParserError::UnexpectedTag(tag_name.to_string()));
                        }
                    }
                }
                Event::Text(text) => {
                    let text = text.bytes().collect::<Result<Vec<u8>, std::io::Error>>()?;
                    let text = String::from_utf8(text)?;
                    if !text.trim().is_empty() {
                        error!(target: "parser", "text elements not allowed, ignoring");
                    }
                    continue;
                }
                Event::Comment(_comment) => continue,
                Event::CData(_) => {
                    return Err(anyhow!("CData not supported"));
                }
                Event::Decl(_) => continue,
                Event::PI(_) => {
                    return Err(anyhow!("Processing Instructions not supported"));
                }
                Event::DocType(_) => {
                    return Err(anyhow!("DocType not supported"));
                }
                Event::Eof => {
                    info!("parsing completed");
                    if !stack.is_empty() {
                        return Err(anyhow!(ParserError::UnclosedTags,));
                    }
                    break;
                }
                Event::GeneralRef(_bytes_ref) => {
                    return Err(anyhow!("General References not supported"));
                }
            }
            // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low
            buf.clear();
        }
        if let Some(tag) = stack.pop() {
            Err(anyhow!("unclosed tag {}", Into::<&str>::into(tag)))
        } else {
            Ok(())
        }
    }
}