ยปCore Development>Code coverage>Lib/idlelib/HyperParser.py

Python code coverage for Lib/idlelib/HyperParser.py

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