aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-12-17 18:34:47 -0800
committerbnewbold <bnewbold@robocracy.org>2016-12-17 18:34:47 -0800
commit9f82aceb9fbdb42f332d68f4a423123bd0788b2c (patch)
treec082b9795be8e9e9d286c8f8f1345d22f3ec1b59 /src/lib.rs
parentf6364ebcac0d0a88a3cc6812fd2120c97b42cc26 (diff)
downloadmodelthing-9f82aceb9fbdb42f332d68f4a423123bd0788b2c.tar.gz
modelthing-9f82aceb9fbdb42f332d68f4a423123bd0788b2c.zip
refactor modelica parser into separate crate
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs68
1 files changed, 4 insertions, 64 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7020a9b..7c3bb8b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,11 +3,9 @@
extern crate log;
extern crate toml;
-extern crate colored;
-extern crate lalrpop_util;
+extern crate modelica_parser;
-pub mod modelica_parser;
-pub mod modelica_ast;
+pub mod modelica_model;
pub mod transpile_scheme;
pub mod transpile_js;
@@ -16,9 +14,6 @@ use std::fs;
use std::io::Read;
use std::fs::File;
-use lalrpop_util::ParseError;
-use colored::*;
-
#[derive(Debug, PartialEq)]
pub struct ModelMetadata {
pub name_en: String,
@@ -47,7 +42,7 @@ pub struct ModelVar {
#[derive(Debug, PartialEq)]
pub struct ModelEntry {
- pub ast: modelica_ast::ModelicaModel,
+ pub ast: modelica_parser::ast::ModelicaModel,
pub metadata: ModelMetadata,
pub markdown: String,
}
@@ -93,7 +88,7 @@ pub fn load_model_entry(p: &Path) -> Result<ModelEntry,String> {
let ast = {
let mut s = String::new();
try!(File::open(p.join("model.modelica")).and_then(|mut f| f.read_to_string(&mut s)).map_err(|e| e.to_string()));
- try!(modelica_parser::parse_model(&s).map_err(|e| format!("{:?}", e)))
+ try!(modelica_parser::parser::parse_model(&s).map_err(|e| format!("{:?}", e)))
};
let metadata = {
@@ -127,58 +122,3 @@ pub fn search_models(p: &Path) -> Vec<String> {
vec![]
}
}
-
-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) },
- }
-}