Skip to content

Commit 368441b

Browse files
committed
CreateWrapper now nearly there - still lots to fix
- structure ok - needs more types mapping and wrapper classes creating - some need special attention
1 parent 994d44c commit 368441b

10 files changed

Lines changed: 4259 additions & 15 deletions

PythonScript/project/PythonScript2010.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<ClInclude Include="..\src\PythonScript.h" />
101101
<ClInclude Include="..\src\resource.h" />
102102
<ClInclude Include="..\src\Scintilla.h" />
103+
<ClInclude Include="..\src\ScintillaCells.h" />
103104
<ClInclude Include="..\src\ScintillaPython.h" />
104105
<ClInclude Include="..\src\ScintillaWrapper.h" />
105106
<ClInclude Include="..\src\stdafx.h" />

PythonScript/project/PythonScript2010.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
<ClInclude Include="..\src\ScintillaPython.h">
6969
<Filter>Header Files</Filter>
7070
</ClInclude>
71+
<ClInclude Include="..\src\ScintillaCells.h">
72+
<Filter>Header Files</Filter>
73+
</ClInclude>
7174
</ItemGroup>
7275
<ItemGroup>
7376
<ResourceCompile Include="..\src\PythonScript.rc">

PythonScript/src/CreateWrapper.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# CreateWrapper.py - regenerate the ScintillaWrapper.h and ScintillaWrapper.cpp
2+
# files from the Scintilla.iface interface definition file.
3+
4+
# Todo:
5+
6+
# GetViewWS
7+
# GetCharAt
8+
# Type: colour
9+
# AutoCSetSeparator
10+
# UserListShow
11+
# AutoCSetTypeSeparator - ignore - cannot change this
12+
# GetReadOnly, GetLineVisible, GetTabIndents,GetBackSpaceUnIndents - need a list of methods that return a bool
13+
# FindText
14+
# Type: findtext
15+
# Type: formatrange
16+
# GetSelText - stringresult
17+
# Type: textrange
18+
# SetSearchFlags - need flags type in list or something
19+
# SetWrapMode - modes
20+
# SetLengthForEncode, TargetAsUTF8 - these want wrapping up, and excluding
21+
# CreateDocument etc, move to advanced?
22+
23+
import sys
24+
import os
25+
import Face
26+
27+
28+
29+
types = {
30+
'string' : 'str',
31+
'position' : 'int',
32+
'cells' : 'ScintillaCells',
33+
'colour' : 'ScintillaColour'
34+
}
35+
36+
castsL = {
37+
'str' : lambda name: "reinterpret_cast<LPARAM>(static_cast<const char*>(extract<const char *>(" + name + ")))"
38+
}
39+
castsW = {
40+
'str' : lambda name: "reinterpret_cast<WPARAM>(static_cast<const char*>(extract<const char *>(" + name + ")))"
41+
}
42+
43+
castsRet = {
44+
'bool' : lambda val: 'static_cast<bool>(' + val + ')'
45+
}
46+
47+
48+
def Contains(s,sub):
49+
return s.find(sub) != -1
50+
51+
def symbolName(v):
52+
return "SCI_" + v["Name"].upper()
53+
54+
def castLparam(ty, name):
55+
return castsL.get(ty, lambda n: n)(name)
56+
57+
def castWparam(ty, name):
58+
return castsW.get(ty, lambda n: n)(name)
59+
60+
def castReturn(ty, val):
61+
return castsRet.get(ty, lambda v: v)(val)
62+
63+
def cellsBody(v, out):
64+
out.write("\treturn call(" + symbolName(v) + ", " + v["Param2Name"] + ".length(), " + v["Param2Name"] + ".cellsPtr());\n")
65+
66+
def constString(v, out):
67+
out.write("\tconst char *raw = extract<const char *>(" + v["Param2Name"] + ");\n")
68+
out.write("\treturn call(" + symbolName(v) + ", len(" + v["Param2Name"] + "), reinterpret_cast<LPARAM>(raw));\n");
69+
70+
def retString(v, out):
71+
out.write("\tint resultLength = call(" + symbolName(v) + ");\n")
72+
out.write("\tchar *result = (char *)malloc(resultLength + 1);\n")
73+
out.write("\tcall(" + symbolName(v) + ", resultLength + 1, reinterpret_cast<LPARAM>(result));\n")
74+
out.write("\tresult[resultLength] = '\0';\n")
75+
out.write("\tstr o = str((const char *)result);\n")
76+
out.write("\tfree(result);\n")
77+
out.write("\treturn o;\n")
78+
79+
def retStringNoLength(v, out):
80+
out.write("\tint resultLength = call(" + symbolName(v) + ");\n")
81+
out.write("\tchar *result = (char *)malloc(resultLength + 1);\n")
82+
out.write("\tcall(" + symbolName(v) + ", 0, reinterpret_cast<LPARAM>(result));\n")
83+
out.write("\tresult[resultLength] = '\0';\n")
84+
out.write("\tstr o = str((const char *)result);\n")
85+
out.write("\tfree(result);\n")
86+
out.write("\treturn o;\n")
87+
88+
89+
90+
def standardBody(v, out):
91+
call = 'call(' + symbolName(v)
92+
93+
if v["Param2Type"] != '' and v["Param1Type"] == '':
94+
call += ', 0'
95+
96+
if v["Param1Type"]:
97+
call += ', ' + castWparam(v["Param1Type"], v["Param1Name"])
98+
99+
if v["Param2Type"]:
100+
call += ', ' + castLparam(v["Param2Type"], v["Param2Name"])
101+
call += ")"
102+
103+
104+
if (v["ReturnType"] != 'void'):
105+
out.write('\treturn ')
106+
out.write(castReturn(v["ReturnType"], call))
107+
else:
108+
out.write('\t' + call)
109+
110+
out.write(";\n")
111+
112+
113+
114+
def mapType(t):
115+
return types.get(t, t)
116+
117+
def mapCompare(t, s):
118+
if (t == s or t == ''):
119+
return True
120+
else:
121+
return False
122+
123+
def mapSignature(s):
124+
for t in argumentMap:
125+
if mapCompare(t[0], s[0]) and mapCompare(t[1], s[1]) and mapCompare(t[2], s[2]) and mapCompare(t[3], s[3]):
126+
return argumentMap[t];
127+
return None
128+
129+
130+
def writeParams(param1Type, param1Name, param2Type, param2Name, out):
131+
if param1Type:
132+
out.write(param1Type + " " + param1Name)
133+
if param2Type:
134+
out.write(', ')
135+
if param2Type:
136+
out.write(param2Type + " " + param2Name)
137+
138+
argumentMap = {
139+
('int', 'length', 'string', '') : ('int', '', 'str', constString),
140+
('int', 'length', 'stringresult', '') : ('str', '' , '', retString),
141+
('', '', 'stringresult', '') : ('str', '' , '', retStringNoLength),
142+
('int', 'length', 'cells', '') : ('int', '', 'ScintillaCells', cellsBody)
143+
}
144+
145+
def printHFile(f,out):
146+
for name in f.order:
147+
v = f.features[name]
148+
if v["Category"] != "Deprecated":
149+
if v["FeatureType"] in ["fun", "get", "set"]:
150+
sig = mapSignature((v["Param1Type"], v["Param1Name"], v["Param2Type"], v["Param2Name"]))
151+
returnType = "int"
152+
if sig is not None:
153+
v["ReturnType"] = sig[0]
154+
v["Param1Type"] = sig[1]
155+
v["Param2Type"] = sig[2]
156+
body = sig[3]
157+
else:
158+
v["ReturnType"] = mapType(v["ReturnType"])
159+
v["Param1Type"] = mapType(v["Param1Type"])
160+
v["Param2Type"] = mapType(v["Param2Type"])
161+
body = standardBody
162+
163+
out.write("/** " + "\n * ".join(v["Comment"]) + "\n */\n")
164+
out.write(v["ReturnType"] + " ScintillaWrapper::" + name + "(")
165+
166+
writeParams(v["Param1Type"], v["Param1Name"], v["Param2Type"], v["Param2Name"], out)
167+
out.write(")\n{\n")
168+
body(v, out)
169+
out.write("}\n\n")
170+
171+
def CopyWithInsertion(input, output, genfn, definition):
172+
copying = 1
173+
for line in input.readlines():
174+
if copying:
175+
output.write(line)
176+
if Contains(line, "/* ++Autogenerated"):
177+
copying = 0
178+
genfn(definition, output)
179+
if Contains(line, "/* --Autogenerated"):
180+
copying = 1
181+
output.write(line)
182+
183+
def contents(filename):
184+
f = open(filename)
185+
t = f.read()
186+
f.close()
187+
return t
188+
189+
def Regenerate(filename, genfn, definition):
190+
inText = contents(filename)
191+
tempname = "HFacer.tmp"
192+
out = open(tempname,"w")
193+
hfile = open(filename)
194+
CopyWithInsertion(hfile, out, genfn, definition)
195+
out.close()
196+
hfile.close()
197+
outText = contents(tempname)
198+
if inText == outText:
199+
os.unlink(tempname)
200+
else:
201+
os.unlink(filename)
202+
os.rename(tempname, filename)
203+
204+
f = Face.Face()
205+
try:
206+
f.ReadFromFile("Scintilla.iface")
207+
printHFile (f, sys.stdout)
208+
#Regenerate("SciLexer.h", printLexHFile, f)
209+
#print("Maximum ID is %s" % max([x for x in f.values if int(x) < 3000]))
210+
except:
211+
raise

PythonScript/src/Face.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Module for reading and parsing Scintilla.iface file
2+
3+
def sanitiseLine(line):
4+
if line[-1:] == '\n': line = line[:-1]
5+
if line.find("##") != -1:
6+
line = line[:line.find("##")]
7+
line = line.strip()
8+
return line
9+
10+
def decodeFunction(featureVal):
11+
retType, rest = featureVal.split(" ", 1)
12+
nameIdent, params = rest.split("(")
13+
name, value = nameIdent.split("=")
14+
params, rest = params.split(")")
15+
param1, param2 = params.split(",")[0:2]
16+
return retType, name, value, param1, param2
17+
18+
def decodeEvent(featureVal):
19+
retType, rest = featureVal.split(" ", 1)
20+
nameIdent, params = rest.split("(")
21+
name, value = nameIdent.split("=")
22+
return retType, name, value
23+
24+
def decodeParam(p):
25+
param = p.strip()
26+
type = ""
27+
name = ""
28+
value = ""
29+
if " " in param:
30+
type, nv = param.split(" ")
31+
if "=" in nv:
32+
name, value = nv.split("=")
33+
else:
34+
name = nv
35+
return type, name, value
36+
37+
class Face:
38+
39+
def __init__(self):
40+
self.order = []
41+
self.features = {}
42+
self.values = {}
43+
self.events = {}
44+
45+
def ReadFromFile(self, name):
46+
currentCategory = ""
47+
currentComment = []
48+
currentCommentFinished = 0
49+
file = open(name)
50+
for line in file.readlines():
51+
line = sanitiseLine(line)
52+
if line:
53+
if line[0] == "#":
54+
if line[1] == " ":
55+
if currentCommentFinished:
56+
currentComment = []
57+
currentCommentFinished = 0
58+
currentComment.append(line[2:])
59+
else:
60+
currentCommentFinished = 1
61+
featureType, featureVal = line.split(" ", 1)
62+
if featureType in ["fun", "get", "set"]:
63+
retType, name, value, param1, param2 = decodeFunction(featureVal)
64+
p1 = decodeParam(param1)
65+
p2 = decodeParam(param2)
66+
self.features[name] = {
67+
"Name": name,
68+
"FeatureType": featureType,
69+
"ReturnType": retType,
70+
"Value": value,
71+
"Param1Type": p1[0], "Param1Name": p1[1], "Param1Value": p1[2],
72+
"Param2Type": p2[0], "Param2Name": p2[1], "Param2Value": p2[2],
73+
"Category": currentCategory, "Comment": currentComment
74+
}
75+
if value in self.values:
76+
raise "Duplicate value " + value + " " + name
77+
self.values[value] = 1
78+
self.order.append(name)
79+
elif featureType == "evt":
80+
retType, name, value = decodeEvent(featureVal)
81+
self.features[name] = {
82+
"FeatureType": featureType,
83+
"ReturnType": retType,
84+
"Value": value,
85+
"Category": currentCategory, "Comment": currentComment
86+
}
87+
if value in self.events:
88+
raise "Duplicate event " + value + " " + name
89+
self.events[value] = 1
90+
self.order.append(name)
91+
elif featureType == "cat":
92+
currentCategory = featureVal
93+
elif featureType == "val":
94+
try:
95+
name, value = featureVal.split("=", 1)
96+
except ValueError:
97+
print("Failure %s" % featureVal)
98+
raise
99+
self.features[name] = {
100+
"FeatureType": featureType,
101+
"Category": currentCategory,
102+
"Value": value }
103+
self.order.append(name)
104+
elif featureType == "enu" or featureType == "lex":
105+
name, value = featureVal.split("=", 1)
106+
self.features[name] = {
107+
"FeatureType": featureType,
108+
"Category": currentCategory,
109+
"Value": value }
110+
self.order.append(name)
111+

0 commit comments

Comments
 (0)