Commit 2da5aa93 authored by Jeff Smits's avatar Jeff Smits

Use the InternedString type alias

parent c152479b
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
name = "strs" name = "strs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"aterm 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)", "aterm 0.19.1 (registry+https://github.com/rust-lang/crates.io-index)",
"fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "linked-hash-map 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)", "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
...@@ -29,7 +29,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -29,7 +29,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "aterm" name = "aterm"
version = "0.18.1" version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
...@@ -307,7 +307,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -307,7 +307,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum aterm 0.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a0f2df2694d64af77e71242cfb2e0d421f4b95c0d9c07d0d0d8a1a4c4adbfd3" "checksum aterm 0.19.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1cafec35b5dd56b9cd172b525d8b9bc7b454f7c24739d18b41e92e79c96a6ec8"
"checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" "checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"
"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" "checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4"
"checksum clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f57e9b63057a545ad2ecd773ea61e49422ed1b1d63d74d5da5ecaee55b3396cd" "checksum clap 2.23.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f57e9b63057a545ad2ecd773ea61e49422ed1b1d63d74d5da5ecaee55b3396cd"
......
...@@ -13,7 +13,7 @@ version = "0.1.0" ...@@ -13,7 +13,7 @@ version = "0.1.0"
phf_codegen = "0.7.21" phf_codegen = "0.7.21"
[dependencies] [dependencies]
aterm = "0.18.1" aterm = "0.19.1"
fnv = "1.0.5" fnv = "1.0.5"
linked-hash-map = "0.4.2" linked-hash-map = "0.4.2"
phf = "0.7.21" phf = "0.7.21"
......
...@@ -24,6 +24,8 @@ use try_from::TryInto; ...@@ -24,6 +24,8 @@ use try_from::TryInto;
use fnv::{FnvHashSet, FnvHashMap}; use fnv::{FnvHashSet, FnvHashMap};
use aterm::string_share::InternedString;
use strs::io; use strs::io;
use strs::factory::ATermFactory; use strs::factory::ATermFactory;
use strs::error::{Error, Result}; use strs::error::{Error, Result};
...@@ -106,7 +108,7 @@ enum Value<'s> { ...@@ -106,7 +108,7 @@ enum Value<'s> {
UnBound, UnBound,
MaybeBound, MaybeBound,
Bound, Bound,
BoundTo(&'s str), BoundTo(InternedString<'s>),
} }
impl<'s> Value<'s> { impl<'s> Value<'s> {
...@@ -135,8 +137,8 @@ impl<'s> PartialOrd<Value<'s>> for Value<'s> { ...@@ -135,8 +137,8 @@ impl<'s> PartialOrd<Value<'s>> for Value<'s> {
} }
} }
impl<'a, 's> From<&'a Option<&'s str>> for Value<'s> { impl<'a, 's> From<&'a Option<InternedString<'s>>> for Value<'s> {
fn from(opt: &'a Option<&'s str>) -> Self { fn from(opt: &'a Option<InternedString<'s>>) -> Self {
match *opt { match *opt {
None => Value::Bound, None => Value::Bound,
Some(name) => Value::BoundTo(name), Some(name) => Value::BoundTo(name),
...@@ -146,8 +148,8 @@ impl<'a, 's> From<&'a Option<&'s str>> for Value<'s> { ...@@ -146,8 +148,8 @@ impl<'a, 's> From<&'a Option<&'s str>> for Value<'s> {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
struct Scope<'s> { struct Scope<'s> {
strategy: FnvHashMap<&'s str, (::std::result::Result<FnvHashSet<&'s str>, DynamicCall>, usize)>, strategy: FnvHashMap<InternedString<'s>, (::std::result::Result<FnvHashSet<InternedString<'s>>, DynamicCall>, usize)>,
term: FnvHashMap<&'s str, Value<'s>>, term: FnvHashMap<InternedString<'s>, Value<'s>>,
is_overlay: bool, is_overlay: bool,
is_unbound_overlay: bool, is_unbound_overlay: bool,
} }
...@@ -155,8 +157,8 @@ struct Scope<'s> { ...@@ -155,8 +157,8 @@ struct Scope<'s> {
impl<'s> Scope<'s> { impl<'s> Scope<'s> {
fn from_args<I1, I2>(no_of_scopes: usize, sargs: I1, targs: I2) -> Self fn from_args<I1, I2>(no_of_scopes: usize, sargs: I1, targs: I2) -> Self
where where
I1: IntoIterator<Item = &'s str>, I1: IntoIterator<Item = InternedString<'s>>,
I2: IntoIterator<Item = &'s str>, I2: IntoIterator<Item = InternedString<'s>>,
{ {
Scope { Scope {
term: targs term: targs
...@@ -192,7 +194,7 @@ impl<'s> Scope<'s> { ...@@ -192,7 +194,7 @@ impl<'s> Scope<'s> {
fn from_fresh_variables<I>(fresh_vars: I) -> Self fn from_fresh_variables<I>(fresh_vars: I) -> Self
where where
I: IntoIterator<Item = &'s str>, I: IntoIterator<Item = InternedString<'s>>,
{ {
Scope { Scope {
term: fresh_vars term: fresh_vars
...@@ -246,11 +248,11 @@ impl<'s> Scopes<'s> { ...@@ -246,11 +248,11 @@ impl<'s> Scopes<'s> {
self.0.push(Scope::unbound_overlay()) self.0.push(Scope::unbound_overlay())
} }
fn get_term(&self, term_name: &str) -> Option<Value<'s>> { fn get_term(&self, term_name: InternedString<'s>) -> Option<Value<'s>> {
let mut found_unbound_overlay = false; let mut found_unbound_overlay = false;
for scope in self.0.iter().rev() { for scope in self.0.iter().rev() {
found_unbound_overlay |= scope.is_unbound_overlay; found_unbound_overlay |= scope.is_unbound_overlay;
if let Some(value) = scope.term.get(term_name) { if let Some(value) = scope.term.get(&term_name) {
if found_unbound_overlay && *value == Value::UnBound { if found_unbound_overlay && *value == Value::UnBound {
return Some(Value::MaybeBound); return Some(Value::MaybeBound);
} else { } else {
...@@ -261,12 +263,12 @@ impl<'s> Scopes<'s> { ...@@ -261,12 +263,12 @@ impl<'s> Scopes<'s> {
None None
} }
fn apply_strategy(&mut self, strat_name: &str) { fn apply_strategy(&mut self, strat_name: InternedString<'s>) {
let (vars, no_of_scopes) = { let (vars, no_of_scopes) = {
let opt = self.0 let opt = self.0
.iter() .iter()
.rev() .rev()
.flat_map(|scope| scope.strategy.get(strat_name)) .flat_map(|scope| scope.strategy.get(&strat_name))
.cloned() .cloned()
.next(); .next();
if opt.is_none() { if opt.is_none() {
...@@ -286,7 +288,7 @@ impl<'s> Scopes<'s> { ...@@ -286,7 +288,7 @@ impl<'s> Scopes<'s> {
if offset == None && scope.is_overlay { if offset == None && scope.is_overlay {
offset = Some(n); offset = Some(n);
} }
if scope.term.get(var) == Some(&Value::UnBound) { if scope.term.get(&var) == Some(&Value::UnBound) {
offset = Some(offset.unwrap_or(n)); offset = Some(offset.unwrap_or(n));
break; break;
} }
...@@ -297,9 +299,9 @@ impl<'s> Scopes<'s> { ...@@ -297,9 +299,9 @@ impl<'s> Scopes<'s> {
} }
} }
fn set_term(&mut self, term_name: &'s str, value: Value<'s>) { fn set_term(&mut self, term_name: InternedString<'s>, value: Value<'s>) {
for scope in self.0.iter_mut().rev() { for scope in self.0.iter_mut().rev() {
if scope.term.contains_key(term_name) || scope.is_overlay { if scope.term.contains_key(&term_name) || scope.is_overlay {
scope.term.insert(term_name, value); scope.term.insert(term_name, value);
return; return;
} }
...@@ -384,7 +386,7 @@ impl<'s> CTreeOptimize for preprocess_ext::Def<'s> { ...@@ -384,7 +386,7 @@ impl<'s> CTreeOptimize for preprocess_ext::Def<'s> {
} }
impl<'s> CTreeOptimize for preprocess::Def<'s> { impl<'s> CTreeOptimize for preprocess::Def<'s> {
type Context = (Option<&'s str>, Scopes<'s>); type Context = (Option<InternedString<'s>>, Scopes<'s>);
fn optimize(mut self, c: &mut Self::Context) -> Self { fn optimize(mut self, c: &mut Self::Context) -> Self {
// eprintln!("CTreeOptimize::optimize(\"LetDef\", _)"); // eprintln!("CTreeOptimize::optimize(\"LetDef\", _)");
...@@ -419,7 +421,7 @@ fn conservative_maybe_bind<'s>(scopes: &mut Scopes<'s>) { ...@@ -419,7 +421,7 @@ fn conservative_maybe_bind<'s>(scopes: &mut Scopes<'s>) {
fn conservative_maybe_bind2<'s, I>(scopes: &mut Scopes<'s>, affected_names: I) fn conservative_maybe_bind2<'s, I>(scopes: &mut Scopes<'s>, affected_names: I)
where where
I: IntoIterator<Item=&'s str>, I: IntoIterator<Item=InternedString<'s>>,
{ {
for name in affected_names { for name in affected_names {
if scopes.get_term(name) == Some(Value::UnBound) { if scopes.get_term(name) == Some(Value::UnBound) {
...@@ -430,7 +432,7 @@ where ...@@ -430,7 +432,7 @@ where
// Pay attention, this is where the optimizing starts // Pay attention, this is where the optimizing starts
impl<'s> CTreeOptimize for preprocess::Strategy<'s> { impl<'s> CTreeOptimize for preprocess::Strategy<'s> {
type Context = (Option<&'s str>, Scopes<'s>); type Context = (Option<InternedString<'s>>, Scopes<'s>);
fn optimize(self, mut c: &mut Self::Context) -> Self { fn optimize(self, mut c: &mut Self::Context) -> Self {
use preprocess::Strategy; use preprocess::Strategy;
...@@ -766,7 +768,7 @@ fn all_vars_bound<'s>(bt: &preprocess::BuildTerm<'s>, m: &Scopes<'s>) -> bool { ...@@ -766,7 +768,7 @@ fn all_vars_bound<'s>(bt: &preprocess::BuildTerm<'s>, m: &Scopes<'s>) -> bool {
} }
impl<'s> CTreeOptimize for preprocess::MatchTerm<'s> { impl<'s> CTreeOptimize for preprocess::MatchTerm<'s> {
type Context = (Option<&'s str>, Scopes<'s>); type Context = (Option<InternedString<'s>>, Scopes<'s>);
fn optimize(self, mut c: &mut Self::Context) -> Self { fn optimize(self, mut c: &mut Self::Context) -> Self {
use preprocess::MatchTerm::*; use preprocess::MatchTerm::*;
...@@ -804,7 +806,7 @@ impl<'s> CTreeOptimize for preprocess::MatchTerm<'s> { ...@@ -804,7 +806,7 @@ impl<'s> CTreeOptimize for preprocess::MatchTerm<'s> {
} }
impl<'s> CTreeOptimize for preprocess::BuildTerm<'s> { impl<'s> CTreeOptimize for preprocess::BuildTerm<'s> {
type Context = (Option<&'s str>, Scopes<'s>); type Context = (Option<InternedString<'s>>, Scopes<'s>);
fn optimize(self, mut c: &mut Self::Context) -> Self { fn optimize(self, mut c: &mut Self::Context) -> Self {
use preprocess::BuildTerm::*; use preprocess::BuildTerm::*;
...@@ -838,14 +840,14 @@ struct DynamicCall; ...@@ -838,14 +840,14 @@ struct DynamicCall;
fn match_vars_in_strategy<'s>( fn match_vars_in_strategy<'s>(
strat: &preprocess::Strategy<'s>, strat: &preprocess::Strategy<'s>,
) -> ::std::result::Result<FnvHashSet<&'s str>, DynamicCall> { ) -> ::std::result::Result<FnvHashSet<InternedString<'s>>, DynamicCall> {
use preprocess::Strategy; use preprocess::Strategy;
match *strat { match *strat {
Strategy::Let(_, ref body) => match_vars_in_strategy(body), Strategy::Let(_, ref body) => match_vars_in_strategy(body),
// TODO: add vars from Call // TODO: add vars from Call
Strategy::CallT(_, ref sargs, _) => { Strategy::CallT(_, ref sargs, _) => {
let result = sargs.iter().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<&'s str>>, DynamicCall>>()?; let result = sargs.iter().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<InternedString<'s>>>, DynamicCall>>()?;
Ok(result.into_iter().flat_map(|s| s).collect()) Ok(result.into_iter().flat_map(|s| s).collect())
}, },
Strategy::CallDynamic(_, _, _) => Err(DynamicCall), Strategy::CallDynamic(_, _, _) => Err(DynamicCall),
...@@ -854,7 +856,7 @@ fn match_vars_in_strategy<'s>( ...@@ -854,7 +856,7 @@ fn match_vars_in_strategy<'s>(
Strategy::Match(ref term) => Ok(match_vars(term)), Strategy::Match(ref term) => Ok(match_vars(term)),
Strategy::Build(_) => Ok(FnvHashSet::default()), Strategy::Build(_) => Ok(FnvHashSet::default()),
Strategy::Scope(ref names, ref body) => { Strategy::Scope(ref names, ref body) => {
let name_set: FnvHashSet<&'s str> = names.iter().map(|&s| s).collect(); let name_set: FnvHashSet<InternedString<'s>> = names.iter().map(|&s| s).collect();
match match_vars_in_strategy(body) { match match_vars_in_strategy(body) {
Ok(mut v) => { Ok(mut v) => {
v.retain(|n| !name_set.contains(n)); v.retain(|n| !name_set.contains(n));
...@@ -864,7 +866,7 @@ fn match_vars_in_strategy<'s>( ...@@ -864,7 +866,7 @@ fn match_vars_in_strategy<'s>(
} }
} }
Strategy::Seq(ref strats) => { Strategy::Seq(ref strats) => {
let result = strats.iter().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<&'s str>>, DynamicCall>>()?; let result = strats.iter().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<InternedString<'s>>>, DynamicCall>>()?;
Ok(result.into_iter().flat_map(|s| s).collect()) Ok(result.into_iter().flat_map(|s| s).collect())
}, },
Strategy::GuardedLChoice(ref pairs, ref s_else) => { Strategy::GuardedLChoice(ref pairs, ref s_else) => {
...@@ -873,18 +875,18 @@ fn match_vars_in_strategy<'s>( ...@@ -873,18 +875,18 @@ fn match_vars_in_strategy<'s>(
.map(|&(ref s_if, ref s_then)| { .map(|&(ref s_if, ref s_then)| {
let one = match_vars_in_strategy(s_if)?; let one = match_vars_in_strategy(s_if)?;
let two = match_vars_in_strategy(s_then)?; let two = match_vars_in_strategy(s_then)?;
Ok(one.union(&two).map(|&s| s).collect::<FnvHashSet<&'s str>>()) Ok(one.union(&two).map(|&s| s).collect::<FnvHashSet<InternedString<'s>>>())
}) })
.collect::<::std::result::Result<Vec<FnvHashSet<&'s str>>, DynamicCall>>()?; .collect::<::std::result::Result<Vec<FnvHashSet<InternedString<'s>>>, DynamicCall>>()?;
let pairs_names: FnvHashSet<&'s str> = pairs_names.into_iter().flat_map(|s| s).collect(); let pairs_names: FnvHashSet<InternedString<'s>> = pairs_names.into_iter().flat_map(|s| s).collect();
Ok(pairs_names.union(&match_vars_in_strategy(s_else)?).map(|&s| s).collect()) Ok(pairs_names.union(&match_vars_in_strategy(s_else)?).map(|&s| s).collect())
} }
Strategy::ConsMatch(ref map, _) => { Strategy::ConsMatch(ref map, _) => {
let result = map.values().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<&'s str>>, DynamicCall>>()?; let result = map.values().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<InternedString<'s>>>, DynamicCall>>()?;
Ok(result.into_iter().flat_map(|s| s).collect()) Ok(result.into_iter().flat_map(|s| s).collect())
}, },
Strategy::PrimT(_, ref sargs, _) => { Strategy::PrimT(_, ref sargs, _) => {
let result = sargs.iter().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<&'s str>>, DynamicCall>>()?; let result = sargs.iter().map(match_vars_in_strategy).collect::<::std::result::Result<Vec<FnvHashSet<InternedString<'s>>>, DynamicCall>>()?;
Ok(result.into_iter().flat_map(|s| s).collect()) Ok(result.into_iter().flat_map(|s| s).collect())
}, },
Strategy::Some(ref s) => match_vars_in_strategy(s), Strategy::Some(ref s) => match_vars_in_strategy(s),
...@@ -894,7 +896,7 @@ fn match_vars_in_strategy<'s>( ...@@ -894,7 +896,7 @@ fn match_vars_in_strategy<'s>(
} }
} }
fn match_vars<'s>(term: &preprocess::MatchTerm<'s>) -> FnvHashSet<&'s str> { fn match_vars<'s>(term: &preprocess::MatchTerm<'s>) -> FnvHashSet<InternedString<'s>> {
use preprocess::MatchTerm; use preprocess::MatchTerm;
let mut result = FnvHashSet::default(); let mut result = FnvHashSet::default();
......
...@@ -6,6 +6,8 @@ use primitives::ssl::State; ...@@ -6,6 +6,8 @@ use primitives::ssl::State;
use fnv::FnvHashMap; use fnv::FnvHashMap;
use aterm::string_share::InternedString;
use std::fmt; use std::fmt;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cell::RefCell; use std::cell::RefCell;
...@@ -38,14 +40,14 @@ impl<'d, 'f: 'd> MutContext<'d, 'f> { ...@@ -38,14 +40,14 @@ impl<'d, 'f: 'd> MutContext<'d, 'f> {
pub fn call_primitive( pub fn call_primitive(
&self, &self,
prim_name: &'f str, prim_name: InternedString<'f>,
sargs: &[StrategyDef<'d, 'f>], sargs: &[StrategyDef<'d, 'f>],
targs: &[ATermRef<'f>], targs: &[ATermRef<'f>],
current: ATermRef<'f>, current: ATermRef<'f>,
) -> Result<ATermRef<'f>> { ) -> Result<ATermRef<'f>> {
let prim_ref = self.primitives let prim_ref = self.primitives
.iter() .iter()
.flat_map(|prims| prims.get(prim_name)) .flat_map(|prims| prims.get(&*prim_name))
.cloned() // ATermRef should be very cheap to clone .cloned() // ATermRef should be very cheap to clone
.next() .next()
.ok_or_else(|| Error::UndefinedPrimitive(String::from(prim_name)))?; .ok_or_else(|| Error::UndefinedPrimitive(String::from(prim_name)))?;
...@@ -59,23 +61,23 @@ impl<'d, 'f: 'd> MutContext<'d, 'f> { ...@@ -59,23 +61,23 @@ impl<'d, 'f: 'd> MutContext<'d, 'f> {
result result
} }
pub fn get_strategy(&self, strat_name: &str) -> Result<StrategyDef<'d, 'f>> { pub fn get_strategy(&self, strat_name: InternedString<'f>) -> Result<StrategyDef<'d, 'f>> {
self.scopes self.scopes
.borrow() .borrow()
.iter() .iter()
.rev() .rev()
.flat_map(|scope| scope.strategy.get(strat_name)) .flat_map(|scope| scope.strategy.get(&strat_name))
.cloned() // StrategyDef is fairly cheap to clone .cloned() // StrategyDef is fairly cheap to clone
.next() .next()
.ok_or_else(|| Error::UndefinedStrategy(String::from(strat_name))) .ok_or_else(|| Error::UndefinedStrategy(String::from(strat_name)))
} }
pub fn get_term(&self, term_name: &str) -> Result<ATermRef<'f>> { pub fn get_term(&self, term_name: InternedString<'f>) -> Result<ATermRef<'f>> {
Scopes::get_term_option(&self.scopes.borrow(), term_name) Scopes::get_term_option(&self.scopes.borrow(), term_name)
.and_then(|(_, o)| o.ok_or(Error::StrategyFailed)) .and_then(|(_, o)| o.ok_or(Error::StrategyFailed))
} }
pub fn match_term(&self, term_name: &'f str, current: &ATermRef<'f>) -> Result<()> { pub fn match_term(&self, term_name: InternedString<'f>, current: &ATermRef<'f>) -> Result<()> {
Scopes::match_term(&mut self.scopes.borrow_mut(), term_name, current) Scopes::match_term(&mut self.scopes.borrow_mut(), term_name, current)
} }
...@@ -161,12 +163,12 @@ impl<'d, 'f: 'd> MutContext<'d, 'f> { ...@@ -161,12 +163,12 @@ impl<'d, 'f: 'd> MutContext<'d, 'f> {
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct StackTracer<'a> { pub struct StackTracer<'a> {
stack: Vec<&'a str>, stack: Vec<InternedString<'a>>,
current_depth: usize, current_depth: usize,
} }
impl<'a> StackTracer<'a> { impl<'a> StackTracer<'a> {
pub fn push(&mut self, str: &'a str) { pub fn push(&mut self, str: InternedString<'a>) {
self.stack.truncate(self.current_depth); self.stack.truncate(self.current_depth);
self.stack.push(str); self.stack.push(str);
self.current_depth += 1; self.current_depth += 1;
...@@ -181,7 +183,7 @@ impl<'a> StackTracer<'a> { ...@@ -181,7 +183,7 @@ impl<'a> StackTracer<'a> {
self.current_depth = self.stack.len(); self.current_depth = self.stack.len();
} }
pub fn stack(&self) -> ::std::slice::Iter<&'a str> { pub fn stack(&self) -> ::std::slice::Iter<InternedString<'a>> {
self.stack.iter() self.stack.iter()
} }
} }
...@@ -201,16 +203,16 @@ impl<'a> fmt::Display for StackTracer<'a> { ...@@ -201,16 +203,16 @@ impl<'a> fmt::Display for StackTracer<'a> {
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct Scope<'d, 'f: 'd, ATerm> { pub struct Scope<'d, 'f: 'd, ATerm> {
pub term: FnvHashMap<&'f str, Option<ATerm>>, pub term: FnvHashMap<InternedString<'f>, Option<ATerm>>,
pub strategy: FnvHashMap<&'f str, StrategyDef<'d, 'f>>, pub strategy: FnvHashMap<InternedString<'f>, StrategyDef<'d, 'f>>,
is_overlay: bool, is_overlay: bool,
} }
impl<'d, 'f: 'd, ATerm> Scope<'d, 'f, ATerm> { impl<'d, 'f: 'd, ATerm> Scope<'d, 'f, ATerm> {
pub fn new<IA, IS>(terms: IA, defs: IS) -> Self pub fn new<IA, IS>(terms: IA, defs: IS) -> Self
where where
IA: IntoIterator<Item = (&'f str, ATerm)>, IA: IntoIterator<Item = (InternedString<'f>, ATerm)>,
IS: IntoIterator<Item = (&'f str, StrategyDef<'d, 'f>)>, IS: IntoIterator<Item = (InternedString<'f>, StrategyDef<'d, 'f>)>,
{ {
Scope { Scope {
term: terms.into_iter().map(|(n, a)| (n, Some(a))).collect(), term: terms.into_iter().map(|(n, a)| (n, Some(a))).collect(),
...@@ -249,7 +251,7 @@ impl<'d, 'f: 'd, ATerm> Scope<'d, 'f, ATerm> { ...@@ -249,7 +251,7 @@ impl<'d, 'f: 'd, ATerm> Scope<'d, 'f, ATerm> {
pub fn from_fresh_variables<I>(fresh_vars: I) -> Self pub fn from_fresh_variables<I>(fresh_vars: I) -> Self
where where
I: IntoIterator<Item = &'f str>, I: IntoIterator<Item = InternedString<'f>>,
{ {
Scope { Scope {
term: fresh_vars term: fresh_vars
...@@ -268,14 +270,14 @@ pub mod Scopes { ...@@ -268,14 +270,14 @@ pub mod Scopes {
pub fn get_term_option<'f>( pub fn get_term_option<'f>(
scopes: &[Scope<ATermRef<'f>>], scopes: &[Scope<ATermRef<'f>>],
term_name: &str, term_name: InternedString<'f>,
) -> Result<(usize, Option<ATermRef<'f>>)> { ) -> Result<(usize, Option<ATermRef<'f>>)> {
let mut offset = None; let mut offset = None;
for (n, scope) in scopes.iter().enumerate().rev() { for (n, scope) in scopes.iter().enumerate().rev() {
if offset == None && scope.is_overlay { if offset == None && scope.is_overlay {
offset = Some(n); offset = Some(n);
} }
if let Some(binding) = scope.term.get(term_name) { if let Some(binding) = scope.term.get(&term_name) {
let n = offset.unwrap_or(n); let n = offset.unwrap_or(n);
return Ok((n, binding.clone())); return Ok((n, binding.clone()));
} }
...@@ -285,7 +287,7 @@ pub mod Scopes { ...@@ -285,7 +287,7 @@ pub mod Scopes {
pub fn match_term<'d, 'f>( pub fn match_term<'d, 'f>(
scopes: &mut Vec<Scope<'d, 'f, ATermRef<'f>>>, scopes: &mut Vec<Scope<'d, 'f, ATermRef<'f>>>,
term_name: &'f str, term_name: InternedString<'f>,
current: &ATermRef<'f>, current: &ATermRef<'f>,
) -> Result<()> { ) -> Result<()> {
let term = get_term_option(scopes, term_name)?; let term = get_term_option(scopes, term_name)?;
......
use aterm::BrokenF32; use aterm::BrokenF32;
use aterm::print::ATermWrite; use aterm::print::ATermWrite;
use aterm::string_share::InternedString;
use try_from::{TryFrom, TryInto}; use try_from::{TryFrom, TryInto};
...@@ -14,17 +15,17 @@ use factory::Term as FTerm; ...@@ -14,17 +15,17 @@ use factory::Term as FTerm;
/// ModName = {ModNamePart "/"}+ /// ModName = {ModNamePart "/"}+
/// ModNamePart = [a-zA-Z\.\_] [a-zA-Z0-9\'\.\-\_]* /// ModNamePart = [a-zA-Z\.\_] [a-zA-Z0-9\'\.\-\_]*
/// ``` /// ```
pub type ModName<'s> = &'s str; pub type ModName<'s> = InternedString<'s>;
/// `[a-zA-Z\_] [a-zA-Z0-9\'\-\_]*` /// `[a-zA-Z\_] [a-zA-Z0-9\'\-\_]*`
pub type Id<'s> = &'s str; pub type Id<'s> = InternedString<'s>;
/// `[a-z] [a-zA-Z0-9\'\-\_]*` /// `[a-z] [a-zA-Z0-9\'\-\_]*`
pub type LCID<'s> = &'s str; pub type LCID<'s> = InternedString<'s>;
/// `[A-Z] [a-zA-Z0-9\'\-\_]*` /// `[A-Z] [a-zA-Z0-9\'\-\_]*`
pub type UCID<'s> = &'s str; pub type UCID<'s> = InternedString<'s>;
#[derive(Debug, PartialEq, Eq, Hash, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Module<'s> { pub enum Module<'s> {
Module(&'s str, Vec<Decl<'s>>), Module(InternedString<'s>, Vec<Decl<'s>>),
Specification(Vec<Decl<'s>>), Specification(Vec<Decl<'s>>),
} }
...@@ -55,7 +56,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Module<'s> { ...@@ -55,7 +56,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Module<'s> {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> { fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let FTerm::Application(c, ref r) = value.term { if let FTerm::Application(c, ref r) = value.term {
match c { match c.as_ref() {
"Module" => { "Module" => {
if r.len() == 2 { if r.len() == 2 {
let str = match_string(&r[0])?; let str = match_string(&r[0])?;
...@@ -117,7 +118,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Decl<'s> { ...@@ -117,7 +118,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Decl<'s> {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> { fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let FTerm::Application(c, ref r) = value.term { if let FTerm::Application(c, ref r) = value.term {
match c { match c.as_ref() {
"Imports" => { "Imports" => {
if r.len() == 1 { if r.len() == 1 {
return match_list(&r[0]).map(Decl::Imports); return match_list(&r[0]).map(Decl::Imports);
...@@ -161,7 +162,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for ImportModName<'s> { ...@@ -161,7 +162,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for ImportModName<'s> {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> { fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let FTerm::Application(c, ref r) = value.term { if let FTerm::Application(c, ref r) = value.term {
match c { match c.as_ref() {
"Import" => { "Import" => {
if r.len() == 1 { if r.len() == 1 {
return match_string(&r[0]).map(ImportModName::Import); return match_string(&r[0]).map(ImportModName::Import);
...@@ -230,7 +231,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for SDecl<'s> { ...@@ -230,7 +231,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for SDecl<'s> {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> { fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let FTerm::Application(c, ref r) = value.term { if let FTerm::Application(c, ref r) = value.term {
match c { match c.as_ref() {
"Sorts" => { "Sorts" => {
if r.len() == 1 { if r.len() == 1 {
return match_list(&r[0]).map(SDecl::Sorts); return match_list(&r[0]).map(SDecl::Sorts);
...@@ -294,7 +295,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Sort<'s> { ...@@ -294,7 +295,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for Sort<'s> {
fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> { fn try_from(value: &'a ATermRef<'s>) -> Result<Self, Self::Err> {
if let FTerm::Application(c, ref r) = value.term { if let FTerm::Application(c, ref r) = value.term {
match c { match c.as_ref() {
"SortVar" => { "SortVar" => {
if r.len() == 1 { if r.len() == 1 {
return match_string(&r[0]).map(Sort::SortVar); return match_string(&r[0]).map(Sort::SortVar);
...@@ -345,10 +346,10 @@ impl<'s> ATermWrite for Sort<'s> { ...@@ -345,10 +346,10 @@ impl<'s> ATermWrite for Sort<'s> {
#[derive(Debug, PartialEq, Eq, Hash, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum OpDecl<'s> { pub enum OpDecl<'s> {
OpDecl(Id<'s>, Type<'s>), OpDecl(Id<'s>, Type<'s>),
OpDeclQ(&'s str, Type<'s>), OpDeclQ(InternedString<'s>, Type<'s>),
OpDeclInj(Type<'s>), OpDeclInj(Type<'s>),
ExtOpDecl(Id<'s>, Type<'s>), ExtOpDecl(Id<'s>, Type<'s>),
ExtOpDeclQ(&'s str, Type<'s>), ExtOpDeclQ(InternedString<'s>, Type<'s>),
ExtOpDeclInj(Type<'s>), ExtOpDeclInj(Type<'s>),
} }
...@@ -371,7 +372,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for OpDecl<'s> { ...@@ -371,7 +372,7 @@ impl<'s, 'a> TryFrom<&'a ATermRef<'s>> for OpDecl<'s> {