Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Static Private Attributes
idlelib.HyperParser.HyperParser Class Reference
Collaboration diagram for idlelib.HyperParser.HyperParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def set_index
def is_in_string
def is_in_code
def get_surrounding_brackets
def get_expression

Public Attributes

 editwin
 text
 rawtext
 stopatindex
 bracketing
 isopener
 indexinrawtext
 indexbracket

Private Member Functions

def _eat_identifier

Static Private Attributes

string _whitespace_chars = " \t\n\\"
string _id_chars = "_"
string _id_first_chars = "_"

Detailed Description

Definition at line 15 of file HyperParser.py.


Constructor & Destructor Documentation

def idlelib.HyperParser.HyperParser.__init__ (   self,
  editwin,
  index 
)
Initialize the HyperParser to analyze the surroundings of the given
index.

Definition at line 17 of file HyperParser.py.

00017 
00018     def __init__(self, editwin, index):
00019         """Initialize the HyperParser to analyze the surroundings of the given
00020         index.
00021         """
00022 
00023         self.editwin = editwin
00024         self.text = text = editwin.text
00025 
00026         parser = PyParse.Parser(editwin.indentwidth, editwin.tabwidth)
00027 
00028         def index2line(index):
00029             return int(float(index))
00030         lno = index2line(text.index(index))
00031 
00032         if not editwin.context_use_ps1:
00033             for context in editwin.num_context_lines:
00034                 startat = max(lno - context, 1)
00035                 startatindex = repr(startat) + ".0"
00036                 stopatindex = "%d.end" % lno
00037                 # We add the newline because PyParse requires a newline at end.
00038                 # We add a space so that index won't be at end of line, so that
00039                 # its status will be the same as the char before it, if should.
00040                 parser.set_str(text.get(startatindex, stopatindex)+' \n')
00041                 bod = parser.find_good_parse_start(
00042                           editwin._build_char_in_string_func(startatindex))
00043                 if bod is not None or startat == 1:
00044                     break
00045             parser.set_lo(bod or 0)
00046         else:
00047             r = text.tag_prevrange("console", index)
00048             if r:
00049                 startatindex = r[1]
00050             else:
00051                 startatindex = "1.0"
00052             stopatindex = "%d.end" % lno
00053             # We add the newline because PyParse requires a newline at end.
00054             # We add a space so that index won't be at end of line, so that
00055             # its status will be the same as the char before it, if should.
00056             parser.set_str(text.get(startatindex, stopatindex)+' \n')
00057             parser.set_lo(0)
00058 
00059         # We want what the parser has, except for the last newline and space.
00060         self.rawtext = parser.str[:-2]
00061         # As far as I can see, parser.str preserves the statement we are in,
00062         # so that stopatindex can be used to synchronize the string with the
00063         # text box indices.
00064         self.stopatindex = stopatindex
00065         self.bracketing = parser.get_last_stmt_bracketing()
00066         # find which pairs of bracketing are openers. These always correspond
00067         # to a character of rawtext.
00068         self.isopener = [i>0 and self.bracketing[i][1] > self.bracketing[i-1][1]
00069                          for i in range(len(self.bracketing))]
00070 
00071         self.set_index(index)

Here is the caller graph for this function:


Member Function Documentation

def idlelib.HyperParser.HyperParser._eat_identifier (   self,
  str,
  limit,
  pos 
) [private]

Definition at line 152 of file HyperParser.py.

00152 
00153     def _eat_identifier(self, str, limit, pos):
00154         i = pos
00155         while i > limit and str[i-1] in self._id_chars:
00156             i -= 1
00157         if i < pos and (str[i] not in self._id_first_chars or \
00158                         keyword.iskeyword(str[i:pos])):
00159             i = pos
00160         return pos - i

Here is the caller graph for this function:

Return a string with the Python expression which ends at the given
index, which is empty if there is no real one.

Definition at line 161 of file HyperParser.py.

00161 
00162     def get_expression(self):
00163         """Return a string with the Python expression which ends at the given
00164         index, which is empty if there is no real one.
00165         """
00166         if not self.is_in_code():
00167             raise ValueError("get_expression should only be called if index "\
00168                              "is inside a code.")
00169 
00170         rawtext = self.rawtext
00171         bracketing = self.bracketing
00172 
00173         brck_index = self.indexbracket
00174         brck_limit = bracketing[brck_index][0]
00175         pos = self.indexinrawtext
00176 
00177         last_identifier_pos = pos
00178         postdot_phase = True
00179 
00180         while 1:
00181             # Eat whitespaces, comments, and if postdot_phase is False - one dot
00182             while 1:
00183                 if pos>brck_limit and rawtext[pos-1] in self._whitespace_chars:
00184                     # Eat a whitespace
00185                     pos -= 1
00186                 elif not postdot_phase and \
00187                      pos > brck_limit and rawtext[pos-1] == '.':
00188                     # Eat a dot
00189                     pos -= 1
00190                     postdot_phase = True
00191                 # The next line will fail if we are *inside* a comment, but we
00192                 # shouldn't be.
00193                 elif pos == brck_limit and brck_index > 0 and \
00194                      rawtext[bracketing[brck_index-1][0]] == '#':
00195                     # Eat a comment
00196                     brck_index -= 2
00197                     brck_limit = bracketing[brck_index][0]
00198                     pos = bracketing[brck_index+1][0]
00199                 else:
00200                     # If we didn't eat anything, quit.
00201                     break
00202 
00203             if not postdot_phase:
00204                 # We didn't find a dot, so the expression end at the last
00205                 # identifier pos.
00206                 break
00207 
00208             ret = self._eat_identifier(rawtext, brck_limit, pos)
00209             if ret:
00210                 # There is an identifier to eat
00211                 pos = pos - ret
00212                 last_identifier_pos = pos
00213                 # Now, in order to continue the search, we must find a dot.
00214                 postdot_phase = False
00215                 # (the loop continues now)
00216 
00217             elif pos == brck_limit:
00218                 # We are at a bracketing limit. If it is a closing bracket,
00219                 # eat the bracket, otherwise, stop the search.
00220                 level = bracketing[brck_index][1]
00221                 while brck_index > 0 and bracketing[brck_index-1][1] > level:
00222                     brck_index -= 1
00223                 if bracketing[brck_index][0] == brck_limit:
00224                     # We were not at the end of a closing bracket
00225                     break
00226                 pos = bracketing[brck_index][0]
00227                 brck_index -= 1
00228                 brck_limit = bracketing[brck_index][0]
00229                 last_identifier_pos = pos
00230                 if rawtext[pos] in "([":
00231                     # [] and () may be used after an identifier, so we
00232                     # continue. postdot_phase is True, so we don't allow a dot.
00233                     pass
00234                 else:
00235                     # We can't continue after other types of brackets
00236                     break
00237 
00238             else:
00239                 # We've found an operator or something.
00240                 break
00241 
00242         return rawtext[last_identifier_pos:self.indexinrawtext]

Here is the call graph for this function:

def idlelib.HyperParser.HyperParser.get_surrounding_brackets (   self,
  openers = '([{', mustclose=False 
)
If the index given to the HyperParser is surrounded by a bracket
defined in openers (or at least has one before it), return the
indices of the opening bracket and the closing bracket (or the
end of line, whichever comes first).
If it is not surrounded by brackets, or the end of line comes before
the closing bracket and mustclose is True, returns None.

Definition at line 104 of file HyperParser.py.

00104 
00105     def get_surrounding_brackets(self, openers='([{', mustclose=False):
00106         """If the index given to the HyperParser is surrounded by a bracket
00107         defined in openers (or at least has one before it), return the
00108         indices of the opening bracket and the closing bracket (or the
00109         end of line, whichever comes first).
00110         If it is not surrounded by brackets, or the end of line comes before
00111         the closing bracket and mustclose is True, returns None.
00112         """
00113         bracketinglevel = self.bracketing[self.indexbracket][1]
00114         before = self.indexbracket
00115         while not self.isopener[before] or \
00116               self.rawtext[self.bracketing[before][0]] not in openers or \
00117               self.bracketing[before][1] > bracketinglevel:
00118             before -= 1
00119             if before < 0:
00120                 return None
00121             bracketinglevel = min(bracketinglevel, self.bracketing[before][1])
00122         after = self.indexbracket + 1
00123         while after < len(self.bracketing) and \
00124               self.bracketing[after][1] >= bracketinglevel:
00125             after += 1
00126 
00127         beforeindex = self.text.index("%s-%dc" %
00128             (self.stopatindex, len(self.rawtext)-self.bracketing[before][0]))
00129         if after >= len(self.bracketing) or \
00130            self.bracketing[after][0] > len(self.rawtext):
00131             if mustclose:
00132                 return None
00133             afterindex = self.stopatindex
00134         else:
00135             # We are after a real char, so it is a ')' and we give the index
00136             # before it.
00137             afterindex = self.text.index("%s-%dc" %
00138                 (self.stopatindex,
00139                  len(self.rawtext)-(self.bracketing[after][0]-1)))
00140 
00141         return beforeindex, afterindex

Is the index given to the HyperParser is in a normal code?

Definition at line 98 of file HyperParser.py.

00098 
00099     def is_in_code(self):
00100         """Is the index given to the HyperParser is in a normal code?"""
00101         return not self.isopener[self.indexbracket] or \
00102                self.rawtext[self.bracketing[self.indexbracket][0]] not in \
00103                                                                 ('#', '"', "'")

Here is the caller graph for this function:

Is the index given to the HyperParser is in a string?

Definition at line 91 of file HyperParser.py.

00091 
00092     def is_in_string(self):
00093         """Is the index given to the HyperParser is in a string?"""
00094         # The bracket to which we belong should be an opener.
00095         # If it's an opener, it has to have a character.
00096         return self.isopener[self.indexbracket] and \
00097                self.rawtext[self.bracketing[self.indexbracket][0]] in ('"', "'")

def idlelib.HyperParser.HyperParser.set_index (   self,
  index 
)
Set the index to which the functions relate. Note that it must be
in the same statement.

Definition at line 72 of file HyperParser.py.

00072 
00073     def set_index(self, index):
00074         """Set the index to which the functions relate. Note that it must be
00075         in the same statement.
00076         """
00077         indexinrawtext = \
00078             len(self.rawtext) - len(self.text.get(index, self.stopatindex))
00079         if indexinrawtext < 0:
00080             raise ValueError("The index given is before the analyzed statement")
00081         self.indexinrawtext = indexinrawtext
00082         # find the rightmost bracket to which index belongs
00083         self.indexbracket = 0
00084         while self.indexbracket < len(self.bracketing)-1 and \
00085               self.bracketing[self.indexbracket+1][0] < self.indexinrawtext:
00086             self.indexbracket += 1
00087         if self.indexbracket < len(self.bracketing)-1 and \
00088            self.bracketing[self.indexbracket+1][0] == self.indexinrawtext and \
00089            not self.isopener[self.indexbracket+1]:
00090             self.indexbracket += 1


Member Data Documentation

Definition at line 145 of file HyperParser.py.

Definition at line 147 of file HyperParser.py.

Definition at line 143 of file HyperParser.py.

Definition at line 64 of file HyperParser.py.

Definition at line 22 of file HyperParser.py.

Definition at line 82 of file HyperParser.py.

Definition at line 80 of file HyperParser.py.

Definition at line 67 of file HyperParser.py.

Definition at line 59 of file HyperParser.py.

Definition at line 63 of file HyperParser.py.

Definition at line 23 of file HyperParser.py.


The documentation for this class was generated from the following file: