aboutsummaryrefslogtreecommitdiff
blob: 555747a8f853f249d98db2b10c7631827ad90dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
from ply import lex
from ply import yacc
import glob
import os

def expand(lst,variables):
    newlst = []
    for item in lst:
        if isinstance(item, list):
            strlst = com_interp(item[0],variables)
            newlst += expand(strlst,variables)
        else:
            newlst.append(item)

    return newlst

def com_interp(string,variables):
    tokens = (
            "COMMAND",
            "COMMA",
            "COL",
            "EQ",
            "TEXT",
            "PERCENT",
            "BEGINCOM",
            "ENDCOM",
            "SPACE",
            )
    states = (
            ("ccode", "exclusive"), #command code
            ("eval", "exclusive"), #code to evaluate
            )

    # Match the first $(. Enter ccode state.
    def t_eval_ccode(t):
        r'\$(\{|\()'
        t.lexer.code_start = t.lexer.lexpos        # Record the starting position
        t.lexer.level = 1                          # Initial level
        t.lexer.push_state('ccode')                # Enter 'ccode' state

    # Rules for the ccode state
    def t_ccode_newcom(t):
        r'\$(\{|\()'
        t.lexer.level +=1

    def t_ccode_endcom(t):
        r'(\}|\))'
        t.lexer.level -=1

        # If closing command, return the code fragment
        if t.lexer.level == 0:
             t.value = t.lexer.lexdata[t.lexer.code_start-1:t.lexer.lexpos]
             t.type = "COMMAND"
             t.lexer.pop_state()
             return t

    def t_ccode_text(t):
        r"[^\$\(\{\)\}]"

    def t_BEGINCOM(t):
        r"(\(|\{)"
        t.lexer.begin("eval")
        return t

    def t_eval_ENDCOM(t):
        r"(\)|\})"
        t.lexer.begin("INITIAL")
        return t

    def t_eval_PERCENT(t):
        r"\%"
        return t

    def t_eval_EQ(t):
        r"="
        return t

    def t_eval_COMMA(t):
        r",[ \t]*"
        return t

    def t_eval_COL(t):
        r":"
        return t

    def t_eval_TEXT(t):
        r"[^ \n\t:=\)\}\(\}\\\$,]+"
        return t

    def t_TEXT(t):
        r"[^ \t$\(\{]"
        return t

    def t_ANY_SPACE(t):
        r"[ \t]"
        return t

    def t_ANY_error(t):
        print("Illegal character '%s'" % t.value[0])
        t.lexer.skip(1)

    lexer = lex.lex()

    #lexer.input(string)
    #for tok in lexer:
    #    print(tok)


    #YACC stuff begins here

    def p_comp(p):
        """
        complst : BEGINCOM newstr ENDCOM
                | func
        """
        if len(p) == 4:
            p[0] = p[2]
        else:
            p[0] = p[1]

    def p_complst(p):
        "complst : compstr"
        p[0] = p[1].split()

    def p_compstr(p):
        """
        compstr : compstr BEGINCOM textstr ENDCOM
                | BEGINCOM textstr ENDCOM
                | compstr textstr
                | compstr spacestr
                | textstr
                | spacestr
        """
        p[0] = ""
        if len(p) == 4:
            for item in expand(variables[p[2]],variables):
                p[0] += item + " "
            p[0] = p[0][:-1]
        elif len(p) == 5:
            for item in expand(variables[p[3]],variables):
                p[1] += item + " "
                p[0] = p[1][:-1]
        elif len(p) == 3:
            p[0] = p[1] + p[2]
        else:
            p[0] = p[1]

    def p_tonewstr(p):
        """
        newstr  : getstr EQ textstr PERCENT textstr
                | getstr EQ PERCENT textstr
                | getstr EQ textstr PERCENT
                | getstr EQ PERCENT
                | getstr EQ textstr
        """
        newtextlist = []
        if p[1] == []:
            p[0] = p[1]
        elif len(p) == 6:
            for text in p[1]:
                newtextlist.append(p[3] + text + p[5])
            p[0] = newtextlist

        elif len(p) == 5:
            if p[3] == "%":
                for text in p[1]:
                    newtextlist.append(text + p[4])
                p[0] = newtextlist
            else:
                for text in p[1]:
                    newtextlist.append(p[3] + text)
                p[0] = newtextlist

        elif p[3] == "%":
            p[0] = p[1]
        else:
            for text in p[1]:
                newtextlist.append(text + p[3])
            p[0] = newtextlist


    def p_getstr(p):
        """
        getstr : textstr COL textstr PERCENT textstr
               | textstr COL PERCENT textstr
               | textstr COL textstr PERCENT
               | textstr COL PERCENT
               | textstr COL textstr
        """
        if not p[1] in variables:
            p[0] = []
        else:
            textlst = expand(variables[p[1]],variables) #make sure it's expanded
            newtextlst = []

            if len(p) == 6:
                l1 = len(p[3]) #length of str1
                l2 = len(p[5])
                for text in textlst:
                    if p[3] == text[0:l1] and p[5] == text[-l2:]:
                        newtextlst.append(text[l1:-l2])

                p[0] = newtextlst

            elif len(p) == 5:
                if p[3] == "%":
                    l1 = len(p[4])
                    for text in textlst:
                        if p[4] == text[-l1:]:
                            newtextlst.append(text[:-l1])

                    p[0] = newtextlst
                else:
                    l1 = len(p[3])
                    for text in textlst:
                        if p[3] == text[0:l1]:
                            newtextlst.append(text[l1:])

                    p[0] = newtextlst
            elif p[3] == "%":
                p[0] = textlst
            else:
                l1 = len(p[3])
                for text in textlst:
                    if p[3] == text[-l1:]:
                        newtextlst.append(text[:-l1])

                p[0] = newtextlst

    def p_func(p):
        """
        func : BEGINCOM textstr SPACE funcinput
        """
        #result = ["This calls a function"]
        result = funcdict[p[2]](p[4],variables)
        p[0] = result

    def p_funcinput(p):
        """
        funcinput : funcinput inputstr COMMA
                  | funcinput inputstr ENDCOM
                  | inputstr COMMA
                  | inputstr ENDCOM
        """
        if len(p) == 4:
            if "(" in p[2]: #command in the str
                p[1].append([p[2]])
            else:
                p[1].append(p[2])
            p[0] = p[1]
        else:
            if "(" in p[1]:
                p[0] = [[p[1]]]
            else:
                p[0] = [p[1]]

    def p_inputstr(p):
        """
        inputstr : inputstr spacestr
                 | inputstr TEXT
                 | inputstr COMMAND
                 | spacestr
                 | TEXT
                 | COMMAND
        """
        if len(p) == 3:
            p[0] = p[1] + p[2]
        else:
            p[0] = p[1]

    def p_command(p):
        """
        textstr : textstr COMMAND
                | COMMAND
        """
        if len(p) == 3:
            for item in com_interp(p[2],variables):
                p[1] += item + " "
            p[0] = p[1][:-1]
        else:
            p[0] = ""
            for item in com_interp(p[1],variables):
                p[0] += item + " "
            p[0] = p[0][:-1] #remove the last space

    def p_textstr(p):
        """
        textstr : textstr TEXT
                | TEXT
        """
        if len(p) == 3:
            p[0] = p[1] + p[2]
        else:
            p[0] = p[1]

    def p_spacestr(p):
        """
        spacestr : spacestr SPACE
                 | SPACE
        """
        if len(p) == 3:
            p[0] = p[1] + p[2]
        else:
            p[0] = p[1]

    def p_error(p):
        print("syntax error at '%s'" % p.type,p.lexpos)
        pass

    yacc.yacc()

    retlst = yacc.parse(string)

    #print(retlst)

    return retlst

def foreach(inputlst,variables):
    result = []
    var = expand(inputlst[0:1],variables)
    lst = expand(inputlst[1:2],variables)
    for item in lst:
        variables[var[0]] = [item]
        result += expand([inputlst[2]],variables)

    return result

def wildcard(inputlst,variables):
    command = expand(inputlst,variables)
    return glob.glob(command[0])

def shell(inputlst,variables):
    return ["dummy shell command"]

def notdir(inputlst,variables): #strip the dir from the file name
    if isinstance(inputlst[0],list):
        files = expand(inputlst,variables)
    else:
        files = inputlst[0].split()

    notdirf = []
    for file in files:
        notdirf.append(os.path.split(file)[1])

    return notdirf

funcdict = {
        "foreach" : foreach,
        "wildcard" : wildcard,
        "shell" : shell,
        "notdir" : notdir,
        }

#print(com_interp("(foreach var,hej hopp,$(var))",{"x":["y","z"], "y":[".py"], "z":["u"]}))