Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(924)

Side by Side Diff: third_party/closure_linter/closure_linter/javascripttokens.py

Issue 2592193002: Remove closure_linter from Chrome (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Copyright 2008 The Closure Linter Authors. All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS-IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """Classes to represent JavaScript tokens."""
18
19 __author__ = ('robbyw@google.com (Robert Walker)',
20 'ajp@google.com (Andy Perelson)')
21
22 from closure_linter.common import tokens
23
24 class JavaScriptTokenType(tokens.TokenType):
25 """Enumeration of JavaScript token types, and useful sets of token types."""
26 NUMBER = 'number'
27 START_SINGLE_LINE_COMMENT = '//'
28 START_BLOCK_COMMENT = '/*'
29 START_DOC_COMMENT = '/**'
30 END_BLOCK_COMMENT = '*/'
31 END_DOC_COMMENT = 'doc */'
32 COMMENT = 'comment'
33 SINGLE_QUOTE_STRING_START = "'string"
34 SINGLE_QUOTE_STRING_END = "string'"
35 DOUBLE_QUOTE_STRING_START = '"string'
36 DOUBLE_QUOTE_STRING_END = 'string"'
37 TEMPLATE_STRING_START = '`string'
38 TEMPLATE_STRING_END = 'string`'
39 STRING_TEXT = 'string'
40 START_BLOCK = '{'
41 END_BLOCK = '}'
42 START_PAREN = '('
43 END_PAREN = ')'
44 START_BRACKET = '['
45 END_BRACKET = ']'
46 REGEX = '/regex/'
47 FUNCTION_DECLARATION = 'function(...)'
48 FUNCTION_NAME = 'function functionName(...)'
49 START_PARAMETERS = 'startparams('
50 PARAMETERS = 'pa,ra,ms'
51 END_PARAMETERS = ')endparams'
52 SEMICOLON = ';'
53 DOC_FLAG = '@flag'
54 DOC_INLINE_FLAG = '{@flag ...}'
55 DOC_START_BRACE = 'doc {'
56 DOC_END_BRACE = 'doc }'
57 DOC_PREFIX = 'comment prefix: * '
58 DOC_TYPE_START_BLOCK = 'Type <'
59 DOC_TYPE_END_BLOCK = 'Type >'
60 DOC_TYPE_MODIFIER = 'modifier'
61 SIMPLE_LVALUE = 'lvalue='
62 KEYWORD = 'keyword'
63 OPERATOR = 'operator'
64 IDENTIFIER = 'identifier'
65
66 STRING_TYPES = frozenset([
67 SINGLE_QUOTE_STRING_START, SINGLE_QUOTE_STRING_END,
68 DOUBLE_QUOTE_STRING_START, DOUBLE_QUOTE_STRING_END,
69 TEMPLATE_STRING_START, TEMPLATE_STRING_END, STRING_TEXT])
70
71 COMMENT_TYPES = frozenset([
72 START_SINGLE_LINE_COMMENT, COMMENT,
73 START_BLOCK_COMMENT, START_DOC_COMMENT,
74 END_BLOCK_COMMENT, END_DOC_COMMENT,
75 DOC_START_BRACE, DOC_END_BRACE,
76 DOC_FLAG, DOC_INLINE_FLAG, DOC_PREFIX,
77 DOC_TYPE_START_BLOCK, DOC_TYPE_END_BLOCK, DOC_TYPE_MODIFIER])
78
79 FLAG_DESCRIPTION_TYPES = frozenset([
80 DOC_INLINE_FLAG, COMMENT, DOC_START_BRACE, DOC_END_BRACE,
81 DOC_TYPE_START_BLOCK, DOC_TYPE_END_BLOCK, DOC_TYPE_MODIFIER])
82
83 FLAG_ENDING_TYPES = frozenset([DOC_FLAG, END_DOC_COMMENT])
84
85 NON_CODE_TYPES = COMMENT_TYPES | frozenset([
86 tokens.TokenType.WHITESPACE, tokens.TokenType.BLANK_LINE])
87
88 UNARY_OPERATORS = ['!', 'new', 'delete', 'typeof', 'void']
89
90 UNARY_OK_OPERATORS = ['--', '++', '-', '+'] + UNARY_OPERATORS
91
92 UNARY_POST_OPERATORS = ['--', '++']
93
94 # An expression ender is any token that can end an object - i.e. we could have
95 # x.y or [1, 2], or (10 + 9) or {a: 10}.
96 EXPRESSION_ENDER_TYPES = [tokens.TokenType.NORMAL, IDENTIFIER, NUMBER,
97 SIMPLE_LVALUE, END_BRACKET, END_PAREN, END_BLOCK,
98 SINGLE_QUOTE_STRING_END, DOUBLE_QUOTE_STRING_END,
99 TEMPLATE_STRING_END]
100
101
102 class JavaScriptToken(tokens.Token):
103 """JavaScript token subclass of Token, provides extra instance checks.
104
105 The following token types have data in attached_object:
106 - All JsDoc flags: a parser.JsDocFlag object.
107 """
108
109 def IsKeyword(self, keyword):
110 """Tests if this token is the given keyword.
111
112 Args:
113 keyword: The keyword to compare to.
114
115 Returns:
116 True if this token is a keyword token with the given name.
117 """
118 return self.type == JavaScriptTokenType.KEYWORD and self.string == keyword
119
120 def IsOperator(self, operator):
121 """Tests if this token is the given operator.
122
123 Args:
124 operator: The operator to compare to.
125
126 Returns:
127 True if this token is a operator token with the given name.
128 """
129 return self.type == JavaScriptTokenType.OPERATOR and self.string == operator
130
131 def IsAssignment(self):
132 """Tests if this token is an assignment operator.
133
134 Returns:
135 True if this token is an assignment operator.
136 """
137 return (self.type == JavaScriptTokenType.OPERATOR and
138 self.string.endswith('=') and
139 self.string not in ('==', '!=', '>=', '<=', '===', '!=='))
140
141 def IsComment(self):
142 """Tests if this token is any part of a comment.
143
144 Returns:
145 True if this token is any part of a comment.
146 """
147 return self.type in JavaScriptTokenType.COMMENT_TYPES
148
149 def IsCode(self):
150 """Tests if this token is code, as opposed to a comment or whitespace."""
151 return self.type not in JavaScriptTokenType.NON_CODE_TYPES
152
153 def __repr__(self):
154 return '<JavaScriptToken: %d, %s, "%s", %r, %r>' % (self.line_number,
155 self.type, self.string,
156 self.values,
157 self.metadata)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698