diff options
author | 2011-06-17 00:08:35 +0200 | |
---|---|---|
committer | 2011-06-17 00:08:35 +0200 | |
commit | efc1cb91507f83a2abdfcc531a1e4b06af9d89b6 (patch) | |
tree | bec2925693b098b41d36264d983e20e434434acf | |
parent | Added branch for the new makefile parser (diff) | |
download | ebuildgen-efc1cb91507f83a2abdfcc531a1e4b06af9d89b6.tar.gz ebuildgen-efc1cb91507f83a2abdfcc531a1e4b06af9d89b6.tar.bz2 ebuildgen-efc1cb91507f83a2abdfcc531a1e4b06af9d89b6.zip |
Worked some more on string replacement
-rw-r--r-- | filetypes/makefilecom.py | 124 | ||||
-rw-r--r-- | filetypes/makefiles.py | 15 |
2 files changed, 116 insertions, 23 deletions
diff --git a/filetypes/makefilecom.py b/filetypes/makefilecom.py index 6bfd29b..e361f8c 100644 --- a/filetypes/makefilecom.py +++ b/filetypes/makefilecom.py @@ -1,10 +1,22 @@ from ply import lex from ply import yacc -def com_interp(string): +def expand(lst,variables): + newlst = [] + for item in lst: + if isinstance(item, list): + strlst = com_interp(item[0],variables) + netlst += expand(strlst,variables) + else: + newlst.append(item) + + return newlst + +def com_interp(string,variables): tokens = ( "COMMAND", "COMMA", + "COL", "EQ", "TEXT", "PERCENT", @@ -51,6 +63,10 @@ def com_interp(string): r"," return t + def t_COL(t): + r":" + return t + def t_TEXT(t): r"[^ \n\t:=\\,]+" return t @@ -69,14 +85,100 @@ def com_interp(string): #for tok in lexer: # print(tok) - tokens = 0 + tokens = [] for tok in lexer: - tokens += 1 - print("gethere") - - print(tokens) - if tokens == 1: - print("gapp") - - -com_interp("HELOO") + tokens += [tok.value] + + if len(tokens) == 1: + if tokens[0] in variables: + return variables[tokens[0]] + else: + return [] + + #YACC stuff begins here + + def p_tonewstr(p): + """ + newstr : getstr EQ TEXT PERCENT TEXT + | getstr EQ PERCENT TEXT + | getstr EQ TEXT PERCENT + | getstr EQ PERCENT + | getstr EQ TEXT + """ + 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 : TEXT COL TEXT PERCENT TEXT + | TEXT COL PERCENT TEXT + | TEXT COL TEXT PERCENT + | TEXT COL PERCENT + | TEXT COL TEXT + """ + if not p[1] in variables: + p[0] = [] + else: + textlst = expand(variables[p[1]]): #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 + + + +com_interp("HELOO",{"HELOO":["mupp"]}) diff --git a/filetypes/makefiles.py b/filetypes/makefiles.py index d87f66f..332d14b 100644 --- a/filetypes/makefiles.py +++ b/filetypes/makefiles.py @@ -1,6 +1,6 @@ from ply import lex from ply import yacc -from makefilecom import com_interp +from makefilecom import expand def scanmakefile(makefile): tokens = ( @@ -166,6 +166,8 @@ def scanmakefile(makefile): ivars = [] #keep track of the immediate variables targets = [] #buildtargets, [[target,deps,options],[target2,.... + def p_target(p): + def p_peq(p): #immediate if peq was defined as immediate before else deferred """ end : end PEQ textlst end @@ -264,17 +266,6 @@ def scanmakefile(makefile): #immediate #deferred -def expand(lst): - newlst = [] - for item in lst: - if isinstance(item, list): - newitem = com_interp(item[0]) - else: - newitem = item - - newlst += newitem - - return newlst file="Makefile2" |