Code cleanup and added interactie parser mode (for shells).

parent 93a5449d
...@@ -40,6 +40,12 @@ class Parser(BisonParser): ...@@ -40,6 +40,12 @@ class Parser(BisonParser):
('right', ('POW', )), ('right', ('POW', )),
) )
interactive = 0
def __init__(self, **kwargs):
BisonParser.__init__(self, **kwargs)
self.interactive = kwargs.get('interactive', 0)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# override default read method with a version that prompts for input # override default read method with a version that prompts for input
# ------------------------------------------------------------------ # ------------------------------------------------------------------
...@@ -67,7 +73,11 @@ class Parser(BisonParser): ...@@ -67,7 +73,11 @@ class Parser(BisonParser):
input : input :
| input line | input line
""" """
return
if option == 1:
if self.interactive:
print values[1]
return values[1]
def on_line(self, target, option, names, values): def on_line(self, target, option, names, values):
""" """
...@@ -75,8 +85,11 @@ class Parser(BisonParser): ...@@ -75,8 +85,11 @@ class Parser(BisonParser):
| exp NEWLINE | exp NEWLINE
""" """
if option == 1: if option == 1:
if self.verbose:
print 'on_line: exp =', values[0] print 'on_line: exp =', values[0]
return values[0]
def on_exp(self, target, option, names, values): def on_exp(self, target, option, names, values):
""" """
exp : NUMBER exp : NUMBER
...@@ -88,6 +101,8 @@ class Parser(BisonParser): ...@@ -88,6 +101,8 @@ class Parser(BisonParser):
| exp POW exp | exp POW exp
| LPAREN exp RPAREN | LPAREN exp RPAREN
""" """
if self.verbose:
print 'on_exp: got %s %s %s %s' % (target, option, names, values) print 'on_exp: got %s %s %s %s' % (target, option, names, values)
if option == 0: if option == 0:
...@@ -146,5 +161,5 @@ class Parser(BisonParser): ...@@ -146,5 +161,5 @@ class Parser(BisonParser):
""" """
if __name__ == '__main__': if __name__ == '__main__':
p = Parser(verbose=0, keepfiles=1) p = Parser(verbose=0, keepfiles=1, interactive=1)
p.run(debug=0) p.run(debug=0)
...@@ -12,7 +12,7 @@ endif ...@@ -12,7 +12,7 @@ endif
test: $(TESTS) build test: $(TESTS) build
coverage: ${COVERAGE} coverage: ${COVERAGE} build
mkdir ${COVERAGE_OUTPUT_DIR} 2>/dev/null || true mkdir ${COVERAGE_OUTPUT_DIR} 2>/dev/null || true
${COVERAGE} erase ${COVERAGE} erase
for t in ${TESTS}; do \ for t in ${TESTS}; do \
...@@ -26,4 +26,4 @@ ${COVERAGE}: ...@@ -26,4 +26,4 @@ ${COVERAGE}:
@echo "Install package 'python-coverage' to generate a coverage report." @echo "Install package 'python-coverage' to generate a coverage report."
@echo "On Debian/Ubuntu use: sudo apt-get install python-coverage"; false @echo "On Debian/Ubuntu use: sudo apt-get install python-coverage"; false
$(TESTS): ; @python -m testrunner $@ $(TESTS): build; @python -m testrunner $@
import unittest import unittest
from src.calc import Parser
class TestParser(Parser):
def __init__(self, input_buffer, **kwargs):
Parser.__init__(self, **kwargs)
self.input_buffer = []
self.input_position = 0
map(self.append, input_buffer)
def append(self, input):
self.input_buffer.append(input + '\n')
def read(self, nbytes):
buffer = ''
try:
buffer = self.input_buffer[self.input_position]
except IndexError:
return ''
self.input_position += 1
return buffer
class TestCalc(unittest.TestCase): class TestCalc(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -9,5 +38,5 @@ class TestCalc(unittest.TestCase): ...@@ -9,5 +38,5 @@ class TestCalc(unittest.TestCase):
def tearDown(self): def tearDown(self):
pass pass
def test_true(self): def test_constructor(self):
assert True assert TestParser(['1+4'], keepfiles=1).run() == 5.0
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