Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow crate-relative imports in grammars #213

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 83 additions & 32 deletions peg-macros/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,43 +1214,94 @@ pub mod peg {
) -> ::peg::RuleResult<()> {
#![allow(non_snake_case, unused)]
{
let mut __repeat_pos = __pos;
let mut __repeat_value = vec![];
loop {
let __pos = __repeat_pos;
let __pos = if __repeat_value.is_empty() {
__pos
} else {
let __sep_res =
match ::peg::ParseLiteral::parse_string_literal(__input, __pos, "::") {
::peg::RuleResult::Matched(__pos, __val) => {
::peg::RuleResult::Matched(__pos, __val)
let __seq_res = match {
let __seq_res =
match ::peg::ParseLiteral::parse_string_literal(__input, __pos, "crate") {
::peg::RuleResult::Matched(__pos, __val) => {
::peg::RuleResult::Matched(__pos, __val)
}
::peg::RuleResult::Failed => __err_state.mark_failure(__pos, "\"crate\""),
};
match __seq_res {
::peg::RuleResult::Matched(__pos, _) => {
let __seq_res =
match ::peg::ParseLiteral::parse_string_literal(__input, __pos, "::") {
::peg::RuleResult::Matched(__pos, __val) => {
::peg::RuleResult::Matched(__pos, __val)
}
::peg::RuleResult::Failed => {
__err_state.mark_failure(__pos, "\"::\"")
}
};
match __seq_res {
::peg::RuleResult::Matched(__pos, _) => {
::peg::RuleResult::Matched(__pos, {})
}
::peg::RuleResult::Failed => __err_state.mark_failure(__pos, "\"::\""),
};
match __sep_res {
::peg::RuleResult::Matched(__newpos, _) => __newpos,
::peg::RuleResult::Failed => break,
::peg::RuleResult::Failed => ::peg::RuleResult::Failed,
}
}
};
let __step_res = match __parse_IDENT(__input, __state, __err_state, __pos) {
::peg::RuleResult::Matched(pos, _) => ::peg::RuleResult::Matched(pos, ()),
::peg::RuleResult::Failed => ::peg::RuleResult::Failed,
};
match __step_res {
::peg::RuleResult::Matched(__newpos, __value) => {
__repeat_pos = __newpos;
__repeat_value.push(__value);
}
::peg::RuleResult::Failed => {
break;
}
} {
::peg::RuleResult::Matched(__newpos, _) => ::peg::RuleResult::Matched(__newpos, ()),
::peg::RuleResult::Failed => ::peg::RuleResult::Matched(__pos, ()),
};
match __seq_res {
::peg::RuleResult::Matched(__pos, _) => {
let __seq_res = {
let mut __repeat_pos = __pos;
let mut __repeat_value = vec![];
loop {
let __pos = __repeat_pos;
let __pos = if __repeat_value.is_empty() {
__pos
} else {
let __sep_res = match ::peg::ParseLiteral::parse_string_literal(
__input, __pos, "::",
) {
::peg::RuleResult::Matched(__pos, __val) => {
::peg::RuleResult::Matched(__pos, __val)
}
::peg::RuleResult::Failed => {
__err_state.mark_failure(__pos, "\"::\"")
}
};
match __sep_res {
::peg::RuleResult::Matched(__newpos, _) => __newpos,
::peg::RuleResult::Failed => break,
}
};
let __step_res =
match __parse_IDENT(__input, __state, __err_state, __pos) {
::peg::RuleResult::Matched(pos, _) => {
::peg::RuleResult::Matched(pos, ())
}
::peg::RuleResult::Failed => ::peg::RuleResult::Failed,
};
match __step_res {
::peg::RuleResult::Matched(__newpos, __value) => {
__repeat_pos = __newpos;
__repeat_value.push(__value);
}
::peg::RuleResult::Failed => {
break;
}
}
}
if __repeat_value.len() >= 1 {
::peg::RuleResult::Matched(__repeat_pos, ())
} else {
::peg::RuleResult::Failed
}
};
match __seq_res {
::peg::RuleResult::Matched(__pos, _) => {
::peg::RuleResult::Matched(__pos, {})
}
::peg::RuleResult::Failed => ::peg::RuleResult::Failed,
}
}
}
if __repeat_value.len() >= 1 {
::peg::RuleResult::Matched(__repeat_pos, ())
} else {
::peg::RuleResult::Failed
::peg::RuleResult::Failed => ::peg::RuleResult::Failed,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion peg-macros/grammar.rustpeg
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ rule rust_use() -> TokenStream
/ ("as" IDENT())?
) ";") { v.to_owned() }

rule rust_path() = IDENT() ++ "::"
rule rust_path()
= ("crate" "::")? IDENT() ++ "::"

rule rust_type()
= BRACKET_GROUP()
Expand Down
20 changes: 20 additions & 0 deletions tests/run-pass/crate_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern crate peg;

peg::parser!{
pub grammar foo_parser() for str {
use crate::types::Foo;

pub rule foo() -> Foo
= "foo" { Foo }
}
}

mod types {
#[derive(PartialEq, Debug)]
pub struct Foo;
}


fn main() {
assert_eq!(foo_parser::foo("foo"), Ok(crate::types::Foo));
}