Grammalecte  Check-in [89ae59c7fa]

Overview
Comment:[build] graph builder: remove action duplicates
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | build | rg
Files: files | file ages | folders
SHA3-256: 89ae59c7fa5cde3a30da138f25e84a9fc0879b2669c4a0cb71f276f2f2066b7b
User & Date: olr on 2018-08-13 10:05:36
Other Links: branch diff | manifest | tags
Context
2018-08-13
20:52
[fr] conversion: regex rules -> graph rules check-in: 0dc636339c user: olr tags: fr, rg
10:05
[build] graph builder: remove action duplicates check-in: 89ae59c7fa user: olr tags: build, rg
09:20
[core] debug mode: label for immunity check-in: 7fe296fd4f user: olr tags: core, rg
Changes

Modified compile_rules_graph.py from [98519668b7] to [482c80fae9].

    12     12   
    13     13   dACTIONS = {}
    14     14   dFUNCTIONS = {}
    15     15   dFUNCNAME = {}
    16     16   
    17     17   
    18     18   def createFunction (sType, sActionId, sCode, bStartWithEqual=False):
    19         -    "create a function (stored in dFUNCTIONS) and return function name"
           19  +    "create a function (stored in <dFUNCTIONS>) and return function name"
    20     20       sCode = prepareFunction(sCode)
    21     21       if sType not in dFUNCNAME:
    22     22           dFUNCNAME[sType] = {}
    23     23       if sCode not in dFUNCNAME[sType]:
    24     24           dFUNCNAME[sType][sCode] = len(dFUNCNAME[sType])+1
    25     25       sFuncName = "_g_" + sType + "_" + str(dFUNCNAME[sType][sCode])
    26     26       dFUNCTIONS[sFuncName] = sCode
    27     27       return sFuncName  if not bStartWithEqual  else "="+sFuncName
    28     28   
           29  +
           30  +def storeAction (sActionId, aAction):
           31  +    "store <aAction> in <dACTIONS> avoiding duplicates"
           32  +    nVar = 0
           33  +    while True:
           34  +        sActionName = sActionId + str(nVar)
           35  +        if sActionName not in dACTIONS:
           36  +            dACTIONS[sActionName] = aAction
           37  +            return sActionName
           38  +        elif aAction == dACTIONS[sActionName]:
           39  +            return sActionName
           40  +        nVar += 1
           41  +
    29     42   
    30     43   def prepareFunction (sCode):
    31     44       "convert simple rule syntax to a string of Python code"
    32     45       if sCode[0:1] == "=":
    33     46           sCode = sCode[1:]
    34     47       sCode = sCode.replace("__also__", "bCondMemo")
    35     48       sCode = sCode.replace("__else__", "not bCondMemo")
................................................................................
   133    146                   iGroup += 1
   134    147                   dPos[iGroup] = i + 1    # we add 1, for we count tokens from 1 to n (not from 0)
   135    148   
   136    149           # Parse actions
   137    150           for iAction, sAction in enumerate(sActions.split(" <<- ")):
   138    151               sAction = sAction.strip()
   139    152               if sAction:
   140         -                sActionId = sRuleName + "__b" + str(iActionBlock) + "_l" + str(iLine) + "_a" + str(iAction) + "_" + str(len(lToken))
          153  +                sActionId = sRuleName + "__b" + str(iActionBlock) + "_a" + str(iAction)
   141    154                   aAction = createAction(sActionId, sAction, nPriority, dOptPriority, len(lToken), dPos)
   142    155                   if aAction:
   143         -                    dACTIONS[sActionId] = aAction
          156  +                    sActionName = storeAction(sActionId, aAction)
   144    157                       lResult = list(lToken)
   145         -                    lResult.extend(["##"+str(iLine), sActionId])
          158  +                    lResult.extend(["##"+str(iLine), sActionName])
   146    159                       #if iLine == 13341:
   147    160                       #    print("  ".join(lToken))
   148    161                       #    print(sActionId, aAction)
   149    162                       yield lResult
   150    163                   else:
   151    164                       print(" # Error on action at line:", iLine)
   152    165                       print(sTokenLine, "\n", sActions)