diff options
author | Sebastian Parborg <darkdefende@gmail.com> | 2011-06-08 00:00:10 +0200 |
---|---|---|
committer | Sebastian Parborg <darkdefende@gmail.com> | 2011-06-08 00:00:10 +0200 |
commit | f7adc621958797a9ee5fe32d7218a3a691e60075 (patch) | |
tree | 2f456f6246704c6fc1a65abd3cbc7be129b2bcf7 | |
parent | Added shebang to cli.py (diff) | |
download | ebuildgen-f7adc621958797a9ee5fe32d7218a3a691e60075.tar.gz ebuildgen-f7adc621958797a9ee5fe32d7218a3a691e60075.tar.bz2 ebuildgen-f7adc621958797a9ee5fe32d7218a3a691e60075.zip |
Basic makefile support done
-rw-r--r-- | makefiles.py | 169 |
1 files changed, 148 insertions, 21 deletions
diff --git a/makefiles.py b/makefiles.py index a754558..43f6de5 100644 --- a/makefiles.py +++ b/makefiles.py @@ -4,17 +4,17 @@ from ply import yacc def scanmakefile(makefile): tokens = ( - "VARS", + "VAR", "COLON", "PERCENT", "TEXT", "DOLLAR", "LPAR", "RPAR", - "CONTLINE", "END", "EQUAL", - "INCLUDEFLAG" + "ENDTAB", + "LESS", ) states = ( @@ -34,11 +34,7 @@ def scanmakefile(makefile): t.lexer.pop_state() return t - def t_INCLUDEFLAG(t): - r"-I" - return t - - def t_VARS(t): + def t_VAR(t): r"[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=" t.value = t.value.split()[0].rstrip("=") #get the name of the var return t @@ -48,6 +44,10 @@ def scanmakefile(makefile): r"-*\.*[a-zA-Z_][-|a-zA-Z0-9_]*" return t + def t_LESS(t): + r"\$<" + pass + def t_DOLLAR(t): r"\$" return t @@ -72,8 +72,12 @@ def scanmakefile(makefile): r"\%" return t - def t_CONTLINE(t): - r"\\" + def t_contline(t): + r"\\\n" + pass + + def t_ENDTAB(t): + r"\n\t" return t def t_END(t): @@ -85,37 +89,160 @@ def scanmakefile(makefile): lexer = lex.lex() - #lexer.input(makefile) - #for tok in lexer: - # print(tok) + lexer.input(makefile) + for tok in lexer: + print(tok) #YACC begins here #a dict with values of defined variables variables = {} + targets = [] #buildtargets, [[target,deps,options],[target2,.... + + def p_target(p): + """ + var : var textlst COLON textlst end + | textlst COLON textlst end + | var textlst COLON textlst options end + | textlst COLON textlst options end + """ + if len(p) == 6: + if p[3] == ":": + targets.append([p[2][0],p[4],[]]) + else: + targets.append([p[1][0],p[3],p[4]]) + elif len(p) == 5: + targets.append([p[1][0],p[3],[]]) + else: + targets.append([p[2][0],p[4],p[5]]) + + def p_lonetarget(p): + """ + var : var textlst COLON options end + | textlst COLON options end + """ + if len(p) == 6: + targets.append([p[2][0],[],p[4]]) + else: + targets.append([p[1][0],[],p[3]]) - def p_sumfiles(p): - "files : files sourcefiles" - p[0] = p[1] + p[2] + def p_depconv(p): + """ + var : var command COLON command end + | var command COLON command options end + """ + if len(p) == 6: + options = [] + else: + options = p[5] - def p_files(p): - "files : sourcefiles" - p[0] = p[1] + p[2] + if p[2][0] == p[4][0] == "%": + for target in targets: + for dep in target[1]: + if p[2][1] in dep: + targets.append([dep,[(dep.replace(p[2][1],p[4][1]))],options]) + else: + print("Unknown command") def p_var(p): - "var : VARS textlst END" + """ + var : VAR textlst end + | VAR end + | var VAR textlst end + | var VAR end + """ + if isinstance(p[2],list): + variables[p[1]] = p[2] + elif len(p) == 5: + variables[p[2]] = p[3] + elif len(p) == 3: + variables[p[1]] = [] + else: + variables[p[2]] = [] + + def p_endtab(p): + """ + options : ENDTAB textlst + | options ENDTAB textlst + """ + if len(p) == 3: + p[0] = p[2] + else: + p[0] = p[1] + p[3] + + def p_usecom(p): + """ + textlst : DOLLAR LPAR textlst COLON command RPAR + | textlst DOLLAR LPAR textlst COLON command RPAR + """ + if len(p) == 8: + o = 1 #offset + else: + o = 0 + p[3+o] = variables[p[3+o][0]] + p[0] = [] + if p[5][0] == "replace": + for text in p[3+o]: + p[0] += [text.replace(p[5+o][1],p[5+o][2])] + else: + for text in p[3+o]: + p[0] += [text + p[5+o][1]] def p_textlst(p): """ textlst : textlst TEXT | TEXT + | DOLLAR LPAR textlst RPAR + | textlst DOLLAR LPAR textlst RPAR """ if len(p) == 2: p[0] = [p[1]] - else: + elif len(p) == 3: p[0] = p[1] + [p[2]] + elif len(p) == 5: + if p[3][0] in variables: + var = variables[p[3][0]] + p[0] = var + else: + p[0] = ["not defined"] + else: + if p[4][0] in variables: + var = variables[p[4][0]] + p[0] = p[1] + var + else: + p[0] = ["not defined"] + + def p_command(p): + """ + command : TEXT EQUAL TEXT + | PERCENT EQUAL PERCENT TEXT + | PERCENT TEXT + """ + if len(p) == 4: + p[0] = ["replace", p[1], p[3]] + elif len(p) == 5: + p[0] = ["append", p[4]] + else: + p[0] = [p[1],p[2]] + + def p_end(p): + """ + end : end END + | END + """ + + def p_error(p): + print("syntax error at '%s'" % p.type,p.lexpos) + pass + + yacc.yacc() + + yacc.parse(makefile) + for target in targets: + print(target) + #print(variables) file = "/usr/portage/distfiles/svn-src/doneyet-read-only/trunk/Makefile" |