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

Parse inf nan #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions pymbolic/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
_closebracket = intern("closebracket")
_true = intern("True")
_false = intern("False")
_inf = intern("inf")
_nan = intern("nan")
_identifier = intern("identifier")
_whitespace = intern("whitespace")
_comma = intern("comma")
Expand Down Expand Up @@ -168,6 +170,8 @@ class Parser:
(_closebracket, pytools.lex.RE(r"\]")),
(_true, pytools.lex.RE(r"True")),
(_false, pytools.lex.RE(r"False")),
(_inf, pytools.lex.RE(r"inf")),
(_nan, pytools.lex.RE(r"nan")),
(_identifier, pytools.lex.RE(r"[@$a-z_A-Z_][@$a-zA-Z_0-9]*")),
(_whitespace, pytools.lex.RE("[ \n\t]*")),
(_comma, pytools.lex.RE(",")),
Expand Down Expand Up @@ -203,6 +207,14 @@ def parse_terminal(self, pstate):
elif next_tag is _false:
assert pstate.next_str_and_advance() == "False"
return False
elif next_tag is _inf:
from math import inf
assert pstate.next_str_and_advance() == "inf"
return inf
elif next_tag is _nan:
from math import nan
assert pstate.next_str_and_advance() == "nan"
return nan
elif next_tag is _identifier:
return primitives.Variable(pstate.next_str_and_advance())
elif next_tag is _if:
Expand Down
3 changes: 3 additions & 0 deletions test/test_pymbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ def test_parser():
parse("1+if(0, 1, 2)")

assert eval(str(parse("1729 if True or False else 42"))) == 1729
import math
assert math.isinf(parse("inf"))
assert math.isnan(parse("nan"))

# }}}

Expand Down