Skip to content

Instantly share code, notes, and snippets.

@bcachet
Last active December 15, 2015 23:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcachet/baae0df3455641c75647 to your computer and use it in GitHub Desktop.
Save bcachet/baae0df3455641c75647 to your computer and use it in GitHub Desktop.
public static readonly Parser<char> Dot = CharP('.');
public static Parser<char> Sign = Sat(c => "+-".Contains(c));
public static Parser<char> Exponent = Sat(c => "Ee".Contains(c));
public static Parser<double> RealNumber() {
return
from integer in Natural
from fractional in (from dot in Dot
from digit in Natural
from r in Return(digit)
select r).Choice(Parsers.Return(0))
from exponent in (from e in Exponent
from sign in Sign.Choice(Parsers.Return('+'))
from v in Natural
from r in Return(double.Parse($"{sign}{v}"))
select r).Choice(Parsers.Return(0d))
from result in Return(double.Parse($"{integer}.{fractional}e{exponent}"))
select result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment