-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathfrom_str.rs
More file actions
177 lines (156 loc) Β· 5.6 KB
/
from_str.rs
File metadata and controls
177 lines (156 loc) Β· 5.6 KB
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
#![cfg(test)]
mod std_trait {
use std::str::FromStr;
use bnf::{Expression, Grammar, Production, Term};
#[test]
fn production_is_empty() {
assert!(Production::new().is_empty());
let mut p = Production::new();
p.add_to_rhs(Expression::new());
assert!(!p.is_empty());
}
fn std_str_trait<T: FromStr>(_: T, input: &str) {
let from_str_result = T::from_str(input);
assert!(from_str_result.is_ok())
}
#[test]
fn expression_from_str() {
let input = "'π΅' 'π' 'π'";
let expression = Expression::new();
std_str_trait(expression, input)
}
#[test]
fn grammar_from_str() {
let input = "<π> ::= 'π΅' 'π' | 'π'
<π€> ::= 'π ' 'π' | 'π'";
let grammar = Grammar::new();
std_str_trait(grammar, input)
}
#[test]
fn production_from_str() {
let input = "<π€> ::= 'π ' 'π' | 'π'";
let production = Production::new();
std_str_trait(production, input)
}
#[test]
fn terminal_from_str() {
let input = "'π '";
let terminal = Term::Terminal(String::new());
std_str_trait(terminal, input)
}
#[test]
fn nonterminal_from_str() {
let input = "<π€>";
let nonterminal = Term::Nonterminal(String::new());
std_str_trait(nonterminal, input)
}
}
mod custom_trait {
use bnf::{Expression, Grammar, Production, Term};
#[test]
fn expression_from_str() {
let input = "'π΅' 'π' 'π'";
let expression: Result<Expression, _> = input.parse();
assert!(expression.is_ok())
}
#[test]
fn grammar_from_str() {
let input = "<π> ::= 'π΅' 'π' | 'π'
<π€> ::= 'π ' 'π' | 'π'";
let grammar: Result<Grammar, _> = input.parse();
assert!(grammar.is_ok())
}
#[test]
fn grammar_from_str_returns_parsed_content() {
let input = "<a> ::= 'x'";
let grammar: Grammar = input.parse().expect("parse");
assert_eq!(
grammar.productions_iter().count(),
1,
"parsed grammar must have one production"
);
let prod = grammar.productions_iter().next().unwrap();
assert_eq!(prod.lhs, bnf::Term::Nonterminal("a".into()));
}
#[test]
fn production_from_str() {
let input = "<π€> ::= 'π ' 'π' | 'π'";
let production: Result<Production, _> = input.parse();
assert!(production.is_ok())
}
#[test]
fn terminal_from_str() {
let input = "'π '";
let terminal: Result<Term, _> = input.parse();
assert!(terminal.is_ok())
}
#[test]
fn nonterminal_from_str() {
let input = "<π€>";
let nonterminal: Result<Term, _> = input.parse();
assert!(nonterminal.is_ok())
}
}
mod comments {
use bnf::{Grammar, Term};
#[test]
fn grammar_with_comments_throughout() {
let input = "<a> ::= 'x' ; end of first rule
; comment-only line
<b> ::= 'y' ; end of second rule";
let grammar: Grammar = input.parse().expect("parse");
assert_eq!(
grammar.productions_iter().count(),
2,
"parsed grammar must have two productions"
);
let mut prods = grammar.productions_iter();
let first = prods.next().unwrap();
assert_eq!(first.lhs, Term::Nonterminal("a".into()));
assert_eq!(first.rhs_iter().next().unwrap().to_string(), "'x'");
let second = prods.next().unwrap();
assert_eq!(second.lhs, Term::Nonterminal("b".into()));
assert_eq!(second.rhs_iter().next().unwrap().to_string(), "'y'");
}
#[test]
fn comment_does_not_break_parsing() {
let input = "<a> ::= 'x' ; note\n<b> ::= 'y'";
let grammar: Grammar = input.parse().expect("parse");
assert_eq!(
grammar.productions_iter().count(),
2,
"parsed grammar must have two productions"
);
let mut prods = grammar.productions_iter();
let first = prods.next().unwrap();
assert_eq!(first.lhs, Term::Nonterminal("a".into()));
assert_eq!(first.rhs_iter().next().unwrap().to_string(), "'x'");
let second = prods.next().unwrap();
assert_eq!(second.lhs, Term::Nonterminal("b".into()));
assert_eq!(second.rhs_iter().next().unwrap().to_string(), "'y'");
}
/// Full annotated DNA grammar: leading comment, inline comment, trailing comment.
/// Comments are stripped; the grammar parses to the same structure as the uncommented version.
#[test]
fn annotated_dna_grammar_with_comments() {
let grammar_str = "; the building blocks of life!
<dna> ::= <base> | <base> <dna>
<base> ::= 'A' | 'C' | 'G' | 'T' ;(Adenine, Cytosine, Guanine, and Thymine)
; the end π";
let grammar: Grammar = grammar_str.parse().expect("parse annotated DNA grammar");
assert_eq!(
grammar.productions_iter().count(),
2,
"annotated grammar must have two productions (dna, base)"
);
let mut prods = grammar.productions_iter();
let dna = prods.next().unwrap();
assert_eq!(dna.lhs, Term::Nonterminal("dna".into()));
let dna_rhs: Vec<_> = dna.rhs_iter().map(|e| e.to_string()).collect();
assert_eq!(dna_rhs, ["<base>", "<base> <dna>"]);
let base = prods.next().unwrap();
assert_eq!(base.lhs, Term::Nonterminal("base".into()));
let base_rhs: Vec<_> = base.rhs_iter().map(|e| e.to_string()).collect();
assert_eq!(base_rhs, ["'A'", "'C'", "'G'", "'T'"]);
}
}