blob: a86fe59e4c0abc403262c9ec0a0c29ab65ccbb28 [file] [log] [blame]
[email protected]cffee7f2013-04-11 17:03:481#!/usr/bin/env python
[email protected]683c8c52013-04-06 17:00:462# 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.
5
6""" Lexer for PPAPI IDL
7
8The lexer uses the PLY library to build a tokenizer which understands both
9WebIDL and Pepper tokens.
10
11WebIDL, and WebIDL regular expressions can be found at:
raphael.kubo.da.costa4bec0d72017-02-22 10:12:4412 http://heycam.github.io/webidl/
[email protected]683c8c52013-04-06 17:00:4613PLY can be found at:
14 http://www.dabeaz.com/ply/
15"""
16
[email protected]683c8c52013-04-06 17:00:4617import os.path
18import sys
19
raphael.kubo.da.costaadf05592017-02-21 12:58:2320SRC_DIR = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
21sys.path.insert(0, os.path.join(SRC_DIR, 'third_party'))
22from ply import lex
23
[email protected]683c8c52013-04-06 17:00:4624
25#
26# IDL Lexer
27#
28class IDLLexer(object):
[email protected]a8f94282013-08-14 01:42:3029 # 'literals' is a value expected by lex which specifies a list of valid
30 # literal tokens, meaning the token type and token value are identical.
31 literals = r'"*.(){}[],;:=+-/~|&^?<>'
32
33 # 't_ignore' contains ignored characters (spaces and tabs)
34 t_ignore = ' \t'
35
[email protected]683c8c52013-04-06 17:00:4636 # 'tokens' is a value required by lex which specifies the complete list
37 # of valid token types.
38 tokens = [
39 # Data types
40 'float',
41 'integer',
42 'string',
43
[email protected]683c8c52013-04-06 17:00:4644 # Symbol and keywords types
45 'COMMENT',
46 'identifier',
47
[email protected]d4b86672013-04-11 16:28:3148 # MultiChar operators
49 'ELLIPSIS',
[email protected]683c8c52013-04-06 17:00:4650 ]
51
52 # 'keywords' is a map of string to token type. All tokens matching
53 # KEYWORD_OR_SYMBOL are matched against keywords dictionary, to determine
54 # if the token is actually a keyword.
55 keywords = {
56 'any' : 'ANY',
57 'attribute' : 'ATTRIBUTE',
58 'boolean' : 'BOOLEAN',
59 'byte' : 'BYTE',
[email protected]5885b692014-06-19 14:43:2460 'ByteString' : 'BYTESTRING',
[email protected]683c8c52013-04-06 17:00:4661 'callback' : 'CALLBACK',
62 'const' : 'CONST',
63 'creator' : 'CREATOR',
64 'Date' : 'DATE',
65 'deleter' : 'DELETER',
66 'dictionary' : 'DICTIONARY',
67 'DOMString' : 'DOMSTRING',
68 'double' : 'DOUBLE',
69 'enum' : 'ENUM',
jl9016ef322014-12-16 15:05:0970 'exception' : 'EXCEPTION',
[email protected]683c8c52013-04-06 17:00:4671 'false' : 'FALSE',
72 'float' : 'FLOAT',
bashicb5c16612015-08-19 02:11:2473 'FrozenArray' : 'FROZENARRAY',
[email protected]683c8c52013-04-06 17:00:4674 'getter': 'GETTER',
75 'implements' : 'IMPLEMENTS',
76 'Infinity' : 'INFINITY',
77 'inherit' : 'INHERIT',
78 'interface' : 'INTERFACE',
jl9016ef322014-12-16 15:05:0979 'iterable': 'ITERABLE',
[email protected]683c8c52013-04-06 17:00:4680 'legacycaller' : 'LEGACYCALLER',
jl9016ef322014-12-16 15:05:0981 'legacyiterable' : 'LEGACYITERABLE',
[email protected]683c8c52013-04-06 17:00:4682 'long' : 'LONG',
jl9016ef322014-12-16 15:05:0983 'maplike': 'MAPLIKE',
[email protected]683c8c52013-04-06 17:00:4684 'Nan' : 'NAN',
85 'null' : 'NULL',
86 'object' : 'OBJECT',
87 'octet' : 'OCTET',
88 'optional' : 'OPTIONAL',
89 'or' : 'OR',
yhirano6ce2b8e2014-10-20 12:49:0990 'partial' : 'PARTIAL',
91 'Promise' : 'PROMISE',
[email protected]683c8c52013-04-06 17:00:4692 'readonly' : 'READONLY',
[email protected]5885b692014-06-19 14:43:2493 'RegExp' : 'REGEXP',
raphael.kubo.da.costa4bec0d72017-02-22 10:12:4494 'record' : 'RECORD',
jl9016ef322014-12-16 15:05:0995 'required' : 'REQUIRED',
[email protected]683c8c52013-04-06 17:00:4696 'sequence' : 'SEQUENCE',
[email protected]5885b692014-06-19 14:43:2497 'serializer' : 'SERIALIZER',
jl9016ef322014-12-16 15:05:0998 'setlike' : 'SETLIKE',
[email protected]683c8c52013-04-06 17:00:4699 'setter': 'SETTER',
100 'short' : 'SHORT',
101 'static' : 'STATIC',
102 'stringifier' : 'STRINGIFIER',
[email protected]683c8c52013-04-06 17:00:46103 'typedef' : 'TYPEDEF',
104 'true' : 'TRUE',
105 'unsigned' : 'UNSIGNED',
106 'unrestricted' : 'UNRESTRICTED',
raphael.kubo.da.costa4bec0d72017-02-22 10:12:44107 'USVString' : 'USVSTRING',
[email protected]683c8c52013-04-06 17:00:46108 'void' : 'VOID'
109 }
110
[email protected]683c8c52013-04-06 17:00:46111 # Token definitions
112 #
113 # Lex assumes any value or function in the form of 't_<TYPE>' represents a
114 # regular expression where a match will emit a token of type <TYPE>. In the
115 # case of a function, the function is called when a match is made. These
116 # definitions come from WebIDL.
[email protected]a8f94282013-08-14 01:42:30117 #
118 # These need to be methods for lexer construction, despite not using self.
119 # pylint: disable=R0201
[email protected]ac7b49d2013-04-12 18:48:47120 def t_ELLIPSIS(self, t):
121 r'\.\.\.'
122 return t
[email protected]683c8c52013-04-06 17:00:46123
[email protected]a8f94282013-08-14 01:42:30124 # Regex needs to be in the docstring
125 # pylint: disable=C0301
[email protected]ac7b49d2013-04-12 18:48:47126 def t_float(self, t):
127 r'-?(([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)'
128 return t
[email protected]683c8c52013-04-06 17:00:46129
[email protected]ac7b49d2013-04-12 18:48:47130 def t_integer(self, t):
[email protected]9f1b57f2013-08-07 05:08:09131 r'-?([1-9][0-9]*|0[Xx][0-9A-Fa-f]+|0[0-7]*)'
[email protected]ac7b49d2013-04-12 18:48:47132 return t
[email protected]d4b86672013-04-11 16:28:31133
[email protected]683c8c52013-04-06 17:00:46134
[email protected]683c8c52013-04-06 17:00:46135 # A line ending '\n', we use this to increment the line number
136 def t_LINE_END(self, t):
137 r'\n+'
138 self.AddLines(len(t.value))
139
140 # We do not process escapes in the IDL strings. Strings are exclusively
141 # used for attributes and enums, and not used as typical 'C' constants.
142 def t_string(self, t):
143 r'"[^"]*"'
144 t.value = t.value[1:-1]
145 self.AddLines(t.value.count('\n'))
146 return t
147
148 # A C or C++ style comment: /* xxx */ or //
149 def t_COMMENT(self, t):
150 r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)'
151 self.AddLines(t.value.count('\n'))
152 return t
153
[email protected]683c8c52013-04-06 17:00:46154 # A symbol or keyword.
155 def t_KEYWORD_OR_SYMBOL(self, t):
156 r'_?[A-Za-z][A-Za-z_0-9]*'
157
158 # All non-keywords are assumed to be symbols
159 t.type = self.keywords.get(t.value, 'identifier')
160
161 # We strip leading underscores so that you can specify symbols with the same
162 # value as a keywords (E.g. a dictionary named 'interface').
163 if t.value[0] == '_':
164 t.value = t.value[1:]
165 return t
166
167 def t_ANY_error(self, t):
168 msg = 'Unrecognized input'
[email protected]ac7b49d2013-04-12 18:48:47169 line = self.Lexer().lineno
[email protected]683c8c52013-04-06 17:00:46170
171 # If that line has not been accounted for, then we must have hit
172 # EoF, so compute the beginning of the line that caused the problem.
173 if line >= len(self.index):
174 # Find the offset in the line of the first word causing the issue
175 word = t.value.split()[0]
176 offs = self.lines[line - 1].find(word)
177 # Add the computed line's starting position
[email protected]ac7b49d2013-04-12 18:48:47178 self.index.append(self.Lexer().lexpos - offs)
[email protected]683c8c52013-04-06 17:00:46179 msg = 'Unexpected EoF reached after'
180
[email protected]ac7b49d2013-04-12 18:48:47181 pos = self.Lexer().lexpos - self.index[line]
[email protected]683c8c52013-04-06 17:00:46182 out = self.ErrorMessage(line, pos, msg)
183 sys.stderr.write(out + '\n')
[email protected]d4b86672013-04-11 16:28:31184 self._lex_errors += 1
[email protected]683c8c52013-04-06 17:00:46185
186
187 def AddLines(self, count):
188 # Set the lexer position for the beginning of the next line. In the case
189 # of multiple lines, tokens can not exist on any of the lines except the
190 # last one, so the recorded value for previous lines are unused. We still
191 # fill the array however, to make sure the line count is correct.
[email protected]ac7b49d2013-04-12 18:48:47192 self.Lexer().lineno += count
[email protected]683c8c52013-04-06 17:00:46193 for _ in range(count):
[email protected]ac7b49d2013-04-12 18:48:47194 self.index.append(self.Lexer().lexpos)
[email protected]683c8c52013-04-06 17:00:46195
196 def FileLineMsg(self, line, msg):
197 # Generate a message containing the file and line number of a token.
[email protected]ac7b49d2013-04-12 18:48:47198 filename = self.Lexer().filename
[email protected]683c8c52013-04-06 17:00:46199 if filename:
200 return "%s(%d) : %s" % (filename, line + 1, msg)
201 return "<BuiltIn> : %s" % msg
202
203 def SourceLine(self, line, pos):
204 # Create a source line marker
[email protected]d4b86672013-04-11 16:28:31205 caret = ' ' * pos + '^'
[email protected]683c8c52013-04-06 17:00:46206 # We decrement the line number since the array is 0 based while the
207 # line numbers are 1 based.
208 return "%s\n%s" % (self.lines[line - 1], caret)
209
210 def ErrorMessage(self, line, pos, msg):
211 return "\n%s\n%s" % (
212 self.FileLineMsg(line, msg),
213 self.SourceLine(line, pos))
214
[email protected]d4b86672013-04-11 16:28:31215#
216# Tokenizer
217#
218# The token function returns the next token provided by IDLLexer for matching
219# against the leaf paterns.
220#
221 def token(self):
[email protected]ac7b49d2013-04-12 18:48:47222 tok = self.Lexer().token()
[email protected]d4b86672013-04-11 16:28:31223 if tok:
224 self.last = tok
225 return tok
226
227
[email protected]683c8c52013-04-06 17:00:46228 def GetTokens(self):
229 outlist = []
230 while True:
[email protected]ac7b49d2013-04-12 18:48:47231 t = self.Lexer().token()
[email protected]683c8c52013-04-06 17:00:46232 if not t:
233 break
234 outlist.append(t)
235 return outlist
236
[email protected]d4b86672013-04-11 16:28:31237 def Tokenize(self, data, filename='__no_file__'):
[email protected]ac7b49d2013-04-12 18:48:47238 lexer = self.Lexer()
239 lexer.lineno = 1
240 lexer.filename = filename
241 lexer.input(data)
[email protected]d4b86672013-04-11 16:28:31242 self.lines = data.split('\n')
[email protected]683c8c52013-04-06 17:00:46243
[email protected]ac7b49d2013-04-12 18:48:47244 def KnownTokens(self):
245 return self.tokens
246
247 def Lexer(self):
248 if not self._lexobj:
249 self._lexobj = lex.lex(object=self, lextab=None, optimize=0)
250 return self._lexobj
251
[email protected]ac7b49d2013-04-12 18:48:47252 def _AddToken(self, token):
253 if token in self.tokens:
254 raise RuntimeError('Same token: ' + token)
255 self.tokens.append(token)
256
257 def _AddTokens(self, tokens):
258 for token in tokens:
259 self._AddToken(token)
260
261 def _AddKeywords(self, keywords):
262 for key in keywords:
263 value = key.upper()
264 self._AddToken(value)
265 self.keywords[key] = value
266
[email protected]a958ace2013-06-29 20:51:01267 def _DelKeywords(self, keywords):
268 for key in keywords:
269 self.tokens.remove(key.upper())
270 del self.keywords[key]
271
[email protected]d4b86672013-04-11 16:28:31272 def __init__(self):
273 self.index = [0]
274 self._lex_errors = 0
275 self.linex = []
276 self.filename = None
[email protected]ac7b49d2013-04-12 18:48:47277 self.keywords = {}
278 self.tokens = []
[email protected]ac7b49d2013-04-12 18:48:47279 self._AddTokens(IDLLexer.tokens)
280 self._AddKeywords(IDLLexer.keywords)
281 self._lexobj = None
[email protected]a8f94282013-08-14 01:42:30282 self.last = None
283 self.lines = None
[email protected]683c8c52013-04-06 17:00:46284
[email protected]ac7b49d2013-04-12 18:48:47285# If run by itself, attempt to build the lexer
286if __name__ == '__main__':
[email protected]a8f94282013-08-14 01:42:30287 lexer_object = IDLLexer()