1 | n/a | """ |
---|
2 | n/a | HyperParser |
---|
3 | n/a | =========== |
---|
4 | n/a | This module defines the HyperParser class, which provides advanced parsing |
---|
5 | n/a | abilities for the ParenMatch and other extensions. |
---|
6 | n/a | The HyperParser uses PyParser. PyParser is intended mostly to give information |
---|
7 | n/a | on the proper indentation of code. HyperParser gives some information on the |
---|
8 | n/a | structure of code, used by extensions to help the user. |
---|
9 | n/a | """ |
---|
10 | n/a | |
---|
11 | n/a | import string |
---|
12 | n/a | import keyword |
---|
13 | n/a | from idlelib import PyParse |
---|
14 | n/a | |
---|
15 | n/a | class HyperParser: |
---|
16 | n/a | |
---|
17 | n/a | def __init__(self, editwin, index): |
---|
18 | n/a | """Initialize the HyperParser to analyze the surroundings of the given |
---|
19 | n/a | index. |
---|
20 | n/a | """ |
---|
21 | n/a | |
---|
22 | n/a | self.editwin = editwin |
---|
23 | n/a | self.text = text = editwin.text |
---|
24 | n/a | |
---|
25 | n/a | parser = PyParse.Parser(editwin.indentwidth, editwin.tabwidth) |
---|
26 | n/a | |
---|
27 | n/a | def index2line(index): |
---|
28 | n/a | return int(float(index)) |
---|
29 | n/a | lno = index2line(text.index(index)) |
---|
30 | n/a | |
---|
31 | n/a | if not editwin.context_use_ps1: |
---|
32 | n/a | for context in editwin.num_context_lines: |
---|
33 | n/a | startat = max(lno - context, 1) |
---|
34 | n/a | startatindex = repr(startat) + ".0" |
---|
35 | n/a | stopatindex = "%d.end" % lno |
---|
36 | n/a | # We add the newline because PyParse requires a newline at end. |
---|
37 | n/a | # We add a space so that index won't be at end of line, so that |
---|
38 | n/a | # its status will be the same as the char before it, if should. |
---|
39 | n/a | parser.set_str(text.get(startatindex, stopatindex)+' \n') |
---|
40 | n/a | bod = parser.find_good_parse_start( |
---|
41 | n/a | editwin._build_char_in_string_func(startatindex)) |
---|
42 | n/a | if bod is not None or startat == 1: |
---|
43 | n/a | break |
---|
44 | n/a | parser.set_lo(bod or 0) |
---|
45 | n/a | else: |
---|
46 | n/a | r = text.tag_prevrange("console", index) |
---|
47 | n/a | if r: |
---|
48 | n/a | startatindex = r[1] |
---|
49 | n/a | else: |
---|
50 | n/a | startatindex = "1.0" |
---|
51 | n/a | stopatindex = "%d.end" % lno |
---|
52 | n/a | # We add the newline because PyParse requires a newline at end. |
---|
53 | n/a | # We add a space so that index won't be at end of line, so that |
---|
54 | n/a | # its status will be the same as the char before it, if should. |
---|
55 | n/a | parser.set_str(text.get(startatindex, stopatindex)+' \n') |
---|
56 | n/a | parser.set_lo(0) |
---|
57 | n/a | |
---|
58 | n/a | # We want what the parser has, except for the last newline and space. |
---|
59 | n/a | self.rawtext = parser.str[:-2] |
---|
60 | n/a | # As far as I can see, parser.str preserves the statement we are in, |
---|
61 | n/a | # so that stopatindex can be used to synchronize the string with the |
---|
62 | n/a | # text box indices. |
---|
63 | n/a | self.stopatindex = stopatindex |
---|
64 | n/a | self.bracketing = parser.get_last_stmt_bracketing() |
---|
65 | n/a | # find which pairs of bracketing are openers. These always correspond |
---|
66 | n/a | # to a character of rawtext. |
---|
67 | n/a | self.isopener = [i>0 and self.bracketing[i][1] > self.bracketing[i-1][1] |
---|
68 | n/a | for i in range(len(self.bracketing))] |
---|
69 | n/a | |
---|
70 | n/a | self.set_index(index) |
---|
71 | n/a | |
---|
72 | n/a | def set_index(self, index): |
---|
73 | n/a | """Set the index to which the functions relate. Note that it must be |
---|
74 | n/a | in the same statement. |
---|
75 | n/a | """ |
---|
76 | n/a | indexinrawtext = \ |
---|
77 | n/a | len(self.rawtext) - len(self.text.get(index, self.stopatindex)) |
---|
78 | n/a | if indexinrawtext < 0: |
---|
79 | n/a | raise ValueError("The index given is before the analyzed statement") |
---|
80 | n/a | self.indexinrawtext = indexinrawtext |
---|
81 | n/a | # find the rightmost bracket to which index belongs |
---|
82 | n/a | self.indexbracket = 0 |
---|
83 | n/a | while self.indexbracket < len(self.bracketing)-1 and \ |
---|
84 | n/a | self.bracketing[self.indexbracket+1][0] < self.indexinrawtext: |
---|
85 | n/a | self.indexbracket += 1 |
---|
86 | n/a | if self.indexbracket < len(self.bracketing)-1 and \ |
---|
87 | n/a | self.bracketing[self.indexbracket+1][0] == self.indexinrawtext and \ |
---|
88 | n/a | not self.isopener[self.indexbracket+1]: |
---|
89 | n/a | self.indexbracket += 1 |
---|
90 | n/a | |
---|
91 | n/a | def is_in_string(self): |
---|
92 | n/a | """Is the index given to the HyperParser is in a string?""" |
---|
93 | n/a | # The bracket to which we belong should be an opener. |
---|
94 | n/a | # If it's an opener, it has to have a character. |
---|
95 | n/a | return self.isopener[self.indexbracket] and \ |
---|
96 | n/a | self.rawtext[self.bracketing[self.indexbracket][0]] in ('"', "'") |
---|
97 | n/a | |
---|
98 | n/a | def is_in_code(self): |
---|
99 | n/a | """Is the index given to the HyperParser is in a normal code?""" |
---|
100 | n/a | return not self.isopener[self.indexbracket] or \ |
---|
101 | n/a | self.rawtext[self.bracketing[self.indexbracket][0]] not in \ |
---|
102 | n/a | ('#', '"', "'") |
---|
103 | n/a | |
---|
104 | n/a | def get_surrounding_brackets(self, openers='([{', mustclose=False): |
---|
105 | n/a | """If the index given to the HyperParser is surrounded by a bracket |
---|
106 | n/a | defined in openers (or at least has one before it), return the |
---|
107 | n/a | indices of the opening bracket and the closing bracket (or the |
---|
108 | n/a | end of line, whichever comes first). |
---|
109 | n/a | If it is not surrounded by brackets, or the end of line comes before |
---|
110 | n/a | the closing bracket and mustclose is True, returns None. |
---|
111 | n/a | """ |
---|
112 | n/a | bracketinglevel = self.bracketing[self.indexbracket][1] |
---|
113 | n/a | before = self.indexbracket |
---|
114 | n/a | while not self.isopener[before] or \ |
---|
115 | n/a | self.rawtext[self.bracketing[before][0]] not in openers or \ |
---|
116 | n/a | self.bracketing[before][1] > bracketinglevel: |
---|
117 | n/a | before -= 1 |
---|
118 | n/a | if before < 0: |
---|
119 | n/a | return None |
---|
120 | n/a | bracketinglevel = min(bracketinglevel, self.bracketing[before][1]) |
---|
121 | n/a | after = self.indexbracket + 1 |
---|
122 | n/a | while after < len(self.bracketing) and \ |
---|
123 | n/a | self.bracketing[after][1] >= bracketinglevel: |
---|
124 | n/a | after += 1 |
---|
125 | n/a | |
---|
126 | n/a | beforeindex = self.text.index("%s-%dc" % |
---|
127 | n/a | (self.stopatindex, len(self.rawtext)-self.bracketing[before][0])) |
---|
128 | n/a | if after >= len(self.bracketing) or \ |
---|
129 | n/a | self.bracketing[after][0] > len(self.rawtext): |
---|
130 | n/a | if mustclose: |
---|
131 | n/a | return None |
---|
132 | n/a | afterindex = self.stopatindex |
---|
133 | n/a | else: |
---|
134 | n/a | # We are after a real char, so it is a ')' and we give the index |
---|
135 | n/a | # before it. |
---|
136 | n/a | afterindex = self.text.index("%s-%dc" % |
---|
137 | n/a | (self.stopatindex, |
---|
138 | n/a | len(self.rawtext)-(self.bracketing[after][0]-1))) |
---|
139 | n/a | |
---|
140 | n/a | return beforeindex, afterindex |
---|
141 | n/a | |
---|
142 | n/a | # This string includes all chars that may be in a white space |
---|
143 | n/a | _whitespace_chars = " \t\n\\" |
---|
144 | n/a | # This string includes all chars that may be in an identifier |
---|
145 | n/a | _id_chars = string.ascii_letters + string.digits + "_" |
---|
146 | n/a | # This string includes all chars that may be the first char of an identifier |
---|
147 | n/a | _id_first_chars = string.ascii_letters + "_" |
---|
148 | n/a | |
---|
149 | n/a | # Given a string and pos, return the number of chars in the identifier |
---|
150 | n/a | # which ends at pos, or 0 if there is no such one. Saved words are not |
---|
151 | n/a | # identifiers. |
---|
152 | n/a | def _eat_identifier(self, str, limit, pos): |
---|
153 | n/a | i = pos |
---|
154 | n/a | while i > limit and str[i-1] in self._id_chars: |
---|
155 | n/a | i -= 1 |
---|
156 | n/a | if i < pos and (str[i] not in self._id_first_chars or \ |
---|
157 | n/a | keyword.iskeyword(str[i:pos])): |
---|
158 | n/a | i = pos |
---|
159 | n/a | return pos - i |
---|
160 | n/a | |
---|
161 | n/a | def get_expression(self): |
---|
162 | n/a | """Return a string with the Python expression which ends at the given |
---|
163 | n/a | index, which is empty if there is no real one. |
---|
164 | n/a | """ |
---|
165 | n/a | if not self.is_in_code(): |
---|
166 | n/a | raise ValueError("get_expression should only be called if index "\ |
---|
167 | n/a | "is inside a code.") |
---|
168 | n/a | |
---|
169 | n/a | rawtext = self.rawtext |
---|
170 | n/a | bracketing = self.bracketing |
---|
171 | n/a | |
---|
172 | n/a | brck_index = self.indexbracket |
---|
173 | n/a | brck_limit = bracketing[brck_index][0] |
---|
174 | n/a | pos = self.indexinrawtext |
---|
175 | n/a | |
---|
176 | n/a | last_identifier_pos = pos |
---|
177 | n/a | postdot_phase = True |
---|
178 | n/a | |
---|
179 | n/a | while 1: |
---|
180 | n/a | # Eat whitespaces, comments, and if postdot_phase is False - one dot |
---|
181 | n/a | while 1: |
---|
182 | n/a | if pos>brck_limit and rawtext[pos-1] in self._whitespace_chars: |
---|
183 | n/a | # Eat a whitespace |
---|
184 | n/a | pos -= 1 |
---|
185 | n/a | elif not postdot_phase and \ |
---|
186 | n/a | pos > brck_limit and rawtext[pos-1] == '.': |
---|
187 | n/a | # Eat a dot |
---|
188 | n/a | pos -= 1 |
---|
189 | n/a | postdot_phase = True |
---|
190 | n/a | # The next line will fail if we are *inside* a comment, but we |
---|
191 | n/a | # shouldn't be. |
---|
192 | n/a | elif pos == brck_limit and brck_index > 0 and \ |
---|
193 | n/a | rawtext[bracketing[brck_index-1][0]] == '#': |
---|
194 | n/a | # Eat a comment |
---|
195 | n/a | brck_index -= 2 |
---|
196 | n/a | brck_limit = bracketing[brck_index][0] |
---|
197 | n/a | pos = bracketing[brck_index+1][0] |
---|
198 | n/a | else: |
---|
199 | n/a | # If we didn't eat anything, quit. |
---|
200 | n/a | break |
---|
201 | n/a | |
---|
202 | n/a | if not postdot_phase: |
---|
203 | n/a | # We didn't find a dot, so the expression end at the last |
---|
204 | n/a | # identifier pos. |
---|
205 | n/a | break |
---|
206 | n/a | |
---|
207 | n/a | ret = self._eat_identifier(rawtext, brck_limit, pos) |
---|
208 | n/a | if ret: |
---|
209 | n/a | # There is an identifier to eat |
---|
210 | n/a | pos = pos - ret |
---|
211 | n/a | last_identifier_pos = pos |
---|
212 | n/a | # Now, in order to continue the search, we must find a dot. |
---|
213 | n/a | postdot_phase = False |
---|
214 | n/a | # (the loop continues now) |
---|
215 | n/a | |
---|
216 | n/a | elif pos == brck_limit: |
---|
217 | n/a | # We are at a bracketing limit. If it is a closing bracket, |
---|
218 | n/a | # eat the bracket, otherwise, stop the search. |
---|
219 | n/a | level = bracketing[brck_index][1] |
---|
220 | n/a | while brck_index > 0 and bracketing[brck_index-1][1] > level: |
---|
221 | n/a | brck_index -= 1 |
---|
222 | n/a | if bracketing[brck_index][0] == brck_limit: |
---|
223 | n/a | # We were not at the end of a closing bracket |
---|
224 | n/a | break |
---|
225 | n/a | pos = bracketing[brck_index][0] |
---|
226 | n/a | brck_index -= 1 |
---|
227 | n/a | brck_limit = bracketing[brck_index][0] |
---|
228 | n/a | last_identifier_pos = pos |
---|
229 | n/a | if rawtext[pos] in "([": |
---|
230 | n/a | # [] and () may be used after an identifier, so we |
---|
231 | n/a | # continue. postdot_phase is True, so we don't allow a dot. |
---|
232 | n/a | pass |
---|
233 | n/a | else: |
---|
234 | n/a | # We can't continue after other types of brackets |
---|
235 | n/a | if rawtext[pos] in "'\"": |
---|
236 | n/a | # Scan a string prefix |
---|
237 | n/a | while pos > 0 and rawtext[pos - 1] in "rRbBuU": |
---|
238 | n/a | pos -= 1 |
---|
239 | n/a | last_identifier_pos = pos |
---|
240 | n/a | break |
---|
241 | n/a | |
---|
242 | n/a | else: |
---|
243 | n/a | # We've found an operator or something. |
---|
244 | n/a | break |
---|
245 | n/a | |
---|
246 | n/a | return rawtext[last_identifier_pos:self.indexinrawtext] |
---|