add working preprocessor (creates docstrings)
[sv2nmigen.git] / pypreproc.py
1 quote = "\""
2 import sys
3 class Preprocessor:
4 def __init__(self):
5 self.docstrings = []
6 def removeComments(self,data):
7 #print(data)
8 ret = ""
9 in_docstring = False
10 docstring = ""
11 for line in data.split("\n"):
12 docstring_end = ("#docstring_end" in line)
13 if "#docstring_begin" in line:
14 ret += "//DOCSTRING_PLACEHOLDER\n"
15 in_docstring = True
16 docstring = ""
17 if(in_docstring==False):
18 ret += line + "\n"
19 else:
20 if(not docstring_end): docstring += line + "\n"
21 if(docstring_end):
22 in_docstring = False
23 self.docstrings += [docstring]
24 print(docstring)
25 return ret
26 def insertDocstrings(self,data):
27 ret =""
28 docstring_counter = 0
29 for line in data.split("\n"):
30 if("//DOCSTRING_PLACEHOLDER" in line):
31 ret += quote*3
32 ret += self.docstrings[docstring_counter]
33 ret += quote*3
34 ret += "\n"
35 docstring_counter += 1
36 else:
37 ret += "#"+line + "\n"
38 return ret
39