Esempio n. 1
0
 def _setI_Rules(self):
     # pre-process rules into strings or copiled regex
     # splitting regex parts and counting match groups
     # in i_rules indexed by lhs
     # with rhs sorted by inverse (clause count, rhs clause string length)
     i_rules = mtutils.sdict()
     for rule in self._rules:
         equation = rule.split("::-")
         if len(equation) == 2:  # only if lhs and rhs
             ok = 1  # default ok
             uc = 0  # dummy usage count for later sorting by most used first
             ep = mtutils.slist()
             for c in range(2):
                 equation[c] = equation[c].strip()  # strip lhs and rhs
             if equation[1].startswith("/") and equation[1].endswith("/"):  # rhs is a regex
                 ep = mtutils._splitRegex(equation[1])
                 try:  # try processing regex:
                     ec, pc = mtutils._pdepth(ep[0])  #  count and validate inner parenthesis
                     if ec == 1:
                         raise mterrors.UnbalancedParenthesisError(ep[0])  #  test error code to raise errors
                     elif ec == 2:
                         raise mterrors.UnspecifiedParenthesisError()
                     self._showDebug(
                         clas="Parser",
                         method="_setI_Rules",
                         line=177,
                         level=1,
                         vars=[["equation[1]", equation[1]], ["ep", ep], ["ok", ok], ["pc", pc]],
                     )
                     if not ep[0].startswith("^"):
                         ep[0] = "^" + ep[0]  #   ensure it matches from beginning
                     try:
                         equation[1] = re.compile(ep[0])  #   try compiling the pattern
                     except:
                         raise mterrors.ParseInvalidRegexError(ep[0])  #   escalate failure
                 except mterrors.ParseError, X:
                     X._notify(c="Parser", m="_setI_Rules()")  # notify ParseError
             elif equation[1].startswith("{") and equation[1].endswith("}"):  # rhs is a userexit Element method
                 equation[1] = mtpexits.Test(equation[1][1:-1].strip())  #  instantiate rhs a userexit Test
             else:  # rhs is a string or clause(s)
                 equation[1] = safesplit.sub(">~#!@!#~<", equation[1])  #  sub special clause seperator
             self._showDebug(
                 clas="Parser",
                 method="_setI_Rules",
                 line=188,
                 level=1,
                 vars=[["equation[0]", equation[0]], ["equation[1]", equation[1]]],
             )
             if ok:  # add to i_rules if OK
                 if equation[0] not in i_rules:  #  if rhs not in
                     i_rules[equation[0]] = [[equation[1], uc, ep]]  #   add it
                     self._showDebug(
                         clas="Parser", method="_setI_Rules", line=192, level=1, vars=[["equation[0]", equation[0]]]
                     )
                 else:  # insert new lhs into list sorted by size
                     l1 = len(i_rules[equation[0]])  # set up manual break and list counter
                     c = -1
                     while c + 1 < l1:
                         c += 1
                         self._showDebug(
                             clas="Parser",
                             method="_setI_Rules",
                             line=198,
                             level=2,
                             vars=[
                                 ["l1", l1],
                                 ["c", c],
                                 ["i_rules[equation[0]][c]", i_rules[equation[0]][c]],
                                 ["equation[1]", equation[1]],
                             ],
                         )
                         if i_rules[equation[0]][c][1] < uc:  # if new rhs has more clauses
                             i_rules[equation[0]][c:c] = [
                                 [equation[1], uc, ep]
                             ]  #   insert it here into the rhs list
                             c = l1  #   and break
                         elif i_rules[equation[0]][c][0] == equation[1]:
                             c = l1  # elif rhs is not new break
                         elif c + 1 == l1:  # elif at end of list
                             i_rules[equation[0]] += [[equation[1], uc, ep]]  # append new rhs to list
                     self._showDebug(
                         clas="Parser",
                         method="_setI_Rules",
                         line=205,
                         level=1,
                         vars=[["i_rules[equation[0]]", i_rules[equation[0]]]],
                     )