Skip to content

Commit

Permalink
refactor Itemizer implementations to reduce duplication and get rid o…
Browse files Browse the repository at this point in the history
…f unnecessary low-level muddling
  • Loading branch information
mattfenwick committed Nov 1, 2013
1 parent d0f1ed5 commit 6429070
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions unparse/combinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ def f(xs, s):

class Itemizer(object):

def __init__(self, itemP):
def __init__(self, item):
'''
itemP :: [t] -> s -> MaybeError e (t, [t], s)
`itemP` is the most basic parser and should:
item :: Parser e s (m t) t
`item` is the most basic parser and should:
- succeed, consuming one single token if there are any tokens left
- fail if there are no tokens left
'''
self.item = Parser(itemP)
self.item = item

def literal(self, x):
'''
Expand Down Expand Up @@ -333,7 +333,7 @@ def oneOf(self, elems):
return self.satisfy(lambda x: x in c_set)


def _item_basic(xs, s):
def _f_item_basic(xs, s):
'''
Simply consumes a single token if one is available, presenting that token
as the value. Fails if token stream is empty.
Expand All @@ -343,38 +343,21 @@ def _item_basic(xs, s):
first, rest = xs.first(), xs.rest()
return good(first, rest, s)

def _bump(c, p):
line, col = p
if c == '\n':
basic = Itemizer(Parser(_f_item_basic))

def _bump(char, position):
line, col = position
if char == '\n':
return (line + 1, 1)
return (line, col + 1)

def _item_position(xs, position):
'''
Does two things:
- consumes a single token if available, failing otherwise (see `_item_basic`)
- updates the position info in state -- `\n` is a newline
This assumes that the state is a 2-tuple of integers, (line, column).
'''
if xs.isEmpty():
return M.zero
first, rest = xs.first(), xs.rest()
return good(first, rest, _bump(first, position))

def _item_count(xs, ct):
'''
Does two things:
1. see `_item_basic`
2. increments a counter -- which tells how many tokens have been consumed
'''
if xs.isEmpty():
return M.zero
first, rest = xs.first(), xs.rest()
return good(first, rest, ct + 1)
def _f_position(c):
return seq2R(updateState(lambda s: _bump(c, s)), pure(c))

_item_position = bind(basic.item, _f_position)
position = Itemizer(_item_position)
basic = Itemizer(_item_basic)

_item_count = seq2L(basic.item, updateState(lambda x: x + 1))
count = Itemizer(_item_count)


Expand Down

0 comments on commit 6429070

Please sign in to comment.