Grammalecte  Check-in [34a15fd0fb]

Overview
Comment:[build] code clarification for graph rules reader
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | build | rg
Files: files | file ages | folders
SHA3-256: 34a15fd0fb9e8528eec642d6a8ad8f7f6c2c7a0d1811f532e9b4a66b335e10b7
User & Date: olr on 2018-06-22 08:40:54
Other Links: branch diff | manifest | tags
Context
2018-06-22
11:11
[build][core][fr] tags for sentence check-in: 22a9b5a923 user: olr tags: fr, core, build, rg
08:40
[build] code clarification for graph rules reader check-in: 34a15fd0fb user: olr tags: build, rg
06:39
[fr] conversion: regex rules -> graph rules check-in: 37884ecdd4 user: olr tags: fr, rg
Changes

Modified compile_rules_graph.py from [c0410177dd] to [06e4c2c6a2].

116
117
118
119
120
121
122
123

124
125
126


127
128













129

130
131
132
133
134
135
136
137
138
139

140
141
142
143
144
145
146
147
148


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174

175
176
177
178
179
180
181
182

183
184
185

186
187
188
189

190
191
192
193
194
195

196
197


198
199
200
201







202
203
204
205

206
207
208
209
210
211
212
213
214
215


216
217
218
219

220
221
222
223
224
225
226
227


228
229
230
231
232
233
234
235
236


237
238
239


240
241
242
243
244
245

246
247
248
249
250
251
252
116
117
118
119
120
121
122

123
124


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

142
143
144
145
146
147
148
149
150
151

152
153
154
155
156
157
158
159


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

179
180
181
182
183
184
185
186

187
188
189
190
191
192
193
194
195
196
197
198

199




200
201





202
203
204
205
206




207
208
209
210
211
212
213


214

215

216
217
218


219
220


221
222
223
224
225

226
227
228
229


230


231
232
233
234
235
236
237
238
239


240
241
242


243
244
245



246

247
248
249
250
251
252
253
254







-
+

-
-
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
-
+









-
+







-
-
+
+

















-
+







-
+








+


-
+
-
-
-
-
+

-
-
-
-
-
+


+
+
-
-
-
-
+
+
+
+
+
+
+
-
-

-
+
-



-
-


-
-
+
+



-
+



-
-

-
-
+
+







-
-
+
+

-
-
+
+

-
-
-

-
+







                if aAction:
                    dACTIONS[sActionId] = aAction
                    lResult = list(lToken)
                    lResult.extend(["##"+str(iLine), sActionId])
                    yield lResult


def changeReferenceToken (s, dPos):
def changeReferenceToken (sText, dPos):
    for i in range(len(dPos), 0, -1):
        s = s.replace("\\"+str(i), "\\"+str(dPos[i]))
    return s
        sText = sText.replace("\\"+str(i), "\\"+str(dPos[i]))
    return sText


def checkTokenNumbers (sText, sActionId, nToken):
    for x in re.finditer(r"\\(\d+)", sText):
        if int(x.group(1)) > nToken:
            print("# Error in token index at line " + sActionId + " ("+str(nToken)+" tokens only)")
            print(sText)


def checkIfThereIsCode (sText, sActionId):
    if re.search("[.]\\w+[(]|sugg\\w+[(]|\\([0-9]|\\[[0-9]", sText):
        print("# Warning at line " + sActionId + ":  This message looks like code. Line should probably begin with =")
        print(sText)


def createAction (sIdAction, sAction, nPriority, nToken, dPos):
def createAction (sActionId, sAction, nPriority, nToken, dPos):
    # Option
    sOption = False
    m = re.match("/(\\w+)/", sAction) 
    if m:
        sOption = m.group(1)
        sAction = sAction[m.end():].strip()
    # valid action?
    m = re.search("(?P<action>[-~=])(?P<start>\\d+|)(?P<end>:\\d+|)>> ", sAction)
    if not m:
        print(" # Error. No action found at: ", sIdAction)
        print(" # Error. No action found at: ", sActionId)
        print("   ==", sAction, "==")
        return None
    # Condition
    sCondition = sAction[:m.start()].strip()
    if sCondition:
        sCondition = prepareFunction(sCondition)
        sCondition = changeReferenceToken(sCondition, dPos)    
        dFUNCTIONS["g_c_"+sIdAction] = sCondition
        sCondition = "g_c_"+sIdAction
        dFUNCTIONS["g_c_"+sActionId] = sCondition
        sCondition = "g_c_"+sActionId
    else:
        sCondition = ""
    # Action
    cAction = m.group("action")
    sAction = sAction[m.end():].strip()
    sAction = changeReferenceToken(sAction, dPos)
    if not m.group("start"):
        iStartAction = 1
        iEndAction = 0
    else:
        iStartAction = int(m.group("start"))
        iEndAction = int(m.group("end")[1:])  if m.group("end")  else iStartAction
    if dPos:
        try:
            iStartAction = dPos[iStartAction]
            iEndAction = dPos[iEndAction]
        except:
            print("# Error. Wrong groups in: " + sIdAction)
            print("# Error. Wrong groups in: " + sActionId)

    if cAction == "-":
        ## error
        iMsg = sAction.find(" # ")
        if iMsg == -1:
            sMsg = "# Error. Error message not found."
            sURL = ""
            print(sMsg + " Action id: " + sIdAction)
            print(sMsg + " Action id: " + sActionId)
        else:
            sMsg = sAction[iMsg+3:].strip()
            sAction = sAction[:iMsg].strip()
            sURL = ""
            mURL = re.search("[|] *(https?://.*)", sMsg)
            if mURL:
                sURL = mURL.group(1).strip()
                sMsg = sMsg[:mURL.start(0)].strip()
            checkTokenNumbers(sMsg, sActionId, nToken)
            if sMsg[0:1] == "=":
                sMsg = prepareFunction(sMsg[1:], True)
                dFUNCTIONS["g_m_"+sIdAction] = sMsg
                dFUNCTIONS["g_m_"+sActionId] = sMsg
                for x in re.finditer("lToken\\[(\\d+)\\]", sMsg):
                    if int(x.group(1)) > nToken:
                        print("# Error in token index in message at line " + sIdAction + " ("+str(nToken)+" tokens only)")
                sMsg = "=g_m_"+sIdAction
                sMsg = "=g_m_"+sActionId
            else:
                for x in re.finditer(r"\\(\d+)", sMsg):
                    if int(x.group(1)) > nToken:
                        print("# Error in token index in message at line " + sIdAction + " ("+str(nToken)+" tokens only)")
                if re.search("[.]\\w+[(]", sMsg):
                    print("# Error in message at line " + sIdAction + ":  This message looks like code. Line should begin with =")
                checkIfThereIsCode(sMsg, sActionId)
            
    # checking consistancy
    checkTokenNumbers(sAction, sActionId, nToken)

    if cAction == "=" or sAction[0:1] == "=":
        if "define" in sAction and not re.search(r"define\(\\\d+ *, *\[.*\] *\)", sAction):
            print("# Error in action at line " + sIdAction + ": second argument for define must be a list of strings")
        for x in re.finditer(r"\\(\d+)", sAction):
    if cAction == ">":
        ## no action, break loop if condition is False
        return [sOption, sCondition, cAction, ""]

    if not sAction:
        print("# Error in action at line " + sActionId + ":  This action is empty.")

            if int(x.group(1)) > nToken:
                print("# Error in token index in replacement at line " + sIdAction + " ("+str(nToken)+" tokens only)")
    if sAction[0:1] != "=":
        if re.search("[.]\\w+[(]|sugg\\w+[(]", sAction):
        checkIfThereIsCode(sAction, sActionId)
            print("# Error in action at line " + sIdAction + ":  This action looks like code. Line should begin with =")

    if cAction == "-":
        ## error detected --> suggestion
        if not sAction:
            print("# Error in action at line " + sIdAction + ":  This action is empty.")
        if sAction[0:1] == "=":
            sAction = prepareFunction(sAction, True)
            dFUNCTIONS["g_s_"+sIdAction] = sAction[1:]
            sAction = "=g_s_"+sIdAction
            dFUNCTIONS["g_s_"+sActionId] = sAction[1:]
            sAction = "=g_s_"+sActionId
        elif sAction.startswith('"') and sAction.endswith('"'):
            sAction = sAction[1:-1]
        if not sMsg:
            print("# Error in action at line " + sIdAction + ":  The message is empty.")
            print("# Error in action at line " + sActionId + ":  The message is empty.")
        return [sOption, sCondition, cAction, sAction, iStartAction, iEndAction, nPriority, sMsg, sURL]
    elif cAction == "~":
        ## text processor
        if not sAction:
            print("# Error in action at line " + sIdAction + ":  This action is empty.")
        if sAction[0:1] == "=":
            dFUNCTIONS["g_p_"+sIdAction] = sAction[1:]
            sAction = "=g_p_"+sIdAction
            dFUNCTIONS["g_p_"+sActionId] = sAction[1:]
            sAction = "=g_p_"+sActionId
        elif sAction.startswith('"') and sAction.endswith('"'):
            sAction = sAction[1:-1]
        return [sOption, sCondition, cAction, sAction, iStartAction, iEndAction]
    elif cAction == "=":
        ## disambiguator
        if sAction[0:1] == "=":
            sAction = sAction[1:]
        if not sAction:
            print("# Error in action at line " + sIdAction + ":  This action is empty.")
        if "define" in sAction and not re.search(r"define\(\\\d+ *, *\[.*\] *\)", sAction):
            print("# Error in action at line " + sActionId + ": second argument for <define> must be a list of strings")
        sAction = prepareFunction(sAction)
        dFUNCTIONS["g_d_"+sIdAction] = sAction
        sAction = "g_d_"+sIdAction
        dFUNCTIONS["g_d_"+sActionId] = sAction
        sAction = "g_d_"+sActionId
        return [sOption, sCondition, cAction, sAction]
    elif cAction == ">":
        ## no action, break loop if condition is False
        return [sOption, sCondition, cAction, ""]
    else:
        print("# Unknown action at line " + sIdAction)
        print("# Unknown action at line " + sActionId)
        return None


def make (lRule, dDef, sLang, bJavaScript):
    "compile rules, returns a dictionary of values"
    # for clarity purpose, don’t create any file here