bug 1310980, part 7: refactor Junk generation into a shared method draft
authorAxel Hecht <axel@pike.org>
Fri, 21 Oct 2016 18:45:05 +0200
changeset 149 e6f765f205272f7569cb02cbe726020424414568
parent 147 72ff392e5c8d2bf696869b40eb26d4186b34baff
child 150 2c6171acb00d268723913c7277ec21c7765c138a
push id31
push useraxel@mozilla.com
push dateMon, 24 Oct 2016 16:06:06 +0000
bugs1310980
bug 1310980, part 7: refactor Junk generation into a shared method MozReview-Commit-ID: KJhUyeCTEeH
compare_locales/parser.py
--- a/compare_locales/parser.py
+++ b/compare_locales/parser.py
@@ -236,27 +236,25 @@ class Parser:
             offset = m.end()
             entity = self.createEntity(ctx, m)
             return (entity, offset)
         m = self.reComment.match(ctx.contents, offset)
         if m:
             offset = m.end()
             self.last_comment = Comment(ctx, *[m.span(i) for i in xrange(4)])
             return (self.last_comment, offset)
-        mkey = self.reKey.search(ctx.contents, offset)
-        mcomment = self.reComment.search(ctx.contents, offset)
-        m = None
-        if mkey and mcomment:
-            m = mkey if mkey.start() < mcomment.start() else mcomment
-        else:
-            m = mkey if mkey else mcomment
-        if m:
-            # we didn't match, but search, so there's junk between offset
-            # and start. We'll match() on the next turn
-            junkend = m.start()
+        return self.getTrailing(ctx, offset, self.reKey, self.reComment)
+
+    def getTrailing(self, ctx, offset, *expressions):
+        junkend = None
+        for exp in expressions:
+            m = exp.search(ctx.contents, offset)
+            if m:
+                junkend = min(junkend, m.start()) if junkend else m.start()
+        if junkend is not None:
             return (Junk(ctx, (offset, junkend)), junkend)
         return (None, offset)
 
     def createEntity(self, ctx, m):
         pre_comment = str(self.last_comment) if self.last_comment else ''
         self.last_comment = ''
         return Entity(ctx, self.postProcessValue, pre_comment,
                       *[m.span(i) for i in xrange(6)])
@@ -402,29 +400,17 @@ class PropertiesParser(Parser):
             entity = Entity(ctx, self.postProcessValue, pre_comment,
                             (m.start(), offset),   # full span
                             m.span(1),  # leading whitespan
                             (m.start(2), offset),   # entity def span
                             m.span(2),   # key span
                             (m.end(), endval),   # value span
                             (offset, offset))  # post comment span, empty
             return (entity, offset)
-        mkey = self.reKey.search(ctx.contents, offset)
-        mcomment = self.reComment.search(ctx.contents, offset)
-        m = None
-        if mkey and mcomment:
-            m = mkey if mkey.start() < mcomment.start() else mcomment
-        else:
-            m = mkey if mkey else mcomment
-        if m:
-            # we didn't match, but search, so there's junk between offset
-            # and start. We'll match() on the next turn
-            junkend = m.start()
-            return (Junk(ctx, (offset, junkend)), junkend)
-        return (None, offset)
+        return self.getTrailing(ctx, offset, self.reKey, self.reComment)
 
     def postProcessValue(self, val):
 
         def unescape(m):
             found = m.groupdict()
             if found['uni']:
                 return unichr(int(found['uni'][1:], 16))
             if found['nl']:
@@ -480,24 +466,18 @@ class DefinesParser(Parser):
         if m:
             offset = m.end()
             return (self.createEntity(ctx, m), offset)
         m = self.rePI.match(contents, offset)
         if m:
             offset = m.end()
             return (DefinesInstruction(ctx, *[m.span(i) for i in xrange(5)]),
                     offset)
-        junkend = None
-        for exp in (self.reComment, self.reKey, self.rePI):
-            m = exp.search(contents, offset)
-            if m:
-                junkend = min(junkend, m.start(1)) if junkend else m.start()
-        if junkend is not None:
-            return (Junk(ctx, (offset, junkend)), junkend)
-        return (None, offset)
+        return self.getTrailing(ctx, offset,
+                                self.reComment, self.reKey, self.rePI)
 
 
 class IniSection(Entity):
     '''Entity-like object representing sections in ini files
     '''
     def __init__(self, ctx, span, pre_ws_span, def_span, val_span, post_span):
         self.ctx = ctx
         self.span = span
@@ -546,22 +526,16 @@ class IniParser(Parser):
         m = self.reSection.match(contents, offset)
         if m:
             offset = m.end()
             return (IniSection(ctx, *[m.span(i) for i in xrange(5)]), offset)
         m = self.reKey.match(contents, offset)
         if m:
             offset = m.end()
             return (self.createEntity(ctx, m), offset)
-        junkend = None
-        for exp in (self.reComment, self.reSection, self.reKey):
-            m = exp.search(contents, offset)
-            if m:
-                junkend = min(junkend, m.start(1)) if junkend else m.start()
-        if junkend is not None:
-            return (Junk(ctx, (offset, junkend)), junkend)
-        return (None, offset)
+        return self.getTrailing(ctx, offset,
+                                self.reComment, self.reSection, self.reKey)
 
 
 __constructors = [('\\.dtd$', DTDParser()),
                   ('\\.properties$', PropertiesParser()),
                   ('\\.ini$', IniParser()),
                   ('\\.inc$', DefinesParser())]