Last active
December 15, 2015 23:17
-
-
Save bcachet/baae0df3455641c75647 to your computer and use it in GitHub Desktop.
Real number parser based on http://blog.leifbattermann.de/2015/11/23/functional-monadic-parsers-ported-to-c/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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