Add support for <mut x:X> allowing for mutable x in action code.

Fixes #136.
This commit is contained in:
Ahmed Charles 2018-02-07 15:15:24 +00:00 committed by CreepySkeleton
parent abbc3aff54
commit 2bd51e0888
12 changed files with 64 additions and 34 deletions

View File

@ -89,6 +89,8 @@ fn emit_user_action_code<W: Write>(
// a (L, T, L) triple where the Ls are locations and // a (L, T, L) triple where the Ls are locations and
// the T is the data. Ignore the locations and bind // the T is the data. Ignore the locations and bind
// the data to the name the user gave. // the data to the name the user gave.
// NEXTCOMMITFIXME
let mut arguments: Vec<String> = data let mut arguments: Vec<String> = data
.arg_patterns .arg_patterns
.iter() .iter()
@ -98,7 +100,11 @@ fn emit_user_action_code<W: Write>(
.cloned() .cloned()
.map(|t| grammar.types.spanned_type(t)), .map(|t| grammar.types.spanned_type(t)),
) )
.map(|(p, t)| format!("(_, {}, _): {}", p, t)) .map(|(p, t)| format!("(_, {} {}, _): {}",
if p.0 { "mut" } else { "" },
p.1,
t)
)
.collect(); .collect();
// If this is a reduce of an empty production, we will // If this is a reduce of an empty production, we will

View File

@ -446,8 +446,8 @@ pub enum SymbolKind {
// <X> // <X>
Choose(Box<Symbol>), Choose(Box<Symbol>),
// x:X // <x:X> or <mut x:X>
Name(Atom, Box<Symbol>), Name(bool, Atom, Box<Symbol>),
// @L // @L
Lookahead, Lookahead,
@ -902,6 +902,7 @@ impl Display for Symbol {
impl Display for SymbolKind { impl Display for SymbolKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
match *self { match *self {
// NEXTCOMMITFIXME
SymbolKind::Expr(ref expr) => write!(fmt, "{}", expr), SymbolKind::Expr(ref expr) => write!(fmt, "{}", expr),
SymbolKind::Terminal(ref s) => write!(fmt, "{}", s), SymbolKind::Terminal(ref s) => write!(fmt, "{}", s),
SymbolKind::Nonterminal(ref s) => write!(fmt, "{}", s), SymbolKind::Nonterminal(ref s) => write!(fmt, "{}", s),
@ -909,7 +910,8 @@ impl Display for SymbolKind {
SymbolKind::Macro(ref m) => write!(fmt, "{}", m), SymbolKind::Macro(ref m) => write!(fmt, "{}", m),
SymbolKind::Repeat(ref r) => write!(fmt, "{}", r), SymbolKind::Repeat(ref r) => write!(fmt, "{}", r),
SymbolKind::Choose(ref s) => write!(fmt, "<{}>", s), SymbolKind::Choose(ref s) => write!(fmt, "<{}>", s),
SymbolKind::Name(ref n, ref s) => write!(fmt, "{}:{}", n, s), SymbolKind::Name(m, ref n, ref s) =>
write!(fmt, "{} {}:{}", if m { "mut" } else { "" }, n, s),
SymbolKind::Lookahead => write!(fmt, "@L"), SymbolKind::Lookahead => write!(fmt, "@L"),
SymbolKind::Lookbehind => write!(fmt, "@R"), SymbolKind::Lookbehind => write!(fmt, "@R"),
SymbolKind::Error => write!(fmt, "error"), SymbolKind::Error => write!(fmt, "error"),

View File

@ -144,7 +144,7 @@ pub enum ActionFnDefnKind {
/// An action fn written by a user. /// An action fn written by a user.
#[derive(Clone, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
pub struct UserActionFnDefn { pub struct UserActionFnDefn {
pub arg_patterns: Vec<Atom>, pub arg_patterns: Vec<(bool, Atom)>,
pub arg_types: Vec<TypeRepr>, pub arg_types: Vec<TypeRepr>,
pub code: String, pub code: String,
} }
@ -663,11 +663,12 @@ impl ActionFnDefn {
impl UserActionFnDefn { impl UserActionFnDefn {
fn to_fn_string(&self, defn: &ActionFnDefn, name: &str) -> String { fn to_fn_string(&self, defn: &ActionFnDefn, name: &str) -> String {
//NEXTCOMMITFIXME
let arg_strings: Vec<String> = self let arg_strings: Vec<String> = self
.arg_patterns .arg_patterns
.iter() .iter()
.zip(self.arg_types.iter()) .zip(self.arg_types.iter())
.map(|(p, t)| format!("{}: {}", p, t)) .map(|(p, t)| format!("{}{}: {}", if p.0 { "mut " } else { "" }, p.1, t))
.collect(); .collect();
format!( format!(

View File

@ -366,10 +366,11 @@ impl<'s> LowerState<'s> {
Symbols::Named(names) => { Symbols::Named(names) => {
// if there are named symbols, we want to give the // if there are named symbols, we want to give the
// arguments the names that the user gave them: // arguments the names that the user gave them:
//NEXTCOMMITFIXME
let arg_patterns = patterns( let arg_patterns = patterns(
names names
.iter() .iter()
.map(|&(index, ref name, _)| (index, name.clone())), .map(|&(index, mutable, ref name, _)| (index, (mutable, name.clone()))),
symbols.len(), symbols.len(),
); );
@ -377,10 +378,11 @@ impl<'s> LowerState<'s> {
match norm_util::check_between_braces(&action) { match norm_util::check_between_braces(&action) {
norm_util::Presence::None => action, norm_util::Presence::None => action,
norm_util::Presence::Normal => { norm_util::Presence::Normal => {
//NEXTCOMMITFIXME
let name_str: String = { let name_str: String = {
let name_strs: Vec<_> = names let name_strs: Vec<_> = names
.iter() .iter()
.map(|&(_, ref name, _)| name.as_ref()) .map(|&(_, _, ref name, _)| name.as_ref())
.collect(); .collect();
name_strs.join(", ") name_strs.join(", ")
}; };
@ -388,9 +390,10 @@ impl<'s> LowerState<'s> {
} }
norm_util::Presence::InCurlyBrackets => { norm_util::Presence::InCurlyBrackets => {
let name_str = { let name_str = {
//NEXTCOMMITFIXME
let name_strs: Vec<_> = names let name_strs: Vec<_> = names
.iter() .iter()
.map(|&(_, ref name, _)| format!("{0}:{0}", &*name)) .map(|&(_, _, ref name, _)| format!("{0}:{0}", &*name))
.collect(); .collect();
name_strs.join(", ") name_strs.join(", ")
}; };
@ -411,11 +414,12 @@ impl<'s> LowerState<'s> {
} }
Symbols::Anon(indices) => { Symbols::Anon(indices) => {
let names: Vec<_> = (0..indices.len()).map(|i| self.fresh_name(i)).collect(); let names: Vec<_> = (0..indices.len()).map(|i| self.fresh_name(i)).collect();
//NEXTCOMMITFIXME
let arg_patterns = patterns( let arg_patterns = patterns(
indices indices
.iter() .iter()
.map(|&(index, _)| index) .map(|&(index, _)| index)
.zip(names.iter().cloned()), .zip(names.iter().cloned().map(|n| (false, n))),
symbols.len(), symbols.len(),
); );
let name_str = { let name_str = {
@ -452,7 +456,7 @@ impl<'s> LowerState<'s> {
match symbol.kind { match symbol.kind {
pt::SymbolKind::Terminal(ref id) => r::Symbol::Terminal(id.clone()), pt::SymbolKind::Terminal(ref id) => r::Symbol::Terminal(id.clone()),
pt::SymbolKind::Nonterminal(ref id) => r::Symbol::Nonterminal(id.clone()), pt::SymbolKind::Nonterminal(ref id) => r::Symbol::Nonterminal(id.clone()),
pt::SymbolKind::Choose(ref s) | pt::SymbolKind::Name(_, ref s) => self.symbol(s), pt::SymbolKind::Choose(ref s) | pt::SymbolKind::Name(_, _, ref s) => self.symbol(s),
pt::SymbolKind::Error => { pt::SymbolKind::Error => {
self.uses_error_recovery = true; self.uses_error_recovery = true;
r::Symbol::Terminal(TerminalString::Error) r::Symbol::Terminal(TerminalString::Error)
@ -475,21 +479,22 @@ impl<'s> LowerState<'s> {
} }
} }
fn patterns<I>(mut chosen: I, num_args: usize) -> Vec<Atom> //NEXTCOMMITFIXME
where fn patterns<I>(mut chosen: I, num_args: usize) -> Vec<(bool, Atom)>
I: Iterator<Item = (usize, Atom)>, where I: Iterator<Item = (usize, (bool, Atom))>
{ {
let blank = Atom::from("_"); let blank = Atom::from("_");
let mut next_chosen = chosen.next(); let mut next_chosen = chosen.next();
let result = (0..num_args) let result = (0..num_args)
//NEXTCOMMITFIXME
.map(|index| match next_chosen.clone() { .map(|index| match next_chosen.clone() {
Some((chosen_index, ref chosen_name)) if chosen_index == index => { Some((chosen_index, (mutable, ref chosen_name))) if chosen_index == index => {
next_chosen = chosen.next(); next_chosen = chosen.next();
chosen_name.clone() (mutable, chosen_name.clone())
} }
_ => blank.clone(), _ => (false, blank.clone()),
}) })
.collect(); .collect();

View File

@ -136,7 +136,8 @@ impl MacroExpander {
SymbolKind::Terminal(_) | SymbolKind::Nonterminal(_) | SymbolKind::Error => { SymbolKind::Terminal(_) | SymbolKind::Nonterminal(_) | SymbolKind::Error => {
return; return;
} }
SymbolKind::Choose(ref mut sym) | SymbolKind::Name(_, ref mut sym) => { //FIXNEXTCOMMIT
SymbolKind::Choose(ref mut sym) | SymbolKind::Name(_, _, ref mut sym) => {
self.replace_symbol(sym); self.replace_symbol(sym);
return; return;
} }
@ -344,6 +345,7 @@ impl MacroExpander {
symbol: &Symbol, symbol: &Symbol,
) -> Symbol { ) -> Symbol {
let kind = match symbol.kind { let kind = match symbol.kind {
//NEXTCOMMITFIXME
SymbolKind::Expr(ref expr) => { SymbolKind::Expr(ref expr) => {
SymbolKind::Expr(self.macro_expand_expr_symbol(args, expr)) SymbolKind::Expr(self.macro_expand_expr_symbol(args, expr))
} }
@ -363,8 +365,8 @@ impl MacroExpander {
SymbolKind::Choose(ref sym) => { SymbolKind::Choose(ref sym) => {
SymbolKind::Choose(Box::new(self.macro_expand_symbol(args, sym))) SymbolKind::Choose(Box::new(self.macro_expand_symbol(args, sym)))
} }
SymbolKind::Name(ref id, ref sym) => { SymbolKind::Name(mutable, ref id, ref sym) => {
SymbolKind::Name(id.clone(), Box::new(self.macro_expand_symbol(args, sym))) SymbolKind::Name(mutable, id.clone(), Box::new(self.macro_expand_symbol(args, sym)))
} }
SymbolKind::Lookahead => SymbolKind::Lookahead, SymbolKind::Lookahead => SymbolKind::Lookahead,
SymbolKind::Lookbehind => SymbolKind::Lookbehind, SymbolKind::Lookbehind => SymbolKind::Lookbehind,
@ -386,10 +388,11 @@ impl MacroExpander {
fn expand_expr_symbol(&mut self, span: Span, expr: ExprSymbol) -> NormResult<GrammarItem> { fn expand_expr_symbol(&mut self, span: Span, expr: ExprSymbol) -> NormResult<GrammarItem> {
let name = NonterminalString(Atom::from(expr.canonical_form())); let name = NonterminalString(Atom::from(expr.canonical_form()));
//NEXTCOMMITFIXME
let ty_ref = let ty_ref =
match norm_util::analyze_expr(&expr) { match norm_util::analyze_expr(&expr) {
Symbols::Named(names) => { Symbols::Named(names) => {
let (_, ref ex_id, ex_sym) = names[0]; let (_, _, ref ex_id, ex_sym) = names[0];
return_err!( return_err!(
span, span,
"named symbols like `{}:{}` are only allowed at the top-level of a nonterminal", "named symbols like `{}:{}` are only allowed at the top-level of a nonterminal",
@ -464,9 +467,11 @@ impl MacroExpander {
Alternative { Alternative {
span, span,
expr: ExprSymbol { expr: ExprSymbol {
//NEXTCOMMITFIXME
symbols: vec![Symbol::new( symbols: vec![Symbol::new(
span, span,
SymbolKind::Name( SymbolKind::Name(
false,
v, v,
Box::new(Symbol::new( Box::new(Symbol::new(
span, span,
@ -511,9 +516,11 @@ impl MacroExpander {
span, span,
expr: ExprSymbol { expr: ExprSymbol {
symbols: vec![ symbols: vec![
//NEXTCOMMITFIXME
Symbol::new( Symbol::new(
span, span,
SymbolKind::Name( SymbolKind::Name(
true,
v, v,
Box::new(Symbol::new( Box::new(Symbol::new(
span, span,
@ -523,7 +530,7 @@ impl MacroExpander {
), ),
Symbol::new( Symbol::new(
span, span,
SymbolKind::Name(e, Box::new(repeat.symbol.clone())), SymbolKind::Name(false, e, Box::new(repeat.symbol.clone())),
), ),
], ],
}, },

View File

@ -46,7 +46,7 @@ grammar;
`(<"Id"> ",")+`: ::std::vec::Vec<#`(<"Id"> ",")`#> = { `(<"Id"> ",")+`: ::std::vec::Vec<#`(<"Id"> ",")`#> = {
`(<"Id"> ",")` => vec![<>], `(<"Id"> ",")` => vec![<>],
<v:`(<"Id"> ",")+`> <e:`(<"Id"> ",")`> => { let mut v = v; v.push(e); v }, <v:`(<"Id"> ",")+`> <e:`(<"Id"> ",")`> => { v.push(e); v },
}; };
"##, "##,
) )

View File

@ -9,7 +9,7 @@ pub enum AlternativeAction<'a> {
#[derive(Debug)] #[derive(Debug)]
pub enum Symbols<'a> { pub enum Symbols<'a> {
Named(Vec<(usize, Atom, &'a Symbol)>), Named(Vec<(usize, bool, Atom, &'a Symbol)>),
Anon(Vec<(usize, &'a Symbol)>), Anon(Vec<(usize, &'a Symbol)>),
} }
@ -24,12 +24,13 @@ pub fn analyze_action(alt: &Alternative) -> AlternativeAction<'_> {
pub fn analyze_expr(expr: &ExprSymbol) -> Symbols<'_> { pub fn analyze_expr(expr: &ExprSymbol) -> Symbols<'_> {
// First look for named symbols. // First look for named symbols.
//NEXTCOMMITFIXME
let named_symbols: Vec<_> = expr let named_symbols: Vec<_> = expr
.symbols .symbols
.iter() .iter()
.enumerate() .enumerate()
.filter_map(|(idx, sym)| match sym.kind { .filter_map(|(idx, sym)| match sym.kind {
SymbolKind::Name(ref id, ref sub) => Some((idx, id.clone(), &**sub)), SymbolKind::Name(mutable, ref id, ref sub) => Some((idx, mutable, id.clone(), &**sub)),
_ => None, _ => None,
}) })
.collect(); .collect();

View File

@ -184,7 +184,8 @@ impl<'grammar> Validator<'grammar> {
match norm_util::analyze_expr(&alternative.expr) { match norm_util::analyze_expr(&alternative.expr) {
Symbols::Named(syms) => { Symbols::Named(syms) => {
if alternative.action.is_none() { if alternative.action.is_none() {
let sym = syms.iter().map(|&(_, _, sym)| sym).next().unwrap(); //NEXTCOMMITFIXME
let sym = syms.iter().map(|&(_, _, _, sym)| sym).next().unwrap();
return_err!( return_err!(
sym.span, sym.span,
"named symbols (like `{}`) require a custom action", "named symbols (like `{}`) require a custom action",
@ -226,11 +227,12 @@ impl<'grammar> Validator<'grammar> {
}) })
.collect(); .collect();
//NEXTCOMMITFIXME
let named: Multimap<Atom, Vec<&Symbol>> = expr let named: Multimap<Atom, Vec<&Symbol>> = expr
.symbols .symbols
.iter() .iter()
.filter_map(|sym| match sym.kind { .filter_map(|sym| match sym.kind {
SymbolKind::Name(ref nt, _) => Some((nt.clone(), sym)), SymbolKind::Name(_, ref nt, _) => Some((nt.clone(), sym)),
_ => None, _ => None,
}) })
.collect(); .collect();
@ -284,7 +286,8 @@ impl<'grammar> Validator<'grammar> {
SymbolKind::Repeat(ref repeat) => { SymbolKind::Repeat(ref repeat) => {
self.validate_symbol(&repeat.symbol)?; self.validate_symbol(&repeat.symbol)?;
} }
SymbolKind::Choose(ref sym) | SymbolKind::Name(_, ref sym) => { //NEXTCOMMITFIXME
SymbolKind::Choose(ref sym) | SymbolKind::Name(_, _, ref sym) => {
self.validate_symbol(sym)?; self.validate_symbol(sym)?;
} }
SymbolKind::Lookahead | SymbolKind::Lookbehind => { SymbolKind::Lookahead | SymbolKind::Lookbehind => {

View File

@ -252,7 +252,8 @@ impl Validator {
SymbolKind::Repeat(ref mut repeat) => { SymbolKind::Repeat(ref mut repeat) => {
self.validate_symbol(scope, &mut repeat.symbol)?; self.validate_symbol(scope, &mut repeat.symbol)?;
} }
SymbolKind::Choose(ref mut sym) | SymbolKind::Name(_, ref mut sym) => { //NEXTCOMMITFIXME
SymbolKind::Choose(ref mut sym) | SymbolKind::Name(_, _, ref mut sym) => {
self.validate_symbol(scope, sym)?; self.validate_symbol(scope, sym)?;
} }
SymbolKind::Lookahead | SymbolKind::Lookbehind | SymbolKind::Error => {} SymbolKind::Lookahead | SymbolKind::Lookbehind | SymbolKind::Error => {}

View File

@ -249,9 +249,13 @@ impl<'grammar> Validator<'grammar> {
SymbolKind::Repeat(ref repeat) => { SymbolKind::Repeat(ref repeat) => {
self.validate_symbol(&repeat.symbol)?; self.validate_symbol(&repeat.symbol)?;
} }
SymbolKind::Choose(ref sym) | SymbolKind::Name(_, ref sym) => { SymbolKind::Choose(ref sym) | SymbolKind::Name(_, _, ref sym) => {
self.validate_symbol(sym)?; self.validate_symbol(sym)?;
} }
SymbolKind::Choose(ref sym) |
SymbolKind::Name(_, _, ref sym) => {
try!(self.validate_symbol(sym));
}
SymbolKind::Lookahead | SymbolKind::Lookbehind | SymbolKind::Error => {} SymbolKind::Lookahead | SymbolKind::Lookbehind | SymbolKind::Error => {}
SymbolKind::AmbiguousId(ref id) => { SymbolKind::AmbiguousId(ref id) => {
panic!("ambiguous id `{}` encountered after name resolution", id) panic!("ambiguous id `{}` encountered after name resolution", id)

View File

@ -355,7 +355,7 @@ impl<'grammar> TypeInferencer<'grammar> {
SymbolKind::Terminal(ref id) => Ok(self.types.terminal_type(id).clone()), SymbolKind::Terminal(ref id) => Ok(self.types.terminal_type(id).clone()),
SymbolKind::Nonterminal(ref id) => self.nonterminal_type(id), SymbolKind::Nonterminal(ref id) => self.nonterminal_type(id),
SymbolKind::Choose(ref s) => self.symbol_type(&s.kind), SymbolKind::Choose(ref s) => self.symbol_type(&s.kind),
SymbolKind::Name(_, ref s) => self.symbol_type(&s.kind), SymbolKind::Name(_, _, ref s) => self.symbol_type(&s.kind),
SymbolKind::Error => Ok(self.types.error_recovery_type().clone()), SymbolKind::Error => Ok(self.types.error_recovery_type().clone()),
SymbolKind::Repeat(..) SymbolKind::Repeat(..)

View File

@ -7,7 +7,7 @@ use util::strip;
use lalrpop_util::ParseError; use lalrpop_util::ParseError;
use super::Top; use super::Top;
//do
grammar<'input>(text: &'input str); grammar<'input>(text: &'input str);
pub Top: Top = { pub Top: Top = {
@ -187,8 +187,8 @@ ExprSymbol: ExprSymbol =
Symbol* => ExprSymbol { symbols: <> }; Symbol* => ExprSymbol { symbols: <> };
Symbol: Symbol = { Symbol: Symbol = {
<lo:@L> "<" @L <l:Id> ":" <s:Symbol0> ">" <hi:@R> => <lo:@L> "<" <m:"mut"?> @L <l:Id> ":" <s:Symbol0> ">" <hi:@R> =>
Symbol::new(Span(lo, hi), SymbolKind::Name(l, Box::new(s))), Symbol::new(Span(lo, hi), SymbolKind::Name(m.is_some(), l, Box::new(s))),
<lo:@L> "<" <s:Symbol0> ">" <hi:@R> => <lo:@L> "<" <s:Symbol0> ">" <hi:@R> =>
Symbol::new(Span(lo, hi), SymbolKind::Choose(Box::new(s))), Symbol::new(Span(lo, hi), SymbolKind::Choose(Box::new(s))),