Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / tools / idl_parser / idl_lexer.py
blob5ba1d4e4e9e74ef34d6912451cefeb70e881b7f3
1 #!/usr/bin/env python
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """ Lexer for PPAPI IDL
8 The lexer uses the PLY library to build a tokenizer which understands both
9 WebIDL and Pepper tokens.
11 WebIDL, and WebIDL regular expressions can be found at:
12 http://www.w3.org/TR/2012/CR-WebIDL-20120419/
13 PLY can be found at:
14 http://www.dabeaz.com/ply/
15 """
17 import os.path
18 import sys
21 # Try to load the ply module, if not, then assume it is in the third_party
22 # directory.
24 try:
25 # Disable lint check which fails to find the ply module.
26 # pylint: disable=F0401
27 from ply import lex
28 except ImportError:
29 module_path, module_name = os.path.split(__file__)
30 third_party = os.path.join(module_path, '..', '..', 'third_party')
31 sys.path.append(third_party)
32 # pylint: disable=F0401
33 from ply import lex
36 # IDL Lexer
38 class IDLLexer(object):
39 # 'literals' is a value expected by lex which specifies a list of valid
40 # literal tokens, meaning the token type and token value are identical.
41 literals = r'"*.(){}[],;:=+-/~|&^?<>'
43 # 't_ignore' contains ignored characters (spaces and tabs)
44 t_ignore = ' \t'
46 # 'tokens' is a value required by lex which specifies the complete list
47 # of valid token types.
48 tokens = [
49 # Data types
50 'float',
51 'integer',
52 'string',
54 # Symbol and keywords types
55 'COMMENT',
56 'identifier',
58 # MultiChar operators
59 'ELLIPSIS',
62 # 'keywords' is a map of string to token type. All tokens matching
63 # KEYWORD_OR_SYMBOL are matched against keywords dictionary, to determine
64 # if the token is actually a keyword.
65 keywords = {
66 'any' : 'ANY',
67 'attribute' : 'ATTRIBUTE',
68 'boolean' : 'BOOLEAN',
69 'byte' : 'BYTE',
70 'ByteString' : 'BYTESTRING',
71 'callback' : 'CALLBACK',
72 'const' : 'CONST',
73 'creator' : 'CREATOR',
74 'Date' : 'DATE',
75 'deleter' : 'DELETER',
76 'dictionary' : 'DICTIONARY',
77 'DOMString' : 'DOMSTRING',
78 'double' : 'DOUBLE',
79 'enum' : 'ENUM',
80 'false' : 'FALSE',
81 'float' : 'FLOAT',
82 'exception' : 'EXCEPTION',
83 'getter': 'GETTER',
84 'implements' : 'IMPLEMENTS',
85 'Infinity' : 'INFINITY',
86 'inherit' : 'INHERIT',
87 'interface' : 'INTERFACE',
88 'legacycaller' : 'LEGACYCALLER',
89 'long' : 'LONG',
90 'Nan' : 'NAN',
91 'null' : 'NULL',
92 'object' : 'OBJECT',
93 'octet' : 'OCTET',
94 'optional' : 'OPTIONAL',
95 'or' : 'OR',
96 'partial' : 'PARTIAL',
97 'Promise' : 'PROMISE',
98 'readonly' : 'READONLY',
99 'RegExp' : 'REGEXP',
100 'sequence' : 'SEQUENCE',
101 'serializer' : 'SERIALIZER',
102 'setter': 'SETTER',
103 'short' : 'SHORT',
104 'static' : 'STATIC',
105 'stringifier' : 'STRINGIFIER',
106 'typedef' : 'TYPEDEF',
107 'true' : 'TRUE',
108 'unsigned' : 'UNSIGNED',
109 'unrestricted' : 'UNRESTRICTED',
110 'void' : 'VOID'
113 # Token definitions
115 # Lex assumes any value or function in the form of 't_<TYPE>' represents a
116 # regular expression where a match will emit a token of type <TYPE>. In the
117 # case of a function, the function is called when a match is made. These
118 # definitions come from WebIDL.
120 # These need to be methods for lexer construction, despite not using self.
121 # pylint: disable=R0201
122 def t_ELLIPSIS(self, t):
123 r'\.\.\.'
124 return t
126 # Regex needs to be in the docstring
127 # pylint: disable=C0301
128 def t_float(self, t):
129 r'-?(([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)'
130 return t
132 def t_integer(self, t):
133 r'-?([1-9][0-9]*|0[Xx][0-9A-Fa-f]+|0[0-7]*)'
134 return t
137 # A line ending '\n', we use this to increment the line number
138 def t_LINE_END(self, t):
139 r'\n+'
140 self.AddLines(len(t.value))
142 # We do not process escapes in the IDL strings. Strings are exclusively
143 # used for attributes and enums, and not used as typical 'C' constants.
144 def t_string(self, t):
145 r'"[^"]*"'
146 t.value = t.value[1:-1]
147 self.AddLines(t.value.count('\n'))
148 return t
150 # A C or C++ style comment: /* xxx */ or //
151 def t_COMMENT(self, t):
152 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)'
153 self.AddLines(t.value.count('\n'))
154 return t
156 # A symbol or keyword.
157 def t_KEYWORD_OR_SYMBOL(self, t):
158 r'_?[A-Za-z][A-Za-z_0-9]*'
160 # All non-keywords are assumed to be symbols
161 t.type = self.keywords.get(t.value, 'identifier')
163 # We strip leading underscores so that you can specify symbols with the same
164 # value as a keywords (E.g. a dictionary named 'interface').
165 if t.value[0] == '_':
166 t.value = t.value[1:]
167 return t
169 def t_ANY_error(self, t):
170 msg = 'Unrecognized input'
171 line = self.Lexer().lineno
173 # If that line has not been accounted for, then we must have hit
174 # EoF, so compute the beginning of the line that caused the problem.
175 if line >= len(self.index):
176 # Find the offset in the line of the first word causing the issue
177 word = t.value.split()[0]
178 offs = self.lines[line - 1].find(word)
179 # Add the computed line's starting position
180 self.index.append(self.Lexer().lexpos - offs)
181 msg = 'Unexpected EoF reached after'
183 pos = self.Lexer().lexpos - self.index[line]
184 out = self.ErrorMessage(line, pos, msg)
185 sys.stderr.write(out + '\n')
186 self._lex_errors += 1
189 def AddLines(self, count):
190 # Set the lexer position for the beginning of the next line. In the case
191 # of multiple lines, tokens can not exist on any of the lines except the
192 # last one, so the recorded value for previous lines are unused. We still
193 # fill the array however, to make sure the line count is correct.
194 self.Lexer().lineno += count
195 for _ in range(count):
196 self.index.append(self.Lexer().lexpos)
198 def FileLineMsg(self, line, msg):
199 # Generate a message containing the file and line number of a token.
200 filename = self.Lexer().filename
201 if filename:
202 return "%s(%d) : %s" % (filename, line + 1, msg)
203 return "<BuiltIn> : %s" % msg
205 def SourceLine(self, line, pos):
206 # Create a source line marker
207 caret = ' ' * pos + '^'
208 # We decrement the line number since the array is 0 based while the
209 # line numbers are 1 based.
210 return "%s\n%s" % (self.lines[line - 1], caret)
212 def ErrorMessage(self, line, pos, msg):
213 return "\n%s\n%s" % (
214 self.FileLineMsg(line, msg),
215 self.SourceLine(line, pos))
218 # Tokenizer
220 # The token function returns the next token provided by IDLLexer for matching
221 # against the leaf paterns.
223 def token(self):
224 tok = self.Lexer().token()
225 if tok:
226 self.last = tok
227 return tok
230 def GetTokens(self):
231 outlist = []
232 while True:
233 t = self.Lexer().token()
234 if not t:
235 break
236 outlist.append(t)
237 return outlist
239 def Tokenize(self, data, filename='__no_file__'):
240 lexer = self.Lexer()
241 lexer.lineno = 1
242 lexer.filename = filename
243 lexer.input(data)
244 self.lines = data.split('\n')
246 def KnownTokens(self):
247 return self.tokens
249 def Lexer(self):
250 if not self._lexobj:
251 self._lexobj = lex.lex(object=self, lextab=None, optimize=0)
252 return self._lexobj
254 def _AddToken(self, token):
255 if token in self.tokens:
256 raise RuntimeError('Same token: ' + token)
257 self.tokens.append(token)
259 def _AddTokens(self, tokens):
260 for token in tokens:
261 self._AddToken(token)
263 def _AddKeywords(self, keywords):
264 for key in keywords:
265 value = key.upper()
266 self._AddToken(value)
267 self.keywords[key] = value
269 def _DelKeywords(self, keywords):
270 for key in keywords:
271 self.tokens.remove(key.upper())
272 del self.keywords[key]
274 def __init__(self):
275 self.index = [0]
276 self._lex_errors = 0
277 self.linex = []
278 self.filename = None
279 self.keywords = {}
280 self.tokens = []
281 self._AddTokens(IDLLexer.tokens)
282 self._AddKeywords(IDLLexer.keywords)
283 self._lexobj = None
284 self.last = None
285 self.lines = None
287 # If run by itself, attempt to build the lexer
288 if __name__ == '__main__':
289 lexer_object = IDLLexer()