Applied pep8 and pyflakes recommendations.

parent 9bb7dede
...@@ -16,6 +16,7 @@ sys.path.insert(1, EXTERNAL_MODS) ...@@ -16,6 +16,7 @@ sys.path.insert(1, EXTERNAL_MODS)
from pybison import BisonParser from pybison import BisonParser
class Parser(BisonParser): class Parser(BisonParser):
""" """
Implements the calculator parser. Grammar rules are defined in the method Implements the calculator parser. Grammar rules are defined in the method
...@@ -197,9 +198,13 @@ class Parser(BisonParser): ...@@ -197,9 +198,13 @@ class Parser(BisonParser):
#define YYSTYPE void * #define YYSTYPE void *
#include "tokens.h" #include "tokens.h"
extern void *py_parser; extern void *py_parser;
extern void (*py_input)(PyObject *parser, char *buf, int *result, int max_size); extern void (*py_input)(PyObject *parser, char *buf, int *result,
#define returntoken(tok) yylval = PyString_FromString(strdup(yytext)); return (tok); int max_size);
#define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); } #define returntoken(tok) \
yylval = PyString_FromString(strdup(yytext)); return (tok);
#define YY_INPUT(buf,result,max_size) { \
(*py_input)(py_parser, buf, &result, max_size); \
}
%} %}
%% %%
...@@ -217,7 +222,8 @@ class Parser(BisonParser): ...@@ -217,7 +222,8 @@ class Parser(BisonParser):
[ \t\v\f] {} [ \t\v\f] {}
[\n] {yylineno++; returntoken(NEWLINE); } [\n] {yylineno++; returntoken(NEWLINE); }
. { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */} . { printf("unknown char %c ignored, yytext=0x%lx\n",
yytext[0], yytext); /* ignore bad chars */}
%% %%
......
...@@ -3,16 +3,16 @@ import logging.config ...@@ -3,16 +3,16 @@ import logging.config
import sys import sys
try: try:
import config import config as cfg
except ImportError: except ImportError:
config = object() cfg = object()
import default_config as default import default_config as default
try: try:
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
format=getattr(config, 'LOG_FORMAT', default.LOG_FORMAT), format=getattr(cfg, 'LOG_FORMAT', default.LOG_FORMAT),
filename=getattr(config, 'LOG_FILE', default.LOG_FILE), filename=getattr(cfg, 'LOG_FILE', default.LOG_FILE),
filemode='a') filemode='a')
except IOError as e: # pragma: no cover except IOError as e: # pragma: no cover
print >>sys.stderr, 'warning: IOError raised: "%s"' % str(e) print >>sys.stderr, 'warning: IOError raised: "%s"' % str(e)
......
...@@ -19,7 +19,7 @@ sys.path.insert(1, EXTERNAL_MODS) ...@@ -19,7 +19,7 @@ sys.path.insert(1, EXTERNAL_MODS)
from pybison import BisonParser, BisonSyntaxError from pybison import BisonParser, BisonSyntaxError
from graph_drawing.graph import generate_graph from graph_drawing.graph import generate_graph
from node import TYPE_OPERATOR, OP_ADD, OP_MUL, OP_SUB, OP_NEG from node import TYPE_OPERATOR, OP_ADD, OP_MUL, OP_SUB
# Check for n-ary operator in child nodes # Check for n-ary operator in child nodes
...@@ -28,13 +28,6 @@ def combine(op, op_type, *nodes): ...@@ -28,13 +28,6 @@ def combine(op, op_type, *nodes):
res = [op] res = [op]
for n in nodes: for n in nodes:
try:
assert n.type != TYPE_OPERATOR or n.op != OP_NEG or len(n.nodes) == 1
assert n.type != TYPE_OPERATOR or n.op != OP_SUB or len(n.nodes) > 1
except AssertionError:
print n, type(n), n.type, OP_NEG, OP_SUB, n.nodes, len(n.nodes)
raise
# Merge the children for all nodes which have the same operator. # Merge the children for all nodes which have the same operator.
if n.type == TYPE_OPERATOR and n.op == op_type: if n.type == TYPE_OPERATOR and n.op == op_type:
res += n.nodes res += n.nodes
...@@ -367,14 +360,20 @@ class Parser(BisonParser): ...@@ -367,14 +360,20 @@ class Parser(BisonParser):
def get_args(): def get_args():
parser = argparse.ArgumentParser(prog='parser', description=__doc__) parser = argparse.ArgumentParser(prog='parser', description=__doc__)
parser.add_argument('--debug', '-d', action='store_true', default=False,
parser.add_argument('--debug', '-d', action='store_true',
default=False,
help='Enable debug mode in bison and flex.') help='Enable debug mode in bison and flex.')
parser.add_argument('--verbose', '-v', action='store_true', default=False, parser.add_argument('--verbose', '-v', action='store_true',
default=False,
help='Enable verbose output messages (printed to stdout).') help='Enable verbose output messages (printed to stdout).')
parser.add_argument('--keepfiles', '-k', action='store_true', default=False, parser.add_argument('--keepfiles', '-k', action='store_true',
default=False,
help='Keep temporary generated bison and lex files.') help='Keep temporary generated bison and lex files.')
parser.add_argument('--batch', '-b', action='store_true', default=False, parser.add_argument('--batch', '-b', action='store_true', default=False,
help='Disable interactive mode and execute expressions in batch mode.') help='Disable interactive mode and execute expressions in batch' \
' mode.')
return parser.parse_args() return parser.parse_args()
......
...@@ -5,6 +5,7 @@ from rules import rules ...@@ -5,6 +5,7 @@ from rules import rules
# (node, funcptr, (args...)) # (node, funcptr, (args...))
def get_node_possibilities(node): def get_node_possibilities(node):
""" """
Get all possible rewrite steps for this node. Get all possible rewrite steps for this node.
...@@ -18,6 +19,7 @@ def get_node_possibilities(node): ...@@ -18,6 +19,7 @@ def get_node_possibilities(node):
return possibilities return possibilities
def get_possibilities(node): def get_possibilities(node):
""" """
Get all possible rewrite steps for this node and its children. Get all possible rewrite steps for this node and its children.
...@@ -29,6 +31,7 @@ def get_possibilities(node): ...@@ -29,6 +31,7 @@ def get_possibilities(node):
return possibilities return possibilities
if __name__ == '__main__': if __name__ == '__main__':
node = main() node = main()
print 'node:', node print 'node:', node
......
...@@ -2,7 +2,7 @@ import unittest ...@@ -2,7 +2,7 @@ import unittest
from src.parser import Parser from src.parser import Parser
from src.node import ExpressionNode as N, ExpressionLeaf as L from src.node import ExpressionNode as N, ExpressionLeaf as L
from tests.parser import ParserWrapper, run_expressions from tests.parser import run_expressions
class TestB1Ch8(unittest.TestCase): class TestB1Ch8(unittest.TestCase):
......
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