ruststep/parser/exchange/
anchor.rs

1use crate::{
2    ast::*,
3    parser::{combinator::*, token::*},
4};
5use nom::{branch::alt, Parser};
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 | [rhs_occurrence_name] | [resource] | [anchor_item_list] .
33pub fn anchor_item(input: &str) -> ParseResult<AnchorItem> {
34    alt((
35        char_('$').map(|_| AnchorItem::NotProvided),
36        integer.map(AnchorItem::Integer),
37        real.map(AnchorItem::Real),
38        string.map(AnchorItem::String),
39        rhs_occurrence_name.map(AnchorItem::Name),
40        enumeration.map(AnchorItem::Enumeration),
41        // FIXME binary
42        anchor_item_list,
43    ))
44    .parse(input)
45}
46
47/// anchor_item_list = `(` \[ [anchor_item] { `,` [anchor_item] } \] `)` .
48pub fn anchor_item_list(input: &str) -> ParseResult<AnchorItem> {
49    tuple_((char_('('), opt_(comma_separated(anchor_item)), char_(')')))
50        .map(|(_open, anchors, _close)| AnchorItem::List(anchors.unwrap_or_default()))
51        .parse(input)
52}
53
54/// anchor_tag = `{` [tag_name] `:` [anchor_item] `}` .
55pub fn anchor_tag(input: &str) -> ParseResult<(String, AnchorItem)> {
56    tuple_((char_('{'), tag_name, char_(':'), anchor_item, char_('}')))
57        .map(|(_open, name, _colon, item, _close)| (name, item))
58        .parse(input)
59}