|
| 1 | +def remove_comments(s): |
| 2 | + """ |
| 3 | + Remove comments of these styles: |
| 4 | +
|
| 5 | + CHASH: # comment python style, up to: EOL |
| 6 | + CSLASHSLASH: // comment C style, up to: EOL |
| 7 | + CSLASHSTAR: /* comment C style (single/multi line), up to: */ |
| 8 | +
|
| 9 | + Strings can be like 'strings' or "strings". |
| 10 | + Any comment-starting chars within strings are not considered. |
| 11 | + Escaping of (string-end) chars via backslash in strings is considered. |
| 12 | +
|
| 13 | + Also, leading and trailing whitespace is removed (after comment removal). |
| 14 | + Indented lines are re-indented afterwards with a single tab char. |
| 15 | +
|
| 16 | + Line numbers stay as in input file because empty lines are kept. |
| 17 | +
|
| 18 | + s: string with comments (can include newlines) |
| 19 | + returns: list of text lines |
| 20 | + """ |
| 21 | + # note: micropython's ure module was not capable enough to process this: |
| 22 | + # missing methods, re modes, recursion limit exceeded, ... |
| 23 | + # simpler hacks also didn't seem powerful enough to address all the |
| 24 | + # corner cases of CSLASHSTAR vs. *STR, so this state machine came to life: |
| 25 | + SRC, CHASH, CSLASHSLASH, CSLASHSTAR, DSTR, SSTR = range(6) # states |
| 26 | + |
| 27 | + line = [] # collect chars of one line |
| 28 | + lines = [] # collect result lines |
| 29 | + |
| 30 | + def finish_line(): |
| 31 | + # assemble a line from characters, try to get rid of trailing and |
| 32 | + # most of leading whitespace (keep/put one tab for indented lines). |
| 33 | + nonlocal line |
| 34 | + line = ''.join(line) |
| 35 | + is_indented = line.startswith(' ') or line.startswith('\t') |
| 36 | + line = line.strip() |
| 37 | + if line and is_indented: |
| 38 | + line = '\t' + line |
| 39 | + lines.append(line) |
| 40 | + line = [] |
| 41 | + |
| 42 | + state = SRC |
| 43 | + i = 0 |
| 44 | + length = len(s) |
| 45 | + while i < length: |
| 46 | + c = s[i] |
| 47 | + cn = s[i + 1] if i + 1 < length else '\0' |
| 48 | + if state == SRC: |
| 49 | + if c == '#': # starting to-EOL comment |
| 50 | + state = CHASH |
| 51 | + i += 1 |
| 52 | + elif c == '/': |
| 53 | + if cn == '/': # starting to-EOL comment |
| 54 | + state = CSLASHSLASH |
| 55 | + i += 2 |
| 56 | + elif cn == '*': # starting a /* comment |
| 57 | + state = CSLASHSTAR |
| 58 | + i += 2 |
| 59 | + else: |
| 60 | + i += 1 |
| 61 | + line.append(c) |
| 62 | + elif c == '"': |
| 63 | + state = DSTR |
| 64 | + i += 1 |
| 65 | + line.append(c) |
| 66 | + elif c == "'": |
| 67 | + state = SSTR |
| 68 | + i += 1 |
| 69 | + line.append(c) |
| 70 | + elif c == '\n': |
| 71 | + i += 1 |
| 72 | + finish_line() |
| 73 | + else: |
| 74 | + i += 1 |
| 75 | + line.append(c) |
| 76 | + elif state == CHASH or state == CSLASHSLASH: |
| 77 | + if c != '\n': # comment runs until EOL |
| 78 | + i += 1 |
| 79 | + else: |
| 80 | + state = SRC |
| 81 | + i += 1 |
| 82 | + finish_line() |
| 83 | + elif state == CSLASHSTAR: |
| 84 | + if c == '*' and cn == '/': # ending a comment */ |
| 85 | + state = SRC |
| 86 | + i += 2 |
| 87 | + elif c == '\n': |
| 88 | + i += 1 |
| 89 | + finish_line() |
| 90 | + else: |
| 91 | + i += 1 |
| 92 | + elif state == DSTR and c == '"' or state == SSTR and c == "'": # string end |
| 93 | + state = SRC |
| 94 | + i += 1 |
| 95 | + line.append(c) |
| 96 | + elif state == DSTR or state == SSTR: |
| 97 | + i += 1 |
| 98 | + line.append(c) |
| 99 | + if c == '\\': # escaping backslash |
| 100 | + i += 1 # do not look at char after the backslash |
| 101 | + line.append(cn) |
| 102 | + else: |
| 103 | + raise Exception("state: %d c: %s cn: %s" % (state, c, cn)) |
| 104 | + if line: |
| 105 | + # no final \n triggered processing these chars yet, do it now |
| 106 | + finish_line() |
| 107 | + return lines |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + import sys |
| 112 | + filename = sys.argv[1] |
| 113 | + with open(filename, "r") as f: |
| 114 | + text = f.read() |
| 115 | + lines = remove_comments(text) |
| 116 | + with open(filename + ".nocomments", "w") as f: |
| 117 | + for line in lines: |
| 118 | + f.write(line + '\n') |
| 119 | + |
0 commit comments