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
use crate::dot::{AttributeType, GraphType};
use crate::parser::ident::ident;

use pom::parser::{end, one_of, sym, tag, Parser};
// use pom::Parser;

pub fn strict_symbol<'a>() -> Parser<'a, char, String> {
  (tag("strict") | tag("STRICT")).map(|_| "strict".to_string())
}

pub fn graph_symbol<'a>() -> Parser<'a, char, AttributeType> {
  (tag("graph") | tag("GRAPH")).map(|_| AttributeType::Graph)
}

pub fn digraph_symbol<'a>() -> Parser<'a, char, bool> {
  (tag("digraph") | tag("DIGRAPH")).map(|_| true)
}

pub fn graph_type<'a>() -> Parser<'a, char, GraphType> {
  graph_symbol().map(|_| GraphType::Graph) | digraph_symbol().map(|_| GraphType::Digraph)
}

pub fn subgraph_symbol<'a>() -> Parser<'a, char, bool> {
  (tag("subgraph") | tag("SUBGRAPH")).map(|_| true)
}

pub fn node_symbol<'a>() -> Parser<'a, char, AttributeType> {
  (tag("node") | tag("NODE")).map(|_| AttributeType::Node)
}

pub fn edge_symbol<'a>() -> Parser<'a, char, AttributeType> {
  (tag("edge") | tag("EDGE")).map(|_| AttributeType::Edge)
}

pub fn space<'a>() -> Parser<'a, char, ()> {
  one_of(" \t\r\n").repeat(0..).discard()
}

pub fn id<'a>() -> Parser<'a, char, String> {
  ident()
}

pub fn comment<'a>() -> Parser<'a, char, ()> {
  let eol = (sym('\n').discard() | end()).name("eol");
  let not_eol = !(sym('\n').discard() | end()).name("not_eol");
  (tag("//") - (not_eol).repeat(1..) - eol).discard()
}


#[cfg(test)]
mod test {
  #[cfg(test)]
  mod comment {
    use super::super::comment;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "// some stuff here.".chars().collect();
      match comment().parse(&input1) {
        Ok(r) => assert_eq!(r, ()),
        Err(e) => panic!(format!(
          "failed with input: {:?} with error: {:?}",
          input1, e
        )),
      };
    }

  }

  #[cfg(test)]
  mod strict {
    use super::super::strict_symbol;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "strict".chars().collect();
      match strict_symbol().parse(&input1) {
        Ok(r) => assert_eq!(r, "strict".to_string()),
        _ => panic!(format!("failed with input: {:?}", input1)),
      };

      let input2: Vec<char> = "STRICT".chars().collect();
      match strict_symbol().parse(&input2) {
        Ok(r) => assert_eq!(r, "strict".to_string()),
        _ => panic!(format!("failed with input: {:?}", input2)),
      };

      let input3: Vec<char> = "strect".chars().collect();
      if strict_symbol().parse(&input3).is_ok() {
        panic!("Should have failed")
      }
    }

  }

  #[cfg(test)]
  mod graph {
    use super::super::*;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "graph".chars().collect();
      match graph_symbol().parse(&input1) {
        Ok(r) => assert_eq!(r, AttributeType::Graph),
        _ => panic!(format!("failed with input: {:?}", input1)),
      };

      let input2: Vec<char> = "GRAPH".chars().collect();
      match graph_symbol().parse(&input2) {
        Ok(r) => assert_eq!(r, AttributeType::Graph),
        _ => panic!(format!("failed with input: {:?}", input2)),
      };

      let input3: Vec<char> = "greph".chars().collect();
      if strict_symbol().parse(&input3).is_ok() {
        panic!("Should have failed")
      }
    }

  }

  #[cfg(test)]
  mod digraph {
    use super::super::*;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "digraph".chars().collect();
      match digraph_symbol().parse(&input1) {
        Ok(r) => assert_eq!(r, true),
        _ => panic!(format!("failed with input: {:?}", input1)),
      };

      let input2: Vec<char> = "DIGRAPH".chars().collect();
      match digraph_symbol().parse(&input2) {
        Ok(r) => assert_eq!(r, true),
        _ => panic!(format!("failed with input: {:?}", input2)),
      };

      let input3: Vec<char> = "diegreph".chars().collect();
      if strict_symbol().parse(&input3).is_ok() {
        panic!("Should have failed")
      }
    }

  }

  #[cfg(test)]
  mod subgraph {
    use super::super::*;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "subgraph".chars().collect();
      match subgraph_symbol().parse(&input1) {
        Ok(r) => assert_eq!(r, true),
        _ => panic!(format!("failed with input: {:?}", input1)),
      };

      let input2: Vec<char> = "SUBGRAPH".chars().collect();
      match subgraph_symbol().parse(&input2) {
        Ok(r) => assert_eq!(r, true),
        _ => panic!(format!("failed with input: {:?}", input2)),
      };

      let input3: Vec<char> = "subgreph".chars().collect();
      if strict_symbol().parse(&input3).is_ok() {
        panic!("Should have failed")
      }
    }
  }

  #[cfg(test)]
  mod node {
    use super::super::*;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "node".chars().collect();
      match node_symbol().parse(&input1) {
        Ok(r) => assert_eq!(r, AttributeType::Node),
        _ => panic!(format!("failed with input: {:?}", input1)),
      };

      let input2: Vec<char> = "NODE".chars().collect();
      match node_symbol().parse(&input2) {
        Ok(r) => assert_eq!(r, AttributeType::Node),
        _ => panic!(format!("failed with input: {:?}", input2)),
      };

      let input3: Vec<char> = "nodd".chars().collect();
      if strict_symbol().parse(&input3).is_ok() {
        panic!("Should have failed")
      }
    }

  }

  #[cfg(test)]
  mod edge {
    use super::super::*;

    #[test]
    fn mixed_tokens() {
      let input1: Vec<char> = "edge".chars().collect();
      match edge_symbol().parse(&input1) {
        Ok(r) => assert_eq!(r, AttributeType::Edge),
        _ => panic!(format!("failed with input: {:?}", input1)),
      };

      let input2: Vec<char> = "EDGE".chars().collect();
      match edge_symbol().parse(&input2) {
        Ok(r) => assert_eq!(r, AttributeType::Edge),
        _ => panic!(format!("failed with input: {:?}", input2)),
      };

      let input3: Vec<char> = "egde".chars().collect();
      if strict_symbol().parse(&input3).is_ok() {
        panic!("Should have failed")
      }
    }

  }
}