|
| 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 |
0 commit comments