Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rule: intern fields and store them in a tree matching the rule structure. #11

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::rule::Rule;
use crate::rule::{Fields, Rule};
use elsa::FrozenVec;
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -93,6 +93,7 @@ macro_rules! interners {
interners! {
IStr => str,
IRule => Rule<Pat>,
IFields => Fields,
}

impl<Pat> InternInCx<Pat> for &'_ str {
Expand All @@ -117,3 +118,18 @@ impl<Pat: Eq + Hash> InternInCx<Pat> for Rule<Pat> {
IRule(cx.interners.IRule.intern(self))
}
}

// FIXME(eddyb) automate this away somehow.
impl AsRef<Self> for Fields {
fn as_ref(&self) -> &Self {
self
}
}

impl<Pat> InternInCx<Pat> for Fields {
type Interned = IFields;

fn intern_in_cx(self, cx: &Context<Pat>) -> Self::Interned {
IFields(cx.interners.IFields.intern(self))
}
}
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::collections::HashMap;
use std::hash::Hash;

pub struct Grammar {
pub rules: IndexMap<IStr, rule::RuleWithNamedFields>,
pub rules: IndexMap<IStr, rule::RuleWithFields>,
}

impl Grammar {
Expand All @@ -44,7 +44,7 @@ impl Grammar {
rules: IndexMap::new(),
}
}
pub fn define(&mut self, name: IStr, rule: rule::RuleWithNamedFields) {
pub fn define(&mut self, name: IStr, rule: rule::RuleWithFields) {
self.rules.insert(name, rule);
}
pub fn extend(&mut self, other: Self) {
Expand All @@ -53,13 +53,13 @@ impl Grammar {
pub fn insert_whitespace<Pat: Eq + Hash>(
self,
cx: &Context<Pat>,
whitespace: rule::RuleWithNamedFields,
whitespace: rule::RuleWithFields,
) -> Self {
Grammar {
rules: self
.rules
.into_iter()
.map(|(name, rule)| (name, rule.insert_whitespace(cx, whitespace.clone())))
.map(|(name, rule)| (name, rule.insert_whitespace(cx, whitespace)))
.collect(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn builtin(cx: &Context) -> crate::Grammar {
let mut g = crate::Grammar::new();

let ident = eat(Pat(vec![FlatTokenPat::Ident(None)])).finish(cx);
g.define(cx.intern("IDENT"), ident.clone());
g.define(cx.intern("IDENT"), ident);

g.define(
cx.intern("LIFETIME"),
Expand All @@ -32,10 +32,10 @@ pub fn builtin(cx: &Context) -> crate::Grammar {
joint: None,
}]))
.finish(cx);
g.define(cx.intern("PUNCT"), punct.clone());
g.define(cx.intern("PUNCT"), punct);

let literal = eat(Pat(vec![FlatTokenPat::Literal])).finish(cx);
g.define(cx.intern("LITERAL"), literal.clone());
g.define(cx.intern("LITERAL"), literal);

let delim = |c| eat(FlatTokenPat::Delim(c));
let group = |open, close| delim(open) + call("TOKEN_TREE").repeat_many() + delim(close);
Expand Down
Loading