Skip to content

Commit

Permalink
A bunch of flake8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Dec 2, 2013
1 parent df1aa3e commit 0b4a254
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions rply/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
from rply.lexergenerator import LexerGenerator
from rply.parsergenerator import ParserGenerator
from rply.token import Token

__all__ = [
"LexerGenerator", "ParserGenerator", "ParsingError", "Token"
]
20 changes: 16 additions & 4 deletions rply/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def add_production(self, prod_name, syms, func, precedence):
try:
prod_prec = self.precedence[precedence]
except KeyError:
raise ParserGeneratorError("Precedence %r doesn't exist" % precedence)
raise ParserGeneratorError(
"Precedence %r doesn't exist" % precedence
)

pnumber = len(self.productions)
self.nonterminals.setdefault(prod_name, [])
Expand All @@ -57,9 +59,15 @@ def add_production(self, prod_name, syms, func, precedence):

def set_precedence(self, term, assoc, level):
if term in self.precedence:
raise ParserGeneratorError("Precedence already specified for %s" % term)
raise ParserGeneratorError(
"Precedence already specified for %s" % term
)
if assoc not in ["left", "right", "nonassoc"]:
raise ParserGeneratorError("Precedence must be one of left, right, nonassoc; not %s" % assoc)
raise ParserGeneratorError(
"Precedence must be one of left, right, nonassoc; not %s" % (
assoc
)
)
self.precedence[term] = (assoc, level)

def set_start(self):
Expand All @@ -69,7 +77,11 @@ def set_start(self):
self.start = start

def unused_terminals(self):
return [t for t, prods in iteritems(self.terminals) if not prods and t != "error"]
return [
t
for t, prods in iteritems(self.terminals)
if not prods and t != "error"
]

def unused_productions(self):
return [p for p, prods in iteritems(self.nonterminals) if not prods]
Expand Down
4 changes: 3 additions & 1 deletion rply/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def next(self):
if match:
colno = self._update_pos(match)
source_pos = SourcePosition(match.start, self._lineno, colno)
token = Token(rule.name, self.s[match.start:match.end], source_pos)
token = Token(
rule.name, self.s[match.start:match.end], source_pos
)
return token
else:
raise LexingError(None, SourcePosition(self.idx, -1, -1))
Expand Down
12 changes: 8 additions & 4 deletions rply/lexergenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def __init__(self, rtyper):

list_repr = FixedSizeListRepr(rtyper, rtyper.getrepr(model.SomeInteger(nonneg=True)))
list_repr._setup_repr()
self.lowleveltype = lltype.Ptr(lltype.GcStruct("RULE",
self.lowleveltype = lltype.Ptr(lltype.GcStruct(
"RULE",
("name", lltype.Ptr(STR)),
("code", list_repr.lowleveltype),
))
Expand Down Expand Up @@ -154,7 +155,8 @@ def rtype_method_matches(self, hop):
c_MATCH_CONTEXT_INIT = hop.inputconst(lltype.Void, self.match_context_init_repr)
c_MATCH_CONTEXT = hop.inputconst(lltype.Void, self.match_context_repr)

return hop.gendirectcall(LLRule.ll_matches,
return hop.gendirectcall(
LLRule.ll_matches,
c_MATCHTYPE, c_MATCH_INIT, c_MATCH_CONTEXTTYPE,
c_MATCH_CONTEXT_INIT, c_MATCH_CONTEXT, v_rule, v_s, v_pos
)
Expand All @@ -170,13 +172,15 @@ def ll_matches(MATCHTYPE, MATCH_INIT, MATCH_CONTEXTTYPE,
s = hlstr(s)
assert pos >= 0
ctx = instantiate(MATCH_CONTEXTTYPE)
hlinvoke(MATCH_CONTEXT_INIT, rsre_core.StrMatchContext.__init__,
hlinvoke(
MATCH_CONTEXT_INIT, rsre_core.StrMatchContext.__init__,
ctx, ll_rule.code, hlstr(s), pos, len(s), 0
)
matched = hlinvoke(MATCH_CONTEXT, rsre_core.match_context, ctx)
if matched:
match = instantiate(MATCHTYPE)
hlinvoke(MATCH_INIT, Match.__init__,
hlinvoke(
MATCH_INIT, Match.__init__,
match, ctx.match_start, ctx.match_end
)
return match
Expand Down
3 changes: 2 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def main(p):

parser = pg.build()

assert parser.parse(iter([Token("VALUE", "abc")])) == Token("VALUE", "abc")
token = parser.parse(iter([Token("VALUE", "abc")]))
assert token == Token("VALUE", "abc")

def test_arithmetic(self):
pg = ParserGenerator(["NUMBER", "PLUS"])
Expand Down

0 comments on commit 0b4a254

Please sign in to comment.