Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Delta Between Two Patch Sets: Lib/idlelib/HyperParser.py

Issue 21686: IDLE - Test hyperparser
Left Patch Set: Created 5 years, 8 months ago
Right Patch Set: Created 5 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/idlelib/idle_test/test_hyperparser.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Provide advanced parsing abilities for the ParenMatch and other extensions. 1 """Provide advanced parsing abilities for ParenMatch and other extensions.
2 2
3 HyperParser uses PyParser. PyParser mostly gives information on the 3 HyperParser uses PyParser. PyParser mostly gives information on the
4 proper indentation of code. HyperParser gives additional information on 4 proper indentation of code. HyperParser gives additional information on
5 the structure of code. 5 the structure of code.
6 """ 6 """
7 7
8 import string 8 import string
9 import keyword 9 import keyword
10 from idlelib import PyParse 10 from idlelib import PyParse
11 11
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 self.indexbracket = 0 81 self.indexbracket = 0
82 while (self.indexbracket < len(self.bracketing)-1 and 82 while (self.indexbracket < len(self.bracketing)-1 and
83 self.bracketing[self.indexbracket+1][0] < self.indexinrawtext): 83 self.bracketing[self.indexbracket+1][0] < self.indexinrawtext):
84 self.indexbracket += 1 84 self.indexbracket += 1
85 if (self.indexbracket < len(self.bracketing)-1 and 85 if (self.indexbracket < len(self.bracketing)-1 and
86 self.bracketing[self.indexbracket+1][0] == self.indexinrawtext and 86 self.bracketing[self.indexbracket+1][0] == self.indexinrawtext and
87 not self.isopener[self.indexbracket+1]): 87 not self.isopener[self.indexbracket+1]):
88 self.indexbracket += 1 88 self.indexbracket += 1
89 89
90 def is_in_string(self): 90 def is_in_string(self):
91 """Is the index given to the HyperParser is in a string?""" 91 """Is the index given to the HyperParser in a string?"""
92 # The bracket to which we belong should be an opener. 92 # The bracket to which we belong should be an opener.
93 # If it's an opener, it has to have a character. 93 # If it's an opener, it has to have a character.
94 return (self.isopener[self.indexbracket] and 94 return (self.isopener[self.indexbracket] and
95 self.rawtext[self.bracketing[self.indexbracket][0]] 95 self.rawtext[self.bracketing[self.indexbracket][0]]
96 in ('"', "'")) 96 in ('"', "'"))
97 97
98 def is_in_code(self): 98 def is_in_code(self):
99 """Is the index given to the HyperParser is in a normal code?""" 99 """Is the index given to the HyperParser in normal code?"""
100 return (not self.isopener[self.indexbracket] or 100 return (not self.isopener[self.indexbracket] or
101 self.rawtext[self.bracketing[self.indexbracket][0]] 101 self.rawtext[self.bracketing[self.indexbracket][0]]
102 not in ('#', '"', "'")) 102 not in ('#', '"', "'"))
103 103
104 def get_surrounding_brackets(self, openers='([{', mustclose=False): 104 def get_surrounding_brackets(self, openers='([{', mustclose=False):
105 """Return bracket indexes or None. 105 """Return bracket indexes or None.
106 106
107 If the index given to the HyperParser is surrounded by a 107 If the index given to the HyperParser is surrounded by a
108 bracket defined in openers (or at least has one before it), 108 bracket defined in openers (or at least has one before it),
109 return the indices of the opening bracket and the closing 109 return the indices of the opening bracket and the closing
110 bracket (or the end of line, whichever comes first). 110 bracket (or the end of line, whichever comes first).
111 111
112 If it is not surrounded by brackets, or the end of line comes 112 If it is not surrounded by brackets, or the end of line comes
113 before the closing bracket and mustclose is True, returns None. 113 before the closing bracket and mustclose is True, returns None.
114 """ 114 """
115 115
116 bracketinglevel = self.bracketing[self.indexbracket][1] 116 bracketinglevel = self.bracketing[self.indexbracket][1]
117 before = self.indexbracket 117 before = self.indexbracket
118 while (not self.isopener[before] or 118 while (not self.isopener[before] or
119 self.rawtext[self.bracketing[before][0]] not in openers or 119 self.rawtext[self.bracketing[before][0]] not in openers or
120 self.bracketing[before][1] > bracketinglevel): 120 self.bracketing[before][1] > bracketinglevel):
121 before -= 1 121 before -= 1
122 if before < 0: 122 if before < 0:
123 return None 123 return None
124 bracketinglevel = min(bracketinglevel, self.bracketing[before][1]) 124 bracketinglevel = min(bracketinglevel, self.bracketing[before][1])
125 after = self.indexbracket + 1 125 after = self.indexbracket + 1
(...skipping 25 matching lines...) Expand all
151 _id_first_chars = string.ascii_letters + "_" 151 _id_first_chars = string.ascii_letters + "_"
152 152
153 # Given a string and pos, return the number of chars in the 153 # Given a string and pos, return the number of chars in the
154 # identifier which ends at pos, or 0 if there is no such one. Saved 154 # identifier which ends at pos, or 0 if there is no such one. Saved
155 # words are not identifiers. 155 # words are not identifiers.
156 def _eat_identifier(self, str, limit, pos): 156 def _eat_identifier(self, str, limit, pos):
157 i = pos 157 i = pos
158 while i > limit and str[i-1] in self._id_chars: 158 while i > limit and str[i-1] in self._id_chars:
159 i -= 1 159 i -= 1
160 if (i < pos and (str[i] not in self._id_first_chars or 160 if (i < pos and (str[i] not in self._id_first_chars or
161 keyword.iskeyword(str[i:pos]))): 161 (keyword.iskeyword(str[i:pos]) and
162 str[i:pos] not in {'None', 'False', 'True'}))):
162 i = pos 163 i = pos
163 return pos - i 164 return pos - i
164 165
165 def get_expression(self): 166 def get_expression(self):
166 """Return a string with the Python expression which ends at the 167 """Return a string with the Python expression which ends at the
167 given index, which is empty if there is no real one. 168 given index, which is empty if there is no real one.
168 """ 169 """
169 if not self.is_in_code(): 170 if not self.is_in_code():
170 raise ValueError("get_expression should only be called" 171 raise ValueError("get_expression should only be called"
171 "if index is inside a code.") 172 "if index is inside a code.")
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 while pos > 0 and rawtext[pos - 1] in "rRbBuU": 242 while pos > 0 and rawtext[pos - 1] in "rRbBuU":
242 pos -= 1 243 pos -= 1
243 last_identifier_pos = pos 244 last_identifier_pos = pos
244 break 245 break
245 246
246 else: 247 else:
247 # We've found an operator or something. 248 # We've found an operator or something.
248 break 249 break
249 250
250 return rawtext[last_identifier_pos:self.indexinrawtext] 251 return rawtext[last_identifier_pos:self.indexinrawtext]
252
253
254 if __name__ == '__main__':
255 import unittest
256 unittest.main('idlelib.idle_test.test_hyperparser', verbosity=2)
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+