Skip to main content

step_p21/parser/exchange/
anchor.rs

1use crate::{
2    ast::*,
3    parser::{combinator::*, token::*},
4};
5use nom::{Parser, branch::alt};
6
7/// anchor_section = `ANCHOR;` [anchor_list] `ENDSEC;` .
8pub fn anchor_section(input: &str) -> ParseResult<'_, Vec<Anchor>> {
9    tuple_((tag_("ANCHOR;"), anchor_list, tag_("ENDSEC;")))
10        .map(|(_start, anchors, _end)| anchors)
11        .parse(input)
12}
13
14/// anchor_list = { [anchor()] } .
15pub fn anchor_list(input: &str) -> ParseResult<'_, Vec<Anchor>> {
16    many0_(anchor).parse(input)
17}
18
19/// anchor = [anchor_name] `=` [anchor_item] { [anchor_tag] } `;` .
20pub fn anchor(input: &str) -> ParseResult<'_, Anchor> {
21    tuple_((
22        anchor_name,
23        char_('='),
24        anchor_item,
25        many0_(anchor_tag),
26        char_(';'),
27    ))
28    .map(|(name, _eq, item, tags, _semicolon)| Anchor { name, item, tags })
29    .parse(input)
30}
31
32/// anchor_item = `$` | [integer] | [real] | [string] | [enumeration] | binary |
33/// [rhs_occurrence_name] | [resource] | [anchor_item_list] .
34pub fn anchor_item(input: &str) -> ParseResult<'_, AnchorItem> {
35    alt((
36        char_('$').map(|_| AnchorItem::NotProvided),
37        integer.map(AnchorItem::Integer),
38        real.map(AnchorItem::Real),
39        string.map(AnchorItem::String),
40        rhs_occurrence_name.map(AnchorItem::Name),
41        enumeration.map(AnchorItem::Enumeration),
42        // FIXME binary
43        anchor_item_list,
44    ))
45    .parse(input)
46}
47
48/// anchor_item_list = `(` \[ [anchor_item] { `,` [anchor_item] } \] `)` .
49pub fn anchor_item_list(input: &str) -> ParseResult<'_, AnchorItem> {
50    tuple_((char_('('), opt_(comma_separated(anchor_item)), char_(')')))
51        .map(|(_open, anchors, _close)| {
52            AnchorItem::List(anchors.unwrap_or_default())
53        })
54        .parse(input)
55}
56
57/// anchor_tag = `{` [tag_name] `:` [anchor_item] `}` .
58pub fn anchor_tag(input: &str) -> ParseResult<'_, (String, AnchorItem)> {
59    tuple_((char_('{'), tag_name, char_(':'), anchor_item, char_('}')))
60        .map(|(_open, name, _colon, item, _close)| (name, item))
61        .parse(input)
62}