2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-06-12 10:59:50 -07:00
|
|
|
// Earley-like parser for macros.
|
2013-02-25 14:11:21 -05:00
|
|
|
use ast;
|
2012-09-04 11:37:29 -07:00
|
|
|
use ast::{matcher, match_tok, match_seq, match_nonterminal, ident};
|
2013-01-30 09:56:33 -08:00
|
|
|
use codemap::{BytePos, mk_sp};
|
2012-12-23 17:41:37 -05:00
|
|
|
use codemap;
|
|
|
|
use parse::lexer::*; //resolve bug?
|
2013-02-21 00:16:31 -08:00
|
|
|
use parse::ParseSess;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::parser::Parser;
|
|
|
|
use parse::token::{Token, EOF, to_str, nonterminal};
|
|
|
|
use parse::token;
|
|
|
|
|
2013-03-22 22:26:32 -04:00
|
|
|
use core::prelude::*;
|
2013-03-26 16:38:07 -04:00
|
|
|
use core::hashmap::linear::LinearMap;
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2012-07-27 19:14:46 -07:00
|
|
|
/* This is an Earley-like parser, without support for in-grammar nonterminals,
|
2012-08-24 18:16:56 -07:00
|
|
|
only by calling out to the main rust parser for named nonterminals (which it
|
2012-07-27 19:14:46 -07:00
|
|
|
commits to fully when it hits one in a grammar). This means that there are no
|
|
|
|
completer or predictor rules, and therefore no need to store one column per
|
|
|
|
token: instead, there's a set of current Earley items and a set of next
|
|
|
|
ones. Instead of NTs, we have a special case for Kleene star. The big-O, in
|
|
|
|
pathological cases, is worse than traditional Earley parsing, but it's an
|
|
|
|
easier fit for Macro-by-Example-style rules, and I think the overhead is
|
2012-08-24 18:16:56 -07:00
|
|
|
lower. (In order to prevent the pathological case, we'd need to lazily
|
|
|
|
construct the resulting `named_match`es at the very end. It'd be a pain,
|
|
|
|
and require more memory to keep around old items, but it would also save
|
|
|
|
overhead)*/
|
|
|
|
|
|
|
|
/* Quick intro to how the parser works:
|
|
|
|
|
|
|
|
A 'position' is a dot in the middle of a matcher, usually represented as a
|
|
|
|
dot. For example `· a $( a )* a b` is a position, as is `a $( · a )* a b`.
|
|
|
|
|
|
|
|
The parser walks through the input a character at a time, maintaining a list
|
|
|
|
of items consistent with the current position in the input string: `cur_eis`.
|
|
|
|
|
|
|
|
As it processes them, it fills up `eof_eis` with items that would be valid if
|
|
|
|
the macro invocation is now over, `bb_eis` with items that are waiting on
|
|
|
|
a Rust nonterminal like `$e:expr`, and `next_eis` with items that are waiting
|
|
|
|
on the a particular token. Most of the logic concerns moving the · through the
|
|
|
|
repetitions indicated by Kleene stars. It only advances or calls out to the
|
|
|
|
real Rust parser when no `cur_eis` items remain
|
|
|
|
|
|
|
|
Example: Start parsing `a a a a b` against [· a $( a )* a b].
|
|
|
|
|
|
|
|
Remaining input: `a a a a b`
|
|
|
|
next_eis: [· a $( a )* a b]
|
|
|
|
|
|
|
|
- - - Advance over an `a`. - - -
|
|
|
|
|
|
|
|
Remaining input: `a a a b`
|
|
|
|
cur: [a · $( a )* a b]
|
|
|
|
Descend/Skip (first item).
|
|
|
|
next: [a $( · a )* a b] [a $( a )* · a b].
|
|
|
|
|
|
|
|
- - - Advance over an `a`. - - -
|
|
|
|
|
|
|
|
Remaining input: `a a b`
|
|
|
|
cur: [a $( a · )* a b] next: [a $( a )* a · b]
|
|
|
|
Finish/Repeat (first item)
|
|
|
|
next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]
|
|
|
|
|
|
|
|
- - - Advance over an `a`. - - - (this looks exactly like the last step)
|
|
|
|
|
|
|
|
Remaining input: `a b`
|
|
|
|
cur: [a $( a · )* a b] next: [a $( a )* a · b]
|
|
|
|
Finish/Repeat (first item)
|
|
|
|
next: [a $( a )* · a b] [a $( · a )* a b] [a $( a )* a · b]
|
|
|
|
|
|
|
|
- - - Advance over an `a`. - - - (this looks exactly like the last step)
|
|
|
|
|
|
|
|
Remaining input: `b`
|
|
|
|
cur: [a $( a · )* a b] next: [a $( a )* a · b]
|
|
|
|
Finish/Repeat (first item)
|
|
|
|
next: [a $( a )* · a b] [a $( · a )* a b]
|
|
|
|
|
|
|
|
- - - Advance over a `b`. - - -
|
|
|
|
|
|
|
|
Remaining input: ``
|
|
|
|
eof: [a $( a )* a b ·]
|
|
|
|
|
|
|
|
*/
|
2012-06-12 10:59:50 -07:00
|
|
|
|
|
|
|
|
2012-07-27 19:14:46 -07:00
|
|
|
/* to avoid costly uniqueness checks, we require that `match_seq` always has a
|
2012-06-12 10:59:50 -07:00
|
|
|
nonempty body. */
|
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub enum matcher_pos_up { /* to break a circularity */
|
2013-02-21 00:16:31 -08:00
|
|
|
matcher_pos_up(Option<~MatcherPos>)
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub fn is_some(&&mpu: matcher_pos_up) -> bool {
|
2012-12-04 15:38:04 -08:00
|
|
|
match &mpu {
|
|
|
|
&matcher_pos_up(None) => false,
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => true
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
pub struct MatcherPos {
|
2013-03-22 15:52:50 -07:00
|
|
|
elts: ~[ast::matcher], // maybe should be <'>? Need to understand regions.
|
2012-10-15 14:56:42 -07:00
|
|
|
sep: Option<Token>,
|
2013-02-21 16:17:23 -08:00
|
|
|
idx: uint,
|
|
|
|
up: matcher_pos_up, // mutable for swapping only
|
2013-03-07 18:37:22 -05:00
|
|
|
matches: ~[~[@named_match]],
|
2012-07-23 15:34:43 -07:00
|
|
|
match_lo: uint, match_hi: uint,
|
2012-11-15 19:37:29 -08:00
|
|
|
sp_lo: BytePos,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
pub fn copy_up(&& mpu: matcher_pos_up) -> ~MatcherPos {
|
2012-12-04 10:50:00 -08:00
|
|
|
match &mpu {
|
|
|
|
&matcher_pos_up(Some(ref mp)) => copy (*mp),
|
2013-02-11 19:26:38 -08:00
|
|
|
_ => fail!()
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub fn count_names(ms: &[matcher]) -> uint {
|
2012-06-30 16:19:07 -07:00
|
|
|
vec::foldl(0u, ms, |ct, m| {
|
2012-08-06 12:34:08 -07:00
|
|
|
ct + match m.node {
|
2012-08-03 19:59:04 -07:00
|
|
|
match_tok(_) => 0u,
|
2012-12-04 10:50:00 -08:00
|
|
|
match_seq(ref more_ms, _, _, _, _) => count_names((*more_ms)),
|
2012-08-03 19:59:04 -07:00
|
|
|
match_nonterminal(_,_,_) => 1u
|
2012-06-12 10:59:50 -07:00
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:35:33 -07:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2013-03-02 11:17:11 -08:00
|
|
|
pub fn initial_matcher_pos(+ms: ~[matcher], +sep: Option<Token>, lo: BytePos)
|
2013-02-21 00:16:31 -08:00
|
|
|
-> ~MatcherPos {
|
2012-07-23 15:34:43 -07:00
|
|
|
let mut match_idx_hi = 0u;
|
2013-02-24 21:27:51 -08:00
|
|
|
for ms.each |elt| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match elt.node {
|
2012-08-03 19:59:04 -07:00
|
|
|
match_tok(_) => (),
|
|
|
|
match_seq(_,_,_,_,hi) => {
|
2012-07-27 19:14:46 -07:00
|
|
|
match_idx_hi = hi; // it is monotonic...
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
match_nonterminal(_,_,pos) => {
|
2012-07-27 19:14:46 -07:00
|
|
|
match_idx_hi = pos+1u; // ...so latest is highest
|
|
|
|
}
|
2012-07-23 15:34:43 -07:00
|
|
|
}
|
|
|
|
}
|
2013-03-07 18:37:22 -05:00
|
|
|
let matches = vec::from_fn(count_names(ms), |_i| ~[]);
|
2013-02-21 00:16:31 -08:00
|
|
|
~MatcherPos {
|
|
|
|
elts: ms,
|
|
|
|
sep: sep,
|
2013-02-21 16:17:23 -08:00
|
|
|
idx: 0u,
|
|
|
|
up: matcher_pos_up(None),
|
2013-02-24 21:27:51 -08:00
|
|
|
matches: matches,
|
2013-02-21 00:16:31 -08:00
|
|
|
match_lo: 0u,
|
|
|
|
match_hi: match_idx_hi,
|
|
|
|
sp_lo: lo
|
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
|
2012-07-27 19:14:46 -07:00
|
|
|
// named_match is a pattern-match result for a single ast::match_nonterminal:
|
|
|
|
// so it is associated with a single ident in a parse, and all
|
|
|
|
// matched_nonterminals in the named_match have the same nonterminal type
|
|
|
|
// (expr, item, etc). All the leaves in a single named_match correspond to a
|
|
|
|
// single matcher_nonterminal in the ast::matcher that produced it.
|
2012-07-27 17:42:32 -07:00
|
|
|
//
|
|
|
|
// It should probably be renamed, it has more or less exact correspondence to
|
2012-07-27 19:14:46 -07:00
|
|
|
// ast::match nodes, and the in-memory structure of a particular named_match
|
2012-07-27 17:42:32 -07:00
|
|
|
// represents the match that occurred when a particular subset of an
|
2012-07-27 19:14:46 -07:00
|
|
|
// ast::match -- those ast::matcher nodes leading to a single
|
|
|
|
// match_nonterminal -- was applied to a particular token tree.
|
2012-07-27 17:42:32 -07:00
|
|
|
//
|
2012-07-27 19:14:46 -07:00
|
|
|
// The width of each matched_seq in the named_match, and the identity of the
|
|
|
|
// matched_nonterminals, will depend on the token tree it was applied to: each
|
|
|
|
// matched_seq corresponds to a single match_seq in the originating
|
|
|
|
// ast::matcher. The depth of the named_match structure will therefore depend
|
|
|
|
// only on the nesting depth of ast::match_seqs in the originating
|
|
|
|
// ast::matcher it was derived from.
|
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub enum named_match {
|
2012-07-27 19:14:46 -07:00
|
|
|
matched_seq(~[@named_match], codemap::span),
|
|
|
|
matched_nonterminal(nonterminal)
|
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
pub type earley_item = ~MatcherPos;
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
pub fn nameize(p_s: @mut ParseSess, ms: ~[matcher], res: ~[@named_match])
|
2013-03-21 15:41:37 -04:00
|
|
|
-> LinearMap<ident,@named_match> {
|
2013-02-21 00:16:31 -08:00
|
|
|
fn n_rec(p_s: @mut ParseSess, m: matcher, res: ~[@named_match],
|
2013-03-21 15:41:37 -04:00
|
|
|
ret_val: &mut LinearMap<ident, @named_match>) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match m {
|
2013-01-30 09:56:33 -08:00
|
|
|
codemap::spanned {node: match_tok(_), _} => (),
|
|
|
|
codemap::spanned {node: match_seq(ref more_ms, _, _, _, _), _} => {
|
2012-12-04 21:13:02 -08:00
|
|
|
for (*more_ms).each() |next_m| {
|
|
|
|
n_rec(p_s, *next_m, res, ret_val)
|
|
|
|
};
|
2012-06-27 15:29:35 -07:00
|
|
|
}
|
2013-01-30 09:56:33 -08:00
|
|
|
codemap::spanned {
|
2013-01-29 13:54:06 -08:00
|
|
|
node: match_nonterminal(bind_name, _, idx), span: sp
|
|
|
|
} => {
|
2013-02-08 17:08:02 -05:00
|
|
|
if ret_val.contains_key(&bind_name) {
|
2012-07-18 16:18:02 -07:00
|
|
|
p_s.span_diagnostic.span_fatal(sp, ~"Duplicated bind name: "+
|
|
|
|
*p_s.interner.get(bind_name))
|
2012-06-27 15:29:35 -07:00
|
|
|
}
|
|
|
|
ret_val.insert(bind_name, res[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 15:41:37 -04:00
|
|
|
let mut ret_val = LinearMap::new();
|
|
|
|
for ms.each() |m| { n_rec(p_s, *m, res, &mut ret_val) }
|
2012-08-01 17:30:05 -07:00
|
|
|
return ret_val;
|
2012-06-27 15:29:35 -07:00
|
|
|
}
|
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub enum parse_result {
|
2013-03-21 15:41:37 -04:00
|
|
|
success(LinearMap<ident, @named_match>),
|
2012-08-10 10:46:04 -07:00
|
|
|
failure(codemap::span, ~str),
|
|
|
|
error(codemap::span, ~str)
|
2012-07-05 17:33:39 -07:00
|
|
|
}
|
|
|
|
|
2013-02-26 20:18:01 -08:00
|
|
|
pub fn parse_or_else(
|
|
|
|
sess: @mut ParseSess,
|
|
|
|
+cfg: ast::crate_cfg,
|
2013-03-12 13:00:50 -07:00
|
|
|
rdr: @reader,
|
2013-02-26 20:18:01 -08:00
|
|
|
ms: ~[matcher]
|
2013-03-21 15:41:37 -04:00
|
|
|
) -> LinearMap<ident, @named_match> {
|
2012-08-06 12:34:08 -07:00
|
|
|
match parse(sess, cfg, rdr, ms) {
|
2012-08-03 19:59:04 -07:00
|
|
|
success(m) => m,
|
2013-03-21 15:41:37 -04:00
|
|
|
failure(sp, str) => sess.span_diagnostic.span_fatal(sp, str),
|
|
|
|
error(sp, str) => sess.span_diagnostic.span_fatal(sp, str)
|
2012-07-12 17:59:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 20:18:01 -08:00
|
|
|
pub fn parse(
|
|
|
|
sess: @mut ParseSess,
|
|
|
|
cfg: ast::crate_cfg,
|
2013-03-12 13:00:50 -07:00
|
|
|
rdr: @reader,
|
2013-02-26 20:18:01 -08:00
|
|
|
ms: ~[matcher]
|
|
|
|
) -> parse_result {
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut cur_eis = ~[];
|
2013-02-24 21:27:51 -08:00
|
|
|
cur_eis.push(initial_matcher_pos(copy ms, None, rdr.peek().sp.lo));
|
2012-06-12 10:59:50 -07:00
|
|
|
|
|
|
|
loop {
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut bb_eis = ~[]; // black-box parsed by parser.rs
|
|
|
|
let mut next_eis = ~[]; // or proceed normally
|
|
|
|
let mut eof_eis = ~[];
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2013-01-30 09:56:33 -08:00
|
|
|
let TokenAndSpan {tok: tok, sp: sp} = rdr.peek();
|
2012-06-12 10:59:50 -07:00
|
|
|
|
|
|
|
/* we append new items to this while we go */
|
|
|
|
while cur_eis.len() > 0u { /* for each Earley Item */
|
2012-09-27 22:20:47 -07:00
|
|
|
let mut ei = cur_eis.pop();
|
2012-06-12 10:59:50 -07:00
|
|
|
|
|
|
|
let idx = ei.idx;
|
|
|
|
let len = ei.elts.len();
|
|
|
|
|
|
|
|
/* at end of sequence */
|
|
|
|
if idx >= len {
|
2013-01-04 09:52:07 -05:00
|
|
|
// can't move out of `match`es, so:
|
2012-06-12 10:59:50 -07:00
|
|
|
if is_some(ei.up) {
|
|
|
|
// hack: a matcher sequence is repeating iff it has a
|
|
|
|
// parent (the top level is just a container)
|
|
|
|
|
|
|
|
|
|
|
|
// disregard separator, try to go up
|
|
|
|
// (remove this condition to make trailing seps ok)
|
|
|
|
if idx == len {
|
|
|
|
// pop from the matcher position
|
|
|
|
|
2013-02-21 16:17:23 -08:00
|
|
|
let mut new_pos = copy_up(ei.up);
|
2012-06-12 10:59:50 -07:00
|
|
|
|
|
|
|
// update matches (the MBE "parse tree") by appending
|
|
|
|
// each tree as a subtree.
|
|
|
|
|
|
|
|
// I bet this is a perf problem: we're preemptively
|
|
|
|
// doing a lot of array work that will get thrown away
|
|
|
|
// most of the time.
|
2012-07-23 15:34:43 -07:00
|
|
|
|
|
|
|
// Only touch the binders we have actually bound
|
|
|
|
for uint::range(ei.match_lo, ei.match_hi) |idx| {
|
2013-03-07 18:37:22 -05:00
|
|
|
let sub = ei.matches[idx];
|
2012-06-27 17:21:41 -07:00
|
|
|
new_pos.matches[idx]
|
2012-07-27 19:14:46 -07:00
|
|
|
.push(@matched_seq(sub,
|
|
|
|
mk_sp(ei.sp_lo,
|
|
|
|
sp.hi)));
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
|
2012-09-10 18:28:00 -07:00
|
|
|
new_pos.idx += 1;
|
2013-02-15 01:15:53 -08:00
|
|
|
cur_eis.push(new_pos);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// can we go around again?
|
|
|
|
|
|
|
|
// the *_t vars are workarounds for the lack of unary move
|
2012-08-06 12:34:08 -07:00
|
|
|
match copy ei.sep {
|
2012-12-04 10:50:00 -08:00
|
|
|
Some(ref t) if idx == len => { // we need a separator
|
|
|
|
if tok == (*t) { //pass the separator
|
2013-02-21 16:17:23 -08:00
|
|
|
let mut ei_t = ei;
|
2012-09-10 18:28:00 -07:00
|
|
|
ei_t.idx += 1;
|
2013-02-15 01:15:53 -08:00
|
|
|
next_eis.push(ei_t);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => { // we don't need a separator
|
2013-02-21 16:17:23 -08:00
|
|
|
let mut ei_t = ei;
|
2012-09-10 18:28:00 -07:00
|
|
|
ei_t.idx = 0;
|
2013-02-15 01:15:53 -08:00
|
|
|
cur_eis.push(ei_t);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-02-15 01:15:53 -08:00
|
|
|
eof_eis.push(ei);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
} else {
|
2012-08-06 12:34:08 -07:00
|
|
|
match copy ei.elts[idx].node {
|
2012-06-12 10:59:50 -07:00
|
|
|
/* need to descend into sequence */
|
2012-12-04 10:50:00 -08:00
|
|
|
match_seq(ref matchers, ref sep, zero_ok,
|
2012-08-03 19:59:04 -07:00
|
|
|
match_idx_lo, match_idx_hi) => {
|
2012-06-12 10:59:50 -07:00
|
|
|
if zero_ok {
|
2013-02-21 16:17:23 -08:00
|
|
|
let mut new_ei = copy ei;
|
2012-06-12 10:59:50 -07:00
|
|
|
new_ei.idx += 1u;
|
2012-07-23 15:34:43 -07:00
|
|
|
//we specifically matched zero repeats.
|
|
|
|
for uint::range(match_idx_lo, match_idx_hi) |idx| {
|
2012-07-27 19:14:46 -07:00
|
|
|
new_ei.matches[idx].push(@matched_seq(~[], sp));
|
2012-07-23 15:34:43 -07:00
|
|
|
}
|
|
|
|
|
2013-02-15 01:15:53 -08:00
|
|
|
cur_eis.push(new_ei);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let matches = vec::map(ei.matches, // fresh, same size:
|
2013-03-07 18:37:22 -05:00
|
|
|
|_m| ~[]);
|
2013-02-15 01:15:53 -08:00
|
|
|
let ei_t = ei;
|
2013-02-21 00:16:31 -08:00
|
|
|
cur_eis.push(~MatcherPos {
|
2013-02-24 21:27:51 -08:00
|
|
|
elts: copy *matchers,
|
|
|
|
sep: copy *sep,
|
2013-02-21 16:17:23 -08:00
|
|
|
idx: 0u,
|
|
|
|
up: matcher_pos_up(Some(ei_t)),
|
2013-02-15 01:15:53 -08:00
|
|
|
matches: matches,
|
2012-07-23 15:34:43 -07:00
|
|
|
match_lo: match_idx_lo, match_hi: match_idx_hi,
|
|
|
|
sp_lo: sp.lo
|
2012-06-12 10:59:50 -07:00
|
|
|
});
|
|
|
|
}
|
2013-02-15 01:15:53 -08:00
|
|
|
match_nonterminal(_,_,_) => { bb_eis.push(ei) }
|
2012-12-04 10:50:00 -08:00
|
|
|
match_tok(ref t) => {
|
2013-02-21 16:17:23 -08:00
|
|
|
let mut ei_t = ei;
|
2012-12-04 10:50:00 -08:00
|
|
|
if (*t) == tok {
|
2012-09-10 18:28:00 -07:00
|
|
|
ei_t.idx += 1;
|
2013-02-15 01:15:53 -08:00
|
|
|
next_eis.push(ei_t);
|
2012-09-10 18:28:00 -07:00
|
|
|
}
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* error messages here could be improved with links to orig. rules */
|
|
|
|
if tok == EOF {
|
2012-07-05 17:33:39 -07:00
|
|
|
if eof_eis.len() == 1u {
|
2013-03-07 18:37:22 -05:00
|
|
|
let mut v = ~[];
|
|
|
|
for vec::each_mut(eof_eis[0u].matches) |dv| {
|
|
|
|
v.push(dv.pop());
|
|
|
|
}
|
|
|
|
return success(nameize(sess, ms, v));
|
2012-06-12 10:59:50 -07:00
|
|
|
} else if eof_eis.len() > 1u {
|
2012-08-10 10:46:04 -07:00
|
|
|
return error(sp, ~"Ambiguity: multiple successful parses");
|
2012-06-12 10:59:50 -07:00
|
|
|
} else {
|
2012-08-01 17:30:05 -07:00
|
|
|
return failure(sp, ~"Unexpected end of macro invocation");
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bb_eis.len() > 0u && next_eis.len() > 0u)
|
|
|
|
|| bb_eis.len() > 1u {
|
2012-06-30 16:19:07 -07:00
|
|
|
let nts = str::connect(vec::map(bb_eis, |ei| {
|
2012-08-06 12:34:08 -07:00
|
|
|
match ei.elts[ei.idx].node {
|
2012-08-03 19:59:04 -07:00
|
|
|
match_nonterminal(bind,name,_) => {
|
2012-08-22 17:24:52 -07:00
|
|
|
fmt!("%s ('%s')", *sess.interner.get(name),
|
|
|
|
*sess.interner.get(bind))
|
2012-07-24 11:44:17 -07:00
|
|
|
}
|
2013-02-11 19:26:38 -08:00
|
|
|
_ => fail!()
|
2012-08-03 19:59:04 -07:00
|
|
|
} }), ~" or ");
|
2012-08-22 17:24:52 -07:00
|
|
|
return error(sp, fmt!(
|
2012-07-05 17:33:39 -07:00
|
|
|
"Local ambiguity: multiple parsing options: \
|
|
|
|
built-in NTs %s or %u other options.",
|
2012-08-22 17:24:52 -07:00
|
|
|
nts, next_eis.len()));
|
2012-06-12 10:59:50 -07:00
|
|
|
} else if (bb_eis.len() == 0u && next_eis.len() == 0u) {
|
2012-09-18 22:36:02 -07:00
|
|
|
return failure(sp, ~"No rules expected the token: "
|
2013-02-24 19:54:37 -08:00
|
|
|
+ to_str(rdr.interner(), &tok));
|
2012-06-12 10:59:50 -07:00
|
|
|
} else if (next_eis.len() > 0u) {
|
|
|
|
/* Now process the next token */
|
|
|
|
while(next_eis.len() > 0u) {
|
2012-09-27 22:20:47 -07:00
|
|
|
cur_eis.push(next_eis.pop());
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
rdr.next_token();
|
|
|
|
} else /* bb_eis.len() == 1 */ {
|
2013-02-26 20:18:01 -08:00
|
|
|
let rust_parser = Parser(sess, copy cfg, rdr.dup());
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2013-02-21 16:17:23 -08:00
|
|
|
let mut ei = bb_eis.pop();
|
2012-08-06 12:34:08 -07:00
|
|
|
match ei.elts[ei.idx].node {
|
2012-08-03 19:59:04 -07:00
|
|
|
match_nonterminal(_, name, idx) => {
|
2012-07-27 19:14:46 -07:00
|
|
|
ei.matches[idx].push(@matched_nonterminal(
|
2012-07-18 16:18:02 -07:00
|
|
|
parse_nt(rust_parser, *sess.interner.get(name))));
|
2012-06-12 10:59:50 -07:00
|
|
|
ei.idx += 1u;
|
|
|
|
}
|
2013-02-11 19:26:38 -08:00
|
|
|
_ => fail!()
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
2013-02-15 01:15:53 -08:00
|
|
|
cur_eis.push(ei);
|
2012-06-12 10:59:50 -07:00
|
|
|
|
2012-11-26 22:34:01 -05:00
|
|
|
for rust_parser.tokens_consumed.times() || {
|
2012-07-06 18:04:28 -07:00
|
|
|
rdr.next_token();
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 13:58:02 -08:00
|
|
|
fail_unless!(cur_eis.len() > 0u);
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 14:41:40 -08:00
|
|
|
pub fn parse_nt(p: Parser, name: ~str) -> nonterminal {
|
2012-08-06 12:34:08 -07:00
|
|
|
match name {
|
|
|
|
~"item" => match p.parse_item(~[]) {
|
2012-08-20 12:23:37 -07:00
|
|
|
Some(i) => token::nt_item(i),
|
|
|
|
None => p.fatal(~"expected an item keyword")
|
2012-08-06 17:14:32 -07:00
|
|
|
},
|
2012-08-03 19:59:04 -07:00
|
|
|
~"block" => token::nt_block(p.parse_block()),
|
|
|
|
~"stmt" => token::nt_stmt(p.parse_stmt(~[])),
|
|
|
|
~"pat" => token::nt_pat(p.parse_pat(true)),
|
|
|
|
~"expr" => token::nt_expr(p.parse_expr()),
|
|
|
|
~"ty" => token::nt_ty(p.parse_ty(false /* no need to disambiguate*/)),
|
2012-06-12 10:59:50 -07:00
|
|
|
// this could be handled like a token, since it is one
|
2013-02-21 18:12:13 -08:00
|
|
|
~"ident" => match *p.token {
|
2012-08-03 19:59:04 -07:00
|
|
|
token::IDENT(sn,b) => { p.bump(); token::nt_ident(sn,b) }
|
|
|
|
_ => p.fatal(~"expected ident, found "
|
2013-02-24 19:54:37 -08:00
|
|
|
+ token::to_str(p.reader.interner(), © *p.token))
|
2012-08-06 17:14:32 -07:00
|
|
|
},
|
2012-08-03 19:59:04 -07:00
|
|
|
~"path" => token::nt_path(p.parse_path_with_tps(false)),
|
|
|
|
~"tt" => {
|
2013-02-21 18:12:13 -08:00
|
|
|
*p.quote_depth += 1u; //but in theory, non-quoted tts might be useful
|
2012-07-27 19:14:46 -07:00
|
|
|
let res = token::nt_tt(@p.parse_token_tree());
|
2013-02-21 18:12:13 -08:00
|
|
|
*p.quote_depth -= 1u;
|
2012-07-06 14:48:01 -07:00
|
|
|
res
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
~"matchers" => token::nt_matchers(p.parse_matchers()),
|
|
|
|
_ => p.fatal(~"Unsupported builtin nonterminal parser: " + name)
|
2012-06-12 10:59:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|