maud/maud_macros/src/parse.rs

753 lines
28 KiB
Rust
Raw Normal View History

2020-09-27 21:36:27 +10:00
use proc_macro2::{Delimiter, Ident, Literal, Spacing, Span, TokenStream, TokenTree};
use proc_macro_error::{abort, abort_call_site, emit_error, SpanRange};
2018-07-29 03:21:57 -06:00
use std::collections::HashMap;
use syn::Lit;
2014-12-18 18:57:55 +13:00
2019-03-16 20:13:16 +13:00
use crate::ast;
2014-12-19 11:53:40 +13:00
pub fn parse(input: TokenStream) -> Vec<ast::Markup> {
Parser::new(input).markups()
2015-09-01 18:26:50 +12:00
}
2017-07-23 13:49:02 +12:00
#[derive(Clone)]
struct Parser {
/// If we're inside an attribute, then this contains the attribute name.
current_attr: Option<String>,
2018-04-16 20:27:41 +12:00
input: <TokenStream as IntoIterator>::IntoIter,
2015-01-07 17:43:37 +13:00
}
2017-07-29 19:41:22 +12:00
impl Iterator for Parser {
type Item = TokenTree;
2017-07-16 22:11:10 +12:00
fn next(&mut self) -> Option<TokenTree> {
2017-07-29 13:24:40 +12:00
self.input.next()
}
2017-07-29 19:41:22 +12:00
}
2017-07-29 19:41:22 +12:00
impl Parser {
fn new(input: TokenStream) -> Parser {
2017-08-12 16:21:13 +12:00
Parser {
current_attr: None,
2017-08-12 16:21:13 +12:00
input: input.into_iter(),
}
}
fn with_input(&self, input: TokenStream) -> Parser {
Parser {
current_attr: self.current_attr.clone(),
2017-08-12 16:21:13 +12:00
input: input.into_iter(),
}
}
2017-07-31 22:24:47 +12:00
/// Returns the next token in the stream without consuming it.
2017-07-16 22:11:10 +12:00
fn peek(&mut self) -> Option<TokenTree> {
2017-07-29 13:24:40 +12:00
self.clone().next()
}
2017-07-31 22:24:47 +12:00
/// Returns the next two tokens in the stream without consuming them.
2017-07-29 13:24:40 +12:00
fn peek2(&mut self) -> Option<(TokenTree, Option<TokenTree>)> {
let mut clone = self.clone();
clone.next().map(|first| (first, clone.next()))
}
2017-07-31 22:24:47 +12:00
/// Advances the cursor by one step.
fn advance(&mut self) {
self.next();
2014-12-21 16:47:40 +13:00
}
2015-01-07 17:43:37 +13:00
2017-07-31 22:24:47 +12:00
/// Advances the cursor by two steps.
2017-07-29 13:24:40 +12:00
fn advance2(&mut self) {
self.next();
self.next();
}
2021-03-13 20:49:05 +11:00
/// Parses multiple blocks of markup.
fn markups(&mut self) -> Vec<ast::Markup> {
let mut result = Vec::new();
2015-01-07 17:43:37 +13:00
loop {
match self.peek2() {
None => break,
Some((TokenTree::Punct(ref punct), _)) if punct.as_char() == ';' => self.advance(),
2020-09-27 21:36:27 +10:00
Some((TokenTree::Punct(ref punct), Some(TokenTree::Ident(ref ident))))
if punct.as_char() == '@' && *ident == "let" =>
{
self.advance2();
let keyword = TokenTree::Ident(ident.clone());
result.push(self.let_expr(punct.span(), keyword));
2020-09-27 21:36:27 +10:00
}
_ => result.push(self.markup()),
2015-01-07 17:43:37 +13:00
}
}
result
2014-12-20 20:53:38 +13:00
}
2021-03-13 20:49:05 +11:00
/// Parses a single block of markup.
fn markup(&mut self) -> ast::Markup {
let token = match self.peek() {
Some(token) => token,
2018-06-22 21:28:35 +12:00
None => {
abort_call_site!("unexpected end of input");
2020-09-27 21:36:27 +10:00
}
};
let markup = match token {
2015-01-11 11:50:52 +13:00
// Literal
TokenTree::Literal(literal) => {
self.advance();
self.literal(literal)
2020-09-27 21:36:27 +10:00
}
// Special form
TokenTree::Punct(ref punct) if punct.as_char() == '@' => {
self.advance();
2018-06-22 21:28:35 +12:00
let at_span = punct.span();
match self.next() {
Some(TokenTree::Ident(ident)) => {
let keyword = TokenTree::Ident(ident.clone());
match ident.to_string().as_str() {
"if" => {
let mut segments = Vec::new();
self.if_expr(at_span, vec![keyword], &mut segments);
2018-05-29 19:18:30 +12:00
ast::Markup::Special { segments }
2020-09-27 21:36:27 +10:00
}
"while" => self.while_expr(at_span, keyword),
"for" => self.for_expr(at_span, keyword),
"match" => self.match_expr(at_span, keyword),
2018-06-22 21:28:35 +12:00
"let" => {
2020-09-27 21:36:27 +10:00
let span = SpanRange {
first: at_span,
last: ident.span(),
};
abort!(span, "`@let` only works inside a block");
2020-09-27 21:36:27 +10:00
}
2018-06-22 21:28:35 +12:00
other => {
2020-09-27 21:36:27 +10:00
let span = SpanRange {
first: at_span,
last: ident.span(),
};
abort!(span, "unknown keyword `@{}`", other);
2018-06-22 21:28:35 +12:00
}
}
2020-09-27 21:36:27 +10:00
}
2018-06-22 21:28:35 +12:00
_ => {
abort!(at_span, "expected keyword after `@`");
2020-09-27 21:36:27 +10:00
}
}
2020-09-27 21:36:27 +10:00
}
2015-01-11 11:50:52 +13:00
// Element
TokenTree::Ident(ident) => {
2020-08-31 10:43:24 +03:00
let ident_string = ident.to_string();
2020-08-31 10:00:01 +03:00
match ident_string.as_str() {
"if" | "while" | "for" | "match" | "let" => {
abort!(
ident,
"found keyword `{}`", ident_string;
help = "should this be a `@{}`?", ident_string
);
}
"true" | "false" => {
if let Some(attr_name) = &self.current_attr {
emit_error!(
ident,
r#"attribute value must be a string"#;
help = "to declare an empty attribute, omit the equals sign: `{}`",
attr_name;
help = "to toggle the attribute, use square brackets: `{}[some_boolean_flag]`",
attr_name;
);
return ast::Markup::ParseError {
span: SpanRange::single_span(ident.span()),
};
}
}
2020-08-31 10:00:01 +03:00
_ => {}
}
// `.try_namespaced_name()` should never fail as we've
// already seen an `Ident`
let name = self.try_namespaced_name().expect("identifier");
self.element(name)
2020-09-27 21:36:27 +10:00
}
// Div element shorthand
TokenTree::Punct(ref punct) if punct.as_char() == '.' || punct.as_char() == '#' => {
let name = TokenTree::Ident(Ident::new("div", punct.span()));
self.element(name.into())
2020-09-27 21:36:27 +10:00
}
// Splice
2018-04-16 20:27:41 +12:00
TokenTree::Group(ref group) if group.delimiter() == Delimiter::Parenthesis => {
self.advance();
2020-09-27 21:36:27 +10:00
ast::Markup::Splice {
expr: group.stream(),
outer_span: SpanRange::single_span(group.span()),
}
}
2015-01-11 11:50:52 +13:00
// Block
2018-04-16 20:27:41 +12:00
TokenTree::Group(ref group) if group.delimiter() == Delimiter::Brace => {
self.advance();
2020-08-31 11:57:55 +03:00
ast::Markup::Block(self.block(group.stream(), SpanRange::single_span(group.span())))
2020-09-27 21:36:27 +10:00
}
2015-01-11 11:50:52 +13:00
// ???
2018-06-22 21:28:35 +12:00
token => {
abort!(token, "invalid syntax");
2020-09-27 21:36:27 +10:00
}
};
markup
2015-01-11 11:50:52 +13:00
}
2021-03-13 20:49:05 +11:00
/// Parses a literal string.
fn literal(&mut self, literal: Literal) -> ast::Markup {
match Lit::new(literal.clone()) {
Lit::Str(lit_str) => {
return ast::Markup::Literal {
content: lit_str.value(),
span: SpanRange::single_span(literal.span()),
}
}
// Boolean literals are idents, so `Lit::Bool` is handled in
// `markup`, not here.
Lit::Int(..) | Lit::Float(..) => {
emit_error!(literal, r#"literal must be double-quoted: `"{}"`"#, literal);
}
Lit::Char(lit_char) => {
emit_error!(
literal,
r#"literal must be double-quoted: `"{}"`"#,
lit_char.value(),
);
}
_ => {
emit_error!(literal, "expected string");
}
}
ast::Markup::ParseError {
span: SpanRange::single_span(literal.span()),
}
2015-01-07 17:43:37 +13:00
}
/// Parses an `@if` expression.
2015-09-14 11:31:40 +12:00
///
2016-02-03 11:50:13 +01:00
/// The leading `@if` should already be consumed.
2020-09-27 21:36:27 +10:00
fn if_expr(&mut self, at_span: Span, prefix: Vec<TokenTree>, segments: &mut Vec<ast::Special>) {
let mut head = prefix;
let body = loop {
2017-07-23 13:56:09 +12:00
match self.next() {
2018-04-16 20:27:41 +12:00
Some(TokenTree::Group(ref block)) if block.delimiter() == Delimiter::Brace => {
2020-08-31 11:57:55 +03:00
break self.block(block.stream(), SpanRange::single_span(block.span()));
2020-09-27 21:36:27 +10:00
}
Some(token) => head.push(token),
2018-06-22 21:28:35 +12:00
None => {
2020-08-31 11:57:55 +03:00
let mut span = ast::span_tokens(head);
span.first = at_span;
abort!(span, "expected body for this `@if`");
2020-09-27 21:36:27 +10:00
}
2017-07-23 13:56:09 +12:00
}
};
segments.push(ast::Special {
2020-08-31 11:57:55 +03:00
at_span: SpanRange::single_span(at_span),
head: head.into_iter().collect(),
body,
});
self.else_if_expr(segments)
}
/// Parses an optional `@else if` or `@else`.
2017-07-31 22:24:47 +12:00
///
/// The leading `@else if` or `@else` should *not* already be consumed.
fn else_if_expr(&mut self, segments: &mut Vec<ast::Special>) {
match self.peek2() {
2020-09-27 21:36:27 +10:00
Some((TokenTree::Punct(ref punct), Some(TokenTree::Ident(ref else_keyword))))
if punct.as_char() == '@' && *else_keyword == "else" =>
{
2017-07-29 13:24:40 +12:00
self.advance2();
let at_span = punct.span();
let else_keyword = TokenTree::Ident(else_keyword.clone());
2017-07-29 13:24:40 +12:00
match self.peek() {
2017-07-23 13:56:09 +12:00
// `@else if`
2020-08-31 09:51:21 +03:00
Some(TokenTree::Ident(ref if_keyword)) if *if_keyword == "if" => {
2017-07-23 13:56:09 +12:00
self.advance();
let if_keyword = TokenTree::Ident(if_keyword.clone());
self.if_expr(at_span, vec![else_keyword, if_keyword], segments)
2020-09-27 21:36:27 +10:00
}
2017-07-23 13:56:09 +12:00
// Just an `@else`
2020-09-27 21:36:27 +10:00
_ => match self.next() {
Some(TokenTree::Group(ref group))
if group.delimiter() == Delimiter::Brace =>
{
let body =
self.block(group.stream(), SpanRange::single_span(group.span()));
segments.push(ast::Special {
at_span: SpanRange::single_span(at_span),
head: vec![else_keyword].into_iter().collect(),
body,
});
}
_ => {
let span = SpanRange {
first: at_span,
last: else_keyword.span(),
};
abort!(span, "expected body for this `@else`");
2017-07-23 13:56:09 +12:00
}
2017-07-29 13:24:40 +12:00
},
2017-07-23 13:56:09 +12:00
}
2020-09-27 21:36:27 +10:00
}
// We didn't find an `@else`; stop
2020-09-27 21:36:27 +10:00
_ => {}
}
2015-02-27 09:27:45 +13:00
}
2021-03-13 20:49:05 +11:00
/// Parses an `@while` expression.
///
/// The leading `@while` should already be consumed.
fn while_expr(&mut self, at_span: Span, keyword: TokenTree) -> ast::Markup {
2018-06-22 21:28:35 +12:00
let keyword_span = keyword.span();
let mut head = vec![keyword];
let body = loop {
2017-07-23 14:03:23 +12:00
match self.next() {
Some(TokenTree::Group(ref block)) if block.delimiter() == Delimiter::Brace => {
2020-08-31 11:57:55 +03:00
break self.block(block.stream(), SpanRange::single_span(block.span()));
2020-09-27 21:36:27 +10:00
}
Some(token) => head.push(token),
2018-06-22 21:28:35 +12:00
None => {
2020-09-27 21:36:27 +10:00
let span = SpanRange {
first: at_span,
last: keyword_span,
};
abort!(span, "expected body for this `@while`");
2020-09-27 21:36:27 +10:00
}
2017-07-23 14:03:23 +12:00
}
};
ast::Markup::Special {
2020-09-27 21:36:27 +10:00
segments: vec![ast::Special {
at_span: SpanRange::single_span(at_span),
head: head.into_iter().collect(),
body,
}],
}
}
/// Parses a `@for` expression.
2015-09-14 11:31:40 +12:00
///
2016-02-03 11:50:13 +01:00
/// The leading `@for` should already be consumed.
fn for_expr(&mut self, at_span: Span, keyword: TokenTree) -> ast::Markup {
2018-06-22 21:28:35 +12:00
let keyword_span = keyword.span();
let mut head = vec![keyword];
2017-07-23 14:08:19 +12:00
loop {
match self.next() {
2020-08-31 09:51:21 +03:00
Some(TokenTree::Ident(ref in_keyword)) if *in_keyword == "in" => {
head.push(TokenTree::Ident(in_keyword.clone()));
break;
2020-09-27 21:36:27 +10:00
}
Some(token) => head.push(token),
2018-06-22 21:28:35 +12:00
None => {
2020-09-27 21:36:27 +10:00
let span = SpanRange {
first: at_span,
last: keyword_span,
};
abort!(span, "missing `in` in `@for` loop");
2020-09-27 21:36:27 +10:00
}
2017-07-23 14:08:19 +12:00
}
}
let body = loop {
2017-07-23 14:08:19 +12:00
match self.next() {
Some(TokenTree::Group(ref block)) if block.delimiter() == Delimiter::Brace => {
2020-08-31 11:57:55 +03:00
break self.block(block.stream(), SpanRange::single_span(block.span()));
2020-09-27 21:36:27 +10:00
}
Some(token) => head.push(token),
2018-06-22 21:28:35 +12:00
None => {
2020-09-27 21:36:27 +10:00
let span = SpanRange {
first: at_span,
last: keyword_span,
};
abort!(span, "expected body for this `@for`");
2020-09-27 21:36:27 +10:00
}
2017-07-23 14:08:19 +12:00
}
};
ast::Markup::Special {
2020-09-27 21:36:27 +10:00
segments: vec![ast::Special {
at_span: SpanRange::single_span(at_span),
head: head.into_iter().collect(),
body,
}],
}
2015-03-14 21:08:08 +13:00
}
/// Parses a `@match` expression.
2016-02-01 20:05:50 +01:00
///
/// The leading `@match` should already be consumed.
fn match_expr(&mut self, at_span: Span, keyword: TokenTree) -> ast::Markup {
2018-06-22 21:28:35 +12:00
let keyword_span = keyword.span();
let mut head = vec![keyword];
let (arms, arms_span) = loop {
match self.next() {
Some(TokenTree::Group(ref body)) if body.delimiter() == Delimiter::Brace => {
2020-08-31 11:57:55 +03:00
let span = SpanRange::single_span(body.span());
break (self.with_input(body.stream()).match_arms(), span);
2020-09-27 21:36:27 +10:00
}
Some(token) => head.push(token),
2018-06-22 21:28:35 +12:00
None => {
2020-09-27 21:36:27 +10:00
let span = SpanRange {
first: at_span,
last: keyword_span,
};
abort!(span, "expected body for this `@match`");
2020-09-27 21:36:27 +10:00
}
}
};
2020-09-27 21:36:27 +10:00
ast::Markup::Match {
at_span: SpanRange::single_span(at_span),
head: head.into_iter().collect(),
arms,
arms_span,
}
2016-02-01 20:05:50 +01:00
}
fn match_arms(&mut self) -> Vec<ast::MatchArm> {
let mut arms = Vec::new();
while let Some(arm) = self.match_arm() {
arms.push(arm);
}
arms
2016-02-01 20:05:50 +01:00
}
fn match_arm(&mut self) -> Option<ast::MatchArm> {
let mut head = Vec::new();
loop {
match self.peek2() {
Some((TokenTree::Punct(ref eq), Some(TokenTree::Punct(ref gt))))
2020-09-27 21:36:27 +10:00
if eq.as_char() == '='
&& gt.as_char() == '>'
&& eq.spacing() == Spacing::Joint =>
{
self.advance2();
head.push(TokenTree::Punct(eq.clone()));
head.push(TokenTree::Punct(gt.clone()));
break;
2020-09-27 21:36:27 +10:00
}
Some((token, _)) => {
self.advance();
head.push(token);
2020-09-27 21:36:27 +10:00
}
2018-06-22 21:28:35 +12:00
None => {
if head.is_empty() {
return None;
} else {
2018-06-22 21:28:35 +12:00
let head_span = ast::span_tokens(head);
abort!(head_span, "unexpected end of @match pattern");
2018-06-22 21:28:35 +12:00
}
2020-09-27 21:36:27 +10:00
}
}
}
let body = match self.next() {
// $pat => { $stmts }
Some(TokenTree::Group(ref body)) if body.delimiter() == Delimiter::Brace => {
2020-08-31 11:57:55 +03:00
let body = self.block(body.stream(), SpanRange::single_span(body.span()));
// Trailing commas are optional if the match arm is a braced block
if let Some(TokenTree::Punct(ref punct)) = self.peek() {
if punct.as_char() == ',' {
self.advance();
}
}
body
2020-09-27 21:36:27 +10:00
}
// $pat => $expr
Some(first_token) => {
2020-08-31 11:57:55 +03:00
let mut span = SpanRange::single_span(first_token.span());
let mut body = vec![first_token];
loop {
match self.next() {
Some(TokenTree::Punct(ref punct)) if punct.as_char() == ',' => break,
Some(token) => {
2020-08-31 11:57:55 +03:00
span.last = token.span();
body.push(token);
2020-09-27 21:36:27 +10:00
}
None => break,
}
}
self.block(body.into_iter().collect(), span)
2020-09-27 21:36:27 +10:00
}
2018-06-22 21:28:35 +12:00
None => {
let span = ast::span_tokens(head);
abort!(span, "unexpected end of @match arm");
2020-09-27 21:36:27 +10:00
}
};
2020-09-27 21:36:27 +10:00
Some(ast::MatchArm {
head: head.into_iter().collect(),
body,
})
2016-02-01 20:05:50 +01:00
}
/// Parses a `@let` expression.
2016-11-12 14:46:49 +13:00
///
/// The leading `@let` should already be consumed.
fn let_expr(&mut self, at_span: Span, keyword: TokenTree) -> ast::Markup {
let mut tokens = vec![keyword];
2017-07-16 22:13:26 +12:00
loop {
match self.next() {
2020-09-27 21:36:27 +10:00
Some(token) => match token {
TokenTree::Punct(ref punct) if punct.as_char() == '=' => {
tokens.push(token.clone());
break;
}
2020-09-27 21:36:27 +10:00
_ => tokens.push(token),
},
2018-06-22 21:28:35 +12:00
None => {
2020-08-31 11:57:55 +03:00
let mut span = ast::span_tokens(tokens);
span.first = at_span;
abort!(span, "unexpected end of `@let` expression");
2018-06-22 21:28:35 +12:00
}
2017-07-16 22:13:26 +12:00
}
}
loop {
2017-07-16 22:13:26 +12:00
match self.next() {
2020-09-27 21:36:27 +10:00
Some(token) => match token {
TokenTree::Punct(ref punct) if punct.as_char() == ';' => {
tokens.push(token.clone());
break;
}
2020-09-27 21:36:27 +10:00
_ => tokens.push(token),
2017-07-16 22:13:26 +12:00
},
2018-06-22 21:28:35 +12:00
None => {
2020-08-31 11:57:55 +03:00
let mut span = ast::span_tokens(tokens);
span.first = at_span;
abort!(
span,
"unexpected end of `@let` expression";
help = "are you missing a semicolon?"
);
2020-09-27 21:36:27 +10:00
}
2017-07-16 22:13:26 +12:00
}
}
2020-09-27 21:36:27 +10:00
ast::Markup::Let {
at_span: SpanRange::single_span(at_span),
tokens: tokens.into_iter().collect(),
}
2016-11-12 14:46:49 +13:00
}
/// Parses an element node.
2015-09-14 11:31:40 +12:00
///
/// The element name should already be consumed.
fn element(&mut self, name: TokenStream) -> ast::Markup {
if self.current_attr.is_some() {
2018-06-22 21:28:35 +12:00
let span = ast::span_tokens(name);
abort!(span, "unexpected element");
2015-01-11 11:50:52 +13:00
}
let attrs = self.attrs();
let body = match self.peek() {
Some(TokenTree::Punct(ref punct))
2020-09-27 21:36:27 +10:00
if punct.as_char() == ';' || punct.as_char() == '/' =>
{
// Void element
self.advance();
if punct.as_char() == '/' {
emit_error!(
punct,
"void elements must use `;`, not `/`";
help = "change this to `;`";
help = "see https://github.com/lambda-fairy/maud/pull/315 for details";
);
}
2020-09-27 21:36:27 +10:00
ast::ElementBody::Void {
semi_span: SpanRange::single_span(punct.span()),
}
}
Some(_) => match self.markup() {
2020-09-27 21:36:27 +10:00
ast::Markup::Block(block) => ast::ElementBody::Block { block },
markup => {
let markup_span = markup.span();
abort!(
markup_span,
"element body must be wrapped in braces";
help = "see https://github.com/lambda-fairy/maud/pull/137 for details"
);
}
},
None => abort_call_site!("expected `;`, found end of macro"),
};
ast::Markup::Element { name, attrs, body }
2014-12-18 18:57:55 +13:00
}
2015-01-07 17:43:37 +13:00
/// Parses the attributes of an element.
2021-03-13 20:25:56 +11:00
fn attrs(&mut self) -> Vec<ast::Attr> {
let mut attrs = Vec::new();
loop {
if let Some(name) = self.try_namespaced_name() {
// Attribute
match self.peek() {
// Non-empty attribute
Some(TokenTree::Punct(ref punct)) if punct.as_char() == '=' => {
self.advance();
// Parse a value under an attribute context
assert!(self.current_attr.is_none());
self.current_attr = Some(ast::name_to_string(name.clone()));
let attr_type = match self.attr_toggler() {
Some(toggler) => ast::AttrType::Optional { toggler },
None => {
let value = self.markup();
ast::AttrType::Normal { value }
}
};
self.current_attr = None;
attrs.push(ast::Attr::Named {
named_attr: ast::NamedAttr { name, attr_type },
});
}
// Empty attribute (legacy syntax)
Some(TokenTree::Punct(ref punct)) if punct.as_char() == '?' => {
self.advance();
let toggler = self.attr_toggler();
attrs.push(ast::Attr::Named {
named_attr: ast::NamedAttr {
name: name.clone(),
attr_type: ast::AttrType::Empty { toggler },
},
});
}
// Empty attribute (new syntax)
_ => {
let toggler = self.attr_toggler();
attrs.push(ast::Attr::Named {
named_attr: ast::NamedAttr {
name: name.clone(),
attr_type: ast::AttrType::Empty { toggler },
},
});
}
2020-09-27 21:36:27 +10:00
}
} else {
match self.peek() {
// Class shorthand
Some(TokenTree::Punct(ref punct)) if punct.as_char() == '.' => {
self.advance();
let name = self.class_or_id_name();
let toggler = self.attr_toggler();
attrs.push(ast::Attr::Class {
dot_span: SpanRange::single_span(punct.span()),
name,
toggler,
});
}
// ID shorthand
Some(TokenTree::Punct(ref punct)) if punct.as_char() == '#' => {
self.advance();
let name = self.class_or_id_name();
attrs.push(ast::Attr::Id {
hash_span: SpanRange::single_span(punct.span()),
name,
});
}
// If it's not a valid attribute, backtrack and bail out
_ => break,
2020-09-27 21:36:27 +10:00
}
2016-06-03 23:06:39 +12:00
}
}
2018-07-29 03:21:57 -06:00
2020-08-31 11:57:55 +03:00
let mut attr_map: HashMap<String, Vec<SpanRange>> = HashMap::new();
let mut has_class = false;
2018-07-29 03:21:57 -06:00
for attr in &attrs {
let name = match attr {
ast::Attr::Class { .. } => {
if has_class {
// Only check the first class to avoid spurious duplicates
continue;
}
has_class = true;
"class".to_string()
2020-09-27 21:36:27 +10:00
}
ast::Attr::Id { .. } => "id".to_string(),
ast::Attr::Named { named_attr } => named_attr
2020-09-27 21:36:27 +10:00
.name
.clone()
.into_iter()
.map(|token| token.to_string())
.collect(),
};
2018-07-29 03:21:57 -06:00
let entry = attr_map.entry(name).or_default();
entry.push(attr.span());
2018-07-29 03:21:57 -06:00
}
for (name, spans) in attr_map {
if spans.len() > 1 {
let mut spans = spans.into_iter();
let first_span = spans.next().expect("spans should be non-empty");
abort!(first_span, "duplicate attribute `{}`", name);
2018-07-29 03:21:57 -06:00
}
}
attrs
}
/// Parses the name of a class or ID.
fn class_or_id_name(&mut self) -> ast::Markup {
if let Some(symbol) = self.try_name() {
ast::Markup::Symbol { symbol }
} else {
self.markup()
}
}
2017-08-12 16:46:40 +12:00
/// Parses the `[cond]` syntax after an empty attribute or class shorthand.
fn attr_toggler(&mut self) -> Option<ast::Toggler> {
match self.peek() {
Some(TokenTree::Group(ref group)) if group.delimiter() == Delimiter::Bracket => {
self.advance();
Some(ast::Toggler {
cond: group.stream(),
2020-08-31 11:57:55 +03:00
cond_span: SpanRange::single_span(group.span()),
})
2020-09-27 21:36:27 +10:00
}
2018-04-16 20:27:41 +12:00
_ => None,
2017-08-12 16:46:40 +12:00
}
}
/// Parses an identifier, without dealing with namespaces.
2018-06-22 21:28:35 +12:00
fn try_name(&mut self) -> Option<TokenStream> {
let mut result = Vec::new();
if let Some(token @ TokenTree::Ident(_)) = self.peek() {
self.advance();
result.push(token);
} else {
2018-06-22 21:28:35 +12:00
return None;
}
let mut expect_ident = false;
loop {
expect_ident = match self.peek() {
Some(TokenTree::Punct(ref punct)) if punct.as_char() == '-' => {
self.advance();
result.push(TokenTree::Punct(punct.clone()));
true
2020-09-27 21:36:27 +10:00
}
Some(TokenTree::Ident(ref ident)) if expect_ident => {
self.advance();
result.push(TokenTree::Ident(ident.clone()));
false
2020-09-27 21:36:27 +10:00
}
_ => break,
};
2016-07-19 14:57:05 +03:00
}
2018-06-22 21:28:35 +12:00
Some(result.into_iter().collect())
2016-07-19 14:57:05 +03:00
}
2016-07-18 14:40:45 +03:00
/// Parses a HTML element or attribute name, along with a namespace
/// if necessary.
2018-06-22 21:28:35 +12:00
fn try_namespaced_name(&mut self) -> Option<TokenStream> {
let mut result = vec![self.try_name()?];
if let Some(TokenTree::Punct(ref punct)) = self.peek() {
if punct.as_char() == ':' {
self.advance();
result.push(TokenStream::from(TokenTree::Punct(punct.clone())));
2018-06-22 21:28:35 +12:00
result.push(self.try_name()?);
}
}
2018-06-22 21:28:35 +12:00
Some(result.into_iter().collect())
2014-12-18 18:57:55 +13:00
}
/// Parses the given token stream as a Maud expression.
2020-08-31 11:57:55 +03:00
fn block(&mut self, body: TokenStream, outer_span: SpanRange) -> ast::Block {
let markups = self.with_input(body).markups();
2020-09-27 21:36:27 +10:00
ast::Block {
markups,
outer_span,
}
2015-01-07 17:43:37 +13:00
}
2014-12-18 18:57:55 +13:00
}