Minor cleanup.

This commit is contained in:
Simon Forman 2022-03-22 21:24:07 -07:00
parent 7dbb3c69ed
commit 4827aa467e
3 changed files with 22 additions and 21 deletions

View File

@ -61,7 +61,7 @@ HELP_TEMPLATE = '''\
%s %s
---- end (%s) ---- end ( %s )
''' '''
@ -331,12 +331,12 @@ def choice(stack):
Use a Boolean value to select one of two items. Use a Boolean value to select one of two items.
:: ::
A B False choice A B false choice
---------------------- ----------------------
A A
A B True choice A B true choice
--------------------- ---------------------
B B
@ -354,12 +354,12 @@ def select(stack):
Use a Boolean value to select one of two items from a sequence. Use a Boolean value to select one of two items from a sequence.
:: ::
[A B] False select [A B] false select
------------------------ ------------------------
A A
[A B] True select [A B] true select
----------------------- -----------------------
B B

View File

@ -111,20 +111,14 @@ def _parse(tokens):
v = frame v = frame
try: frame = stack.pop() try: frame = stack.pop()
except IndexError: except IndexError:
raise ParseError('Extra closing bracket.') raise ParseError('Extra closing bracket.') from None
frame.append(list_to_stack(v)) frame.append(list_to_stack(v))
elif tok == 'true': elif tok == 'true': frame.append(True)
frame.append(True) elif tok == 'false': frame.append(False)
elif tok == 'false': elif isinstance(tok, Snippet): frame.append(tok)
frame.append(False)
elif isinstance(tok, Snippet):
frame.append(tok)
else: else:
try: try: thing = int(tok)
thing = int(tok) except ValueError: thing = Symbol(tok)
except ValueError:
thing = Symbol(tok)
frame.append(thing) frame.append(thing)
if stack: if stack: raise ParseError('Unclosed bracket.')
raise ParseError('Unclosed bracket.')
return list_to_stack(frame) return list_to_stack(frame)

View File

@ -1,8 +1,10 @@
from collections import namedtuple from collections import namedtuple
from re import compile as RE from re import compile as RE
Snippet = namedtuple('Snippet', 'sha offset length') Snippet = namedtuple('Snippet', 'sha offset length')
_fmt = '{%s %i %i}'
pat = ( pat = (
'{' '{'
'\s*' '\s*'
@ -14,14 +16,18 @@ pat = (
'\s*' '\s*'
'}' '}'
) )
_PAT = RE(pat) _PAT = RE(pat)
def to_string(snip): def to_string(snip):
return _fmt % _ts(*snip) return _ts(*snip)
def _ts(sha, offset, length): def _ts(sha, offset, length):
return sha.decode('ascii'), offset, length return f'{{{sha.decode("ascii")} {offset} {length}}}'
def from_string(text): def from_string(text):
m = _PAT.match(text) m = _PAT.match(text)
@ -29,5 +35,6 @@ def from_string(text):
raise ValueError raise ValueError
return _fs(**m.groupdict()) return _fs(**m.groupdict())
def _fs(sha, offset, length): def _fs(sha, offset, length):
return Snippet(sha.encode('ascii'), int(offset), int(length)) return Snippet(sha.encode('ascii'), int(offset), int(length))