Lines Matching refs:token

44 #    For each token, we determine if it is a block or continuation token.
60 """Stores information about a token.
63 token: The token
64 is_block: Whether the token represents a block indentation.
65 is_transient: Whether the token should be automatically removed without
66 finding a matching end token.
67 overridden_by: TokenInfo for a token that overrides the indentation that
68 this token would require.
69 is_permanent_override: Whether the override on this token should persist
70 even after the overriding token is removed from the stack. For example:
76 line_number: The effective line number of this token. Will either be the
81 def __init__(self, token, is_block=False):
85 token: The token
86 is_block: Whether the token represents a block indentation.
88 self.token = token
92 self.is_transient = not is_block and token.type not in (
94 self.line_number = token.line_number
97 result = '\n %s' % self.token
100 result, self.overridden_by.token.string)
127 def CheckToken(self, token, state):
128 """Checks a token for indentation errors.
131 token: The current token under consideration
135 An error array [error code, error string, error token] if the token is
139 token_type = token.type
142 is_first = self._IsFirstNonWhitespaceTokenInLine(token)
158 goog_scope = tokenutil.GoogScopeOrNoneFromStartBlock(start_token.token)
160 if not token.line.endswith('; // goog.scope\n'):
161 if (token.line.find('//') > -1 and
162 token.line.find('goog.scope') >
163 token.line.find('//')):
169 token,
170 Position(token.start_index, token.length)])
178 token,
179 Position(token.start_index, token.length)])
181 elif token_type == Type.KEYWORD and token.string in ('case', 'default'):
184 elif is_first and token.string == '.':
185 # This token should have been on the previous line, so treat it as if it
187 info = TokenInfo(token)
188 info.line_number = token.line_number - 1
195 token.metadata.IsUnaryOperator())
196 not_dot = token.string != '.'
197 if is_first and not_binary_operator and not_dot and token.type not in (
200 print 'Line #%d: stack %r' % (token.line_number, stack)
208 actual = self._GetActualIndentation(token)
213 next_code = tokenutil.SearchExcept(token, Type.NON_CODE_TYPES)
227 token,
229 self._start_index_offset[token.line_number] = expected[0] - actual
234 token=token,
235 is_block=token.metadata.context.type == Context.ARRAY_LITERAL))
237 elif token_type == Type.START_BLOCK or token.metadata.is_implied_block:
238 self._Add(TokenInfo(token=token, is_block=True))
241 self._Add(TokenInfo(token=token, is_block=False))
243 elif token_type == Type.KEYWORD and token.string == 'return':
244 self._Add(TokenInfo(token))
246 elif not token.IsLastInLine() and (
247 token.IsAssignment() or token.IsOperator('?')):
248 self._Add(TokenInfo(token=token))
251 if token.metadata.is_implied_block_close:
255 is_last = self._IsLastCodeInLine(token)
258 if token.string == ':':
259 if stack and stack[-1].token.string == '?':
262 if token.line_number == stack[-1].token.line_number:
263 self._Add(TokenInfo(token))
264 elif token.metadata.context.type == Context.CASE_BLOCK:
272 self._Add(TokenInfo(token=token, is_block=True))
273 elif token.metadata.context.type == Context.LITERAL_ELEMENT:
276 self._Add(TokenInfo(token))
283 elif token.string != ',':
284 self._Add(TokenInfo(token))
286 # The token is a comma.
287 if token.metadata.context.type == Context.VAR:
288 self._Add(TokenInfo(token))
289 elif token.metadata.context.type != Context.PARAMETERS:
292 elif (token.string.endswith('.')
294 self._Add(TokenInfo(token))
295 elif token_type == Type.PARAMETERS and token.string.endswith(','):
297 self._Add(TokenInfo(token))
298 elif token.IsKeyword('var'):
299 self._Add(TokenInfo(token))
300 elif token.metadata.is_implied_semicolon:
302 elif token.IsAssignment():
303 self._Add(TokenInfo(token))
324 def _IsHardStop(self, token):
325 """Determines if the given token can have a hard stop after it.
328 token: token to examine
331 Whether the token can have a hard stop after it.
333 Hard stops are indentations defined by the position of another token as in
336 return (token.type in self._HARD_STOP_TYPES or
337 token.string in self._HARD_STOP_STRINGS or
338 token.IsAssignment())
358 token = token_info.token
361 if not token_info.overridden_by and token.string != 'return':
375 if self._IsHardStop(token):
378 token_info.overridden_by.token))
380 start_index = token.start_index
381 if token.line_number in self._start_index_offset:
382 start_index += self._start_index_offset[token.line_number]
383 if (token.type in (Type.START_PAREN, Type.START_PARAMETERS) and
387 elif token.string == 'return' and not token_info.overridden_by:
390 elif token.type == Type.START_BRACKET:
393 elif token.IsAssignment():
394 hard_stops.add(start_index + len(token.string) + 1)
396 elif token.IsOperator('?') and not token_info.overridden_by:
401 def _GetActualIndentation(self, token):
402 """Gets the actual indentation of the line containing the given token.
405 token: Any token on the line.
408 The actual indentation of the line containing the given token. Returns
411 # Move to the first token in the line
412 token = tokenutil.GetFirstTokenInSameLine(token)
415 if token.type == Type.WHITESPACE:
416 if token.string.find('\t') >= 0:
419 return len(token.string)
420 elif token.type == Type.PARAMETERS:
421 return len(token.string) - len(token.string.lstrip())
425 def _IsFirstNonWhitespaceTokenInLine(self, token):
426 """Determines if the given token is the first non-space token on its line.
429 token: The token.
432 True if the token is the first non-whitespace token on its line.
434 if token.type in (Type.WHITESPACE, Type.BLANK_LINE):
436 if token.IsFirstInLine():
438 return (token.previous and token.previous.IsFirstInLine() and
439 token.previous.type == Type.WHITESPACE)
441 def _IsLastCodeInLine(self, token):
442 """Determines if the given token is the last code token on its line.
445 token: The token.
448 True if the token is the last code token on its line.
450 if token.type in Type.NON_CODE_TYPES:
452 start_token = token
454 token = token.next
455 if not token or token.line_number != start_token.line_number:
457 if token.type not in Type.NON_CODE_TYPES:
461 """Adds the given token info to the stack.
464 token_info: The token information to add.
466 if self._stack and self._stack[-1].token == token_info.token:
467 # Don't add the same token twice.
470 if token_info.is_block or token_info.token.type == Type.START_PAREN:
472 tokenutil.GoogScopeOrNoneFromStartBlock(token_info.token))
476 stack_token = stack_info.token
482 if (token_info.token.type == Type.START_BLOCK and
492 close_block = token_info.token.metadata.context.end_token
494 close_block.line_number != token_info.token.line_number)
495 elif (token_info.token.type == Type.START_BLOCK and
496 token_info.token.metadata.context.type == Context.BLOCK and
514 """Pops the top token from the stack.
517 The popped token info.
520 if token_info.token.type not in (Type.START_BLOCK, Type.START_BRACKET):
526 token_check = token_info.token
529 if token_info.token.type == Type.START_BRACKET:
533 line_number = token_info.token.line_number
547 """Pops the stack until an implied block token is found."""
548 while not self._Pop().token.metadata.is_implied_block:
552 """Pops the stack until a token of the given type is popped.
555 stop_type: The type of token to pop to.
558 The token info of the given type that was popped.
563 if last.token.type == stop_type:
568 """Marks any token that was overridden by this token as active again.
571 token_info: The token that is being removed from the stack.