<commit_before>//! Wrapper around rust-peg generated parser methods.

peg_file! parse("lea.rustpeg");

pub use self::parse::{ident, literal};

use super::ast::*;
use super::span::Span;
use super::visit;
use super::visit::Visitor;
use super::expr_parser::ExprParser;

/// Custom error type adding a dummy span and providing a `format` method.
#[derive(Debug, PartialEq, Eq)]
pub struct ParseError {
    err: parse::ParseError,
    span: Span,
}

impl ParseError {
    fn new(err: parse::ParseError) -> ParseError {
        ParseError {
            span: Span::new(err.offset, err.offset + 1),
            err: err,
        }
    }

    pub fn format(&self, code: &str, source_name: &str) -> String {
        let mut res = format!("{}: {}\n", source_name, self.err);
        res.push_str(self.span.format(code, source_name).as_slice());

        res
    }
}

/// Parses an expression
pub fn expression(input: &str) -> Result<Expr, ParseError> {
    let mut e = match parse::expression(input) {
        Err(err) => { return Err(ParseError::new(err)); },
        Ok(res) => res,
    };
    e = ExprParser.visit_expr(e);

    Ok(e)
}

/// Parses a statement
pub fn statement(input: &str) -> Result<Stmt, ParseError> {
    let mut s = match parse::statement(input) {
        Err(err) => { return Err(ParseError::new(err)); },
        Ok(res) => res,
    };
    s = ExprParser.visit_stmt(s);

    Ok(s)
}

/// Parses a block of statements
pub fn block(input: &str) -> Result<Block, ParseError> {
    let mut b = match parse::block(input) {
        Err(err) => { return Err(ParseError::new(err)); },
        Ok(res) => res,
    };
    b = visit::walk_block(b, &mut ExprParser);

    Ok(b)
}

/// Parses a block of statements and builds a function wrapper that can be run as the main function
/// around it.
pub fn parse_main(input: &str) -> Result<Function, ParseError> {
    let blk = try!(block(input));

    Ok(Function::new(vec![], true, blk))
}

/// Parses a raw expression. This only runs the PEG parser, not the dedicated expression parser.
///
/// The returned expression will be of the variant `ERawOp`.
#[inline(always)]
pub fn expression_raw(input: &str) -> Result<Expr, ParseError> {
    match parse::expression(input) {
        Err(err) => Err(ParseError::new(err)),
        Ok(res) => Ok(res),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use compiler::ast::*;
    use compiler::span::Spanned;

    use op::*;
<commit_msg>Make ParseError implement FromError<commit_after>//! Wrapper around rust-peg generated parser methods.

peg_file! parse("lea.rustpeg");

pub use self::parse::{ident, literal};

use super::ast::*;
use super::span::Span;
use super::visit;
use super::visit::Visitor;
use super::expr_parser::ExprParser;

use std::error::FromError;

/// Custom error type adding a dummy span and providing a `format` method.
#[derive(Debug, PartialEq, Eq)]
pub struct ParseError {
    err: parse::ParseError,
    span: Span,
}

impl ParseError {
    pub fn format(&self, code: &str, source_name: &str) -> String {
        let mut res = format!("{}: {}\n", source_name, self.err);
        res.push_str(self.span.format(code, source_name).as_slice());

        res
    }
}

impl FromError<parse::ParseError> for ParseError {
    fn from_error(err: parse::ParseError) -> ParseError {
        ParseError {
            span: Span::new(err.offset, err.offset + 1),
            err: err,
        }
    }
}

/// Parses an expression
pub fn expression(input: &str) -> Result<Expr, ParseError> {
    let mut e = try!(parse::expression(input));
    e = ExprParser.visit_expr(e);

    Ok(e)
}

/// Parses a statement
pub fn statement(input: &str) -> Result<Stmt, ParseError> {
    let mut s = try!(parse::statement(input));
    s = ExprParser.visit_stmt(s);

    Ok(s)
}

/// Parses a block of statements
pub fn block(input: &str) -> Result<Block, ParseError> {
    let mut b = try!(parse::block(input));
    b = visit::walk_block(b, &mut ExprParser);

    Ok(b)
}

/// Parses a block of statements and builds a function wrapper that can be run as the main function
/// around it.
pub fn parse_main(input: &str) -> Result<Function, ParseError> {
    let blk = try!(block(input));

    Ok(Function::new(vec![], true, blk))
}

/// Parses a raw expression. This only runs the PEG parser, not the dedicated expression parser.
///
/// The returned expression will be of the variant `ERawOp`.
#[inline(always)]
pub fn expression_raw(input: &str) -> Result<Expr, ParseError> {
    Ok(try!(parse::expression(input)))
}

#[cfg(test)]
mod tests {
    use super::*;

    use compiler::ast::*;
    use compiler::span::Spanned;

    use op::*;

{'fraction_non_alphanumeric': 0.1586890901250539, 'fraction_numerical': 0.00043122035360068997, 'mean_word_length': 3.2716390423572745, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '79979', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1501, 'n_words': 438}