Skip to content

Commit

Permalink
Performance improvements in mismatch handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yanivmo committed Jun 7, 2017
1 parent 7de64f2 commit 4746202
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/ruler/base_rules.py
Expand Up @@ -10,6 +10,7 @@ def __init__(self):
self._name = ''
self.matched = None
self.error = None
self._mismatch = Mismatch()

def match(self, text):
raise NotImplementedError
Expand Down Expand Up @@ -120,7 +121,12 @@ def __repr__(self):


class Mismatch(object):
def __init__(self, text, position, description):
def __init__(self):
self.text = ''
self.position = -1
self.description = ''

def set(self, text, position, description):
self.text = text
self.position = position
self.description = description
Expand Down
11 changes: 7 additions & 4 deletions src/ruler/rules.py
Expand Up @@ -8,7 +8,7 @@

import re

from .base_rules import BaseRule, BaseCompoundRule, Mismatch
from .base_rules import BaseRule, BaseCompoundRule


class Grammar(object):
Expand Down Expand Up @@ -57,7 +57,8 @@ def match(self, text):
text_to_match = text_to_match[len(sub_rule.matched):]
else:
mismatch_position = len(text) - len(text_to_match) + sub_rule.error.position
self.error = Mismatch(text, mismatch_position, sub_rule.error.description)
self._mismatch.set(text, mismatch_position, sub_rule.error.description)
self.error = self._mismatch
self.matched = None
return False

Expand Down Expand Up @@ -87,7 +88,8 @@ def match(self, text):
error_text = '"{}" does not match "{}"'.format(text, self._regex.pattern)
else:
error_text = 'reached end of line but expected "{}"'.format(self._regex.pattern)
self.error = Mismatch(text, 0, error_text)
self._mismatch.set(text, 0, error_text)
self.error = self._mismatch
self.matched = None
return False

Expand Down Expand Up @@ -135,7 +137,8 @@ def match(self, text):
if r.error.position == furthest_mismatch_position
))
)
self.error = Mismatch(text, furthest_mismatch_position, description)
self._mismatch.set(text, furthest_mismatch_position, description)
self.error = self._mismatch
return False


Expand Down

0 comments on commit 4746202

Please sign in to comment.