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

Backtrack over capture? #223

Open
scand1sk opened this issue Apr 29, 2019 · 1 comment
Open

Backtrack over capture? #223

scand1sk opened this issue Apr 29, 2019 · 1 comment

Comments

@scand1sk
Copy link

scand1sk commented Apr 29, 2019

This may be a feature, but I need to backtrack after a capture and it does not seem to work.
I have this use case:
I want to parse an array of the form [a,b,c,d,…] with a, b, c being either integer or floating-point number (using standard C-like format), i.e. :

  def int_const[_: P] = P(CharIn("+\\-").? ~~ CharIn("0-9").repX(1)).!.map(i => i.toInt)

  def float_const[_: P] = P(
    CharIn("+\\-").? ~~ CharIn("0-9").repX ~~ "." ~~ CharIn("0-9").repX(1) ~~ (
      CharIn("eE") ~~ CharIn("+\\-").? ~~ CharIn("0-9").repX(1)).?).!.map(f => f.toDouble)

  def array[_: P] = P(
    "[" ~~ (int_const | float_const).rep(sep = ",") ~~ "]").!

The issue is that when I want to parse a float (e.g., 500.0), it is first parsed as an int (500), before a "." symbol is encountered instead of the expected ",". I expect that the parser backtracks and parses 500.0 as a float instead.
Is this a bug, a feature, or am I missing something?
I am using FastParse 2.1.2 and ScriptWhitespace.

@He-Pin
Copy link
Contributor

He-Pin commented May 19, 2024

why not float_const | int_const?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants