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

Add is_eof() function to Parse trait #252

Merged
merged 1 commit into from
Feb 21, 2021
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
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