Skip to content

Commit

Permalink
Minor refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
eldipa committed Nov 23, 2018
1 parent 1998b69 commit 233afd3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions byexample/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .common import log, tohuman, constant
from .options import OptionParser, UnrecognizedOption, ExtendOptionParserMixin
from .expected import _LinearExpected, _RegexExpected
from .parser_state_machine import SM_NormWS, SM
from .parser_state_machine import SM_NormWS, SM_NotNormWS

def tag_name_as_regex_name(name):
return name.replace('-', '_')
Expand Down Expand Up @@ -1146,7 +1146,7 @@ def expected_as_regexs_TMP(self, expected, tags_enabled, normalize_whitespace):

tokenizer = self.expected_tokenizer(expected, tags_enabled)

sm = SM(self)
sm = SM_NotNormWS(self)
while not sm.ended():
charno, ttype, token = next(tokenizer, (None, None, None))
sm.feed(charno, ttype, token)
Expand Down
51 changes: 37 additions & 14 deletions byexample/parser_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
tWS = ('wspaces', 'newlines')
tLIT = ('wspaces', 'newlines', 'literals')

class SM_NormWS:
class SM(object):
def __init__(self, parser):
self.stash = []
self.results = []
Expand All @@ -13,8 +13,6 @@ def __init__(self, parser):
self.tags_by_idx = {}
self.names_seen = set()

self.emit(0, r'\A', 0)

def ended(self):
return self.state in (EXHAUSTED, ERROR)

Expand All @@ -27,16 +25,6 @@ def drop(self, last=False):
def emit(self, charno, regex, rcount):
self.results.append((charno, regex, rcount))

def emit_ws(self, just_one=False):
charno, _ = self.pull()
if just_one:
rx = r'\s'
else:
rx = r'\s+(?!\s)'
rc = 1

self.emit(charno, rx, rc)

def emit_literals(self):
charno, l = self.pull()
rx = re.escape(l)
Expand Down Expand Up @@ -82,6 +70,29 @@ def emit_eof(self, ws):
rc = 0
self.emit(charno, rx, rc)

class SM_NormWS(SM):
def __init__(self, parser):
SM.__init__(self, parser)
self.emit(0, r'\A', 0)

def emit_ws(self, just_one=False):
charno, _ = self.pull()
if just_one:
rx = r'\s'
else:
rx = r'\s+(?!\s)'
rc = 1

self.emit(charno, rx, rc)

def emit_tag(self, mode):
assert mode in ('l', 'r', 'b', '0')
return SM.emit_tag(self, mode)

def emit_eof(self, ws):
assert ws == r'\s'
return SM.emit_eof(self, ws)

def feed(self, charno, ttype, token):
push = self.stash.append
drop = self.drop
Expand Down Expand Up @@ -185,7 +196,19 @@ def feed(self, charno, ttype, token):
else:
assert False

class SM(SM_NormWS):
class SM_NotNormWS(SM):
def __init__(self, parser):
SM.__init__(self, parser)
self.emit(0, r'\A', 0)

def emit_tag(self, mode):
assert mode in ('e', '0')
return SM.emit_tag(self, mode)

def emit_eof(self, ws):
assert ws == r'\n'
return SM.emit_eof(self, ws)

def feed(self, charno, ttype, token):
push = self.stash.append
drop = self.drop
Expand Down

0 comments on commit 233afd3

Please sign in to comment.