Patchwork [4,of,5] parser: factor out function that parses right-hand side of prefix/infix ops

login
register
mail settings
Submitter Yuya Nishihara
Date July 14, 2015, 2:44 p.m.
Message ID <c98978f8aaeb6db1b770.1436885068@mimosa>
Download mbox | patch
Permalink /patch/9974/
State Accepted
Headers show

Comments

Yuya Nishihara - July 14, 2015, 2:44 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1436087355 -32400
#      Sun Jul 05 18:09:15 2015 +0900
# Node ID c98978f8aaeb6db1b770a54921147e7d3836a050
# Parent  f759d0d4f83339bf075084fb700997ff8601e0a1
parser: factor out function that parses right-hand side of prefix/infix ops

These two had common pattern. The significant difference was just a result
expression:

  prefix: (op-name, rhs)
  infix:  (op-name, lhs, rhs)

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -35,6 +35,15 @@  class parser(object):
             raise error.ParseError(_("unexpected token: %s") % self.current[0],
                                    self.current[2])
         self._advance()
+    def _parseoperand(self, bind, m=None):
+        'gather right-hand-side operand until an end condition or binding met'
+        if m and self.current[0] == m:
+            expr = None
+        else:
+            expr = self._parse(bind)
+        if m:
+            self._match(m)
+        return expr
     def _parse(self, bind=0):
         token, value, pos = self._advance()
         # handle prefix rules on current token
@@ -44,13 +53,7 @@  class parser(object):
         if len(prefix) == 1:
             expr = (prefix[0], value)
         else:
-            if len(prefix) > 2 and prefix[2] == self.current[0]:
-                self._match(prefix[2])
-                expr = (prefix[0], None)
-            else:
-                expr = (prefix[0], self._parse(prefix[1]))
-                if len(prefix) > 2:
-                    self._match(prefix[2])
+            expr = (prefix[0], self._parseoperand(*prefix[1:]))
         # gather tokens until we meet a lower binding strength
         while bind < self._elements[self.current[0]][0]:
             token, value, pos = self._advance()
@@ -62,13 +65,7 @@  class parser(object):
                 # handle infix rules
                 if not infix:
                     raise error.ParseError(_("not an infix: %s") % token, pos)
-                if len(infix) == 3 and infix[2] == self.current[0]:
-                    self._match(infix[2])
-                    expr = (infix[0], expr, (None))
-                else:
-                    expr = (infix[0], expr, self._parse(infix[1]))
-                    if len(infix) == 3:
-                        self._match(infix[2])
+                expr = (infix[0], expr, self._parseoperand(*infix[1:]))
         return expr
     def parse(self, tokeniter):
         'generate a parse tree from tokens'