From 98d01a922916c9a47441b670484194fffae8c912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Willenb=C3=BCcher?= Date: Sat, 20 Feb 2021 10:19:20 +0100 Subject: [PATCH] Add is_eof() function to Parse trait Use Parse::is_eof() to replace the previous check for end-of-input. --- peg-macros/grammar.rs | 4 ++-- peg-macros/tokens.rs | 4 ++++ peg-macros/translate.rs | 4 ++-- peg-runtime/lib.rs | 1 + peg-runtime/slice.rs | 4 ++++ peg-runtime/str.rs | 4 ++++ 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/peg-macros/grammar.rs b/peg-macros/grammar.rs index b6f54bd..ad4d408 100644 --- a/peg-macros/grammar.rs +++ b/peg-macros/grammar.rs @@ -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"); @@ -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" ); diff --git a/peg-macros/tokens.rs b/peg-macros/tokens.rs index 3a76477..34cd91d 100644 --- a/peg-macros/tokens.rs +++ b/peg-macros/tokens.rs @@ -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] { diff --git a/peg-macros/translate.rs b/peg-macros/translate.rs index cbb5db4..f7bedfb 100644 --- a/peg-macros/translate.rs +++ b/peg-macros/translate.rs @@ -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"); @@ -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"); diff --git a/peg-runtime/lib.rs b/peg-runtime/lib.rs index 4cfa72d..8afdab0 100644 --- a/peg-runtime/lib.rs +++ b/peg-runtime/lib.rs @@ -18,6 +18,7 @@ pub enum RuleResult { 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; } diff --git a/peg-runtime/slice.rs b/peg-runtime/slice.rs index eb131d1..ff4b066 100644 --- a/peg-runtime/slice.rs +++ b/peg-runtime/slice.rs @@ -6,6 +6,10 @@ impl Parse for [T] { 0 } + fn is_eof(&self, pos: usize) -> bool { + pos >= self.len() + } + fn position_repr(&self, pos: usize) -> usize { pos } diff --git a/peg-runtime/str.rs b/peg-runtime/str.rs index 94a4d82..3b912ce 100644 --- a/peg-runtime/str.rs +++ b/peg-runtime/str.rs @@ -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;