Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 54 additions & 56 deletions fuzzydate/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,72 +797,67 @@ struct Ones;

impl Ones {
fn parse(l: &[Lexeme]) -> Option<(u32, usize)> {
let mut res = match l.first() {
Some(Lexeme::One) => Some(1),
Some(Lexeme::Two) => Some(2),
Some(Lexeme::Three) => Some(3),
Some(Lexeme::Four) => Some(4),
Some(Lexeme::Five) => Some(5),
Some(Lexeme::Six) => Some(6),
Some(Lexeme::Seven) => Some(7),
Some(Lexeme::Eight) => Some(8),
Some(Lexeme::Nine) => Some(9),
let mut tokens = 1; // starting at 1 because we'll either match one token or return None
if let Some(Lexeme::ST | Lexeme::RD | Lexeme::ND | Lexeme::TH) = l.get(1) {
tokens += 1;
}
match l.first() {
Some(Lexeme::One) => Some((1, tokens)),
Some(Lexeme::Two) => Some((2, tokens)),
Some(Lexeme::Three) => Some((3, tokens)),
Some(Lexeme::Four) => Some((4, tokens)),
Some(Lexeme::Five) => Some((5, tokens)),
Some(Lexeme::Six) => Some((6, tokens)),
Some(Lexeme::Seven) => Some((7, tokens)),
Some(Lexeme::Eight) => Some((8, tokens)),
Some(Lexeme::Nine) => Some((9, tokens)),
Some(Lexeme::Num(num)) if *num < 10 => Some((*num, tokens)),
_ => None,
};

if res.is_none() {
if let Some(Lexeme::Num(n)) = l.first() {
if *n < 10 {
res = Some(*n);
}
}
}

res.map(|n| (n, 1))
}
}

struct Teens;
impl Teens {
fn parse(l: &[Lexeme]) -> Option<(u32, usize)> {
let mut res = match l.first() {
Some(Lexeme::Ten) => Some((10, 1)),
Some(Lexeme::Eleven) => Some((11, 1)),
Some(Lexeme::Twelve) => Some((12, 1)),
Some(Lexeme::Thirteen) => Some((13, 1)),
Some(Lexeme::Fourteen) => Some((14, 1)),
Some(Lexeme::Fifteen) => Some((15, 1)),
Some(Lexeme::Sixteen) => Some((16, 1)),
Some(Lexeme::Seventeen) => Some((17, 1)),
Some(Lexeme::Eighteen) => Some((18, 1)),
Some(Lexeme::Nineteen) => Some((19, 1)),
let mut tokens = 1;
if let Some(Lexeme::TH) = l.get(1) {
tokens += 1;
}
match l.first() {
Some(Lexeme::Ten) => Some((10, tokens)),
Some(Lexeme::Eleven) => Some((11, tokens)),
Some(Lexeme::Twelve) => Some((12, tokens)),
Some(Lexeme::Thirteen) => Some((13, tokens)),
Some(Lexeme::Fourteen) => Some((14, tokens)),
Some(Lexeme::Fifteen) => Some((15, tokens)),
Some(Lexeme::Sixteen) => Some((16, tokens)),
Some(Lexeme::Seventeen) => Some((17, tokens)),
Some(Lexeme::Eighteen) => Some((18, tokens)),
Some(Lexeme::Nineteen) => Some((19, tokens)),
Some(Lexeme::Num(num)) if *num >= 10 && *num < 20 => Some((*num, tokens)),
_ => None,
};

if res.is_none() {
if let Some(Lexeme::Num(n)) = l.first() {
if *n >= 10 && *n <= 19 {
res = Some((*n, 1));
}
}
}

res
}
}

struct Tens;
impl Tens {
fn parse(l: &[Lexeme]) -> Option<(u32, usize)> {
let mut tokens = 1; // starting at 1 because we'll either match one token or return None
if let Some(Lexeme::TH) = l.get(1) {
tokens += 1;
}

match l.first() {
Some(Lexeme::Twenty) => Some((20, 1)),
Some(Lexeme::Thirty) => Some((30, 1)),
Some(Lexeme::Fourty) => Some((40, 1)),
Some(Lexeme::Fifty) => Some((50, 1)),
Some(Lexeme::Sixty) => Some((60, 1)),
Some(Lexeme::Seventy) => Some((70, 1)),
Some(Lexeme::Eighty) => Some((80, 1)),
Some(Lexeme::Ninety) => Some((90, 1)),
Some(Lexeme::Twenty) => Some((20, tokens)),
Some(Lexeme::Thirty) => Some((30, tokens)),
Some(Lexeme::Fourty) => Some((40, tokens)),
Some(Lexeme::Fifty) => Some((50, tokens)),
Some(Lexeme::Sixty) => Some((60, tokens)),
Some(Lexeme::Seventy) => Some((70, tokens)),
Some(Lexeme::Eighty) => Some((80, tokens)),
Some(Lexeme::Ninety) => Some((90, tokens)),
_ => None,
}
}
Expand Down Expand Up @@ -913,7 +908,6 @@ struct NumTriple;
impl NumTriple {
fn parse(l: &[Lexeme]) -> Option<(u32, usize)> {
let mut tokens = 0;

if let Some((ones, t)) = Ones::parse(&l[tokens..]) {
tokens += t;

Expand Down Expand Up @@ -960,14 +954,17 @@ impl NumTriple {
return Some((num_double, tokens));
}

tokens = 0;
if let Some(&Lexeme::Num(n)) = l.get(tokens) {
// number literal
if let Some(Lexeme::Num(num)) = l.first() {
tokens += 1;
if n > 99 && n < 1000 {
return Some((n, tokens));
if let Some(Lexeme::ST | Lexeme::RD | Lexeme::ND | Lexeme::TH) = l.get(1) {
tokens += 1;
}
}

if *num > 99 && *num < 1000 {
return Some((*num, tokens));
}
}
None
}
}
Expand Down Expand Up @@ -1039,7 +1036,8 @@ impl Num {
}

tokens = 0;
// NUM

// number literal
if let Some(&Lexeme::Num(n)) = l.get(tokens) {
tokens += 1;
if n >= 1000 {
Expand Down
41 changes: 41 additions & 0 deletions fuzzydate/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,47 @@ lazy_static! {
map.insert("thousand", Lexeme::Thousand);
map.insert("million", Lexeme::Million);
map.insert("billion", Lexeme::Billion);
map.insert("first", Lexeme::One);
map.insert("second", Lexeme::Two);
map.insert("third", Lexeme::Three);
map.insert("fourth", Lexeme::Four);
map.insert("fifth", Lexeme::Five);
map.insert("sixth", Lexeme::Six);
map.insert("seventh", Lexeme::Seven);
map.insert("eigth", Lexeme::Eight);
map.insert("ninth", Lexeme::Nine);
map.insert("tenth", Lexeme::Ten);
map.insert("eleventh", Lexeme::Eleven);
map.insert("twelfth", Lexeme::Twelve);
map.insert("thirteenth", Lexeme::Thirteen);
map.insert("fourteenth", Lexeme::Fourteen);
map.insert("fifteenth", Lexeme::Fifteen);
map.insert("sixteenth", Lexeme::Sixteen);
map.insert("seventeenth", Lexeme::Seventeen);
map.insert("eighteenth", Lexeme::Eighteen);
map.insert("nineteenth", Lexeme::Nineteen);
map.insert("twentieth", Lexeme::Twenty);
map.insert("thirtieth", Lexeme::Thirty);
map.insert("fourtieth", Lexeme::Fourty);
map.insert("fiftieth", Lexeme::Fifty);
map.insert("sixtieth", Lexeme::Sixty);
map.insert("seventieth", Lexeme::Seventy);
map.insert("eightieth", Lexeme::Eighty);
map.insert("ninetieth", Lexeme::Ninety);
map.insert("hundredth", Lexeme::Hundred);
map.insert("thousandth", Lexeme::Thousand);
map.insert("millionth", Lexeme::Million);
map.insert("billionth", Lexeme::Billion);
map.insert("before", Lexeme::Before);
map.insert("ago", Lexeme::Ago);
map.insert("midnight", Lexeme::Midnight);
map.insert("noon", Lexeme::Noon);
map.insert("a", Lexeme::A);
map.insert("the", Lexeme::The);
map.insert("nd", Lexeme::ND);
map.insert("st", Lexeme::ST);
map.insert("rd", Lexeme::RD);
map.insert("th", Lexeme::RD);

map
};
Expand Down Expand Up @@ -198,6 +233,12 @@ pub enum Lexeme {
Million,
Billion,
Last,

// Suffixes
ST,
ND,
RD,
TH,
}

const LEXER_STACK_SIZE: usize = 20;
Expand Down
Loading