changeset 3912:01ba9b2e96f6

ParserBase: refactoring: reduce code duplication, remove unused code, rearrange
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 02 Aug 2008 14:33:40 +0200
parents 348c9b6e0033
children ab09415de20b
files MoinMoin/parser/_ParserBase.py
diffstat 1 files changed, 11 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/parser/_ParserBase.py	Sat Aug 02 13:48:59 2008 +0200
+++ b/MoinMoin/parser/_ParserBase.py	Sat Aug 02 14:33:40 2008 +0200
@@ -157,10 +157,12 @@
         self._formatting_rules_n2r = {}
         self._formatting_rule_index = 0
         self.rule_fmt = {}
-        self.line_count = len(raw.split('\n')) + 1
+        #self.line_count = len(raw.split('\n')) + 1
 
     def setupRules(self):
         self.def_format = FormatText('Default')
+        self.reserved_word_format = FormatText('ResWord')
+        self.constant_word_format = FormatText('ConsWord')
         self.ID_format = FormatTextID('ID', self._ignore_case)
         self.addRuleFormat("ID", self.ID_format)
         self.addRuleFormat("Operator")
@@ -175,22 +177,18 @@
         self.addRuleFormat("Special")
         self.addRuleFormat("Preprc")
         self.addRuleFormat("Error")
-        self.reserved_word_format = FormatText('ResWord')
-        self.constant_word_format = FormatText('ConsWord')
+
+    def _addRule(self, name, fmt):
+        self._formatting_rule_index += 1
+        name = "%s_%s" % (name, self._formatting_rule_index) # create unique name
+        self._formatting_rules.append((name, fmt))
+        self._formatting_rules_n2r[name] = fmt
 
     def addRule(self, name, str_re):
-        self._formatting_rule_index += 1
-        n = "%s_%s" % (name, self._formatting_rule_index)
-        f = FormattingRuleSingle(name, str_re, self._ignore_case)
-        self._formatting_rules.append((n, f))
-        self._formatting_rules_n2r[n] = f
+        self._addRule(name, FormattingRuleSingle(name, str_re, self._ignore_case))
 
     def addRulePair(self, name, start_re, end_re):
-        self._formatting_rule_index += 1
-        n = "%s_%s" % (name, self._formatting_rule_index)
-        f = FormattingRulePair(name, start_re, end_re, self._ignore_case)
-        self._formatting_rules.append((n, f))
-        self._formatting_rules_n2r[n] = f
+        self._addRule(name, FormattingRulePair(name, start_re, end_re, self._ignore_case))
 
     def addWords(self, words, fmt):
         if not isinstance(fmt, FormatTextBase):