| 1 |
import re |
| 2 |
import sys |
| 3 |
|
| 4 |
# unicode can't be memoryviewed, so just slice |
| 5 |
buffer = lambda s, i=0: s[i:] |
| 6 |
|
| 7 |
from pymol import cmd, parsing |
| 8 |
|
| 9 |
DEBUG = False |
| 10 |
|
| 11 |
STRICT = [parsing.STRICT, parsing.ABORT, parsing.SECURE, parsing.LEGACY] |
| 12 |
LITERAL = [ |
| 13 |
parsing.LITERAL, parsing.LITERAL1, parsing.LITERAL2, parsing.MOVIE, |
| 14 |
parsing.PYTHON |
| 15 |
] |
| 16 |
MULTILINE = [parsing.PYTHON_BLOCK, parsing.EMBED, parsing.SKIP] |
| 17 |
|
| 18 |
brackopen = {'(': ')', '[': ']', '{': '}'} |
| 19 |
|
| 20 |
|
| 21 |
def fancysplit(s, delim=',', stop=(';', '\n'), maxsplit=-1): |
| 22 |
stack = [] |
| 23 |
quote = '' |
| 24 |
tristart = -1 |
| 25 |
escaped = False |
| 26 |
|
| 27 |
for i, c in enumerate(s): |
| 28 |
if i == maxsplit: |
| 29 |
break |
| 30 |
|
| 31 |
if escaped: |
| 32 |
escaped = False |
| 33 |
elif c == '\\': |
| 34 |
escaped = True |
| 35 |
elif quote: |
| 36 |
if c == quote: |
| 37 |
if tristart == -1: |
| 38 |
quote = '' |
| 39 |
elif i > tristart + 2 and s[i + 1:i + 3] == c + c: |
| 40 |
tristart = -1 |
| 41 |
quote = '' |
| 42 |
elif c in ('"', "'"): |
| 43 |
quote = c |
| 44 |
if s[i + 1:i + 3] == c + c: |
| 45 |
tristart = i |
| 46 |
elif c in brackopen: |
| 47 |
stack.append(brackopen[c]) |
| 48 |
elif stack: |
| 49 |
if c == stack[-1]: |
| 50 |
stack.pop() |
| 51 |
elif c == delim: |
| 52 |
yield i |
| 53 |
elif c in stop: |
| 54 |
yield i |
| 55 |
return |
| 56 |
|
| 57 |
if stack and DEBUG: |
| 58 |
print('warning: unmatched quotes') |
| 59 |
|
| 60 |
yield len(s) |
| 61 |
|
| 62 |
|
| 63 |
def findnewline(s): |
| 64 |
m = re.search(r'(?:^|[^\\])$', s, re.M) |
| 65 |
if m is None: |
| 66 |
return len(s) |
| 67 |
return m.end() + 1 |
| 68 |
|
| 69 |
|
| 70 |
def parse_pml(text): |
| 71 |
i = 0 |
| 72 |
L = len(text) |
| 73 |
|
| 74 |
while i < L: |
| 75 |
expr_start = i |
| 76 |
|
| 77 |
m = re.match(r'(_ )?[ \t]*($|#|/|@|[^\s;]+)', buffer(text, i), re.M) |
| 78 |
if m is None: |
| 79 |
break |
| 80 |
|
| 81 |
com = m.group(2) |
| 82 |
|
| 83 |
hit = { |
| 84 |
'type': 'python', |
| 85 |
'quiet': m.group(1) is not None, |
| 86 |
'command': (m.start(2) + i, m.end(2) + i), |
| 87 |
'multiline': 0, # 0, 1 (closed), 2 (open) |
| 88 |
} |
| 89 |
|
| 90 |
mode = parsing.PYTHON |
| 91 |
|
| 92 |
if com == '': |
| 93 |
i += m.end() + 1 |
| 94 |
continue |
| 95 |
elif com == '/': |
| 96 |
pass |
| 97 |
elif com == '#': |
| 98 |
# TODO could be handled as command |
| 99 |
hit['type'] = 'comment' |
| 100 |
elif com == '@': |
| 101 |
# TODO could be handled as command |
| 102 |
hit['type'] = 'command' |
| 103 |
mode = parsing.STRICT |
| 104 |
else: |
| 105 |
com = cmd.kwhash.shortcut.get(com, com) |
| 106 |
if com in cmd.keyword: |
| 107 |
mode = cmd.keyword[com][4] |
| 108 |
|
| 109 |
if mode != parsing.PYTHON: |
| 110 |
hit['type'] = 'command' |
| 111 |
else: |
| 112 |
i += m.start() - m.end() |
| 113 |
|
| 114 |
i += m.end() |
| 115 |
arg_i = [i] |
| 116 |
|
| 117 |
if mode in LITERAL: |
| 118 |
i += findnewline(buffer(text, i)) |
| 119 |
arg_i.append(i) |
| 120 |
else: |
| 121 |
arg_i.extend((i + p) for p in fancysplit(buffer(text, i))) |
| 122 |
i = arg_i[-1] + 1 |
| 123 |
|
| 124 |
if mode in MULTILINE: |
| 125 |
m = re.search(r'^(_ )?[ \t]*' + com + r' end\b', |
| 126 |
buffer(text, arg_i[-1]), re.M) |
| 127 |
if m is None: |
| 128 |
hit['multiline'] = 2 |
| 129 |
i = len(text) |
| 130 |
arg_i.append(i) |
| 131 |
else: |
| 132 |
hit['multiline'] = 1 |
| 133 |
arg_i.append(i + m.start()) |
| 134 |
i += m.end() |
| 135 |
|
| 136 |
hit['args'] = arg_i |
| 137 |
hit['expr'] = expr_start, i |
| 138 |
|
| 139 |
yield hit |