From 2f24687e069abfc82383171d4e05d06487f69a06 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 6 Jun 2015 21:15:59 +0300 Subject: Add script for autogenerate node and parsers based on dir and name. --- scripts/addfile.py | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/tpl/node.tpl | 35 ++++++++++++++++++++++ scripts/tpl/parser.tpl | 36 +++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100755 scripts/addfile.py create mode 100644 scripts/tpl/node.tpl create mode 100644 scripts/tpl/parser.tpl diff --git a/scripts/addfile.py b/scripts/addfile.py new file mode 100755 index 0000000..ab1b937 --- /dev/null +++ b/scripts/addfile.py @@ -0,0 +1,80 @@ +#! /usr/bin/env python2 +# -*- coding: utf8 -*- +# +# Copyright (C) 2015 Andrei Karas + +import sys + + +def readFile(path): + with open(path, "r") as f: + return f.read() +def writeFile(fileName, data): + with open(fileName, "w") as w: + w.write(data) +def firstBigLetter(text): + return text[0].upper() + text[1:] + + +if len(sys.argv) < 2 or len(sys.argv) > 3: + print "Usage:" + print " addfile.py [dir] nodename" + exit(1) + +nodeTemplate = readFile("tpl/node.tpl") +parserTemplate = readFile("tpl/parser.tpl") +if len(sys.argv) == 2: + dirName = "" + nodeName = sys.argv[1].lower() +else: + dirName = sys.argv[1].lower() + nodeName = sys.argv[2].lower() +suffixSize = 0 +parserAdditionalCode1 = "" +parserAdditionalCode2 = "" +parserBaseInclude = "" + +if nodeName[-4:] == "decl": + suffixSize = 4 + parserAdditionalCode1 = " fillLocation(node);\n fillDeclLabel(node);\n" + parserAdditionalCode2 = "\n fillDeclAutoGenerated(node);\n fillDeclAttributes(node);\n" + parserBaseInclude = "#include \"parsers/base/decl.h\"\n" +elif nodeName[-4:] == "type": + suffixSize = 4 + parserAdditionalCode2 = "\n fillTypeName(node);\n fillTypeAttributes(node);\n" + parserBaseInclude = "#include \"parsers/base/type.h\"\n" +elif nodeName[-3:] == "cst": + suffixSize = 3 +elif nodeName[-4:] == "expr": + suffixSize = 4 + parserAdditionalCode2 = "\n fillExprOperands(node);\n" + parserBaseInclude = "#include \"parsers/base/expr.h\"\n" +elif nodeName[-4:] == "list": + suffixSize = 4 +else: + print "unknown nodename" + exit(1) + +word1 = nodeName[0 : len(nodeName) - suffixSize] +word2 = nodeName[- suffixSize:] +typeName = firstBigLetter(word1) + firstBigLetter(word2) +fileName = word1 + "_" + word2 +nodeInclude = "" +if dirName != "": + guardHeader = (dirName + "_" + nodeName).upper() + baseName = dirName + baseTypeName = firstBigLetter(dirName) + nodeInclude = dirName + "/" + fileName +else: + guardHeader = nodeName.upper() + baseName = "node" + baseTypeName = "Node" + nodeInclude = fileName + +writeFile("../src/nodes/{0}/{1}.h".format(dirName, fileName), + nodeTemplate.format(guardHeader, baseName, typeName, baseTypeName)) +writeFile("../src/parsers/{0}/{1}.cpp".format(dirName, fileName), + parserTemplate.format(typeName, nodeInclude, parserBaseInclude, parserAdditionalCode1, parserAdditionalCode2)) + +#print nodeTemplate.format(guardHeader, baseName, typeName, baseTypeName) +#print parserTemplate.format(typeName, nodeInclude, parserBaseInclude, parserAdditionalCode1, parserAdditionalCode2) diff --git a/scripts/tpl/node.tpl b/scripts/tpl/node.tpl new file mode 100644 index 0000000..30783a5 --- /dev/null +++ b/scripts/tpl/node.tpl @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 Andrei Karas + * + * This file is part of AstDumper. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef NODES_{0}NODE_H +#define NODES_{0}NODE_H + +#include "nodes/base/{1}.h" + +#include + +struct {2}Node : public {3}Node +{{ + {2}Node() : + {3}Node() + {{ + }} +}}; + +#endif // NODES_{0}NODE_H diff --git a/scripts/tpl/parser.tpl b/scripts/tpl/parser.tpl new file mode 100644 index 0000000..b5fb927 --- /dev/null +++ b/scripts/tpl/parser.tpl @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2015 Andrei Karas + * + * This file is part of AstDumper. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "parsers/parserincludes.h" + +parserDefine({0}); + +{2} +#include "nodes/{1}.h" + +namespace Generic +{{ + +void parse{0}Node({0}Node *node) +{{ + fillType(node); +{3} Log::log(node); +{4}}} + +}} -- cgit v1.2.3-70-g09d2