commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r8440 - trunk/gnue-common/src/formatting/masks


From: jamest
Subject: [gnue] r8440 - trunk/gnue-common/src/formatting/masks
Date: Wed, 19 Apr 2006 19:56:17 -0500 (CDT)

Author: jamest
Date: 2006-04-19 19:56:17 -0500 (Wed, 19 Apr 2006)
New Revision: 8440

Modified:
   trunk/gnue-common/src/formatting/masks/InputMask.py
Log:
moved the lexicon generator out of the __init__
cleanup


Modified: trunk/gnue-common/src/formatting/masks/InputMask.py
===================================================================
--- trunk/gnue-common/src/formatting/masks/InputMask.py 2006-04-19 21:46:27 UTC 
(rev 8439)
+++ trunk/gnue-common/src/formatting/masks/InputMask.py 2006-04-20 00:56:17 UTC 
(rev 8440)
@@ -365,59 +365,11 @@
         
         return inputTokens
 
-    # 
===========================================================================
-    #  Internal lexicon init crap
-    # 
===========================================================================
-
-    def __init__(self, mask, numeric=False, date=False):
+    def _generateLexicon(self, tokens):
         """
-        InputMask constructor
-
-        InputMasks can be of 3 differnt types (text, numeric, and date).  The 
type of
-        input mask is determined by the boolean values of the numeric and date
-        arguments to the constructor.  The default mask type is text but if 
either
-        of the two booleans are set then the mask type switchs to that.
-
-        When building an input mask 2 token lists are built.  The initial 
token list
-        is created from the mask text passed into the constructor.  This token 
list
-
-        @param mask: The input mask the initialized instance of the class 
should
-                     support
-        @param numeric: Is this input mask numeric input only?
-        @param date: Is this input mask date input only?
+        Build a lexicon from a list of input tokens
         """
-
-        # 
-------------------------------------------------------------------------
-        # Generate a list of parser tokens that define the input mask
-        # 
-------------------------------------------------------------------------
-        #
-        parser = MaskTokenizer.MaskTokenizer(mask,'inline')
-
-        self.pp = pprint.PrettyPrinter(indent=4)
-        self.isnumeric = numeric
-        self.isdate = date
-
-        # If non-zero, position of the right-to-left token
-        rtl_pos = self.rtl_pos = parser.rtl_pos
-
-        # Set the type of input mask based upon
-        # the parser text, numeric, or date
-        self.type = parser.type
-
-        validationRule = None
-
-        # This contains a list of each token's "empty" marker,
-        # which will usually be '_' or, if a literal, the literal's
-        # value.
-        self.emptyDisplay = []
-
-        # List of all tokens. Note that all {#}
-        # expansions have already happened.
-        self.tokens = tokens = self._generateInputTokens(parser.tokens)
-
-        # 
-------------------------------------------------------------------------
-        # Next, we will build the actual lexicon. We start
-        # at the end of the mask and work backwards, as
+        # We start at the end of the mask and work backwards, as
         # any optional mask tokens will need to reference the
         # next token's initial grammar elements.
         #
@@ -429,28 +381,33 @@
         #
         # Each path represents one text string which would
         # pass the lexicons test.
-        # 
-------------------------------------------------------------------------
-        i = len(tokens)
+
+        tokenIndex = len(tokens)
+
         lexicon = [
-           # The first rule will always be Bol (to init stuff)
-           State("", [(Bol, Begin((0,0,0))) ]),
-           # The final rule prevents any trailing characters
-           # The Eof is just a dummy rule that won't ever be matched.
-           State((i,0,0), [(Eof, IGNORE)]) ]
+             # The first rule will always be Bol (to init stuff)
+             State("", [(Bol, Begin((0,0,0))) ]),
+             # The final rule prevents any trailing characters
+             # The Eof is just a dummy rule that won't ever be matched.
+             State((tokenIndex,0,0), [(Eof, IGNORE)]) 
+           ]
 
+        leadin = []
         last_leadin = []
-        leadin = []
 
-        while i > 0:
+        while tokenIndex > 0:
             # Iterate backward through the tokens in the input mask
-            i -= 1
-            token = tokens[i]
-            if not token.optional:
+            tokenIndex -= 1
+            currentToken = tokens[tokenIndex]
+            if not currentToken.optional:
                 leadin = []
 
-            # Iterate forward through each token's path lists (ruleset)
-            j = 0 # j is used in the naming of the next valid lexicon state
-            for ruleset in token.paths:
+            # Iterate forward through each token's path lists (ruleset) =======
+
+            lexiconStateCount = 0 # used in the naming of the next 
+                                  # valid lexicon state
+
+            for ruleset in currentToken.paths:
                 ks = 0
                 possibly_completed = False  # Once a ruleset encounters an 
object
                                             # of class forcible then it is 
possible
@@ -458,15 +415,15 @@
 
                 # Iterate forward through the ruleset and define a (pattern, 
action)
                 # tuple (rule) for the lexicon we are constructing
-                for k in range(len(ruleset)):
-                    path = ruleset[k]
+                for rulesetIndex in range(len(ruleset)):
+                    path = ruleset[rulesetIndex]
                     lexi = []
 
                     # See if the current rule may be the last one needed
                     # to complete the ruleset
                     try:
                         possibly_completed = possibly_completed or \
-                                             ruleset[k+1]==InputTokens.forcible
+                                             
ruleset[rulesetIndex+1]==InputTokens.forcible
                     except IndexError:
                         pass
 
@@ -474,15 +431,15 @@
                     # as they are not actually tokens
                     if not path == InputTokens.forcible:
 
-                        if (k < len(ruleset) - 1):
+                        if (rulesetIndex < len(ruleset) - 1):
                             # There are additional items in this ruleset so
                             # set the next state to point to the next rule in 
the
                             # set
-                            next_state = (i, j, ks+1)
+                            next_state = (tokenIndex, lexiconStateCount, ks+1)
                         else:
                             # There are no additional rules in this ruleset so
                             # set the next state to the next character's tokens
-                            next_state = (i+1,0,0)
+                            next_state = (tokenIndex+1,0,0)
 
                         # Construct the lexicon pattern (rule) and store with a
                         # lambda based action function.  Note that the lambda
@@ -490,11 +447,11 @@
                         # are the scanner(parser), text arguments that plex 
will pass to
                         # the action function
                         rule = (path,
-                           lambda p, t, c=self._tokenFound, st=(i, j, ks), 
ns=next_state:
-                              c(p, t, st, ns))
+                           lambda p, t, st=(tokenIndex, lexiconStateCount, 
ks), ns=next_state:
+                              self._tokenFound(p, t, st, ns))
 
                         # Store the first rule of each path list
-                        if k == 0:
+                        if rulesetIndex == 0:
                             leadin.append(rule)
 
                         # Add the rule to the list of rules to be inserted into
@@ -508,20 +465,71 @@
                             lexi += last_leadin
 
                         # I
-                        if j or ks:
-                            lexicon.append((State((i, j, ks), lexi)))
+                        if lexiconStateCount or ks:
+                            lexicon.append((State((tokenIndex, 
lexiconStateCount, ks), lexi)))
                         ks += 1
-                j += 1
-##      print "lexicon"
-##      self.pp.pprint(leadin[:])
+                lexiconStateCount += 1
+
             # Append the created state to the main lexicon
             # we are creating
-            lexicon.append(State((i,0,0), leadin[:]))
-            print "Leadin", leadin
+            lexicon.append(State((tokenIndex,0,0), leadin[:]))
             last_leadin = leadin # Assign the current leadin to previous leadin
                                  # this will be used in the next iteration to
-                                 #
+        return lexicon
 
+    # 
===========================================================================
+    #  Internal lexicon init crap
+    # 
===========================================================================
+
+    def __init__(self, mask, numeric=False, date=False):
+        """
+        InputMask constructor
+
+        InputMasks can be of 3 differnt types (text, numeric, and date).  The 
type of
+        input mask is determined by the boolean values of the numeric and date
+        arguments to the constructor.  The default mask type is text but if 
either
+        of the two booleans are set then the mask type switchs to that.
+
+        When building an input mask 2 token lists are built.  The initial 
token list
+        is created from the mask text passed into the constructor.  This mask 
token
+        list is then used to create a list of input tokens.  Input tokens 
contain
+        the lexicon rules required to 
+
+        @param mask: The input mask the initialized instance of the class 
should
+                     support
+        @param numeric: Is this input mask numeric input only?
+        @param date: Is this input mask date input only?
+        """
+
+        # 
-------------------------------------------------------------------------
+        # Generate a list of parser tokens that define the input mask
+        # 
-------------------------------------------------------------------------
+        #
+        parser = MaskTokenizer.MaskTokenizer(mask,'inline')
+
+        self.pp = pprint.PrettyPrinter(indent=4)
+        self.isnumeric = numeric
+        self.isdate = date
+
+        # If non-zero, position of the right-to-left token
+        rtl_pos = self.rtl_pos = parser.rtl_pos
+
+        # Set the type of input mask based upon
+        # the parser text, numeric, or date
+        self.type = parser.type
+
+        validationRule = None
+
+        # This contains a list of each token's "empty" marker,
+        # which will usually be '_' or, if a literal, the literal's
+        # value.
+        self.emptyDisplay = []
+
+        # List of all tokens. Note that all {#}
+        # expansions have already happened.
+        self.tokens = tokens = self._generateInputTokens(parser.tokens)
+        lexicon = self._generateLexicon(tokens)
+        
         InputTokens.printLexiconTree(lexicon)
 
         # Create a consolidated validation rule so we





reply via email to

[Prev in Thread] Current Thread [Next in Thread]