#![allow(non_snake_case)]
pub mod s3;
use self::s3::{CompileResult, Project, Target, Costume, ProjectMetadata};
use pest::{Position, Parser};
use pest::iterators::Pair;
use std::collections::HashMap;
#[cfg(debug_assertions)]
const _GRAMMAR: &'static str = include_str!("../grammar.pest");
#[derive(Parser)]
#[grammar = "grammar.pest"]
struct Grammar;
fn fmt_pos(pos: Position) -> String {
let (line, col) = pos.line_col();
format!("line {} col {}", line, col)
}
fn error_at_pos(pos: Position, msg: &'static str) -> String {
msg.to_owned() + " at " + &fmt_pos(pos)
}
fn error_at(rule_pair: Pair<Rule>, msg: &'static str) -> String {
error_at_pos(rule_pair.clone().into_span().start_pos(), msg)
}
pub fn compile(source: String) -> CompileResult { let parse_result = Grammar::parse(Rule::file, &source);
let mut project = Project {
targets: Vec::new(),
meta: ProjectMetadata {
semver: "3.0.0".to_string(),
vm: "0.1.0".to_string(),
agent: "bosh (https://github.com/heyitsmeuralex/bosh)".to_string()
}
};
match parse_result.clone().err() {
Some(err) => return CompileResult::Fail(format!("{}", err)),
None => {
for declaration in parse_result.unwrap() {
let mut pairs = declaration.clone().into_inner();
let declaration_type = pairs.nth(0);
if declaration_type.is_none() {
return CompileResult::Fail(error_at(declaration.clone(), "Illegal empty list, expected declaration"));
}
match declaration_type.unwrap().into_span().as_str() {
"sprite" | "stage" => {
return CompileResult::Fail("Not yet implemented".to_string());
},
_ => return CompileResult::Fail(error_at(declaration.clone(), "Expected declaration (eg. `sprite` call)"))
}
}
},
}
CompileResult::Tree(project)
}