Grammalecte  Check-in [1d9246b161]

Overview
Comment:[build][core] gc engine update
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | core | build | rg
Files: files | file ages | folders
SHA3-256: 1d9246b16105c00694de326bff6d54a51e53d75ec8786a66d6bf4b53a356ded0
User & Date: olr on 2018-06-03 13:51:56
Other Links: branch diff | manifest | tags
Context
2018-06-03
17:43
[core] gc engine, text processor: remove useless commands, code clarification check-in: 6e4f6d12f0 user: olr tags: core, rg
13:51
[build][core] gc engine update check-in: 1d9246b161 user: olr tags: build, core, rg
09:05
[graphspell] default suggestions for French language check-in: fd12f98d29 user: olr tags: graphspell, rg
Changes

Modified compile_rules_graph.py from [e2184e369e] to [0bf3596a1c].

    37     37       s = re.sub(r"isNextNotCOD\(\s*\)", 'isNextNotCOD(dDA, s[m.end():], m.end())', s)        # isNextNotCOD(s)
    38     38       s = re.sub(r"isNextVerb\(\s*\)", 'isNextVerb(dDA, s[m.end():], m.end())', s)            # isNextVerb(s)
    39     39       s = re.sub(r"\bspell *[(]", '_oSpellChecker.isValid(', s)
    40     40       s = re.sub(r"[\\](\d+)", 'lToken[\\1]', s)
    41     41       return s
    42     42   
    43     43   
    44         -def changeReferenceToken (s, dPos):
    45         -    for i in range(len(dPos), 0, -1):
    46         -        s = s.replace("\\"+str(i), "\\"+str(dPos[i]))
    47         -    return s
    48         -
    49         -
    50         -def genTokenRules (sTokenLine):
           44  +def genTokenLines (sTokenLine):
           45  +    "tokenize a string and return a list of lines of tokens"
    51     46       lToken = sTokenLine.split()
    52         -    lTokenRules = None
           47  +    lTokenLines = None
    53     48       for i, sToken in enumerate(lToken):
    54     49           if sToken.startswith("{") and sToken.endswith("}") and sToken in dDEF:
    55     50               lToken[i] = dDEF[sToken]
    56     51           if ( (sToken.startswith("[") and sToken.endswith("]")) or (sToken.startswith("([") and sToken.endswith("])")) ):
    57     52               bSelectedGroup = sToken.startswith("(") and sToken.endswith(")")
    58     53               if bSelectedGroup:
    59     54                   sToken = sToken[1:-1]
    60     55               # multiple token
    61         -            if not lTokenRules:
    62         -                lTokenRules = [ [s]  for s  in sToken[1:-1].split("|") ]
           56  +            if not lTokenLines:
           57  +                lTokenLines = [ [s]  for s  in sToken[1:-1].split("|") ]
    63     58               else:
    64     59                   lNewTemp = []
    65         -                for aRule in lTokenRules:
           60  +                for aRule in lTokenLines:
    66     61                       lElem = sToken[1:-1].split("|")
    67     62                       sElem1 = lElem.pop(0)
    68     63                       if bSelectedGroup:
    69     64                           sElem1 = "(" + sElem1 + ")"
    70     65                       for sElem in lElem:
    71     66                           if bSelectedGroup:
    72     67                               sElem = "(" + sElem + ")"
    73     68                           aNew = list(aRule)
    74     69                           aNew.append(sElem)
    75     70                           lNewTemp.append(aNew)
    76     71                       aRule.append(sElem1)
    77         -                lTokenRules.extend(lNewTemp)
           72  +                lTokenLines.extend(lNewTemp)
    78     73           else:
    79     74               # simple token
    80         -            if not lTokenRules:
    81         -                lTokenRules = [[sToken]]
           75  +            if not lTokenLines:
           76  +                lTokenLines = [[sToken]]
    82     77               else:
    83         -                for aRule in lTokenRules:
           78  +                for aRule in lTokenLines:
    84     79                       aRule.append(sToken)
    85         -    for aRule in lTokenRules:
           80  +    for aRule in lTokenLines:
    86     81           yield aRule
    87     82   
    88     83   
    89     84   def createRule (iLine, sRuleName, sTokenLine, sActions, nPriority):
    90     85       # print(iLine, "//", sRuleName, "//", sTokenLine, "//", sActions, "//", nPriority)
    91         -    for lToken in genTokenRules(sTokenLine):
           86  +    for lToken in genTokenLines(sTokenLine):
    92     87           # Calculate positions
    93         -        dPos = {}
    94         -        nGroup = 0
           88  +        dPos = {}   # key: iGroup, value: iToken
           89  +        iGroup = 0
    95     90           for i, sToken in enumerate(lToken):
    96     91               if sToken.startswith("(") and sToken.endswith(")"):
    97     92                   lToken[i] = sToken[1:-1]
    98         -                nGroup += 1
    99         -                dPos[nGroup] = i
           93  +                iGroup += 1
           94  +                dPos[iGroup] = i + 1    # we add 1, for we count tokens from 1 to n (not from 0)
   100     95   
   101     96           # Parse actions
   102     97           for nAction, sAction in enumerate(sActions.split(" <<- ")):
   103     98               if sAction.strip():
   104     99                   sActionId = sRuleName + "_a" + str(nAction)
   105         -                aAction = createAction(sActionId, sAction, nGroup, nPriority, dPos)
          100  +                aAction = createAction(sActionId, sAction, nPriority, len(lToken), dPos)
   106    101                   if aAction:
   107    102                       dACTIONS[sActionId] = aAction
   108    103                       lResult = list(lToken)
   109    104                       lResult.extend(["##"+str(iLine), sActionId])
   110    105                       yield lResult
   111    106   
   112    107   
   113         -def createAction (sIdAction, sAction, nGroup, nPriority, dPos):
          108  +def changeReferenceToken (s, dPos):
          109  +    for i in range(len(dPos), 0, -1):
          110  +        s = s.replace("\\"+str(i), "\\"+str(dPos[i]))
          111  +    return s
          112  +
          113  +
          114  +def createAction (sIdAction, sAction, nPriority, nToken, dPos):
   114    115       m = re.search("([-~=])(\\d+|)(:\\d+|)>> ", sAction)
   115    116       if not m:
   116    117           print(" # Error. No action found at: ", sIdAction)
   117    118           print("   ==", sAction, "==")
   118    119           return None
   119    120       # Condition
   120    121       sCondition = sAction[:m.start()].strip()
................................................................................
   127    128           sCondition = ""
   128    129       # Action
   129    130       cAction = m.group(1)
   130    131       sAction = sAction[m.end():].strip()
   131    132       sAction = changeReferenceToken(sAction, dPos)
   132    133       iStartAction = int(m.group(2))  if m.group(2)  else 0
   133    134       iEndAction = int(m.group(3)[1:])  if m.group(3)  else iStartAction
   134         -    if nGroup:
          135  +    if dPos:
   135    136           try:
   136    137               iStartAction = dPos[iStartAction]
   137    138               iEndAction = dPos[iEndAction]
   138    139           except:
   139    140               print("# Error. Wrong groups in: " + sIdAction)
   140    141   
   141    142       if cAction == "-":
................................................................................
   153    154               if mURL:
   154    155                   sURL = mURL.group(1).strip()
   155    156                   sMsg = sMsg[:mURL.start(0)].strip()
   156    157               if sMsg[0:1] == "=":
   157    158                   sMsg = prepareFunction(sMsg[1:])
   158    159                   lFUNCTIONS.append(("g_m_"+sIdAction, sMsg))
   159    160                   for x in re.finditer("group[(](\\d+)[)]", sMsg):
   160         -                    if int(x.group(1)) > nGroup:
   161         -                        print("# Error in groups in message at line " + sIdAction + " ("+str(nGroup)+" groups only)")
          161  +                    if int(x.group(1)) > nToken:
          162  +                        print("# Error in token index in message at line " + sIdAction + " ("+str(nToken)+" tokens only)")
   162    163                   sMsg = "=g_m_"+sIdAction
   163    164               else:
   164    165                   for x in re.finditer(r"\\(\d+)", sMsg):
   165         -                    if int(x.group(1)) > nGroup:
   166         -                        print("# Error in groups in message at line " + sIdAction + " ("+str(nGroup)+" groups only)")
          166  +                    if int(x.group(1)) > nToken:
          167  +                        print("# Error in token index in message at line " + sIdAction + " ("+str(nToken)+" tokens only)")
   167    168                   if re.search("[.]\\w+[(]", sMsg):
   168    169                       print("# Error in message at line " + sIdAction + ":  This message looks like code. Line should begin with =")
   169    170               
   170    171       if sAction[0:1] == "=" or cAction == "=":
   171    172           if "define" in sAction and not re.search(r"define\(\\\d+ *, *\[.*\] *\)", sAction):
   172    173               print("# Error in action at line " + sIdAction + ": second argument for define must be a list of strings")
   173    174           sAction = prepareFunction(sAction)
   174    175           for x in re.finditer("group[(](\\d+)[)]", sAction):
   175         -            if int(x.group(1)) > nGroup:
   176         -                print("# Error in groups in replacement at line " + sIdAction + " ("+str(nGroup)+" groups only)")
          176  +            if int(x.group(1)) > nToken:
          177  +                print("# Error in token index in replacement at line " + sIdAction + " ("+str(nToken)+" tokens only)")
   177    178       else:
   178    179           for x in re.finditer(r"\\(\d+)", sAction):
   179         -            if int(x.group(1)) > nGroup:
   180         -                print("# Error in groups in replacement at line " + sIdAction + " ("+str(nGroup)+" groups only)")
          180  +            if int(x.group(1)) > nToken:
          181  +                print("# Error in token index in replacement at line " + sIdAction + " ("+str(nToken)+" tokens only)")
   181    182           if re.search("[.]\\w+[(]|sugg\\w+[(]", sAction):
   182    183               print("# Error in action at line " + sIdAction + ":  This action looks like code. Line should begin with =")
   183    184   
   184    185       if cAction == "-":
   185    186           ## error detected --> suggestion
   186    187           if not sAction:
   187    188               print("# Error in action at line " + sIdAction + ":  This action is empty.")
................................................................................
   330    331               continue
   331    332           sPyCallables += "def {} ({}):\n".format(sFuncName, sParams)
   332    333           sPyCallables += "    return " + sReturn + "\n"
   333    334           #sJSCallables += "    {}: function ({})".format(sFuncName, sParams) + " {\n"
   334    335           #sJSCallables += "        return " + jsconv.py2js(sReturn) + ";\n"
   335    336           #sJSCallables += "    },\n"
   336    337       #sJSCallables += "}\n"
          338  +
          339  +    for sActionName, aAction in dACTIONS.items():
          340  +        print(sActionName, aAction)
   337    341   
   338    342       # Result
   339    343       d = {
   340    344           "graph_callables": sPyCallables,
   341    345           "graph_gctests": sGCTests,
   342    346           "rules_graph": oRuleGraph,
   343    347           "rules_actions": dACTIONS
   344    348       }
   345    349   
   346    350       return d
   347    351   
   348    352   

Modified gc_core/py/lang_core/gc_engine.py from [d669c0fe4f] to [dc1fba7ea9].

   728    728               lPointer.extend(lNewPointer)
   729    729               # check arcs of first nodes
   730    730               for dNode in self._getNextMatchingNodes(dToken, dGraph[0]):
   731    731                   lPointer.append({"nOffset": dToken["i"], "dNode": dNode})
   732    732               # check if there is rules to check for each pointer
   733    733               for dPointer in lPointer:
   734    734                   if "<rules>" in dPointer["dNode"]:
   735         -                    bHasChanged, errs = self._executeActions(dPointer["dNode"]["<rules>"], dPointer["nOffset"], dPriority, dOpt, bShowRuleId, bContext)
          735  +                    bHasChanged, errs = self._executeActions(dPointer["dNode"]["<rules>"], dPointer["nOffset"]-1, dPriority, dOpt, bShowRuleId, bContext)
   736    736                       dErr.update(errs)
   737    737                       if bHasChanged:
   738    738                           bChange = True
   739    739           if dErr:
   740    740               print(dErr)
   741    741           return (bChange, dErr)
   742    742