Commit dd699e45 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added support for curly bracket notation instead of parenthesis.

parent 68a227a2
...@@ -96,8 +96,9 @@ class Parser(BisonParser): ...@@ -96,8 +96,9 @@ class Parser(BisonParser):
# of tokens of the lex script. # of tokens of the lex script.
tokens = ['NUMBER', 'IDENTIFIER', 'NEWLINE', 'QUIT', 'RAISE', 'GRAPH', tokens = ['NUMBER', 'IDENTIFIER', 'NEWLINE', 'QUIT', 'RAISE', 'GRAPH',
'LPAREN', 'RPAREN', 'FUNCTION', 'FUNCTION_LPAREN', 'LBRACKET', 'LPAREN', 'RPAREN', 'FUNCTION', 'FUNCTION_LPAREN', 'LBRACKET',
'RBRACKET', 'PIPE', 'PRIME', 'DERIVATIVE'] \ 'RBRACKET', 'LCBRACKET', 'RCBRACKET', 'PIPE', 'PRIME',
+ filter(lambda t: t != 'FUNCTION', TOKEN_MAP.values()) 'DERIVATIVE'] \
+ filter(lambda t: t != 'FUNCTION', TOKEN_MAP.values())
# ------------------------------ # ------------------------------
# precedences # precedences
...@@ -468,6 +469,7 @@ class Parser(BisonParser): ...@@ -468,6 +469,7 @@ class Parser(BisonParser):
| IDENTIFIER | IDENTIFIER
| LPAREN exp RPAREN | LPAREN exp RPAREN
| LBRACKET exp RBRACKET | LBRACKET exp RBRACKET
| LCBRACKET exp RCBRACKET
| unary | unary
| binary | binary
| nary | nary
...@@ -482,10 +484,11 @@ class Parser(BisonParser): ...@@ -482,10 +484,11 @@ class Parser(BisonParser):
if option == 1: # rule: IDENTIFIER if option == 1: # rule: IDENTIFIER
return Leaf(values[0]) return Leaf(values[0])
if option in (2, 3): # rule: LPAREN exp RPAREN | LBRACKET exp RBRACKET if 2 <= option <= 4: # rule: LPAREN exp RPAREN | LBRACKET exp RBRACKET
# | LCBRACKET exp RCBRACKET
return values[1] return values[1]
if 4 <= option <= 6: # rule: unary | binary | nary if 5 <= option <= 7: # rule: unary | binary | nary
return values[0] return values[0]
raise BisonSyntaxError('Unsupported option %d in target "%s".' raise BisonSyntaxError('Unsupported option %d in target "%s".'
...@@ -714,6 +717,8 @@ class Parser(BisonParser): ...@@ -714,6 +717,8 @@ class Parser(BisonParser):
")" { returntoken(RPAREN); } ")" { returntoken(RPAREN); }
"[" { returntoken(LBRACKET); } "[" { returntoken(LBRACKET); }
"]" { returntoken(RBRACKET); } "]" { returntoken(RBRACKET); }
"{" { returntoken(LCBRACKET); }
"}" { returntoken(RCBRACKET); }
"'" { returntoken(PRIME); } "'" { returntoken(PRIME); }
"|" { returntoken(PIPE); } "|" { returntoken(PIPE); }
log_([0-9]+|[a-zA-Z])"*(" { returntoken(FUNCTION_LPAREN); } log_([0-9]+|[a-zA-Z])"*(" { returntoken(FUNCTION_LPAREN); }
......
...@@ -102,6 +102,9 @@ class TestParser(RulesTestCase): ...@@ -102,6 +102,9 @@ class TestParser(RulesTestCase):
self.assertEqual(*tree('[x ^ 2], x ^ 2')) self.assertEqual(*tree('[x ^ 2], x ^ 2'))
self.assertEqual(*tree('[x ^ 2](x), x ^ 2 * x')) self.assertEqual(*tree('[x ^ 2](x), x ^ 2 * x'))
self.assertEqual(*tree('{x}, x'))
self.assertEqual(*tree('{x ^ 2}, x ^ 2'))
def test_derivative(self): def test_derivative(self):
x = tree('x') x = tree('x')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment