Skip to content

Commit

Permalink
Merge pull request #252 from adrianwn/master
Browse files Browse the repository at this point in the history
Add is_eof() function to Parse trait
  • Loading branch information
kevinmehall committed Feb 21, 2021
2 parents 01440d0 + 98d01a9 commit 24b185e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions peg-macros/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod peg {
::peg::Parse::start(__input),
) {
::peg::RuleResult::Matched(__pos, __value) => {
if __pos == __input.len() {
if ::peg::Parse::is_eof(__input, __pos) {
return Ok(__value);
} else {
__err_state.mark_failure(__pos, "EOF");
Expand All @@ -50,7 +50,7 @@ pub mod peg {
::peg::Parse::start(__input),
) {
::peg::RuleResult::Matched(__pos, __value) => {
if __pos == __input.len() {
if ::peg::Parse::is_eof(__input, __pos) {
panic!(
"Parser is nondeterministic: succeeded when reparsing for error position"
);
Expand Down
4 changes: 4 additions & 0 deletions peg-macros/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl Parse for FlatTokenStream {
0
}

fn is_eof(&self, pos: usize) -> bool {
pos >= self.len()
}

fn position_repr(&self, pos: usize) -> Sp {
Sp(
match &self.tokens[pos] {
Expand Down
4 changes: 2 additions & 2 deletions peg-macros/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ fn compile_rule_export(context: &Context, rule: &Rule) -> TokenStream {
let mut __state = ParseState::new();
match #parse_fn(__input, &mut __state, &mut __err_state, ::peg::Parse::start(__input) #extra_args_call #(, #rule_params_call)*) {
::peg::RuleResult::Matched(__pos, __value) => {
if __pos == __input.len() {
if ::peg::Parse::is_eof(__input, __pos) {
return Ok(__value)
} else {
__err_state.mark_failure(__pos, "EOF");
Expand All @@ -293,7 +293,7 @@ fn compile_rule_export(context: &Context, rule: &Rule) -> TokenStream {

match #parse_fn(__input, &mut __state, &mut __err_state, ::peg::Parse::start(__input) #extra_args_call #(, #rule_params_call)*) {
::peg::RuleResult::Matched(__pos, __value) => {
if __pos == __input.len() {
if ::peg::Parse::is_eof(__input, __pos) {
panic!("Parser is nondeterministic: succeeded when reparsing for error position");
} else {
__err_state.mark_failure(__pos, "EOF");
Expand Down
1 change: 1 addition & 0 deletions peg-runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum RuleResult<T> {
pub trait Parse {
type PositionRepr: Display;
fn start<'input>(&'input self) -> usize;
fn is_eof<'input>(&'input self, p: usize) -> bool;
fn position_repr<'input>(&'input self, p: usize) -> Self::PositionRepr;
}

Expand Down
4 changes: 4 additions & 0 deletions peg-runtime/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ impl<T> Parse for [T] {
0
}

fn is_eof(&self, pos: usize) -> bool {
pos >= self.len()
}

fn position_repr(&self, pos: usize) -> usize {
pos
}
Expand Down
4 changes: 4 additions & 0 deletions peg-runtime/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl Parse for str {
0
}

fn is_eof(&self, pos: usize) -> bool {
pos >= self.len()
}

fn position_repr(&self, pos: usize) -> LineCol {
let before = &self[..pos];
let line = before.as_bytes().iter().filter(|&&c| c == b'\n').count() + 1;
Expand Down

0 comments on commit 24b185e

Please sign in to comment.