diff options
Diffstat (limited to 'src/LexGen.py')
-rw-r--r-- | src/LexGen.py | 86 |
1 files changed, 54 insertions, 32 deletions
diff --git a/src/LexGen.py b/src/LexGen.py index 290ba509c..4ad1bfaed 100644 --- a/src/LexGen.py +++ b/src/LexGen.py @@ -21,6 +21,17 @@ import sys import os import glob +# EOL constants +CR = "\r" +LF = "\n" +CRLF = "\r\n" +if sys.platform == "win32": + NATIVE = CRLF +else: + # Yes, LF is the native EOL even on Mac OS X. CR is just for + # Mac OS <=9 (a.k.a. "Mac Classic") + NATIVE = LF + # Automatically generated sections contain start and end comments, # a definition line and the results. # The results are replaced by regenerating based on the definition line. @@ -30,11 +41,11 @@ import glob # Backslash is used as an escape within the definition line. # The part between \( and \) is repeated for each item in the list. # \* is replaced by each list item. \t, and \n are tab and newline. -def CopyWithInsertion(input, commentPrefix, retainDefs, *lists): +def CopyWithInsertion(input, commentPrefix, retainDefs, eolType, *lists): copying = 1 listid = 0 output = [] - for line in input.split("\n"): + for line in input.splitlines(0): isStartGenerated = line.startswith(commentPrefix + "++Autogenerated") if copying and not isStartGenerated: output.append(line) @@ -79,14 +90,14 @@ def CopyWithInsertion(input, commentPrefix, retainDefs, *lists): pos = 0 outro = definition[endRepeat+2:] out += outro + out = out.replace("\n", eolType) # correct EOLs in generated content output.append(out) elif line.startswith(commentPrefix + "--Autogenerated"): copying = 1 if retainDefs: output.append(line) - ret = "\n".join(output) - ret = ret.replace(" \n", "\n") - return ret + output = [line.rstrip(" \t") for line in output] # trim trailing whitespace + return eolType.join(output) + eolType def UpdateFile(filename, updated): """ If the file is different to updated then copy updated @@ -108,30 +119,37 @@ def UpdateFile(filename, updated): out.write(updated) out.close() print "Changed", filename - -def RegenerateOverLists(inpath, outpath, commentPrefix, crlf, *lists): + #~ else: + #~ print "Unchanged", filename + +def Generate(inpath, outpath, commentPrefix, eolType, *lists): + """Generate 'outpath' from 'inpath'. + + "eolType" indicates the type of EOLs to use in the generated + file. It should be one of following constants: LF, CRLF, + CR, or NATIVE. + """ + #print "generate '%s' -> '%s' (comment prefix: %r, eols: %r)"\ + # % (inpath, outpath, commentPrefix, eolType) try: - infile = open(inpath, "rb") + infile = open(inpath, "r") except IOError: print "Can not open", inpath return original = infile.read() infile.close() - contents = original.replace("\r\n", "\n") - updated = CopyWithInsertion(contents, commentPrefix, - inpath == outpath, *lists) - if crlf: - updated = updated.replace("\n", "\r\n") + updated = CopyWithInsertion(original, commentPrefix, + inpath == outpath, eolType, *lists) UpdateFile(outpath, updated) -def Regenerate(filename, commentPrefix, *lists): - RegenerateOverLists(filename, filename, commentPrefix, 1, *lists) +def Regenerate(filename, commentPrefix, eolType, *lists): + """Regenerate the given file. -def RegenerateBinary(filename, commentPrefix, *lists): - RegenerateOverLists(filename, filename, commentPrefix, 0, *lists) - -def Generate(inpath, outpath, commentPrefix, *lists): - RegenerateOverLists(inpath, outpath, commentPrefix, 1, *lists) + "eolType" indicates the type of EOLs to use in the generated + file. It should be one of following constants: LF, CRLF, + CR, or NATIVE. + """ + Generate(filename, filename, commentPrefix, eolType, *lists) def FindModules(lexFile): modules = [] @@ -143,7 +161,7 @@ def FindModules(lexFile): return modules def ciCompare(a,b): - return a.lower() < b.lower() + return a.lower() < b.lower() def RegenerateAll(): root="../../" @@ -171,15 +189,19 @@ def RegenerateAll(): ids = [id for id in [l.split()[1] for l in lines if l.startswith("#define")] if id.startswith("IDM_")] #print ids - Regenerate(root + "scintilla/src/KeyWords.cxx", "//", lexerModules) - Regenerate(root + "scintilla/win32/makefile", "#", lexFiles) - Regenerate(root + "scintilla/win32/scintilla.mak", "#", lexFiles) - Regenerate(root + "scintilla/win32/scintilla_vc6.mak", "#", lexFiles) - RegenerateBinary(root + "scintilla/gtk/makefile", "#", lexFiles) - Regenerate(root + "scintilla/gtk/scintilla.mak", "#", lexFiles) - Regenerate(root + "scite/win32/makefile", "#", lexFiles, propFiles) - Regenerate(root + "scite/win32/scite.mak", "#", lexFiles, propFiles) - Regenerate(root + "scite/src/SciTEProps.cxx", "//", ids) - Generate(root + "scite/boundscheck/vcproj.gen", root + "scite/boundscheck/SciTE.vcproj", "#", lexFiles) - + Regenerate(root + "scintilla/src/KeyWords.cxx", "//", NATIVE, lexerModules) + Regenerate(root + "scintilla/win32/makefile", "#", NATIVE, lexFiles) + Regenerate(root + "scintilla/win32/scintilla.mak", "#", NATIVE, lexFiles) + Regenerate(root + "scintilla/win32/scintilla_vc6.mak", "#", NATIVE, lexFiles) + # Use Unix EOLs for gtk Makefiles so they work for Linux users when + # extracted from the Scintilla source ZIP (typically created on + # Windows). + Regenerate(root + "scintilla/gtk/makefile", "#", LF, lexFiles) + Regenerate(root + "scintilla/gtk/scintilla.mak", "#", NATIVE, lexFiles) + Regenerate(root + "scite/win32/makefile", "#", NATIVE, lexFiles, propFiles) + Regenerate(root + "scite/win32/scite.mak", "#", NATIVE, lexFiles, propFiles) + Regenerate(root + "scite/src/SciTEProps.cxx", "//", NATIVE, ids) + Generate(root + "scite/boundscheck/vcproj.gen", + root + "scite/boundscheck/SciTE.vcproj", "#", NATIVE, lexFiles) + RegenerateAll() |