Removed duplication in left recursive infix operator parsers
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 6 Aug 2017 18:59:10 +0000 (14:59 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 6 Aug 2017 18:59:10 +0000 (14:59 -0400)
parsing.py

index b30556e..462c2b4 100644 (file)
@@ -220,80 +220,63 @@ def _literal_level_expression_parser(index, tokens):
         _symbol_expression_parser,
     )(index, tokens)
 
-def _multiplication_level_expression_parser(index, tokens):
-    failure = (False, index, None)
-
-    success, index, result = _literal_level_expression_parser(index, tokens)
-
-    if not success:
-        return failure
-
-    while success and index < len(tokens) and tokens[index].type == 'multiplication_level_operator':
-        success = False
-
-        if index + 1 < len(tokens):
-            success, try_index, value = _literal_level_expression_parser(index + 1, tokens)
+def _left_recursive_infix_operator_parser(token_type, operand_parser, operator_to_expression_type_mapping):
+    def result_parser(index, tokens):
+        failure = (False, index, None)
 
-        if success:
-            result = {
-                '*': FurMultiplicationExpression,
-                '//': FurIntegerDivisionExpression,
-                '%': FurModularDivisionExpression,
-            }[tokens[index].match](left=result, right=value)
-            index = try_index
+        success, index, result = operand_parser(index, tokens)
 
-    return True, index, result
+        if not success:
+            return failure
 
-def _addition_level_expression_parser(index, tokens):
-    failure = (False, index, None)
+        while success and index < len(tokens) and tokens[index].type == token_type:
+            success = False
 
-    success, index, result = _multiplication_level_expression_parser(index, tokens)
+            if index + 1 < len(tokens):
+                success, try_index, value = operand_parser(index + 1, tokens)
 
-    if not success:
-        return failure
+            if success:
+                result = operator_to_expression_type_mapping[tokens[index].match](left=result, right=value)
+                index = try_index
 
-    while success and index < len(tokens) and tokens[index].type == 'addition_level_operator':
-        success = False
+        return True, index, result
 
-        if index + 1 < len(tokens):
-            success, try_index, value = _multiplication_level_expression_parser(index + 1, tokens)
+    return result_parser
 
-        if success:
-            result = {
-                '+': FurAdditionExpression,
-                '-': FurSubtractionExpression,
-            }[tokens[index].match](left=result, right=value)
-            index = try_index
+def _multiplication_level_expression_parser(index, tokens):
+    return _left_recursive_infix_operator_parser(
+        'multiplication_level_operator',
+        _literal_level_expression_parser,
+        {
+            '*': FurMultiplicationExpression,
+            '//': FurIntegerDivisionExpression,
+            '%': FurModularDivisionExpression,
+        },
+    )(index, tokens)
 
-    return True, index, result
+def _addition_level_expression_parser(index, tokens):
+    return _left_recursive_infix_operator_parser(
+        'addition_level_operator',
+        _multiplication_level_expression_parser,
+        {
+            '+': FurAdditionExpression,
+            '-': FurSubtractionExpression,
+        },
+    )(index, tokens)
 
 def _equality_level_expression_parser(index, tokens):
-    failure = (False, index, None)
-
-    success, index, result = _addition_level_expression_parser(index, tokens)
-
-    if not success:
-        return failure
-
-    while success and index < len(tokens) and tokens[index].type == 'equality_level_operator':
-        success = False
-
-        if index + 1 < len(tokens):
-            success, try_index, value = _addition_level_expression_parser(index + 1, tokens)
-
-        if success:
-            result = {
-                '==': FurEqualityExpression,
-                '!=': FurInequalityExpression,
-                '>=': FurGreaterThanOrEqualExpression,
-                '<=': FurLessThanOrEqualExpression,
-                '>': FurGreaterThanExpression,
-                '<': FurLessThanExpression,
-            }[tokens[index].match](left=result, right=value)
-            index = try_index
-
-    return True, index, result
-
+    return _left_recursive_infix_operator_parser(
+        'equality_level_operator',
+        _addition_level_expression_parser,
+        {
+            '==': FurEqualityExpression,
+            '!=': FurInequalityExpression,
+            '>=': FurGreaterThanOrEqualExpression,
+            '<=': FurLessThanOrEqualExpression,
+            '>': FurGreaterThanExpression,
+            '<': FurLessThanExpression,
+        },
+    )(index, tokens)
 
 def _comma_separated_list_parser(index, tokens):
     failure = (False, index, None)