diff options
Diffstat (limited to 'modelica-parser-lalrpop/src/lib.rs')
-rw-r--r-- | modelica-parser-lalrpop/src/lib.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/modelica-parser-lalrpop/src/lib.rs b/modelica-parser-lalrpop/src/lib.rs new file mode 100644 index 0000000..f6f1e8b --- /dev/null +++ b/modelica-parser-lalrpop/src/lib.rs @@ -0,0 +1,65 @@ + +extern crate lalrpop_util; +extern crate colored; + +pub mod parser; +pub mod ast; + +use colored::*; +use lalrpop_util::ParseError; + + +fn pp_segment(raw: &str, start: usize, end: usize) -> String { + let mut line_start = 0; + let mut num = 0; + let mut ret = String::new(); + for line in raw.lines() { + num += 1; + let line_end = line_start + line.len(); + if (line_start <= start) && (start < line_end) { + ret += &format!(" {}\n{:>3} {} {}{}{}\n {} {}{}\n", + "|".blue().bold(), + num.to_string().blue().bold(), + "|".blue().bold(), + raw[line_start..start].normal(), + raw[start..end].red().bold(), + if end < line_end { + raw[end..line_end].normal() + } else { + "".normal() + }, + "|".blue().bold(), + std::iter::repeat(" ").take(start - line_start).collect::<String>(), + std::iter::repeat("^").take(end - start).collect::<String>().red().bold()); + } + line_start += line.len() + 1; + if line_start > end { break }; + } + ret +} + +pub fn pp_parseerror(raw: &str, pe: ParseError<usize, (usize, &str), ()>) -> String { + match pe { + ParseError::InvalidToken{location} => { + format!("{} invalid token starting at:\n{}", + "parse error:".red().bold(), + pp_segment(raw, location, location+1)) }, + ParseError::UnrecognizedToken{token: Some((start, (_, tok), end)), expected} => { + format!("{} unrecognized token '{}' (expected one of {:?}):\n{}", + "parse error:".red().bold(), + tok, + expected, + pp_segment(raw, start, end)) }, + ParseError::UnrecognizedToken{token: None, expected} => { + format!("{} premature end-of-file (expected one of {:?})", + "parse error:".red().bold(), + expected) }, + ParseError::ExtraToken{token: (start, (_, tok), end)} => { + format!("{} unexpected extra token '{}':\n{}", + "parse error:".red().bold(), + tok, + pp_segment(raw, start, end)) }, + _ => { + format!("{} {:?}", "parse error:".red().bold(), pe) }, + } +} |