From 8dd121a8e7b6b3578ba4f78a5480ac5591c62ab9 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 18 Jun 2015 06:13:18 -0400 Subject: [PATCH] lower test passes --- src/grammar/repr.rs | 24 +++++++++++++++++++++--- src/intern/mod.rs | 15 ++++++++++++++- src/normalize/lower/test.rs | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/grammar/repr.rs b/src/grammar/repr.rs index a380c35..ebb550c 100644 --- a/src/grammar/repr.rs +++ b/src/grammar/repr.rs @@ -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 @@ -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), } } } diff --git a/src/intern/mod.rs b/src/intern/mod.rs index ef4069e..945e835 100644 --- a/src/intern/mod.rs +++ b/src/intern/mod.rs @@ -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, } -#[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 for InternedString { + fn partial_cmp(&self, other: &InternedString) -> Option { + 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))) + } +} diff --git a/src/normalize/lower/test.rs b/src/normalize/lower/test.rs index de17f60..43c3bd3 100644 --- a/src/normalize/lower/test.rs +++ b/src/normalize/lower/test.rs @@ -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) -> Vec { (__0) }, + fn _(v: std::vec::Vec, e: std::option::Option) -> Vec { v.into_iter().chain(e.into_iter()).collect() }, + fn _(__0: Tok) -> std::option::Option { Some(__0) }, + fn _() -> std::option::Option { None }, + fn _() -> std::vec::Vec { vec![] }, + fn _(v: std::vec::Vec, e: Tok) -> std::vec::Vec { { 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), + ("(~\"Id\" \",\")", Tok), + ("(~\"Id\" \",\")*", std::vec::Vec), + ("Comma<\"Id\">", Vec), + ("Ids", Vec) + ] + } +}"#) + }