Skip to content

Commit b94c949

Browse files
author
Robert Durfee
committed
fixed one issue, still more to fix
1 parent 8282b62 commit b94c949

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

src/lib.rs

+54-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::collections::{
22
BTreeMap as Map,
33
BTreeSet as Set,
4+
VecDeque,
45
};
6+
use std::fmt::Debug;
57

68
use regular_expression::re::RE;
79
use finite_automata::{
@@ -43,7 +45,7 @@ impl<'a, T> Iterator for TokenIds<'a, T> {
4345
}
4446
}
4547

46-
pub fn lex<T: Clone + Ord>(input: &str, productions: &Map<RE, Option<T>>) -> Result<Vec<T>> {
48+
pub fn lex<T: Clone + Debug + Ord>(input: &str, productions: &Map<RE, Option<T>>) -> Result<Vec<T>> {
4749
let mut res = Vec::new();
4850
for (re, token) in productions {
4951
res.push(re.into_enfa(&mut TokenIds::new(token)));
@@ -60,9 +62,9 @@ pub fn lex<T: Clone + Ord>(input: &str, productions: &Map<RE, Option<T>>) -> Res
6062
}
6163
let dfa: DFA<Set<(&Option<T>, u32)>, char> = DFA::from(alt);
6264
let mut tokens = Vec::new();
63-
let mut characters: Vec<char> = input.chars().collect();
65+
let mut characters: VecDeque<char> = input.chars().collect();
6466
let mut source_index = dfa.initial_index();
65-
while let Some(character) = characters.pop() {
67+
while let Some(character) = characters.pop_front() {
6668
if let Some(transition_index) = dfa.contains(&(source_index, &character)) {
6769
let (_, _, target_index) = dfa.at(transition_index);
6870
source_index = target_index;
@@ -72,16 +74,16 @@ pub fn lex<T: Clone + Ord>(input: &str, productions: &Map<RE, Option<T>>) -> Res
7274
let token = tokens_iter.next().unwrap();
7375
while let Some(current_token) = tokens_iter.next() {
7476
if current_token != token {
75-
return Err(Error::from(ErrorKind::InconsistentTokensInFinalState));
77+
return Err(Error::new(ErrorKind::InconsistentTokensInFinalState, format!("{:?} != {:?}", current_token, token)));
7678
}
7779
}
7880
if let Some(token) = token {
7981
tokens.push(token.clone());
8082
}
8183
source_index = dfa.initial_index();
82-
characters.push(character);
84+
characters.push_front(character);
8385
} else {
84-
return Err(Error::from(ErrorKind::FailedToReachFinalState));
86+
return Err(Error::new(ErrorKind::FailedToReachFinalState, format!("{:?}", dfa.at(source_index))));
8587
}
8688
}
8789
}
@@ -90,14 +92,14 @@ pub fn lex<T: Clone + Ord>(input: &str, productions: &Map<RE, Option<T>>) -> Res
9092
let token = tokens_iter.next().unwrap();
9193
while let Some(current_token) = tokens_iter.next() {
9294
if current_token != token {
93-
return Err(Error::from(ErrorKind::InconsistentTokensInFinalState));
95+
return Err(Error::new(ErrorKind::InconsistentTokensInFinalState, format!("{:?} != {:?}", current_token, token)));
9496
}
9597
}
9698
if let Some(token) = token {
9799
tokens.push(token.clone());
98100
}
99101
} else {
100-
return Err(Error::from(ErrorKind::FailedToReachFinalState));
102+
return Err(Error::new(ErrorKind::FailedToReachFinalState, format!("{:?}", dfa.at(source_index))));
101103
}
102104
Ok(tokens)
103105
}
@@ -108,24 +110,63 @@ pub fn productions<T>(_input: &str) -> Result<Map<RE, Option<T>>> {
108110

109111
#[cfg(test)]
110112
mod tests {
111-
use regular_expression::{sym, rep};
113+
use regular_expression::{sym, rep, cat};
112114

113115
use crate::{Result, lex};
114116

115117
#[test]
116118
fn test_1() -> Result<()> {
117119
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
118-
enum Binary {
120+
enum Token {
119121
ZERO,
120122
ONE,
121123
};
122-
let expected = vec![Binary::ZERO, Binary::ONE, Binary::ZERO];
124+
let expected = vec![Token::ZERO, Token::ONE, Token::ZERO];
123125
let actual = lex("0 1 0 ", &map![
124-
sym!('0') => Some(Binary::ZERO),
125-
sym!('1') => Some(Binary::ONE),
126+
sym!('0') => Some(Token::ZERO),
127+
sym!('1') => Some(Token::ONE),
126128
rep!(sym!(' ')) => None
127129
]);
128130
assert_eq!(expected, actual?);
129131
Ok(())
130132
}
133+
134+
#[test]
135+
fn test_2() -> Result<()> {
136+
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
137+
#[allow(non_camel_case_types)]
138+
enum Token {
139+
ZERO_REP,
140+
ONE_REP
141+
};
142+
let expected = vec![Token::ZERO_REP, Token::ONE_REP, Token::ONE_REP];
143+
let actual = lex("00000001111 1111", &map![
144+
rep!(sym!('0')) => Some(Token::ZERO_REP),
145+
rep!(sym!('1')) => Some(Token::ONE_REP),
146+
rep!(sym!(' ')) => None
147+
]);
148+
assert_eq!(expected, actual?);
149+
Ok(())
150+
}
151+
152+
#[test]
153+
fn test_3() -> Result<()> {
154+
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
155+
#[allow(non_camel_case_types)]
156+
enum Token {
157+
ZERO,
158+
ZERO_ONE,
159+
ONE_ONE,
160+
ONE,
161+
};
162+
let expected = vec![Token::ZERO_ONE, Token::ONE];
163+
let actual = lex("011", &map![
164+
sym!('0') => Some(Token::ZERO),
165+
cat![sym!('0'), sym!('1')] => Some(Token::ZERO_ONE),
166+
cat![sym!('1'), sym!('1')] => Some(Token::ONE_ONE),
167+
sym!('1') => Some(Token::ONE)
168+
]);
169+
assert_eq!(expected, actual?);
170+
Ok(())
171+
}
131172
}

0 commit comments

Comments
 (0)