Clean up parser a bit
This commit is contained in:
parent
c68f98d3a7
commit
aee18ee06f
1 changed files with 33 additions and 16 deletions
|
@ -79,18 +79,18 @@ struct Parser<'cx, 'i> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'cx, 'i> Parser<'cx, 'i> {
|
impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
/// Finalize the `Parser`, returning the `Renderer` underneath.
|
/// Finalizes the `Parser`, returning the `Renderer` underneath.
|
||||||
fn into_render(self) -> Renderer<'cx> {
|
fn into_render(self) -> Renderer<'cx> {
|
||||||
let Parser { render, .. } = self;
|
let Parser { render, .. } = self;
|
||||||
render
|
render
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consume `n` items from the input.
|
/// Consumes `n` items from the input.
|
||||||
fn shift(&mut self, n: usize) {
|
fn shift(&mut self, n: usize) {
|
||||||
self.input = &self.input[n..];
|
self.input = &self.input[n..];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a Rust AST parser from the given token tree.
|
/// Constructs a Rust AST parser from the given token tree.
|
||||||
fn with_rust_parser<F, T>(&self, tts: Vec<TokenTree>, callback: F) -> T where
|
fn with_rust_parser<F, T>(&self, tts: Vec<TokenTree>, callback: F) -> T where
|
||||||
F: FnOnce(&mut RustParser<'cx>) -> T
|
F: FnOnce(&mut RustParser<'cx>) -> T
|
||||||
{
|
{
|
||||||
|
@ -106,6 +106,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders multiple blocks of markup.
|
||||||
fn markups(&mut self) {
|
fn markups(&mut self) {
|
||||||
loop {
|
loop {
|
||||||
match self.input {
|
match self.input {
|
||||||
|
@ -116,6 +117,9 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders a single block of markup.
|
||||||
|
///
|
||||||
|
/// Returns `false` on error.
|
||||||
fn markup(&mut self) -> bool {
|
fn markup(&mut self) -> bool {
|
||||||
match self.input {
|
match self.input {
|
||||||
// Literal
|
// Literal
|
||||||
|
@ -154,7 +158,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
self.element(sp, &name.name.as_str());
|
self.element(sp, &name.name.as_str());
|
||||||
},
|
},
|
||||||
// Block
|
// Block
|
||||||
[TtDelimited(_, ref d), ..] if d.delim == token::DelimToken::Brace => {
|
[TtDelimited(_, ref d), ..] if d.delim == DelimToken::Brace => {
|
||||||
self.shift(1);
|
self.shift(1);
|
||||||
{
|
{
|
||||||
// Parse the contents of the block, emitting the
|
// Parse the contents of the block, emitting the
|
||||||
|
@ -178,6 +182,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders a literal string or number.
|
||||||
fn literal(&mut self, tt: &TokenTree, minus: bool) {
|
fn literal(&mut self, tt: &TokenTree, minus: bool) {
|
||||||
let lit = self.with_rust_parser(vec![tt.clone()], RustParser::parse_lit);
|
let lit = self.with_rust_parser(vec![tt.clone()], RustParser::parse_lit);
|
||||||
let lit = match lit {
|
let lit = match lit {
|
||||||
|
@ -190,6 +195,9 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders an `#if` expression.
|
||||||
|
///
|
||||||
|
/// The leading `#if` should already be consumed.
|
||||||
fn if_expr(&mut self, sp: Span) {
|
fn if_expr(&mut self, sp: Span) {
|
||||||
// Parse the initial if
|
// Parse the initial if
|
||||||
let mut if_cond = vec![];
|
let mut if_cond = vec![];
|
||||||
|
@ -236,6 +244,9 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
self.render.emit_if(if_cond, if_body, else_body);
|
self.render.emit_if(if_cond, if_body, else_body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders a `#for` expression.
|
||||||
|
///
|
||||||
|
/// The leading `#for` should already be consumed.
|
||||||
fn for_expr(&mut self, sp: Span) {
|
fn for_expr(&mut self, sp: Span) {
|
||||||
let mut pattern = vec![];
|
let mut pattern = vec![];
|
||||||
loop { match self.input {
|
loop { match self.input {
|
||||||
|
@ -268,13 +279,18 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
self.render.emit_for(pattern, iterable, body);
|
self.render.emit_for(pattern, iterable, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders a `$splice`.
|
||||||
|
///
|
||||||
|
/// The leading `$` should already be consumed.
|
||||||
fn splice(&mut self, sp: Span) -> P<Expr> {
|
fn splice(&mut self, sp: Span) -> P<Expr> {
|
||||||
let mut tts = vec![];
|
|
||||||
// First, munch a single token tree
|
// First, munch a single token tree
|
||||||
if let [ref tt, ..] = self.input {
|
let mut tts = match self.input {
|
||||||
self.shift(1);
|
[ref tt, ..] => {
|
||||||
tts.push(tt.clone());
|
self.shift(1);
|
||||||
}
|
vec![tt.clone()]
|
||||||
|
},
|
||||||
|
[] => self.render.cx.span_fatal(sp, "expected expression for this splice"),
|
||||||
|
};
|
||||||
loop { match self.input {
|
loop { match self.input {
|
||||||
// Munch attribute lookups e.g. `$person.address.street`
|
// Munch attribute lookups e.g. `$person.address.street`
|
||||||
[ref dot @ dot!(), ref ident @ ident!(_), ..] => {
|
[ref dot @ dot!(), ref ident @ ident!(_), ..] => {
|
||||||
|
@ -283,19 +299,18 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
tts.push(ident.clone());
|
tts.push(ident.clone());
|
||||||
},
|
},
|
||||||
// Munch function calls `()` and indexing operations `[]`
|
// Munch function calls `()` and indexing operations `[]`
|
||||||
[TtDelimited(sp, ref d), ..] if d.delim != token::DelimToken::Brace => {
|
[TtDelimited(sp, ref d), ..] if d.delim != DelimToken::Brace => {
|
||||||
self.shift(1);
|
self.shift(1);
|
||||||
tts.push(TtDelimited(sp, d.clone()));
|
tts.push(TtDelimited(sp, d.clone()));
|
||||||
},
|
},
|
||||||
_ => break,
|
_ => break,
|
||||||
}}
|
}}
|
||||||
if tts.is_empty() {
|
self.with_rust_parser(tts, RustParser::parse_expr)
|
||||||
self.render.cx.span_fatal(sp, "expected expression for this splice");
|
|
||||||
} else {
|
|
||||||
self.with_rust_parser(tts, RustParser::parse_expr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders an element node.
|
||||||
|
///
|
||||||
|
/// The element name should already be consumed.
|
||||||
fn element(&mut self, sp: Span, name: &str) {
|
fn element(&mut self, sp: Span, name: &str) {
|
||||||
if self.in_attr {
|
if self.in_attr {
|
||||||
self.render.cx.span_err(sp, "unexpected element, you silly bumpkin");
|
self.render.cx.span_err(sp, "unexpected element, you silly bumpkin");
|
||||||
|
@ -312,6 +327,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses and renders the attributes of an element.
|
||||||
fn attrs(&mut self) {
|
fn attrs(&mut self) {
|
||||||
loop { match self.input {
|
loop { match self.input {
|
||||||
[ident!(name), eq!(), ..] => {
|
[ident!(name), eq!(), ..] => {
|
||||||
|
@ -351,6 +367,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses the given token tree, returning a vector of statements.
|
||||||
fn block(&mut self, sp: Span, tts: &[TokenTree]) -> Vec<P<Stmt>> {
|
fn block(&mut self, sp: Span, tts: &[TokenTree]) -> Vec<P<Stmt>> {
|
||||||
let mut parse = Parser {
|
let mut parse = Parser {
|
||||||
in_attr: self.in_attr,
|
in_attr: self.in_attr,
|
||||||
|
@ -363,7 +380,7 @@ impl<'cx, 'i> Parser<'cx, 'i> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a literal to a string.
|
/// Converts a literal to a string.
|
||||||
fn lit_to_string(cx: &ExtCtxt, lit: Lit, minus: bool) -> Option<String> {
|
fn lit_to_string(cx: &ExtCtxt, lit: Lit, minus: bool) -> Option<String> {
|
||||||
use syntax::ast::Lit_::*;
|
use syntax::ast::Lit_::*;
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
|
|
Loading…
Add table
Reference in a new issue