Lines Matching refs:token

32 def GetFirstTokenInSameLine(token):
33 """Returns the first token in the same line as token.
36 token: Any token in the line.
39 The first token in the same line as token.
41 while not token.IsFirstInLine():
42 token = token.previous
43 return token
46 def GetFirstTokenInPreviousLine(token):
47 """Returns the first token in the previous line as token.
50 token: Any token in the line.
53 The first token in the previous line as token, or None if token is on the
56 first_in_line = GetFirstTokenInSameLine(token)
63 def GetLastTokenInSameLine(token):
64 """Returns the last token in the same line as token.
67 token: Any token in the line.
70 The last token in the same line as token.
72 while not token.IsLastInLine():
73 token = token.next
74 return token
77 def GetAllTokensInSameLine(token):
78 """Returns all tokens in the same line as the given token.
81 token: Any token in the line.
84 All tokens on the same line as the given token.
86 first_token = GetFirstTokenInSameLine(token)
87 last_token = GetLastTokenInSameLine(token)
100 """Returns the first token where func is True within distance of this token.
103 start_token: The token to start searching from
104 func: The function to call to test a token for applicability
105 end_func: The function to call to test a token to determine whether to abort
108 be positive. If unspecified, will search until the end of the token
114 The first token matching func within distance of this token, or None if no
115 such token is found.
117 token = start_token
119 while token and (distance is None or distance > 0):
120 previous = token.previous
127 token = previous
132 while token and (distance is None or distance > 0):
133 next_token = token.next
140 token = next_token
148 """Returns the first token of type in token_types within distance.
151 start_token: The token to start searching from
152 token_types: The allowable types of the token being searched for
154 be positive. If unspecified, will search until the end of the token
160 The first token of any type in token_types within distance of this token, or
161 None if no such token is found.
163 return CustomSearch(start_token, lambda token: token.IsAnyType(token_types),
168 """Returns the first token not of any type in token_types within distance.
171 start_token: The token to start searching from
172 token_types: The unallowable types of the token being searched for
174 be positive. If unspecified, will search until the end of the token
180 The first token of any type in token_types within distance of this token, or
181 None if no such token is found.
184 lambda token: not token.IsAnyType(token_types),
190 """Returns the first token of type in token_types before a token of end_type.
193 start_token: The token to start searching from.
194 token_types: The allowable types of the token being searched for.
197 be positive. If unspecified, will search until the end of the token
203 The first token of any type in token_types within distance of this token
204 before any tokens of type in end_type, or None if no such token is found.
206 return CustomSearch(start_token, lambda token: token.IsAnyType(token_types),
207 lambda token: token.IsAnyType(end_types),
211 def DeleteToken(token):
212 """Deletes the given token from the linked list.
215 token: The token to delete
217 if token.previous:
218 token.previous.next = token.next
220 if token.next:
221 token.next.previous = token.previous
223 following_token = token.next
224 while following_token and following_token.metadata.last_code == token:
225 following_token.metadata.last_code = token.metadata.last_code
229 def DeleteTokens(token, token_count):
230 """Deletes the given number of tokens starting with the given token.
233 token: The token to start deleting at.
237 DeleteToken(token.next)
238 DeleteToken(token)
241 def InsertTokenAfter(new_token, token):
242 """Insert new_token after token.
245 new_token: A token to be added to the stream
246 token: A token already in the stream
248 new_token.previous = token
249 new_token.next = token.next
251 new_token.metadata = copy.copy(token.metadata)
253 if token.IsCode():
254 new_token.metadata.last_code = token
257 following_token = token.next
258 while following_token and following_token.metadata.last_code == token:
262 token.next = new_token
267 if new_token.line_number == token.line_number:
268 new_token.start_index = token.start_index + len(token.string)
278 def InsertTokensAfter(new_tokens, token):
279 """Insert multiple tokens after token.
283 token: A token already in the stream
287 current_token = token
293 def InsertSpaceTokenAfter(token):
294 """Inserts a space token after the given token.
297 token: The token to insert a space token after
300 A single space token
302 space_token = JavaScriptToken(' ', Type.WHITESPACE, token.line,
303 token.line_number)
304 InsertTokenAfter(space_token, token)
307 def InsertBlankLineAfter(token):
308 """Inserts a blank line after the given token.
311 token: The token to insert a blank line after
314 A single space token
317 token.line_number + 1)
318 InsertLineAfter(token, [blank_token])
321 def InsertLineAfter(token, new_tokens):
322 """Inserts a new line consisting of new_tokens after the given token.
325 token: The token to insert after.
328 insert_location = token
340 def SplitToken(token, position):
341 """Splits the token into two tokens at position.
344 token: The token to split
345 position: The position to split at. Will be the beginning of second token.
348 The new second token.
350 new_string = token.string[position:]
351 token.string = token.string[:position]
353 new_token = JavaScriptToken(new_string, token.type, token.line,
354 token.line_number)
355 InsertTokenAfter(new_token, token)
364 token1: The first token to compare.
365 token2: The second token to compare.
368 A negative integer, zero, or a positive integer as the first token is
369 before, equal, or after the second in the token stream.