Skip to content

Commit

Permalink
Merge pull request #90 from jonas-schievink/spanfix
Browse files Browse the repository at this point in the history
Fix error spans in PEG grammars
  • Loading branch information
kevinmehall committed May 28, 2015
2 parents b3167dd + d4b76fc commit 76cfa35
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
15 changes: 6 additions & 9 deletions src/rustast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ pub fn module(items: Vec<P<Item>>) -> P<Mod> {
})
}

pub fn parse_path(e: &str) -> ast::Path {
let ps = syntax::parse::ParseSess::new();
let mut p = syntax::parse::new_parser_from_source_str(&ps, Vec::new(), "file".to_string(), e.to_string());
pub fn parse_path(ctxt: &ExtCtxt, e: &str) -> ast::Path {
let mut p = syntax::parse::new_parser_from_source_str(&ctxt.parse_sess, Vec::new(), "file".to_string(), e.to_string());
let r = p.parse_path(syntax::parse::parser::NoTypesAllowed);
p.abort_if_errors();
r.unwrap_or_else(|_|panic!())
Expand All @@ -28,17 +27,15 @@ pub fn parse_path_vec(s: &str) -> Vec<ast::Ident> {
s.split("::").map(|i| str_to_ident(i)).collect()
}

pub fn parse_block(e: &str) -> P<ast::Block> {
let ps = syntax::parse::ParseSess::new();
let mut p = syntax::parse::new_parser_from_source_str(&ps, Vec::new(), "file".to_string(), e.to_string());
pub fn parse_block(ctxt: &ExtCtxt, e: &str) -> P<ast::Block> {
let mut p = syntax::parse::new_parser_from_source_str(&ctxt.parse_sess, Vec::new(), "file".to_string(), e.to_string());
let r = p.parse_block();
p.abort_if_errors();
r.unwrap_or_else(|e| panic!(e))
}

pub fn parse_type(e: &str) -> P<ast::Ty> {
let ps = syntax::parse::ParseSess::new();
let mut p = syntax::parse::new_parser_from_source_str(&ps, Vec::new(), "file".to_string(), e.to_string());
pub fn parse_type(ctxt: &ExtCtxt, e: &str) -> P<ast::Ty> {
let mut p = syntax::parse::new_parser_from_source_str(&ctxt.parse_sess, Vec::new(), "file".to_string(), e.to_string());
let r = p.parse_ty();
p.abort_if_errors();
r
Expand Down
10 changes: 5 additions & 5 deletions src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn compile_grammar(ctxt: &rustast::ExtCtxt, grammar: &Grammar) -> rustast::P
pub fn translate_view_items(ctxt: &rustast::ExtCtxt, imports: &[RustUse]) -> Vec<rustast::P<rustast::Item>> {
imports.iter().map(| i |{
match *i {
RustUseSimple(ref p) => ctxt.item_use_simple(DUMMY_SP, rustast::ast::Inherited, rustast::parse_path(&p)),
RustUseSimple(ref p) => ctxt.item_use_simple(DUMMY_SP, rustast::ast::Inherited, rustast::parse_path(ctxt, &p)),
RustUseGlob(ref p) => ctxt.item_use_glob(DUMMY_SP, rustast::ast::Inherited, rustast::parse_path_vec(&p)),
RustUseList(ref p, ref v) => ctxt.item_use_list(DUMMY_SP, rustast::ast::Inherited, rustast::parse_path_vec(&p),
&v.iter().map(|s| rustast::str_to_ident(&s)).collect::<Vec<_>>()
Expand All @@ -95,7 +95,7 @@ fn make_parse_state(ctxt: &rustast::ExtCtxt, rules: &[Rule]) -> Vec<rustast::P<r
for rule in rules {
if rule.cached {
let name = rustast::str_to_ident(&format!("{}_cache", rule.name));
let map_type = rustast::parse_type(
let map_type = rustast::parse_type(ctxt,
&format!("::std::collections::HashMap<usize, RuleResult<{}>>", rule.ret_type));

cache_fields.append(&mut quote_tokens!(ctxt, $name: $map_type,));
Expand Down Expand Up @@ -280,7 +280,7 @@ pub fn header_items(ctxt: &rustast::ExtCtxt) -> Vec<rustast::P<rustast::Item>> {
fn compile_rule(ctxt: &rustast::ExtCtxt, grammar: &Grammar, rule: &Rule) -> rustast::P<rustast::Item> {
let ref rule_name = rule.name;
let name = rustast::str_to_ident(&format!("parse_{}", rule.name));
let ret = rustast::parse_type(&rule.ret_type);
let ret = rustast::parse_type(ctxt, &rule.ret_type);
let body = compile_expr(ctxt, grammar, &*rule.expr, (&rule.ret_type as &str) != "()");
let wrapped_body = if cfg!(feature = "trace") {
quote_expr!(ctxt, {
Expand Down Expand Up @@ -320,7 +320,7 @@ fn compile_rule(ctxt: &rustast::ExtCtxt, grammar: &Grammar, rule: &Rule) -> rust

fn compile_rule_export(ctxt: &rustast::ExtCtxt, rule: &Rule) -> rustast::P<rustast::Item> {
let name = rustast::str_to_ident(&rule.name);
let ret = rustast::parse_type(&rule.ret_type);
let ret = rustast::parse_type(ctxt, &rule.ret_type);
let parse_fn = rustast::str_to_ident(&format!("parse_{}", rule.name));
(quote_item!(ctxt,
pub fn $name<'input>(input: &'input str) -> ParseResult<$ret> {
Expand Down Expand Up @@ -626,7 +626,7 @@ fn compile_expr(ctxt: &rustast::ExtCtxt, grammar: &Grammar, e: &Expr, result_use
)
}
None => {
let code_block = rustast::parse_block(code);
let code_block = rustast::parse_block(ctxt, code);

if is_cond {
quote_expr!(ctxt, {
Expand Down

0 comments on commit 76cfa35

Please sign in to comment.