lower test passes

This commit is contained in:
Niko Matsakis 2015-06-18 06:13:18 -04:00
parent 088ad2f444
commit 8dd121a8e7
3 changed files with 68 additions and 5 deletions

View File

@ -4,6 +4,7 @@
* representation incrementally.
*/
use std::cmp::Ord;
use intern::InternedString;
use grammar::parse_tree::Span;
use std::collections::HashMap;
@ -47,7 +48,7 @@ pub enum TypeRepr {
Lifetime(InternedString),
}
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Types {
terminal_type: TypeRepr,
nonterminal_types: HashMap<InternedString, TypeRepr>
@ -76,6 +77,23 @@ impl Types {
}
}
#[derive(Debug)]
struct DummyTypes<'a> {
terminal_type: &'a TypeRepr,
nonterminal_types: Vec<(&'a InternedString, &'a TypeRepr)>,
}
impl Debug for Types {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
let mut nonterminal_types: Vec<_> = self.nonterminal_types.iter()
.collect();
nonterminal_types.sort_by(|k1, k2| Ord::cmp(&k1.0, &k2.0));
let dummy = DummyTypes { terminal_type: &self.terminal_type,
nonterminal_types: nonterminal_types };
Debug::fmt(&dummy, fmt)
}
}
impl Display for TypeRepr {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self {
@ -113,8 +131,8 @@ impl ActionFn {
impl Symbol {
pub fn ty<'ty>(&self, t: &'ty Types) -> &'ty TypeRepr {
match *self {
Symbol::Nonterminal(_) => t.terminal_type(),
Symbol::Terminal(id) => t.nonterminal_type(id),
Symbol::Terminal(_) => t.terminal_type(),
Symbol::Nonterminal(id) => t.nonterminal_type(id),
}
}
}

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::cell::RefCell;
use std::fmt::{Debug, Display, Error, Formatter};
use std::cmp::{PartialOrd, Ord, Ordering};
#[cfg(test)]
mod test;
@ -15,7 +16,7 @@ pub struct Interner {
strings: Vec<String>,
}
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Copy, Clone, Hash, Eq, PartialEq)]
pub struct InternedString {
index: u32
}
@ -78,3 +79,15 @@ impl Display for InternedString {
read(|interner| Display::fmt(&interner.data(*self), fmt))
}
}
impl PartialOrd<InternedString> for InternedString {
fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
read(|interner| PartialOrd::partial_cmp(interner.data(*self), interner.data(*other)))
}
}
impl Ord for InternedString {
fn cmp(&self, other: &InternedString) -> Ordering {
read(|interner| Ord::cmp(interner.data(*self), interner.data(*other)))
}
}

View File

@ -16,5 +16,37 @@ grammar Foo {
}
").unwrap();
let actual = normalize(grammar).unwrap();
expect_debug(actual, "foo");
expect_debug(actual,
r#"Grammar {
action_fn_defns: [
fn _(__0: Vec<Tok>) -> Vec<Tok> { (__0) },
fn _(v: std::vec::Vec<Tok>, e: std::option::Option<Tok>) -> Vec<Tok> { v.into_iter().chain(e.into_iter()).collect() },
fn _(__0: Tok) -> std::option::Option<Tok> { Some(__0) },
fn _() -> std::option::Option<Tok> { None },
fn _() -> std::vec::Vec<Tok> { vec![] },
fn _(v: std::vec::Vec<Tok>, e: Tok) -> std::vec::Vec<Tok> { { let mut v = v; v.push(e); v } },
fn _(__0: Tok, _: Tok) -> Tok { (__0) }
],
productions: [
Ids = Comma<"Id"> => ActionFn(0);,
Comma<"Id"> = (~"Id" ",")*, "Id"? => ActionFn(1);,
"Id"? = "Id" => ActionFn(2);,
"Id"? = => ActionFn(3);,
(~"Id" ",")* = => ActionFn(4);,
(~"Id" ",")* = (~"Id" ",")*, (~"Id" ",") => ActionFn(5);,
(~"Id" ",") = "Id", "," => ActionFn(6);
],
conversions: {},
types: DummyTypes {
terminal_type: Tok,
nonterminal_types: [
("\"Id\"?", std::option::Option<Tok>),
("(~\"Id\" \",\")", Tok),
("(~\"Id\" \",\")*", std::vec::Vec<Tok>),
("Comma<\"Id\">", Vec<Tok>),
("Ids", Vec<Tok>)
]
}
}"#)
}