<commit_before>use std::fmt::{self, Debug, Display};
use std::iter::FromIterator;
use std::slice;
use std::vec;

use proc_macro2::{
    Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
};
#[cfg(feature = "printing")]
use quote::ToTokens;

#[cfg(feature = "parsing")]
use crate::buffer::Cursor;
use crate::thread::ThreadBound;

/// The result of a Syn parser.
pub type Result<T> = std::result::Result<T, Error>;

/// Error returned when a Syn parser cannot parse the input tokens.
///
/// # Error reporting in proc macros
///
/// The correct way to report errors back to the compiler from a procedural
/// macro is by emitting an appropriately spanned invocation of
/// [`compile_error!`] in the generated code. This produces a better diagnostic
/// message than simply panicking the macro.
///
/// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
///
/// When parsing macro input, the [`parse_macro_input!`] macro handles the
/// conversion to `compile_error!` automatically.
///
/// ```
/// extern crate proc_macro;
///
/// use proc_macro::TokenStream;
/// use syn::{parse_macro_input, AttributeArgs, ItemFn};
///
/// # const IGNORE: &str = stringify! {
/// #[proc_macro_attribute]
/// # };
/// pub fn my_attr(args: TokenStream, input: TokenStream) -> TokenStream {
///     let args = parse_macro_input!(args as AttributeArgs);
///     let input = parse_macro_input!(input as ItemFn);
///
///     /* ... */
///     # TokenStream::new()
/// }
/// ```
///
/// For errors that arise later than the initial parsing stage, the
/// [`.to_compile_error()`] method can be used to perform an explicit conversion
/// to `compile_error!`.
///
/// [`.to_compile_error()`]: Error::to_compile_error
///
/// ```
/// # extern crate proc_macro;
/// #
/// # use proc_macro::TokenStream;
/// # use syn::{parse_macro_input, DeriveInput};
/// #
/// # const IGNORE: &str = stringify! {
/// #[proc_macro_derive(MyDerive)]
/// # };
/// pub fn my_derive(input: TokenStream) -> TokenStream {
///     let input = parse_macro_input!(input as DeriveInput);
///
///     // fn(DeriveInput) -> syn::Result<proc_macro2::TokenStream>
///     expand::my_derive(input)
///         .unwrap_or_else(|err| err.to_compile_error())
///         .into()
/// }
/// #
/// # mod expand {
/// #     use proc_macro2::TokenStream;
/// #     use syn::{DeriveInput, Result};
/// #
/// #     pub fn my_derive(input: DeriveInput) -> Result<TokenStream> {
/// #         unimplemented!()
/// #     }
/// # }
/// ```
#[derive(Clone)]
pub struct Error {
    messages: Vec<ErrorMessage>,
}

struct ErrorMessage {
    // Span is implemented as an index into a thread-local interner to keep the
    // size small. It is not safe to access from a different thread. We want
    // errors to be Send and Sync to play nicely with the Failure crate, so pin
    // the span we're given to its original thread and assume it is
    // Span::call_site if accessed from any other thread.
    start_span: ThreadBound<Span>,
    end_span: ThreadBound<Span>,
    message: String,
}

#[cfg(test)]
struct _Test
where
    Error: Send + Sync;

impl Error {
    /// Usually the [`ParseStream::error`] method will be used instead, which
    /// automatically uses the correct span from the current position of the
    /// parse stream.
    ///
    /// Use `Error::new` when the error needs to be triggered on some span other
    /// than where the parse stream is currently positioned.
    ///
    /// [`ParseStream::error`]: crate::parse::ParseBuffer::error
    ///
    /// # Example
    ///
    /// ```
    /// use syn::{Error, Ident, LitStr, Result, Token};
    /// use syn::parse::ParseStream;
    ///
    /// // Parses input that looks like `name = "string"` where the key must be
    /// // the identifier `name` and the value may be any string literal.
    /// // Returns the string literal.
    /// fn parse_name(input: ParseStream) -> Result<LitStr> {
    ///     let name_token: Ident = input.parse()?;
    ///     if name_token != "name" {
    ///         // Trigger an error not on the current position of the stream,
    ///         // but on the position of the unexpected identifier.
    ///         return Err(Error::new(name_token.span(), "expected `name`"));
    ///     }
    ///     input.parse::<Token![=]>()?;
    ///     let s: LitStr = input.parse()?;
    ///     Ok(s)
    /// }
    /// ```
    pub fn new<T: Display>(span: Span, message: T) -> Self {
        Error {
            messages: vec![ErrorMessage {
                start_span: ThreadBound::new(span),
                end_span: ThreadBound::new(span),
                message: message.to_string(),
            }],
        }
    }

    /// Creates an error with the specified message spanning the given syntax
    /// tree node.
    ///
    /// Unlike the `Error::new` constructor, this constructor takes an argument
    /// `tokens` which is a syntax tree node. This allows the resulting `Error`
    /// to attempt to span all tokens inside of `tokens`. While you would
    /// typically be able to use the `Spanned` trait with the above `Error::new`
    /// constructor, implementation limitations today mean that
    /// `Error::new_spanned` may provide a higher-quality error message on
    /// stable Rust.
    ///
    /// When in doubt it's recommended to stick to `Error::new` (or
    /// `ParseStream::error`)!
    #[cfg(feature = "printing")]
    pub fn new_spanned<T: ToTokens, U: Display>(tokens: T, message: U) -> Self {
        let mut iter = tokens.into_token_stream().into_iter();
        let start = iter.next().map_or_else(Span::call_site, |t| t.span());
        let end = iter.last().map_or(start, |t| t.span());
        Error {
            messages: vec![ErrorMessage {
                start_span: ThreadBound::new(start),
                end_span: ThreadBound::new(end),
                message: message.to_string(),
            }],
        }
    }

    /// The source location of the error.
    ///
    /// Spans are not thread-safe so this function returns `Span::call_site()`
    /// if called from a different thread than the one on which the `Error` was
    /// originally created.
    pub fn span(&self) -> Span {
        let start = match self.messages[0].start_span.get() {
            Some(span) => *span,
            None => return Span::call_site(),
        };
        let end = match self.messages[0].end_span.get() {
            Some(span) => *span,
            None => return Span::call_site(),
        };
        start.join(end).unwrap_or(start)
    }

    /// Render the error as an invocation of [`compile_error!`].
    ///
    /// The [`parse_macro_input!`] macro provides a convenient way to invoke
    /// this method correctly in a procedural macro.
    ///
    /// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
    pub fn to_compile_error(&self) -> TokenStream {
        self.messages
            .iter()
            .map(ErrorMessage::to_compile_error)
            .collect()
    }

    /// Add another error message to self such that when `to_compile_error()` is
    /// called, both errors will be emitted together.
    pub fn combine(&mut self, another: Error) {
        self.messages.extend(another.messages)
    }
}

impl ErrorMessage {
    fn to_compile_error(&self) -> TokenStream {
        let start = self
            .start_span
            .get()
            .cloned()
            .unwrap_or_else(Span::call_site);
        let end = self.end_span.get().cloned().unwrap_or_else(Span::call_site);

        // compile_error!($message)
        TokenStream::from_iter(vec![
            TokenTree::Ident(Ident::new("compile_error", start)),
            TokenTree::Punct({
                let mut punct = Punct::new('!', Spacing::Alone);
                punct.set_span(start);
                punct
            }),
            TokenTree::Group({
                let mut group = Group::new(Delimiter::Brace, {
                    TokenStream::from_iter(vec![TokenTree::Literal({
                        let mut string = Literal::string(&self.message);
                        string.set_span(end);
                        string
                    })])
                });
                group.set_span(end);
                group
            }),
        ])
    }
}

#[cfg(feature = "parsing")]
pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error {
    if cursor.eof() {
        Error::new(scope, format!("unexpected end of input, {}", message))
    } else {
        let span = crate::buffer::open_span_of_group(cursor);
        Error::new(span, message)
    }
}

#[cfg(all(feature = "parsing", any(feature = "full", feature = "derive")))]
pub fn new2<T: Display>(start: Span, end: Span, message: T) -> Error {
    Error {
        messages: vec![ErrorMessage {
            start_span: ThreadBound::new(start),
            end_span: ThreadBound::new(end),
            message: message.to_string(),
        }],
    }
}

impl Debug for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        if self.messages.len() == 1 {
            formatter
                .debug_tuple("Error")
                .field(&self.messages[0])
                .finish()
        } else {
            formatter
                .debug_tuple("Error")
                .field(&self.messages)
                .finish()
        }
    }
}

impl Debug for ErrorMessage {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        Debug::fmt(&self.message, formatter)
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(&self.messages[0].message)
    }
}

impl Clone for ErrorMessage {
    fn clone(&self) -> Self {
        let start = self
            .start_span
            .get()
            .cloned()
            .unwrap_or_else(Span::call_site);
        let end = self.end_span.get().cloned().unwrap_or_else(Span::call_site);
        ErrorMessage {
            start_span: ThreadBound::new(start),
            end_span: ThreadBound::new(end),
            message: self.message.clone(),
        }
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        "parse error"
    }
}

impl From<LexError> for Error {
    fn from(err: LexError) -> Self {
        Error::new(Span::call_site(), format!("{:?}", err))
    }
}

impl IntoIterator for Error {
    type Item = Error;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            messages: self.messages.into_iter(),
        }
    }
}

pub struct IntoIter {
    messages: vec::IntoIter<ErrorMessage>,
}

impl Iterator for IntoIter {
    type Item = Error;

    fn next(&mut self) -> Option<Self::Item> {
        Some(Error {
            messages: vec![self.messages.next()?],
        })
    }
}

impl<'a> IntoIterator for &'a Error {
    type Item = Error;
    type IntoIter = Iter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        Iter {
            messages: self.messages.iter(),
        }
    }
}

pub struct Iter<'a> {
    messages: slice::Iter<'a, ErrorMessage>,
}

impl<'a> Iterator for Iter<'a> {
    type Item = Error;

    fn next(&mut self) -> Option<Self::Item> {
        Some(Error {
            messages: vec![self.messages.next()?.clone()],
        })
    }
}
<commit_msg>Implement Extend<Error> for Error<commit_after>use std::fmt::{self, Debug, Display};
use std::iter::FromIterator;
use std::slice;
use std::vec;

use proc_macro2::{
    Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
};
#[cfg(feature = "printing")]
use quote::ToTokens;

#[cfg(feature = "parsing")]
use crate::buffer::Cursor;
use crate::thread::ThreadBound;

/// The result of a Syn parser.
pub type Result<T> = std::result::Result<T, Error>;

/// Error returned when a Syn parser cannot parse the input tokens.
///
/// # Error reporting in proc macros
///
/// The correct way to report errors back to the compiler from a procedural
/// macro is by emitting an appropriately spanned invocation of
/// [`compile_error!`] in the generated code. This produces a better diagnostic
/// message than simply panicking the macro.
///
/// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
///
/// When parsing macro input, the [`parse_macro_input!`] macro handles the
/// conversion to `compile_error!` automatically.
///
/// ```
/// extern crate proc_macro;
///
/// use proc_macro::TokenStream;
/// use syn::{parse_macro_input, AttributeArgs, ItemFn};
///
/// # const IGNORE: &str = stringify! {
/// #[proc_macro_attribute]
/// # };
/// pub fn my_attr(args: TokenStream, input: TokenStream) -> TokenStream {
///     let args = parse_macro_input!(args as AttributeArgs);
///     let input = parse_macro_input!(input as ItemFn);
///
///     /* ... */
///     # TokenStream::new()
/// }
/// ```
///
/// For errors that arise later than the initial parsing stage, the
/// [`.to_compile_error()`] method can be used to perform an explicit conversion
/// to `compile_error!`.
///
/// [`.to_compile_error()`]: Error::to_compile_error
///
/// ```
/// # extern crate proc_macro;
/// #
/// # use proc_macro::TokenStream;
/// # use syn::{parse_macro_input, DeriveInput};
/// #
/// # const IGNORE: &str = stringify! {
/// #[proc_macro_derive(MyDerive)]
/// # };
/// pub fn my_derive(input: TokenStream) -> TokenStream {
///     let input = parse_macro_input!(input as DeriveInput);
///
///     // fn(DeriveInput) -> syn::Result<proc_macro2::TokenStream>
///     expand::my_derive(input)
///         .unwrap_or_else(|err| err.to_compile_error())
///         .into()
/// }
/// #
/// # mod expand {
/// #     use proc_macro2::TokenStream;
/// #     use syn::{DeriveInput, Result};
/// #
/// #     pub fn my_derive(input: DeriveInput) -> Result<TokenStream> {
/// #         unimplemented!()
/// #     }
/// # }
/// ```
#[derive(Clone)]
pub struct Error {
    messages: Vec<ErrorMessage>,
}

struct ErrorMessage {
    // Span is implemented as an index into a thread-local interner to keep the
    // size small. It is not safe to access from a different thread. We want
    // errors to be Send and Sync to play nicely with the Failure crate, so pin
    // the span we're given to its original thread and assume it is
    // Span::call_site if accessed from any other thread.
    start_span: ThreadBound<Span>,
    end_span: ThreadBound<Span>,
    message: String,
}

#[cfg(test)]
struct _Test
where
    Error: Send + Sync;

impl Error {
    /// Usually the [`ParseStream::error`] method will be used instead, which
    /// automatically uses the correct span from the current position of the
    /// parse stream.
    ///
    /// Use `Error::new` when the error needs to be triggered on some span other
    /// than where the parse stream is currently positioned.
    ///
    /// [`ParseStream::error`]: crate::parse::ParseBuffer::error
    ///
    /// # Example
    ///
    /// ```
    /// use syn::{Error, Ident, LitStr, Result, Token};
    /// use syn::parse::ParseStream;
    ///
    /// // Parses input that looks like `name = "string"` where the key must be
    /// // the identifier `name` and the value may be any string literal.
    /// // Returns the string literal.
    /// fn parse_name(input: ParseStream) -> Result<LitStr> {
    ///     let name_token: Ident = input.parse()?;
    ///     if name_token != "name" {
    ///         // Trigger an error not on the current position of the stream,
    ///         // but on the position of the unexpected identifier.
    ///         return Err(Error::new(name_token.span(), "expected `name`"));
    ///     }
    ///     input.parse::<Token![=]>()?;
    ///     let s: LitStr = input.parse()?;
    ///     Ok(s)
    /// }
    /// ```
    pub fn new<T: Display>(span: Span, message: T) -> Self {
        Error {
            messages: vec![ErrorMessage {
                start_span: ThreadBound::new(span),
                end_span: ThreadBound::new(span),
                message: message.to_string(),
            }],
        }
    }

    /// Creates an error with the specified message spanning the given syntax
    /// tree node.
    ///
    /// Unlike the `Error::new` constructor, this constructor takes an argument
    /// `tokens` which is a syntax tree node. This allows the resulting `Error`
    /// to attempt to span all tokens inside of `tokens`. While you would
    /// typically be able to use the `Spanned` trait with the above `Error::new`
    /// constructor, implementation limitations today mean that
    /// `Error::new_spanned` may provide a higher-quality error message on
    /// stable Rust.
    ///
    /// When in doubt it's recommended to stick to `Error::new` (or
    /// `ParseStream::error`)!
    #[cfg(feature = "printing")]
    pub fn new_spanned<T: ToTokens, U: Display>(tokens: T, message: U) -> Self {
        let mut iter = tokens.into_token_stream().into_iter();
        let start = iter.next().map_or_else(Span::call_site, |t| t.span());
        let end = iter.last().map_or(start, |t| t.span());
        Error {
            messages: vec![ErrorMessage {
                start_span: ThreadBound::new(start),
                end_span: ThreadBound::new(end),
                message: message.to_string(),
            }],
        }
    }

    /// The source location of the error.
    ///
    /// Spans are not thread-safe so this function returns `Span::call_site()`
    /// if called from a different thread than the one on which the `Error` was
    /// originally created.
    pub fn span(&self) -> Span {
        let start = match self.messages[0].start_span.get() {
            Some(span) => *span,
            None => return Span::call_site(),
        };
        let end = match self.messages[0].end_span.get() {
            Some(span) => *span,
            None => return Span::call_site(),
        };
        start.join(end).unwrap_or(start)
    }

    /// Render the error as an invocation of [`compile_error!`].
    ///
    /// The [`parse_macro_input!`] macro provides a convenient way to invoke
    /// this method correctly in a procedural macro.
    ///
    /// [`compile_error!`]: https://doc.rust-lang.org/std/macro.compile_error.html
    pub fn to_compile_error(&self) -> TokenStream {
        self.messages
            .iter()
            .map(ErrorMessage::to_compile_error)
            .collect()
    }

    /// Add another error message to self such that when `to_compile_error()` is
    /// called, both errors will be emitted together.
    pub fn combine(&mut self, another: Error) {
        self.messages.extend(another.messages)
    }
}

impl ErrorMessage {
    fn to_compile_error(&self) -> TokenStream {
        let start = self
            .start_span
            .get()
            .cloned()
            .unwrap_or_else(Span::call_site);
        let end = self.end_span.get().cloned().unwrap_or_else(Span::call_site);

        // compile_error!($message)
        TokenStream::from_iter(vec![
            TokenTree::Ident(Ident::new("compile_error", start)),
            TokenTree::Punct({
                let mut punct = Punct::new('!', Spacing::Alone);
                punct.set_span(start);
                punct
            }),
            TokenTree::Group({
                let mut group = Group::new(Delimiter::Brace, {
                    TokenStream::from_iter(vec![TokenTree::Literal({
                        let mut string = Literal::string(&self.message);
                        string.set_span(end);
                        string
                    })])
                });
                group.set_span(end);
                group
            }),
        ])
    }
}

#[cfg(feature = "parsing")]
pub fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error {
    if cursor.eof() {
        Error::new(scope, format!("unexpected end of input, {}", message))
    } else {
        let span = crate::buffer::open_span_of_group(cursor);
        Error::new(span, message)
    }
}

#[cfg(all(feature = "parsing", any(feature = "full", feature = "derive")))]
pub fn new2<T: Display>(start: Span, end: Span, message: T) -> Error {
    Error {
        messages: vec![ErrorMessage {
            start_span: ThreadBound::new(start),
            end_span: ThreadBound::new(end),
            message: message.to_string(),
        }],
    }
}

impl Debug for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        if self.messages.len() == 1 {
            formatter
                .debug_tuple("Error")
                .field(&self.messages[0])
                .finish()
        } else {
            formatter
                .debug_tuple("Error")
                .field(&self.messages)
                .finish()
        }
    }
}

impl Debug for ErrorMessage {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        Debug::fmt(&self.message, formatter)
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(&self.messages[0].message)
    }
}

impl Clone for ErrorMessage {
    fn clone(&self) -> Self {
        let start = self
            .start_span
            .get()
            .cloned()
            .unwrap_or_else(Span::call_site);
        let end = self.end_span.get().cloned().unwrap_or_else(Span::call_site);
        ErrorMessage {
            start_span: ThreadBound::new(start),
            end_span: ThreadBound::new(end),
            message: self.message.clone(),
        }
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        "parse error"
    }
}

impl From<LexError> for Error {
    fn from(err: LexError) -> Self {
        Error::new(Span::call_site(), format!("{:?}", err))
    }
}

impl IntoIterator for Error {
    type Item = Error;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            messages: self.messages.into_iter(),
        }
    }
}

pub struct IntoIter {
    messages: vec::IntoIter<ErrorMessage>,
}

impl Iterator for IntoIter {
    type Item = Error;

    fn next(&mut self) -> Option<Self::Item> {
        Some(Error {
            messages: vec![self.messages.next()?],
        })
    }
}

impl<'a> IntoIterator for &'a Error {
    type Item = Error;
    type IntoIter = Iter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        Iter {
            messages: self.messages.iter(),
        }
    }
}

pub struct Iter<'a> {
    messages: slice::Iter<'a, ErrorMessage>,
}

impl<'a> Iterator for Iter<'a> {
    type Item = Error;

    fn next(&mut self) -> Option<Self::Item> {
        Some(Error {
            messages: vec![self.messages.next()?.clone()],
        })
    }
}

impl Extend<Error> for Error {
    fn extend<T: IntoIterator<Item = Error>>(&mut self, iter: T) {
        for err in iter {
            self.combine(err);
        }
    }
}
<|endoftext|>


<commit_before>use parser_combinators::primitives::{Stream, State, Parser};
use parser_combinators::{ParseResult, parser};
use parser_combinators::combinator::{optional, ParserExt, sep_by, many, many1};
use parser_combinators::combinator::{chainl1, between, choice};

use util::join;

use super::Block;
use super::token::{Token, ParseToken};
use super::token::TokenType as Tok;
use super::token::lift;

type ChainFun = fn(Expression, Expression) -> Expression;

#[derive(Debug, Clone)]
pub struct Param {
    pub name: String,
    pub default_value: Option<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum Comparator {
    Eq,
    NotEq,
    Less,
    LessEq,
    Greater,
    GreaterEq,
}

#[derive(Debug, Clone)]
pub enum Expression {
    Name(String),
    Str(String),
    Num(String),
    New(Box<Expression>),
    Attr(Box<Expression>, String),
    Mul(Box<Expression>, Box<Expression>),
    Div(Box<Expression>, Box<Expression>),
    Add(Box<Expression>, Box<Expression>),
    Sub(Box<Expression>, Box<Expression>),
    Comparison(Comparator, Box<Expression>, Box<Expression>),
    Call(Box<Expression>, Vec<Expression>),
}

#[derive(Debug, Clone)]
pub enum LinkDest {
    Stream(Expression),
    Mapping(Expression, Expression),
}

#[derive(Debug, Clone)]
pub enum Link {
    One(String, LinkDest),
    Multi(Vec<(String, Option<String>)>, LinkDest),
}

#[derive(Debug, Clone)]
pub enum Statement {
    Element {
        name: String,
        classes: Vec<(String, Option<Expression>)>,
        attributes: Vec<(String, Expression)>,
        body: Vec<Statement>,
    },
    Text(String),
    Output(Expression),
    Store(String, Expression),
    Link(Vec<Link>),
    Condition(Vec<(Expression, Vec<Statement>)>, Option<Vec<Statement>>),
}

fn param<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Param, I>
{
    lift(Tok::Ident).and(optional(lift(Tok::Equals)
        .with(lift(Tok::String))))  // TODO(tailhook) more expressions
    .map(|((_, name, _), opt)| Param {
        name: String::from(name),
        default_value: opt.map(|x| String::from(x.1)),
    })
    .parse_state(input)
}

fn dash_name<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<String, I>
{
    sep_by::<Vec<_>, _, _>(
        lift(Tok::Ident).map(ParseToken::into_string),
        lift(Tok::Dash))
    .map(|x| join(x.iter(), "-"))
    .parse_state(input)
}

fn element_start<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<(String, Vec<(String, Option<Expression>)>), I>
{
    let element_head = lift(Tok::Ident)
        .map(ParseToken::into_string)
        .and(many::<Vec<_>, _>(lift(Tok::Dot)
            .with(parser(dash_name))
            .and(optional(lift(Tok::Question)
                .with(between(lift(Tok::OpenParen), lift(Tok::CloseParen),
                    parser(expression)))))
        ));
    let div_head = many1::<Vec<_>, _>(
        lift(Tok::Dot)
        .with(parser(dash_name))
        .and(optional(lift(Tok::Question)
            .with(between(lift(Tok::OpenParen), lift(Tok::CloseParen),
                parser(expression))))))
        .map(|items| (String::from("div"), items));
    element_head
    .or(div_head)
    .parse_state(input)
}

fn attributes<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<(String, Expression)>>, I>
{
    optional(lift(Tok::OpenBracket)
        .with(sep_by::<Vec<_>, _, _>(
            lift(Tok::Ident).map(ParseToken::into_string)
                .skip(lift(Tok::Equals))
                .and(parser(expression)),
            lift(Tok::Comma)))
        .skip(lift(Tok::CloseBracket)))
    .parse_state(input)
}

fn element<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    parser(element_start)
    .and(parser(attributes))
    .skip(lift(Tok::Newline))
    .and(parser(chunk))
    .map(|(((name, classes), opt_attributes), opt_body)| Statement::Element {
        name: name,
        classes: classes,
        attributes: opt_attributes.unwrap_or(vec!()),
        body: opt_body.unwrap_or(vec!()),
        })
    .parse_state(input)
}

fn literal<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::String).skip(lift(Tok::Newline))
    .map(|tok| Statement::Text(tok.unescape()))
    .parse_state(input)
}

fn expr_params<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<Expression>>, I>
{
    optional(lift(Tok::OpenParen)
        .with(sep_by::<Vec<_>, _, _>(
            parser(expression),
            lift(Tok::Comma)))
        .skip(lift(Tok::CloseParen)))
    .parse_state(input)
}

fn call<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    lift(Tok::Ident).map(ParseToken::into_string)
    .and(many::<Vec<_>, _>(lift(Tok::Dot)
        .with(lift(Tok::Ident).map(ParseToken::into_string))))
    .map(|(head, tail)| {
        tail.iter().fold(Expression::Name(head),
            |expr, name| Expression::Attr(Box::new(expr), name.clone()))
    })
    .and(parser(expr_params))
    .map(|(mut expr, opt_paren)| {
        if let Some(paren) = opt_paren {
            expr = Expression::Call(Box::new(expr), paren);
        }
        expr
    })
    .parse_state(input)
}
fn atom<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    parser(call)
    .or(lift(Tok::New).with(parser(call))
        .map(|x| Expression::New(Box::new(x))))
    .or(lift(Tok::String)
        .map(ParseToken::into_string).map(Expression::Str))
    .or(lift(Tok::Number)
        .map(ParseToken::into_string).map(Expression::Num))
    .parse_state(input)
}

fn multiply(l: Expression, r: Expression) -> Expression {
    Expression::Mul(Box::new(l), Box::new(r))
}
fn divide(l: Expression, r: Expression) -> Expression {
    Expression::Div(Box::new(l), Box::new(r))
}
fn add(l: Expression, r: Expression) -> Expression {
    Expression::Add(Box::new(l), Box::new(r))
}
fn subtract(l: Expression, r: Expression) -> Expression {
    Expression::Sub(Box::new(l), Box::new(r))
}

fn sum<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    let factor = lift(Tok::Multiply).map(|_| multiply as ChainFun)
                 .or(lift(Tok::Divide).map(|_| divide as ChainFun));
    let sum = lift(Tok::Plus).map(|_| add as ChainFun)
              .or(lift(Tok::Dash).map(|_| subtract as ChainFun));
    let factor = chainl1(parser(atom), factor);
    chainl1(factor, sum)
    .parse_state(input)
}

fn expression<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    parser(sum).and(optional(many(choice([
        lift(Tok::Eq),
        lift(Tok::NotEq),
        lift(Tok::Less),
        lift(Tok::LessEq),
        lift(Tok::Greater),
        lift(Tok::GreaterEq),
        ]).and(parser(sum)))))
    .map(|(expr, opt_tails): (Expression, Option<Vec<(_, Expression)>>)| {
        let mut expr = expr;
        let mut prev = expr.clone();
        if let Some(tail) = opt_tails {
            for ((tok, _, _), value) in tail {
                let comp = match tok {
                    Tok::Eq => Comparator::Eq,
                    Tok::NotEq => Comparator::NotEq,
                    Tok::Less => Comparator::Less,
                    Tok::LessEq => Comparator::LessEq,
                    Tok::Greater => Comparator::Greater,
                    Tok::GreaterEq => Comparator::GreaterEq,
                    _ => unreachable!(),
                };
                expr = Expression::Comparison(comp,
                    Box::new(prev),
                    Box::new(value.clone()));
                prev = value;
            }
        }
        return expr;
    })
    .parse_state(input)
}

fn store<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::Store)
    .with(lift(Tok::Ident).map(ParseToken::into_string))
    .skip(lift(Tok::Equals))
    .and(parser(expression)).skip(lift(Tok::Newline))
    .map(|(name, value)| Statement::Store(name, value))
    .parse_state(input)
}

fn link_dest<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<LinkDest, I>
{
    parser(expression)
    .and(optional(lift(Tok::ArrowRight)
                  .with(parser(expression))))
    .map(|(x, y)| match y {
        Some(dest) => LinkDest::Mapping(x, dest),
        None => LinkDest::Stream(x),
    })
    .parse_state(input)
}

fn multi_link<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Link, I>
{
    lift(Tok::OpenBrace)
    .with(sep_by::<Vec<_>, _, _>(
        lift(Tok::Ident).map(ParseToken::into_string)
        .and(optional(lift(Tok::Colon).with(
            lift(Tok::Ident).map(ParseToken::into_string)))),
        lift(Tok::Comma)))
    .skip(lift(Tok::CloseBrace))
    .skip(lift(Tok::Equals))
    .and(parser(link_dest))
    .map(|(lst, dest)| Link::Multi(lst, dest))
    .parse_state(input)
}

fn single_link<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Link, I>
{
    lift(Tok::Ident)
    .skip(lift(Tok::Equals))
    .and(parser(link_dest))
    .map(|(name, dest)| Link::One(name.into_string(), dest))
    .parse_state(input)
}

fn link<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::Link)
    .with(sep_by::<Vec<_>, _, _>(
        parser(single_link).or(parser(multi_link)),
        lift(Tok::Comma)))
    .skip(lift(Tok::Newline))
    .map(Statement::Link)
    .parse_state(input)
}

fn condition<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::If)
    .with(parser(expression))
    .skip(lift(Tok::Colon))
    .skip(lift(Tok::Newline))
    .and(parser(chunk))
    .and(optional(many::<Vec<_>,_>(
        lift(Tok::Elif)
        .with(parser(expression))
        .skip(lift(Tok::Colon))
        .skip(lift(Tok::Newline))
        .and(parser(chunk))
        )))
    .and(optional(lift(Tok::Else)
        .skip(lift(Tok::Colon))
        .skip(lift(Tok::Newline))
        .with(parser(chunk))
        ))
    .map(|(((cond, body), opt_elifs), opt_else)| Statement::Condition(
        vec![(cond, body.unwrap_or(vec!()))]
        .into_iter()
        .chain(opt_elifs.map(
            |v| v.into_iter()
                 .map(|(expr, opt_body)| (expr, opt_body.unwrap_or(vec!())))
                 .collect()
            ).unwrap_or(vec!()).into_iter())
        .collect(),
        opt_else.and_then(|x| x)))
    .parse_state(input)
}

fn output<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::Equals)
    .with(parser(expression))
    .skip(lift(Tok::Newline))
    .map(Statement::Output)
    .parse_state(input)
}

fn statement<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    parser(element)
    .or(parser(literal))
    .or(parser(store))
    .or(parser(link))
    .or(parser(condition))
    .or(parser(output))
    .parse_state(input)
}

fn params<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<Param>>, I>
{
    optional(lift(Tok::OpenParen)
        .with(sep_by::<Vec<_>, _, _>(parser(param), lift(Tok::Comma)))
        .skip(lift(Tok::CloseParen)))
    .parse_state(input)
}

fn events<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<String>>, I>
{
    optional(lift(Tok::Events)
        .with(sep_by::<Vec<_>, _, _>(
            lift(Tok::Ident).map(ParseToken::into_string),
            lift(Tok::Comma),
        )))
    .parse_state(input)
}

pub fn chunk<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<Statement>>, I>
{
    optional(
        lift(Tok::Indent)
        .with(many1(parser(statement)))
        .skip(lift(Tok::Dedent)))
    .parse_state(input)
}

pub fn block<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Block, I>
{
    lift(Tok::Ident).map(ParseToken::into_string)
    .and(parser(params))
    .and(parser(events))
    .skip(lift(Tok::Colon))
    .skip(lift(Tok::Newline))
    .and(parser(chunk))
    .map(|(((name, opt_params), opt_events), opt_stmtlist)| {
        Block::Html {
            name: name,
            params: opt_params.unwrap_or(vec!()),
            events: opt_events.unwrap_or(vec!()),
            statements: opt_stmtlist.unwrap_or(vec!()),
        }
    })
    .parse_state(input)
}

<commit_msg>Fix double-escaping of strings<commit_after>use parser_combinators::primitives::{Stream, State, Parser};
use parser_combinators::{ParseResult, parser};
use parser_combinators::combinator::{optional, ParserExt, sep_by, many, many1};
use parser_combinators::combinator::{chainl1, between, choice};

use util::join;

use super::Block;
use super::token::{Token, ParseToken};
use super::token::TokenType as Tok;
use super::token::lift;

type ChainFun = fn(Expression, Expression) -> Expression;

#[derive(Debug, Clone)]
pub struct Param {
    pub name: String,
    pub default_value: Option<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum Comparator {
    Eq,
    NotEq,
    Less,
    LessEq,
    Greater,
    GreaterEq,
}

#[derive(Debug, Clone)]
pub enum Expression {
    Name(String),
    Str(String),
    Num(String),
    New(Box<Expression>),
    Attr(Box<Expression>, String),
    Mul(Box<Expression>, Box<Expression>),
    Div(Box<Expression>, Box<Expression>),
    Add(Box<Expression>, Box<Expression>),
    Sub(Box<Expression>, Box<Expression>),
    Comparison(Comparator, Box<Expression>, Box<Expression>),
    Call(Box<Expression>, Vec<Expression>),
}

#[derive(Debug, Clone)]
pub enum LinkDest {
    Stream(Expression),
    Mapping(Expression, Expression),
}

#[derive(Debug, Clone)]
pub enum Link {
    One(String, LinkDest),
    Multi(Vec<(String, Option<String>)>, LinkDest),
}

#[derive(Debug, Clone)]
pub enum Statement {
    Element {
        name: String,
        classes: Vec<(String, Option<Expression>)>,
        attributes: Vec<(String, Expression)>,
        body: Vec<Statement>,
    },
    Text(String),
    Output(Expression),
    Store(String, Expression),
    Link(Vec<Link>),
    Condition(Vec<(Expression, Vec<Statement>)>, Option<Vec<Statement>>),
}

fn param<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Param, I>
{
    lift(Tok::Ident).and(optional(lift(Tok::Equals)
        .with(lift(Tok::String))))  // TODO(tailhook) more expressions
    .map(|((_, name, _), opt)| Param {
        name: String::from(name),
        default_value: opt.map(ParseToken::unescape)
    })
    .parse_state(input)
}

fn dash_name<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<String, I>
{
    sep_by::<Vec<_>, _, _>(
        lift(Tok::Ident).map(ParseToken::into_string),
        lift(Tok::Dash))
    .map(|x| join(x.iter(), "-"))
    .parse_state(input)
}

fn element_start<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<(String, Vec<(String, Option<Expression>)>), I>
{
    let element_head = lift(Tok::Ident)
        .map(ParseToken::into_string)
        .and(many::<Vec<_>, _>(lift(Tok::Dot)
            .with(parser(dash_name))
            .and(optional(lift(Tok::Question)
                .with(between(lift(Tok::OpenParen), lift(Tok::CloseParen),
                    parser(expression)))))
        ));
    let div_head = many1::<Vec<_>, _>(
        lift(Tok::Dot)
        .with(parser(dash_name))
        .and(optional(lift(Tok::Question)
            .with(between(lift(Tok::OpenParen), lift(Tok::CloseParen),
                parser(expression))))))
        .map(|items| (String::from("div"), items));
    element_head
    .or(div_head)
    .parse_state(input)
}

fn attributes<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<(String, Expression)>>, I>
{
    optional(lift(Tok::OpenBracket)
        .with(sep_by::<Vec<_>, _, _>(
            lift(Tok::Ident).map(ParseToken::into_string)
                .skip(lift(Tok::Equals))
                .and(parser(expression)),
            lift(Tok::Comma)))
        .skip(lift(Tok::CloseBracket)))
    .parse_state(input)
}

fn element<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    parser(element_start)
    .and(parser(attributes))
    .skip(lift(Tok::Newline))
    .and(parser(chunk))
    .map(|(((name, classes), opt_attributes), opt_body)| Statement::Element {
        name: name,
        classes: classes,
        attributes: opt_attributes.unwrap_or(vec!()),
        body: opt_body.unwrap_or(vec!()),
        })
    .parse_state(input)
}

fn literal<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::String).skip(lift(Tok::Newline))
    .map(|tok| Statement::Text(tok.unescape()))
    .parse_state(input)
}

fn expr_params<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<Expression>>, I>
{
    optional(lift(Tok::OpenParen)
        .with(sep_by::<Vec<_>, _, _>(
            parser(expression),
            lift(Tok::Comma)))
        .skip(lift(Tok::CloseParen)))
    .parse_state(input)
}

fn call<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    lift(Tok::Ident).map(ParseToken::into_string)
    .and(many::<Vec<_>, _>(lift(Tok::Dot)
        .with(lift(Tok::Ident).map(ParseToken::into_string))))
    .map(|(head, tail)| {
        tail.iter().fold(Expression::Name(head),
            |expr, name| Expression::Attr(Box::new(expr), name.clone()))
    })
    .and(parser(expr_params))
    .map(|(mut expr, opt_paren)| {
        if let Some(paren) = opt_paren {
            expr = Expression::Call(Box::new(expr), paren);
        }
        expr
    })
    .parse_state(input)
}
fn atom<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    parser(call)
    .or(lift(Tok::New).with(parser(call))
        .map(|x| Expression::New(Box::new(x))))
    .or(lift(Tok::String)
        .map(ParseToken::unescape).map(Expression::Str))
    .or(lift(Tok::Number)
        .map(ParseToken::into_string).map(Expression::Num))
    .parse_state(input)
}

fn multiply(l: Expression, r: Expression) -> Expression {
    Expression::Mul(Box::new(l), Box::new(r))
}
fn divide(l: Expression, r: Expression) -> Expression {
    Expression::Div(Box::new(l), Box::new(r))
}
fn add(l: Expression, r: Expression) -> Expression {
    Expression::Add(Box::new(l), Box::new(r))
}
fn subtract(l: Expression, r: Expression) -> Expression {
    Expression::Sub(Box::new(l), Box::new(r))
}

fn sum<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    let factor = lift(Tok::Multiply).map(|_| multiply as ChainFun)
                 .or(lift(Tok::Divide).map(|_| divide as ChainFun));
    let sum = lift(Tok::Plus).map(|_| add as ChainFun)
              .or(lift(Tok::Dash).map(|_| subtract as ChainFun));
    let factor = chainl1(parser(atom), factor);
    chainl1(factor, sum)
    .parse_state(input)
}

fn expression<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Expression, I>
{
    parser(sum).and(optional(many(choice([
        lift(Tok::Eq),
        lift(Tok::NotEq),
        lift(Tok::Less),
        lift(Tok::LessEq),
        lift(Tok::Greater),
        lift(Tok::GreaterEq),
        ]).and(parser(sum)))))
    .map(|(expr, opt_tails): (Expression, Option<Vec<(_, Expression)>>)| {
        let mut expr = expr;
        let mut prev = expr.clone();
        if let Some(tail) = opt_tails {
            for ((tok, _, _), value) in tail {
                let comp = match tok {
                    Tok::Eq => Comparator::Eq,
                    Tok::NotEq => Comparator::NotEq,
                    Tok::Less => Comparator::Less,
                    Tok::LessEq => Comparator::LessEq,
                    Tok::Greater => Comparator::Greater,
                    Tok::GreaterEq => Comparator::GreaterEq,
                    _ => unreachable!(),
                };
                expr = Expression::Comparison(comp,
                    Box::new(prev),
                    Box::new(value.clone()));
                prev = value;
            }
        }
        return expr;
    })
    .parse_state(input)
}

fn store<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::Store)
    .with(lift(Tok::Ident).map(ParseToken::into_string))
    .skip(lift(Tok::Equals))
    .and(parser(expression)).skip(lift(Tok::Newline))
    .map(|(name, value)| Statement::Store(name, value))
    .parse_state(input)
}

fn link_dest<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<LinkDest, I>
{
    parser(expression)
    .and(optional(lift(Tok::ArrowRight)
                  .with(parser(expression))))
    .map(|(x, y)| match y {
        Some(dest) => LinkDest::Mapping(x, dest),
        None => LinkDest::Stream(x),
    })
    .parse_state(input)
}

fn multi_link<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Link, I>
{
    lift(Tok::OpenBrace)
    .with(sep_by::<Vec<_>, _, _>(
        lift(Tok::Ident).map(ParseToken::into_string)
        .and(optional(lift(Tok::Colon).with(
            lift(Tok::Ident).map(ParseToken::into_string)))),
        lift(Tok::Comma)))
    .skip(lift(Tok::CloseBrace))
    .skip(lift(Tok::Equals))
    .and(parser(link_dest))
    .map(|(lst, dest)| Link::Multi(lst, dest))
    .parse_state(input)
}

fn single_link<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Link, I>
{
    lift(Tok::Ident)
    .skip(lift(Tok::Equals))
    .and(parser(link_dest))
    .map(|(name, dest)| Link::One(name.into_string(), dest))
    .parse_state(input)
}

fn link<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::Link)
    .with(sep_by::<Vec<_>, _, _>(
        parser(single_link).or(parser(multi_link)),
        lift(Tok::Comma)))
    .skip(lift(Tok::Newline))
    .map(Statement::Link)
    .parse_state(input)
}

fn condition<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::If)
    .with(parser(expression))
    .skip(lift(Tok::Colon))
    .skip(lift(Tok::Newline))
    .and(parser(chunk))
    .and(optional(many::<Vec<_>,_>(
        lift(Tok::Elif)
        .with(parser(expression))
        .skip(lift(Tok::Colon))
        .skip(lift(Tok::Newline))
        .and(parser(chunk))
        )))
    .and(optional(lift(Tok::Else)
        .skip(lift(Tok::Colon))
        .skip(lift(Tok::Newline))
        .with(parser(chunk))
        ))
    .map(|(((cond, body), opt_elifs), opt_else)| Statement::Condition(
        vec![(cond, body.unwrap_or(vec!()))]
        .into_iter()
        .chain(opt_elifs.map(
            |v| v.into_iter()
                 .map(|(expr, opt_body)| (expr, opt_body.unwrap_or(vec!())))
                 .collect()
            ).unwrap_or(vec!()).into_iter())
        .collect(),
        opt_else.and_then(|x| x)))
    .parse_state(input)
}

fn output<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    lift(Tok::Equals)
    .with(parser(expression))
    .skip(lift(Tok::Newline))
    .map(Statement::Output)
    .parse_state(input)
}

fn statement<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Statement, I>
{
    parser(element)
    .or(parser(literal))
    .or(parser(store))
    .or(parser(link))
    .or(parser(condition))
    .or(parser(output))
    .parse_state(input)
}

fn params<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<Param>>, I>
{
    optional(lift(Tok::OpenParen)
        .with(sep_by::<Vec<_>, _, _>(parser(param), lift(Tok::Comma)))
        .skip(lift(Tok::CloseParen)))
    .parse_state(input)
}

fn events<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<String>>, I>
{
    optional(lift(Tok::Events)
        .with(sep_by::<Vec<_>, _, _>(
            lift(Tok::Ident).map(ParseToken::into_string),
            lift(Tok::Comma),
        )))
    .parse_state(input)
}

pub fn chunk<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Option<Vec<Statement>>, I>
{
    optional(
        lift(Tok::Indent)
        .with(many1(parser(statement)))
        .skip(lift(Tok::Dedent)))
    .parse_state(input)
}

pub fn block<'a, I: Stream<Item=Token<'a>>>(input: State<I>)
    -> ParseResult<Block, I>
{
    lift(Tok::Ident).map(ParseToken::into_string)
    .and(parser(params))
    .and(parser(events))
    .skip(lift(Tok::Colon))
    .skip(lift(Tok::Newline))
    .and(parser(chunk))
    .map(|(((name, opt_params), opt_events), opt_stmtlist)| {
        Block::Html {
            name: name,
            params: opt_params.unwrap_or(vec!()),
            events: opt_events.unwrap_or(vec!()),
            statements: opt_stmtlist.unwrap_or(vec!()),
        }
    })
    .parse_state(input)
}

<|endoftext|>


<commit_before>use std::mem;
use std::num::One;
use num::Integer;

pub fn triangle<T: One + Add<T, T>>() -> Triangle<T> {
    let one: T = One::one();
    Triangle { diff: one + one, next: one }
}

pub struct Triangle<T> { diff: T, next: T }

impl<T: Add<T, T> + One> Iterator<T> for Triangle<T> {
    #[inline]
    fn next(&mut self) -> Option<T> {
        let new_next = self.next + self.diff;
        self.diff = self.diff + One::one();
        Some(mem::replace(&mut self.next, new_next))
    }
}

pub fn prim_pythagorean(m: uint) -> PrimPythagoreanIterator {
    let n0 = if m.is_even() { 1 } else { 2 };
    PrimPythagoreanIterator { m: m, n: n0 }
}

pub struct PrimPythagoreanIterator { m: uint, n: uint }

impl Iterator<(uint, uint, uint)> for PrimPythagoreanIterator {
    fn next(&mut self) -> Option<(uint, uint, uint)> {
        let m = self.m;
        while self.n < m {
            let n = self.n;
            self.n += 2;

            if m.gcd(&n) != 1 { continue }

            let (m2, n2)  = (m * m, n * n);
            let (a, b, c) = (m2 - n2, 2 * m * n, m2 + n2);
            if a < b {
                return Some((a, b, c))
            } else {
                return Some((b, a, c))
            }
        };
        None
    }
}

#[cfg(test)]
mod tests {
    use std::fmt;
    fn check<T: Eq + fmt::Show, I: Iterator<T>>(expected: &[T], mut it: I) {
        assert_eq!(expected, it.collect::<Vec<_>>().as_slice());
    }

    #[test]
    fn test_triangle() {
        let tri = &[1u, 3, 6, 10, 15, 21];
        check(tri, super::triangle::<uint>().take(tri.len()));
    }

    #[test]
    fn test_prim_pythagorean_iterator() {
        fn check(m: uint, v: &[(uint, uint, uint)]) {
            assert_eq!(super::prim_pythagorean(m).collect::<Vec<_>>().as_slice(), v);
        }

        check(2, [(3, 4, 5)]);
        check(3, [(5, 12, 13)]);
        check(4, [(8, 15, 17), (7, 24, 25)]);
    }
}
<commit_msg>remove unused struct<commit_after>use num::Integer;

pub fn prim_pythagorean(m: uint) -> PrimPythagoreanIterator {
    let n0 = if m.is_even() { 1 } else { 2 };
    PrimPythagoreanIterator { m: m, n: n0 }
}

pub struct PrimPythagoreanIterator { m: uint, n: uint }

impl Iterator<(uint, uint, uint)> for PrimPythagoreanIterator {
    fn next(&mut self) -> Option<(uint, uint, uint)> {
        let m = self.m;
        while self.n < m {
            let n = self.n;
            self.n += 2;

            if m.gcd(&n) != 1 { continue }

            let (m2, n2)  = (m * m, n * n);
            let (a, b, c) = (m2 - n2, 2 * m * n, m2 + n2);
            if a < b {
                return Some((a, b, c))
            } else {
                return Some((b, a, c))
            }
        };
        None
    }
}

#[cfg(test)]
mod tests {
    use std::fmt;
    fn check<T: Eq + fmt::Show, I: Iterator<T>>(expected: &[T], mut it: I) {
        assert_eq!(expected, it.collect::<Vec<_>>().as_slice());
    }

    #[test]
    fn test_prim_pythagorean_iterator() {
        fn check(m: uint, v: &[(uint, uint, uint)]) {
            assert_eq!(super::prim_pythagorean(m).collect::<Vec<_>>().as_slice(), v);
        }

        check(2, [(3, 4, 5)]);
        check(3, [(5, 12, 13)]);
        check(4, [(8, 15, 17), (7, 24, 25)]);
    }
}
<|endoftext|>


<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 use dom::bindings::codegen::Bindings::TestBindingProxyBinding::TestBindingProxyMethods;
 use dom::bindings::utils::Reflector;
 use util::str::DOMString;

 #[dom_struct]
 pub struct TestBindingProxy {
     reflector_: Reflector
 }

 impl<'a> TestBindingProxyMethods for &'a TestBindingProxy {

     fn GetNamedItem(self, _: DOMString) -> DOMString {"".to_owned()}
     fn SetNamedItem(self, _: DOMString, _: DOMString) -> () {}
     fn GetItem(self, _: u32) -> DOMString {"".to_owned()}
     fn SetItem(self, _: u32, _: DOMString) -> () {}
     fn RemoveItem(self, _: DOMString) -> () {}
     fn Stringifier(self) -> DOMString {"".to_owned()}
     fn NamedCreator(self, _: DOMString, _: DOMString) -> () {}
     fn IndexedGetter(self, _: u32, _: &mut bool) -> DOMString {"".to_owned()}
     fn NamedDeleter(self, _: DOMString) -> () {}
     fn IndexedSetter(self, _: u32, _: DOMString) -> () {}
     fn NamedSetter(self, _: DOMString, _: DOMString) -> () {}
     fn IndexedCreator(self, _: u32, _: DOMString) -> () {}
     fn NamedGetter(self, _: DOMString, _: &mut bool) -> DOMString {"".to_owned()}

 }
<commit_msg>Auto merge of #6460 - michaelwu:fix-indentation, r=Ms2ger<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::TestBindingProxyBinding::TestBindingProxyMethods;
use dom::bindings::utils::Reflector;
use util::str::DOMString;

#[dom_struct]
pub struct TestBindingProxy {
    reflector_: Reflector
}

impl<'a> TestBindingProxyMethods for &'a TestBindingProxy {

    fn GetNamedItem(self, _: DOMString) -> DOMString {"".to_owned()}
    fn SetNamedItem(self, _: DOMString, _: DOMString) -> () {}
    fn GetItem(self, _: u32) -> DOMString {"".to_owned()}
    fn SetItem(self, _: u32, _: DOMString) -> () {}
    fn RemoveItem(self, _: DOMString) -> () {}
    fn Stringifier(self) -> DOMString {"".to_owned()}
    fn NamedCreator(self, _: DOMString, _: DOMString) -> () {}
    fn IndexedGetter(self, _: u32, _: &mut bool) -> DOMString {"".to_owned()}
    fn NamedDeleter(self, _: DOMString) -> () {}
    fn IndexedSetter(self, _: u32, _: DOMString) -> () {}
    fn NamedSetter(self, _: DOMString, _: DOMString) -> () {}
    fn IndexedCreator(self, _: u32, _: DOMString) -> () {}
    fn NamedGetter(self, _: DOMString, _: &mut bool) -> DOMString {"".to_owned()}

}
<|endoftext|>


<commit_before>//! `completions` crate provides utilities for generating completions of user input.

#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]

mod completions;
mod config;
mod context;
mod item;
mod render;

#[cfg(test)]
mod tests;
mod snippet;

use ide_db::{
    base_db::FilePosition,
    helpers::mod_path_to_ast,
    imports::{
        import_assets::NameToImport,
        insert_use::{self, ImportScope},
    },
    items_locator, RootDatabase,
};
use syntax::algo;
use text_edit::TextEdit;

use crate::{
    completions::Completions,
    context::{
        CompletionAnalysis, CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx,
        PathKind,
    },
};

pub use crate::{
    config::{CallableSnippets, CompletionConfig},
    item::{
        CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
    },
    snippet::{Snippet, SnippetScope},
};

//FIXME: split the following feature into fine-grained features.

// Feature: Magic Completions
//
// In addition to usual reference completion, rust-analyzer provides some ✨magic✨
// completions as well:
//
// Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor
// is placed at the appropriate position. Even though `if` is easy to type, you
// still want to complete it, to get ` { }` for free! `return` is inserted with a
// space or `;` depending on the return type of the function.
//
// When completing a function call, `()` are automatically inserted. If a function
// takes arguments, the cursor is positioned inside the parenthesis.
//
// There are postfix completions, which can be triggered by typing something like
// `foo().if`. The word after `.` determines postfix completion. Possible variants are:
//
// - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result`
// - `expr.match` -> `match expr {}`
// - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result`
// - `expr.ref` -> `&expr`
// - `expr.refm` -> `&mut expr`
// - `expr.let` -> `let $0 = expr;`
// - `expr.letm` -> `let mut $0 = expr;`
// - `expr.not` -> `!expr`
// - `expr.dbg` -> `dbg!(expr)`
// - `expr.dbgr` -> `dbg!(&expr)`
// - `expr.call` -> `(expr)`
//
// There also snippet completions:
//
// .Expressions
// - `pd` -> `eprintln!(" = {:?}", );`
// - `ppd` -> `eprintln!(" = {:#?}", );`
//
// .Items
// - `tfn` -> `#[test] fn feature(){}`
// - `tmod` ->
// ```rust
// #[cfg(test)]
// mod tests {
//     use super::*;
//
//     #[test]
//     fn test_name() {}
// }
// ```
//
// And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities.
// Those are the additional completion options with automatic `use` import and options from all project importable items,
// fuzzy matched against the completion input.
//
// image::https://user-images.githubusercontent.com/48062697/113020667-b72ab880-917a-11eb-8778-716cf26a0eb3.gif[]

/// Main entry point for completion. We run completion as a two-phase process.
///
/// First, we look at the position and collect a so-called `CompletionContext.
/// This is a somewhat messy process, because, during completion, syntax tree is
/// incomplete and can look really weird.
///
/// Once the context is collected, we run a series of completion routines which
/// look at the context and produce completion items. One subtlety about this
/// phase is that completion engine should not filter by the substring which is
/// already present, it should give all possible variants for the identifier at
/// the caret. In other words, for
///
/// ```no_run
/// fn f() {
///     let foo = 92;
///     let _ = bar$0
/// }
/// ```
///
/// `foo` *should* be present among the completion variants. Filtering by
/// identifier prefix/fuzzy match should be done higher in the stack, together
/// with ordering of completions (currently this is done by the client).
///
/// # Speculative Completion Problem
///
/// There's a curious unsolved problem in the current implementation. Often, you
/// want to compute completions on a *slightly different* text document.
///
/// In the simplest case, when the code looks like `let x = `, you want to
/// insert a fake identifier to get a better syntax tree: `let x = complete_me`.
///
/// We do this in `CompletionContext`, and it works OK-enough for *syntax*
/// analysis. However, we might want to, eg, ask for the type of `complete_me`
/// variable, and that's where our current infrastructure breaks down. salsa
/// doesn't allow such "phantom" inputs.
///
/// Another case where this would be instrumental is macro expansion. We want to
/// insert a fake ident and re-expand code. There's `expand_speculative` as a
/// work-around for this.
///
/// A different use-case is completion of injection (examples and links in doc
/// comments). When computing completion for a path in a doc-comment, you want
/// to inject a fake path expression into the item being documented and complete
/// that.
///
/// IntelliJ has CodeFragment/Context infrastructure for that. You can create a
/// temporary PSI node, and say that the context ("parent") of this node is some
/// existing node. Asking for, eg, type of this `CodeFragment` node works
/// correctly, as the underlying infrastructure makes use of contexts to do
/// analysis.
pub fn completions(
    db: &RootDatabase,
    config: &CompletionConfig,
    position: FilePosition,
    trigger_character: Option<char>,
) -> Option<Vec<CompletionItem>> {
    let (ctx, analysis) = &CompletionContext::new(db, position, config)?;
    let mut completions = Completions::default();

    // prevent `(` from triggering unwanted completion noise
    if trigger_character == Some('(') {
        if let CompletionAnalysis::NameRef(NameRefContext { kind, .. }) = &analysis {
            if let NameRefKind::Path(
                path_ctx @ PathCompletionCtx { kind: PathKind::Vis { has_in_token }, .. },
            ) = kind
            {
                completions::vis::complete_vis_path(&mut completions, ctx, path_ctx, has_in_token);
            }
        }
        // prevent `(` from triggering unwanted completion noise
        return Some(completions.into());
    }

    {
        let acc = &mut completions;

        match &analysis {
            CompletionAnalysis::Name(name_ctx) => completions::complete_name(acc, ctx, name_ctx),
            CompletionAnalysis::NameRef(name_ref_ctx) => {
                completions::complete_name_ref(acc, ctx, name_ref_ctx)
            }
            CompletionAnalysis::Lifetime(lifetime_ctx) => {
                completions::lifetime::complete_label(acc, ctx, lifetime_ctx);
                completions::lifetime::complete_lifetime(acc, ctx, lifetime_ctx);
            }
            CompletionAnalysis::String { original, expanded: Some(expanded) } => {
                completions::extern_abi::complete_extern_abi(acc, ctx, expanded);
                completions::format_string::format_string(acc, ctx, original, expanded);
                completions::env_vars::complete_cargo_env_vars(acc, expanded);
            }
            CompletionAnalysis::UnexpandedAttrTT {
                colon_prefix,
                fake_attribute_under_caret: Some(attr),
            } => {
                completions::attribute::complete_known_attribute_input(
                    acc,
                    ctx,
                    colon_prefix,
                    attr,
                );
            }
            CompletionAnalysis::UnexpandedAttrTT { .. } | CompletionAnalysis::String { .. } => (),
        }
    }

    Some(completions.into())
}

/// Resolves additional completion data at the position given.
/// This is used for import insertion done via completions like flyimport and custom user snippets.
pub fn resolve_completion_edits(
    db: &RootDatabase,
    config: &CompletionConfig,
    FilePosition { file_id, offset }: FilePosition,
    imports: impl IntoIterator<Item = (String, String)>,
) -> Option<Vec<TextEdit>> {
    let _p = profile::span("resolve_completion_edits");
    let sema = hir::Semantics::new(db);

    let original_file = sema.parse(file_id);
    let original_token =
        syntax::AstNode::syntax(&original_file).token_at_offset(offset).left_biased()?;
    let position_for_import = &original_token.parent()?;
    let scope = ImportScope::find_insert_use_container(position_for_import, &sema)?;

    let current_module = sema.scope(position_for_import)?.module();
    let current_crate = current_module.krate();
    let new_ast = scope.clone_for_update();
    let mut import_insert = TextEdit::builder();

    imports.into_iter().for_each(|(full_import_path, imported_name)| {
        let items_with_name = items_locator::items_with_name(
            &sema,
            current_crate,
            NameToImport::exact_case_sensitive(imported_name),
            items_locator::AssocItemSearch::Include,
            Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
        );
        let import = items_with_name
            .filter_map(|candidate| {
                current_module.find_use_path_prefixed(
                    db,
                    candidate,
                    config.insert_use.prefix_kind,
                    config.prefer_no_std,
                )
            })
            .find(|mod_path| mod_path.to_string() == full_import_path);
        if let Some(import_path) = import {
            insert_use::insert_use(&new_ast, mod_path_to_ast(&import_path), &config.insert_use);
        }
    });

    algo::diff(scope.as_syntax_node(), new_ast.as_syntax_node()).into_text_edit(&mut import_insert);
    Some(vec![import_insert.finish()])
}
<commit_msg>Pass context to env vars completion<commit_after>//! `completions` crate provides utilities for generating completions of user input.

#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]

mod completions;
mod config;
mod context;
mod item;
mod render;

#[cfg(test)]
mod tests;
mod snippet;

use ide_db::{
    base_db::FilePosition,
    helpers::mod_path_to_ast,
    imports::{
        import_assets::NameToImport,
        insert_use::{self, ImportScope},
    },
    items_locator, RootDatabase,
};
use syntax::algo;
use text_edit::TextEdit;

use crate::{
    completions::Completions,
    context::{
        CompletionAnalysis, CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx,
        PathKind,
    },
};

pub use crate::{
    config::{CallableSnippets, CompletionConfig},
    item::{
        CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
    },
    snippet::{Snippet, SnippetScope},
};

//FIXME: split the following feature into fine-grained features.

// Feature: Magic Completions
//
// In addition to usual reference completion, rust-analyzer provides some ✨magic✨
// completions as well:
//
// Keywords like `if`, `else` `while`, `loop` are completed with braces, and cursor
// is placed at the appropriate position. Even though `if` is easy to type, you
// still want to complete it, to get ` { }` for free! `return` is inserted with a
// space or `;` depending on the return type of the function.
//
// When completing a function call, `()` are automatically inserted. If a function
// takes arguments, the cursor is positioned inside the parenthesis.
//
// There are postfix completions, which can be triggered by typing something like
// `foo().if`. The word after `.` determines postfix completion. Possible variants are:
//
// - `expr.if` -> `if expr {}` or `if let ... {}` for `Option` or `Result`
// - `expr.match` -> `match expr {}`
// - `expr.while` -> `while expr {}` or `while let ... {}` for `Option` or `Result`
// - `expr.ref` -> `&expr`
// - `expr.refm` -> `&mut expr`
// - `expr.let` -> `let $0 = expr;`
// - `expr.letm` -> `let mut $0 = expr;`
// - `expr.not` -> `!expr`
// - `expr.dbg` -> `dbg!(expr)`
// - `expr.dbgr` -> `dbg!(&expr)`
// - `expr.call` -> `(expr)`
//
// There also snippet completions:
//
// .Expressions
// - `pd` -> `eprintln!(" = {:?}", );`
// - `ppd` -> `eprintln!(" = {:#?}", );`
//
// .Items
// - `tfn` -> `#[test] fn feature(){}`
// - `tmod` ->
// ```rust
// #[cfg(test)]
// mod tests {
//     use super::*;
//
//     #[test]
//     fn test_name() {}
// }
// ```
//
// And the auto import completions, enabled with the `rust-analyzer.completion.autoimport.enable` setting and the corresponding LSP client capabilities.
// Those are the additional completion options with automatic `use` import and options from all project importable items,
// fuzzy matched against the completion input.
//
// image::https://user-images.githubusercontent.com/48062697/113020667-b72ab880-917a-11eb-8778-716cf26a0eb3.gif[]

/// Main entry point for completion. We run completion as a two-phase process.
///
/// First, we look at the position and collect a so-called `CompletionContext.
/// This is a somewhat messy process, because, during completion, syntax tree is
/// incomplete and can look really weird.
///
/// Once the context is collected, we run a series of completion routines which
/// look at the context and produce completion items. One subtlety about this
/// phase is that completion engine should not filter by the substring which is
/// already present, it should give all possible variants for the identifier at
/// the caret. In other words, for
///
/// ```no_run
/// fn f() {
///     let foo = 92;
///     let _ = bar$0
/// }
/// ```
///
/// `foo` *should* be present among the completion variants. Filtering by
/// identifier prefix/fuzzy match should be done higher in the stack, together
/// with ordering of completions (currently this is done by the client).
///
/// # Speculative Completion Problem
///
/// There's a curious unsolved problem in the current implementation. Often, you
/// want to compute completions on a *slightly different* text document.
///
/// In the simplest case, when the code looks like `let x = `, you want to
/// insert a fake identifier to get a better syntax tree: `let x = complete_me`.
///
/// We do this in `CompletionContext`, and it works OK-enough for *syntax*
/// analysis. However, we might want to, eg, ask for the type of `complete_me`
/// variable, and that's where our current infrastructure breaks down. salsa
/// doesn't allow such "phantom" inputs.
///
/// Another case where this would be instrumental is macro expansion. We want to
/// insert a fake ident and re-expand code. There's `expand_speculative` as a
/// work-around for this.
///
/// A different use-case is completion of injection (examples and links in doc
/// comments). When computing completion for a path in a doc-comment, you want
/// to inject a fake path expression into the item being documented and complete
/// that.
///
/// IntelliJ has CodeFragment/Context infrastructure for that. You can create a
/// temporary PSI node, and say that the context ("parent") of this node is some
/// existing node. Asking for, eg, type of this `CodeFragment` node works
/// correctly, as the underlying infrastructure makes use of contexts to do
/// analysis.
pub fn completions(
    db: &RootDatabase,
    config: &CompletionConfig,
    position: FilePosition,
    trigger_character: Option<char>,
) -> Option<Vec<CompletionItem>> {
    let (ctx, analysis) = &CompletionContext::new(db, position, config)?;
    let mut completions = Completions::default();

    // prevent `(` from triggering unwanted completion noise
    if trigger_character == Some('(') {
        if let CompletionAnalysis::NameRef(NameRefContext { kind, .. }) = &analysis {
            if let NameRefKind::Path(
                path_ctx @ PathCompletionCtx { kind: PathKind::Vis { has_in_token }, .. },
            ) = kind
            {
                completions::vis::complete_vis_path(&mut completions, ctx, path_ctx, has_in_token);
            }
        }
        // prevent `(` from triggering unwanted completion noise
        return Some(completions.into());
    }

    {
        let acc = &mut completions;

        match &analysis {
            CompletionAnalysis::Name(name_ctx) => completions::complete_name(acc, ctx, name_ctx),
            CompletionAnalysis::NameRef(name_ref_ctx) => {
                completions::complete_name_ref(acc, ctx, name_ref_ctx)
            }
            CompletionAnalysis::Lifetime(lifetime_ctx) => {
                completions::lifetime::complete_label(acc, ctx, lifetime_ctx);
                completions::lifetime::complete_lifetime(acc, ctx, lifetime_ctx);
            }
            CompletionAnalysis::String { original, expanded: Some(expanded) } => {
                completions::extern_abi::complete_extern_abi(acc, ctx, expanded);
                completions::format_string::format_string(acc, ctx, original, expanded);
                completions::env_vars::complete_cargo_env_vars(acc, ctx, expanded);
            }
            CompletionAnalysis::UnexpandedAttrTT {
                colon_prefix,
                fake_attribute_under_caret: Some(attr),
            } => {
                completions::attribute::complete_known_attribute_input(
                    acc,
                    ctx,
                    colon_prefix,
                    attr,
                );
            }
            CompletionAnalysis::UnexpandedAttrTT { .. } | CompletionAnalysis::String { .. } => (),
        }
    }

    Some(completions.into())
}

/// Resolves additional completion data at the position given.
/// This is used for import insertion done via completions like flyimport and custom user snippets.
pub fn resolve_completion_edits(
    db: &RootDatabase,
    config: &CompletionConfig,
    FilePosition { file_id, offset }: FilePosition,
    imports: impl IntoIterator<Item = (String, String)>,
) -> Option<Vec<TextEdit>> {
    let _p = profile::span("resolve_completion_edits");
    let sema = hir::Semantics::new(db);

    let original_file = sema.parse(file_id);
    let original_token =
        syntax::AstNode::syntax(&original_file).token_at_offset(offset).left_biased()?;
    let position_for_import = &original_token.parent()?;
    let scope = ImportScope::find_insert_use_container(position_for_import, &sema)?;

    let current_module = sema.scope(position_for_import)?.module();
    let current_crate = current_module.krate();
    let new_ast = scope.clone_for_update();
    let mut import_insert = TextEdit::builder();

    imports.into_iter().for_each(|(full_import_path, imported_name)| {
        let items_with_name = items_locator::items_with_name(
            &sema,
            current_crate,
            NameToImport::exact_case_sensitive(imported_name),
            items_locator::AssocItemSearch::Include,
            Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
        );
        let import = items_with_name
            .filter_map(|candidate| {
                current_module.find_use_path_prefixed(
                    db,
                    candidate,
                    config.insert_use.prefix_kind,
                    config.prefer_no_std,
                )
            })
            .find(|mod_path| mod_path.to_string() == full_import_path);
        if let Some(import_path) = import {
            insert_use::insert_use(&new_ast, mod_path_to_ast(&import_path), &config.insert_use);
        }
    });

    algo::diff(scope.as_syntax_node(), new_ast.as_syntax_node()).into_text_edit(&mut import_insert);
    Some(vec![import_insert.finish()])
}
<|endoftext|>


<commit_before>use std::cmp::Ordering;
use std::iter::Fuse;

use super::adaptors::{PutBack, put_back};
use either_or_both::EitherOrBoth;

/// Return an iterator adaptor that merge-joins items from the two base iterators in ascending order.
///
/// See [`.merge_join_by()`](trait.Itertools.html#method.merge_join_by) for more information.
pub fn merge_join_by<I, J, F>(left: I, right: J, cmp_fn: F)
    -> MergeJoinBy<I::IntoIter, J::IntoIter, F>
    where I: IntoIterator,
          J: IntoIterator,
          F: FnMut(&I::Item, &J::Item) -> Ordering
{
    MergeJoinBy {
        left: put_back(left.into_iter().fuse()),
        right: put_back(right.into_iter().fuse()),
        cmp_fn: cmp_fn
    }
}

/// An iterator adaptor that merge-joins items from the two base iterators in ascending order.
///
/// See [`.merge_join_by()`](../trait.Itertools.html#method.merge_join_by) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct MergeJoinBy<I: Iterator, J: Iterator, F> {
    left: PutBack<Fuse<I>>,
    right: PutBack<Fuse<J>>,
    cmp_fn: F
}

impl<I, J, F> Iterator for MergeJoinBy<I, J, F>
    where I: Iterator,
          J: Iterator,
          F: FnMut(&I::Item, &J::Item) -> Ordering
{
    type Item = EitherOrBoth<I::Item, J::Item>;

    fn next(&mut self) -> Option<Self::Item> {
        match (self.left.next(), self.right.next()) {
            (None, None) => None,
            (Some(left), None) =>
                Some(EitherOrBoth::Left(left)),
            (None, Some(right)) =>
                Some(EitherOrBoth::Right(right)),
            (Some(left), Some(right)) => {
                match (self.cmp_fn)(&left, &right) {
                    Ordering::Equal =>
                        Some(EitherOrBoth::Both(left, right)),
                    Ordering::Less => {
                        self.right.put_back(right);
                        Some(EitherOrBoth::Left(left))
                    },
                    Ordering::Greater => {
                        self.left.put_back(left);
                        Some(EitherOrBoth::Right(right))
                    }
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (a_lower, a_upper) = self.left.size_hint();
        let (b_lower, b_upper) = self.right.size_hint();

        let lower = ::std::cmp::max(a_lower, b_lower);

        let upper = match (a_upper, b_upper) {
            (Some(x), Some(y)) => Some(x + y),
            _ => None,
        };

        (lower, upper)
    }
}
<commit_msg>Impl Debug for MergeJoinBy<commit_after>use std::cmp::Ordering;
use std::iter::Fuse;
use std::fmt;

use super::adaptors::{PutBack, put_back};
use either_or_both::EitherOrBoth;

/// Return an iterator adaptor that merge-joins items from the two base iterators in ascending order.
///
/// See [`.merge_join_by()`](trait.Itertools.html#method.merge_join_by) for more information.
pub fn merge_join_by<I, J, F>(left: I, right: J, cmp_fn: F)
    -> MergeJoinBy<I::IntoIter, J::IntoIter, F>
    where I: IntoIterator,
          J: IntoIterator,
          F: FnMut(&I::Item, &J::Item) -> Ordering
{
    MergeJoinBy {
        left: put_back(left.into_iter().fuse()),
        right: put_back(right.into_iter().fuse()),
        cmp_fn: cmp_fn
    }
}

/// An iterator adaptor that merge-joins items from the two base iterators in ascending order.
///
/// See [`.merge_join_by()`](../trait.Itertools.html#method.merge_join_by) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct MergeJoinBy<I: Iterator, J: Iterator, F> {
    left: PutBack<Fuse<I>>,
    right: PutBack<Fuse<J>>,
    cmp_fn: F
}

impl<I, J, F> fmt::Debug for MergeJoinBy<I, J, F>
    where I: Iterator + fmt::Debug,
          I::Item: fmt::Debug,
          J: Iterator + fmt::Debug,
          J::Item: fmt::Debug,
{
    debug_fmt_fields!(MergeJoinBy, left, right);
}

impl<I, J, F> Iterator for MergeJoinBy<I, J, F>
    where I: Iterator,
          J: Iterator,
          F: FnMut(&I::Item, &J::Item) -> Ordering
{
    type Item = EitherOrBoth<I::Item, J::Item>;

    fn next(&mut self) -> Option<Self::Item> {
        match (self.left.next(), self.right.next()) {
            (None, None) => None,
            (Some(left), None) =>
                Some(EitherOrBoth::Left(left)),
            (None, Some(right)) =>
                Some(EitherOrBoth::Right(right)),
            (Some(left), Some(right)) => {
                match (self.cmp_fn)(&left, &right) {
                    Ordering::Equal =>
                        Some(EitherOrBoth::Both(left, right)),
                    Ordering::Less => {
                        self.right.put_back(right);
                        Some(EitherOrBoth::Left(left))
                    },
                    Ordering::Greater => {
                        self.left.put_back(left);
                        Some(EitherOrBoth::Right(right))
                    }
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (a_lower, a_upper) = self.left.size_hint();
        let (b_lower, b_upper) = self.right.size_hint();

        let lower = ::std::cmp::max(a_lower, b_lower);

        let upper = match (a_upper, b_upper) {
            (Some(x), Some(y)) => Some(x + y),
            _ => None,
        };

        (lower, upper)
    }
}
<|endoftext|>


<commit_before>//! Completes mod declarations.

use base_db::{SourceDatabaseExt, VfsPath};
use hir::{Module, ModuleSource};
use ide_db::RootDatabase;
use rustc_hash::FxHashSet;

use crate::{CompletionItem, CompletionItemKind};

use super::{
    completion_context::CompletionContext, completion_item::CompletionKind,
    completion_item::Completions,
};

/// Complete mod declaration, i.e. `mod <|> ;`
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
    let mod_under_caret = match &ctx.mod_declaration_under_caret {
        Some(mod_under_caret) if mod_under_caret.item_list().is_some() => return None,
        Some(mod_under_caret) => mod_under_caret,
        None => return None,
    };

    let _p = profile::span("completion::complete_mod");

    let current_module = ctx.scope.module()?;

    let module_definition_file =
        current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
    let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
    let directory_to_look_for_submodules = directory_to_look_for_submodules(
        current_module,
        ctx.db,
        source_root.path_for_file(&module_definition_file)?,
    )?;

    let existing_mod_declarations = current_module
        .children(ctx.db)
        .filter_map(|module| Some(module.name(ctx.db)?.to_string()))
        .collect::<FxHashSet<_>>();

    let module_declaration_file =
        current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
            module_declaration_source_file.file_id.original_file(ctx.db)
        });

    source_root
        .iter()
        .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
        .filter(|submodule_candidate_file| {
            Some(submodule_candidate_file) != module_declaration_file.as_ref()
        })
        .filter_map(|submodule_file| {
            let submodule_path = source_root.path_for_file(&submodule_file)?;
            let directory_with_submodule = submodule_path.parent()?;
            match submodule_path.name_and_extension()? {
                ("lib", Some("rs")) | ("main", Some("rs")) => None,
                ("mod", Some("rs")) => {
                    if directory_with_submodule.parent()? == directory_to_look_for_submodules {
                        match directory_with_submodule.name_and_extension()? {
                            (directory_name, None) => Some(directory_name.to_owned()),
                            _ => None,
                        }
                    } else {
                        None
                    }
                }
                (file_name, Some("rs"))
                    if directory_with_submodule == directory_to_look_for_submodules =>
                {
                    Some(file_name.to_owned())
                }
                _ => None,
            }
        })
        .filter(|name| !existing_mod_declarations.contains(name))
        .for_each(|submodule_name| {
            let mut label = submodule_name;
            if mod_under_caret.semicolon_token().is_none() {
                label.push(';')
            }
            acc.add(
                CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
                    .kind(CompletionItemKind::Module),
            )
        });

    Some(())
}

fn directory_to_look_for_submodules(
    module: Module,
    db: &RootDatabase,
    module_file_path: &VfsPath,
) -> Option<VfsPath> {
    let directory_with_module_path = module_file_path.parent()?;
    let base_directory = match module_file_path.name_and_extension()? {
        ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
            Some(directory_with_module_path)
        }
        (regular_rust_file_name, Some("rs")) => {
            if matches!(
                (
                    directory_with_module_path
                        .parent()
                        .as_ref()
                        .and_then(|path| path.name_and_extension()),
                    directory_with_module_path.name_and_extension(),
                ),
                (Some(("src", None)), Some(("bin", None)))
            ) {
                // files in /src/bin/ can import each other directly
                Some(directory_with_module_path)
            } else {
                directory_with_module_path.join(regular_rust_file_name)
            }
        }
        _ => None,
    }?;

    let mut resulting_path = base_directory;
    for module in module_chain_to_containing_module_file(module, db) {
        if let Some(name) = module.name(db) {
            resulting_path = resulting_path.join(&name.to_string())?;
        }
    }

    Some(resulting_path)
}

fn module_chain_to_containing_module_file(
    current_module: Module,
    db: &RootDatabase,
) -> Vec<Module> {
    let mut path = Vec::new();

    let mut current_module = Some(current_module);
    while let Some(ModuleSource::Module(_)) =
        current_module.map(|module| module.definition_source(db).value)
    {
        if let Some(module) = current_module {
            path.insert(0, module);
            current_module = module.parent(db);
        } else {
            current_module = None;
        }
    }

    path
}

#[cfg(test)]
mod tests {
    use crate::completion::{test_utils::completion_list, CompletionKind};
    use expect_test::{expect, Expect};

    fn check(ra_fixture: &str, expect: Expect) {
        let actual = completion_list(ra_fixture, CompletionKind::Magic);
        expect.assert_eq(&actual);
    }

    #[test]
    fn lib_module_completion() {
        check(
            r#"
            //- /lib.rs
            mod <|>
            //- /foo.rs
            fn foo() {}
            //- /foo/ignored_foo.rs
            fn ignored_foo() {}
            //- /bar/mod.rs
            fn bar() {}
            //- /bar/ignored_bar.rs
            fn ignored_bar() {}
        "#,
            expect![[r#"
                md bar;
                md foo;
            "#]],
        );
    }

    #[test]
    fn main_module_completion() {
        check(
            r#"
            //- /main.rs
            mod <|>
            //- /foo.rs
            fn foo() {}
            //- /foo/ignored_foo.rs
            fn ignored_foo() {}
            //- /bar/mod.rs
            fn bar() {}
            //- /bar/ignored_bar.rs
            fn ignored_bar() {}
        "#,
            expect![[r#"
                md bar;
                md foo;
            "#]],
        );
    }

    #[test]
    fn main_test_module_completion() {
        check(
            r#"
            //- /main.rs
            mod tests {
                mod <|>;
            }
            //- /tests/foo.rs
            fn foo() {}
        "#,
            expect![[r#"
                md foo
            "#]],
        );
    }

    #[test]
    fn directly_nested_module_completion() {
        check(
            r#"
            //- /lib.rs
            mod foo;
            //- /foo.rs
            mod <|>;
            //- /foo/bar.rs
            fn bar() {}
            //- /foo/bar/ignored_bar.rs
            fn ignored_bar() {}
            //- /foo/baz/mod.rs
            fn baz() {}
            //- /foo/moar/ignored_moar.rs
            fn ignored_moar() {}
        "#,
            expect![[r#"
                md bar
                md baz
            "#]],
        );
    }

    #[test]
    fn nested_in_source_module_completion() {
        check(
            r#"
            //- /lib.rs
            mod foo;
            //- /foo.rs
            mod bar {
                mod <|>
            }
            //- /foo/bar/baz.rs
            fn baz() {}
        "#,
            expect![[r#"
                md baz;
            "#]],
        );
    }

    // FIXME binart modules are not picked up in tests
    // #[test]
    // fn regular_bin_module_completion() {
    //     check(
    //         r#"
    //         //- /src/main.rs
    //         fn main() {}
    //         //- /src/main/foo.rs
    //         mod <|>
    //         //- /src/main/bar.rs
    //         fn bar() {}
    //         //- /src/main/bar/bar_ignored.rs
    //         fn bar_ignored() {}
    //     "#,
    //         expect![[r#"
    //             md bar;
    //         "#]],
    //     );
    // }

    #[test]
    fn already_declared_bin_module_completion_omitted() {
        check(
            r#"
            //- /src/main.rs
            fn main() {}
            //- /src/main/foo.rs
            mod <|>
            //- /src/main/bar.rs
            mod foo;
            fn bar() {}
            //- /src/main/bar/bar_ignored.rs
            fn bar_ignored() {}
        "#,
            expect![[r#""#]],
        );
    }
}
<commit_msg>One more test<commit_after>//! Completes mod declarations.

use base_db::{SourceDatabaseExt, VfsPath};
use hir::{Module, ModuleSource};
use ide_db::RootDatabase;
use rustc_hash::FxHashSet;

use crate::{CompletionItem, CompletionItemKind};

use super::{
    completion_context::CompletionContext, completion_item::CompletionKind,
    completion_item::Completions,
};

/// Complete mod declaration, i.e. `mod <|> ;`
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
    let mod_under_caret = match &ctx.mod_declaration_under_caret {
        Some(mod_under_caret) if mod_under_caret.item_list().is_some() => return None,
        Some(mod_under_caret) => mod_under_caret,
        None => return None,
    };

    let _p = profile::span("completion::complete_mod");

    let current_module = ctx.scope.module()?;

    let module_definition_file =
        current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
    let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
    let directory_to_look_for_submodules = directory_to_look_for_submodules(
        current_module,
        ctx.db,
        source_root.path_for_file(&module_definition_file)?,
    )?;

    let existing_mod_declarations = current_module
        .children(ctx.db)
        .filter_map(|module| Some(module.name(ctx.db)?.to_string()))
        .collect::<FxHashSet<_>>();

    let module_declaration_file =
        current_module.declaration_source(ctx.db).map(|module_declaration_source_file| {
            module_declaration_source_file.file_id.original_file(ctx.db)
        });

    source_root
        .iter()
        .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file)
        .filter(|submodule_candidate_file| {
            Some(submodule_candidate_file) != module_declaration_file.as_ref()
        })
        .filter_map(|submodule_file| {
            let submodule_path = source_root.path_for_file(&submodule_file)?;
            let directory_with_submodule = submodule_path.parent()?;
            match submodule_path.name_and_extension()? {
                ("lib", Some("rs")) | ("main", Some("rs")) => None,
                ("mod", Some("rs")) => {
                    if directory_with_submodule.parent()? == directory_to_look_for_submodules {
                        match directory_with_submodule.name_and_extension()? {
                            (directory_name, None) => Some(directory_name.to_owned()),
                            _ => None,
                        }
                    } else {
                        None
                    }
                }
                (file_name, Some("rs"))
                    if directory_with_submodule == directory_to_look_for_submodules =>
                {
                    Some(file_name.to_owned())
                }
                _ => None,
            }
        })
        .filter(|name| !existing_mod_declarations.contains(name))
        .for_each(|submodule_name| {
            let mut label = submodule_name;
            if mod_under_caret.semicolon_token().is_none() {
                label.push(';')
            }
            acc.add(
                CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
                    .kind(CompletionItemKind::Module),
            )
        });

    Some(())
}

fn directory_to_look_for_submodules(
    module: Module,
    db: &RootDatabase,
    module_file_path: &VfsPath,
) -> Option<VfsPath> {
    let directory_with_module_path = module_file_path.parent()?;
    let base_directory = match module_file_path.name_and_extension()? {
        ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => {
            Some(directory_with_module_path)
        }
        (regular_rust_file_name, Some("rs")) => {
            if matches!(
                (
                    directory_with_module_path
                        .parent()
                        .as_ref()
                        .and_then(|path| path.name_and_extension()),
                    directory_with_module_path.name_and_extension(),
                ),
                (Some(("src", None)), Some(("bin", None)))
            ) {
                // files in /src/bin/ can import each other directly
                Some(directory_with_module_path)
            } else {
                directory_with_module_path.join(regular_rust_file_name)
            }
        }
        _ => None,
    }?;

    let mut resulting_path = base_directory;
    for module in module_chain_to_containing_module_file(module, db) {
        if let Some(name) = module.name(db) {
            resulting_path = resulting_path.join(&name.to_string())?;
        }
    }

    Some(resulting_path)
}

fn module_chain_to_containing_module_file(
    current_module: Module,
    db: &RootDatabase,
) -> Vec<Module> {
    let mut path = Vec::new();

    let mut current_module = Some(current_module);
    while let Some(ModuleSource::Module(_)) =
        current_module.map(|module| module.definition_source(db).value)
    {
        if let Some(module) = current_module {
            path.insert(0, module);
            current_module = module.parent(db);
        } else {
            current_module = None;
        }
    }

    path
}

#[cfg(test)]
mod tests {
    use crate::completion::{test_utils::completion_list, CompletionKind};
    use expect_test::{expect, Expect};

    fn check(ra_fixture: &str, expect: Expect) {
        let actual = completion_list(ra_fixture, CompletionKind::Magic);
        expect.assert_eq(&actual);
    }

    #[test]
    fn lib_module_completion() {
        check(
            r#"
            //- /lib.rs
            mod <|>
            //- /foo.rs
            fn foo() {}
            //- /foo/ignored_foo.rs
            fn ignored_foo() {}
            //- /bar/mod.rs
            fn bar() {}
            //- /bar/ignored_bar.rs
            fn ignored_bar() {}
        "#,
            expect![[r#"
                md bar;
                md foo;
            "#]],
        );
    }

    #[test]
    fn no_module_completion_with_module_body() {
        check(
            r#"
            //- /lib.rs
            mod <|> {

            }
            //- /foo.rs
            fn foo() {}
        "#,
            expect![[r#""#]],
        );
    }

    #[test]
    fn main_module_completion() {
        check(
            r#"
            //- /main.rs
            mod <|>
            //- /foo.rs
            fn foo() {}
            //- /foo/ignored_foo.rs
            fn ignored_foo() {}
            //- /bar/mod.rs
            fn bar() {}
            //- /bar/ignored_bar.rs
            fn ignored_bar() {}
        "#,
            expect![[r#"
                md bar;
                md foo;
            "#]],
        );
    }

    #[test]
    fn main_test_module_completion() {
        check(
            r#"
            //- /main.rs
            mod tests {
                mod <|>;
            }
            //- /tests/foo.rs
            fn foo() {}
        "#,
            expect![[r#"
                md foo
            "#]],
        );
    }

    #[test]
    fn directly_nested_module_completion() {
        check(
            r#"
            //- /lib.rs
            mod foo;
            //- /foo.rs
            mod <|>;
            //- /foo/bar.rs
            fn bar() {}
            //- /foo/bar/ignored_bar.rs
            fn ignored_bar() {}
            //- /foo/baz/mod.rs
            fn baz() {}
            //- /foo/moar/ignored_moar.rs
            fn ignored_moar() {}
        "#,
            expect![[r#"
                md bar
                md baz
            "#]],
        );
    }

    #[test]
    fn nested_in_source_module_completion() {
        check(
            r#"
            //- /lib.rs
            mod foo;
            //- /foo.rs
            mod bar {
                mod <|>
            }
            //- /foo/bar/baz.rs
            fn baz() {}
        "#,
            expect![[r#"
                md baz;
            "#]],
        );
    }

    // FIXME binart modules are not picked up in tests
    // #[test]
    // fn regular_bin_module_completion() {
    //     check(
    //         r#"
    //         //- /src/main.rs
    //         fn main() {}
    //         //- /src/main/foo.rs
    //         mod <|>
    //         //- /src/main/bar.rs
    //         fn bar() {}
    //         //- /src/main/bar/bar_ignored.rs
    //         fn bar_ignored() {}
    //     "#,
    //         expect![[r#"
    //             md bar;
    //         "#]],
    //     );
    // }

    #[test]
    fn already_declared_bin_module_completion_omitted() {
        check(
            r#"
            //- /src/main.rs
            fn main() {}
            //- /src/main/foo.rs
            mod <|>
            //- /src/main/bar.rs
            mod foo;
            fn bar() {}
            //- /src/main/bar/bar_ignored.rs
            fn bar_ignored() {}
        "#,
            expect![[r#""#]],
        );
    }
}
<|endoftext|>