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
use crate::{
ast::*,
parser::{combinator::*, exchange::*, token::*},
};
use nom::{branch::alt, Parser};
pub fn data_section(input: &str) -> ParseResult<DataSection> {
tuple_((
tag_("DATA"),
opt_(tuple_((char_('('), parameter_list, char_(')')))),
char_(';'),
entity_instance_list,
tag_("ENDSEC;"),
))
.map(|(_start, meta, _semicolon, entities, _end)| DataSection {
meta: meta
.map(|(_open, params, _close)| params)
.unwrap_or_default(),
entities,
})
.parse(input)
}
pub fn entity_instance_list(input: &str) -> ParseResult<Vec<EntityInstance>> {
many0_(entity_instance).parse(input)
}
pub fn entity_instance(input: &str) -> ParseResult<EntityInstance> {
alt((simple_entity_instance, complex_entity_instance)).parse(input)
}
pub fn simple_entity_instance(input: &str) -> ParseResult<EntityInstance> {
tuple_((entity_instance_name, char_('='), simple_record, char_(';')))
.map(|(id, _eq, record, _semicolon)| EntityInstance::Simple { id, record })
.parse(input)
}
pub fn complex_entity_instance(input: &str) -> ParseResult<EntityInstance> {
tuple_((
entity_instance_name,
char_('='),
subsuper_record,
char_(';'),
))
.map(|(id, _eq, subsuper, _semicolon)| EntityInstance::Complex { id, subsuper })
.parse(input)
}
pub fn simple_record(input: &str) -> ParseResult<Record> {
tuple_((keyword, char_('('), opt_(parameter_list), char_(')')))
.map(|(name, _open, parameter, _close)| Record {
name,
parameter: parameter.unwrap_or_default().into_iter().collect(),
})
.parse(input)
}
pub fn simple_record_list(input: &str) -> ParseResult<Vec<Record>> {
many0_(simple_record).parse(input)
}
pub fn subsuper_record(input: &str) -> ParseResult<SubSuperRecord> {
tuple_((char_('('), simple_record_list, char_(')')))
.map(|(_open, records, _close)| SubSuperRecord(records))
.parse(input)
}
#[cfg(test)]
mod tests {
use nom::Finish;
#[test]
fn simple_recode1() {
let (res, record) = super::simple_record("A(1, 2.0)").finish().unwrap();
dbg!(record);
assert_eq!(res, "");
}
#[test]
fn simple_recode2() {
let (res, record) = super::simple_record(
"LENGTH_MEASURE_WITH_UNIT( LENGTH_MEASURE( 1.00000000000000 ), #359 )",
)
.finish()
.unwrap();
dbg!(record);
assert_eq!(res, "");
}
}