forked from Armavica/rustlex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproperties.rs
53 lines (44 loc) · 1.2 KB
/
properties.rs
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
#![feature(rustc_private,plugin)]
#![plugin(rustlex)]
#[allow(plugin_as_library)]
extern crate rustlex;
#[macro_use] extern crate log;
use std::io::BufReader;
use self::Token::{Open,Close};
#[derive(PartialEq,Debug)]
pub enum Token {
Open,
Close
}
rustlex! PropertiesLexer {
property depth:isize = 0;
let OPEN = '(';
let CLOSE = ')';
. => |_:&mut PropertiesLexer<R>| { None }
OPEN => |lexer:&mut PropertiesLexer<R>| { lexer.depth += 1; Some(Open) }
CLOSE => |lexer:&mut PropertiesLexer<R>| {
lexer.depth -= 1;
if lexer.depth<0 { panic!("invalid parens nesting") };
Some(Close) }
}
#[test]
fn test_ok() {
let inp = BufReader::new("((()()))".as_bytes());
let lexer = PropertiesLexer::new(inp);
let result:Vec<Token> = lexer.collect();
assert_eq!(8, result.len());
}
#[test]
fn test_not_closed() {
let inp = BufReader::new("(((()))".as_bytes());
let mut lexer = PropertiesLexer::new(inp);
while lexer.next().is_some() {}
assert_eq!(1, lexer.depth);
}
#[test]
#[should_panic]
fn test_not_open() {
let inp = BufReader::new("(()))".as_bytes());
let mut lexer = PropertiesLexer::new(inp);
while lexer.next().is_some() {}
}