summaryrefslogtreecommitdiffstats
path: root/tools/idl_parser/idl_ppapi_parser.py
blob: 8914c841997070902a33d98cac259485ad47187f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

""" Parser for PPAPI IDL """

#
# IDL Parser
#
# The parser is uses the PLY yacc library to build a set of parsing rules based
# on WebIDL.
#
# WebIDL, and WebIDL grammar can be found at:
#   http://heycam.github.io/webidl/
# PLY can be found at:
#   http://www.dabeaz.com/ply/
#
# The parser generates a tree by recursively matching sets of items against
# defined patterns.  When a match is made, that set of items is reduced
# to a new item.   The new item can provide a match for parent patterns.
# In this way an AST is built (reduced) depth first.
#

#
# Disable check for line length and Member as Function due to how grammar rules
# are defined with PLY
#
# pylint: disable=R0201
# pylint: disable=C0301

import sys

from idl_ppapi_lexer import IDLPPAPILexer
from idl_parser import IDLParser, ListFromConcat, ParseFile
from idl_node import IDLNode

class IDLPPAPIParser(IDLParser):
#
# We force all input files to start with two comments.  The first comment is a
# Copyright notice followed by a file comment and finally by file level
# productions.
#
  # [0] Insert a TOP definition for Copyright and Comments
  def p_Top(self, p):
    """Top : COMMENT COMMENT Definitions"""
    Copyright = self.BuildComment('Copyright', p, 1)
    Filedoc = self.BuildComment('Comment', p, 2)
    p[0] = ListFromConcat(Copyright, Filedoc, p[3])

#
#The parser is based on the WebIDL standard.  See:
# http://heycam.github.io/webidl/#idl-grammar
#
  # [1]
  def p_Definitions(self, p):
    """Definitions : ExtendedAttributeList Definition Definitions
           | """
    if len(p) > 1:
      p[2].AddChildren(p[1])
      p[0] = ListFromConcat(p[2], p[3])

      # [2] Add INLINE definition
  def p_Definition(self, p):
    """Definition : CallbackOrInterface
                  | Struct
                  | Partial
                  | Dictionary
                  | Exception
                  | Enum
                  | Typedef
                  | ImplementsStatement
                  | Label
                  | Inline"""
    p[0] = p[1]

  def p_Inline(self, p):
    """Inline : INLINE"""
    words = p[1].split()
    name = self.BuildAttribute('NAME', words[1])
    lines = p[1].split('\n')
    value = self.BuildAttribute('VALUE', '\n'.join(lines[1:-1]) + '\n')
    children = ListFromConcat(name, value)
    p[0] = self.BuildProduction('Inline', p, 1, children)

#
# Label
#
# A label is a special kind of enumeration which allows us to go from a
# set of version numbrs to releases
#
  def p_Label(self, p):
    """Label : LABEL identifier '{' LabelList '}' ';'"""
    p[0] = self.BuildNamed('Label', p, 2, p[4])

  def p_LabelList(self, p):
    """LabelList : identifier '=' float LabelCont"""
    val  = self.BuildAttribute('VALUE', p[3])
    label = self.BuildNamed('LabelItem', p, 1, val)
    p[0] = ListFromConcat(label, p[4])

  def p_LabelCont(self, p):
    """LabelCont : ',' LabelList
                 |"""
    if len(p) > 1:
      p[0] = p[2]

  def p_LabelContError(self, p):
    """LabelCont : error LabelCont"""
    p[0] = p[2]

  # [5.1] Add "struct" style interface
  def p_Struct(self, p):
    """Struct : STRUCT identifier Inheritance '{' StructMembers '}' ';'"""
    p[0] = self.BuildNamed('Struct', p, 2, ListFromConcat(p[3], p[5]))

  def p_StructMembers(self, p):
    """StructMembers : StructMember StructMembers
                     |"""
    if len(p) > 1:
      p[0] = ListFromConcat(p[1], p[2])

  def p_StructMember(self, p):
    """StructMember : ExtendedAttributeList Type identifier ';'"""
    p[0] = self.BuildNamed('Member', p, 3, ListFromConcat(p[1], p[2]))

  def p_Typedef(self, p):
    """Typedef : TYPEDEF ExtendedAttributeListNoComments Type identifier ';'"""
    p[0] = self.BuildNamed('Typedef', p, 4, ListFromConcat(p[2], p[3]))

  def p_TypedefFunc(self, p):
    """Typedef : TYPEDEF ExtendedAttributeListNoComments ReturnType identifier '(' ArgumentList ')' ';'"""
    args = self.BuildProduction('Arguments', p, 5, p[6])
    p[0] = self.BuildNamed('Callback', p, 4, ListFromConcat(p[2], p[3], args))

  def p_ConstValue(self, p):
    """ConstValue : integer
                  | integer LSHIFT integer
                  | integer RSHIFT integer"""
    val = str(p[1])
    if len(p) > 2:
      val = "%s %s %s" % (p[1], p[2], p[3])
    p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'integer'),
                          self.BuildAttribute('VALUE', val))

  def p_ConstValueStr(self, p):
    """ConstValue : string"""
    p[0] = ListFromConcat(self.BuildAttribute('TYPE', 'string'),
                          self.BuildAttribute('VALUE', p[1]))

  # Boolean & Float Literals area already BuildAttributes
  def p_ConstValueLiteral(self, p):
    """ConstValue : FloatLiteral
                  | BooleanLiteral """
    p[0] = p[1]

  def p_EnumValueList(self, p):
    """EnumValueList : EnumValue EnumValues"""
    p[0] = ListFromConcat(p[1], p[2])

  def p_EnumValues(self, p):
    """EnumValues : ',' EnumValue EnumValues
                  |"""
    if len(p) > 1:
      p[0] = ListFromConcat(p[2], p[3])

  def p_EnumValue(self, p):
    """EnumValue : ExtendedAttributeList identifier
                 | ExtendedAttributeList identifier '=' ConstValue"""
    p[0] = self.BuildNamed('EnumItem', p, 2, p[1])
    if len(p) > 3:
      p[0].AddChildren(p[4])

  # Omit PromiseType, as it is a JS type.
  def p_NonAnyType(self, p):
    """NonAnyType : PrimitiveType TypeSuffix
                  | identifier TypeSuffix
                  | SEQUENCE '<' Type '>' Null"""
    IDLParser.p_NonAnyType(self, p)

  def p_PrimitiveType(self, p):
    """PrimitiveType : IntegerType
                     | UnsignedIntegerType
                     | FloatType
                     | HandleType
                     | PointerType"""
    if type(p[1]) == str:
      p[0] = self.BuildNamed('PrimitiveType', p, 1)
    else:
      p[0] = p[1]

  def p_PointerType(self, p):
    """PointerType : STR_T
                   | MEM_T
                   | CSTR_T
                   | INTERFACE_T
                   | NULL"""
    p[0] = p[1]

  def p_HandleType(self, p):
    """HandleType : HANDLE_T
                  | PP_FILEHANDLE"""
    p[0] = p[1]

  def p_FloatType(self, p):
    """FloatType : FLOAT_T
                 | DOUBLE_T"""
    p[0] = p[1]

  def p_UnsignedIntegerType(self, p):
    """UnsignedIntegerType : UINT8_T
                           | UINT16_T
                           | UINT32_T
                           | UINT64_T"""
    p[0] = p[1]


  def p_IntegerType(self, p):
    """IntegerType : CHAR
                   | INT8_T
                   | INT16_T
                   | INT32_T
                   | INT64_T"""
    p[0] = p[1]

  # These targets are no longer used
  def p_OptionalLong(self, p):
    """ """
    pass

  def p_UnrestrictedFloatType(self, p):
    """ """
    pass

  def p_null(self, p):
    """ """
    pass

  def p_PromiseType(self, p):
    """ """
    pass

  def p_EnumValueListComma(self, p):
    """ """
    pass

  def p_EnumValueListString(self, p):
    """ """
    pass

  # We only support:
  #    [ identifier ]
  #    [ identifier ( ArgumentList )]
  #    [ identifier ( ValueList )]
  #    [ identifier = identifier ]
  #    [ identifier = ( IdentifierList )]
  #    [ identifier = ConstValue ]
  #    [ identifier = identifier ( ArgumentList )]
  # [51] map directly to 74-77
  # [52-54, 56] are unsupported
  def p_ExtendedAttribute(self, p):
    """ExtendedAttribute : ExtendedAttributeNoArgs
                         | ExtendedAttributeArgList
                         | ExtendedAttributeValList
                         | ExtendedAttributeIdent
                         | ExtendedAttributeIdentList
                         | ExtendedAttributeIdentConst
                         | ExtendedAttributeNamedArgList"""
    p[0] = p[1]

  def p_ExtendedAttributeValList(self, p):
    """ExtendedAttributeValList : identifier '(' ValueList ')'"""
    arguments = self.BuildProduction('Values', p, 2, p[3])
    p[0] = self.BuildNamed('ExtAttribute', p, 1, arguments)

  def p_ValueList(self, p):
    """ValueList : ConstValue ValueListCont"""
    p[0] = ListFromConcat(p[1], p[2])

  def p_ValueListCont(self, p):
    """ValueListCont : ValueList
                     |"""
    if len(p) > 1:
      p[0] = p[1]

  def p_ExtendedAttributeIdentConst(self, p):
    """ExtendedAttributeIdentConst : identifier '=' ConstValue"""
    p[0] = self.BuildNamed('ExtAttribute', p, 1, p[3])


  def __init__(self, lexer, verbose=False, debug=False, mute_error=False):
    IDLParser.__init__(self, lexer, verbose, debug, mute_error)


def main(argv):
  nodes = []
  parser = IDLPPAPIParser(IDLPPAPILexer())
  errors = 0

  for filename in argv:
    filenode = ParseFile(parser, filename)
    if filenode:
      errors += filenode.GetProperty('ERRORS')
      nodes.append(filenode)

  ast = IDLNode('AST', '__AST__', 0, 0, nodes)

  print '\n'.join(ast.Tree(accept_props=['PROD', 'TYPE', 'VALUE']))
  if errors:
    print '\nFound %d errors.\n' % errors


  return errors


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))