Bug 785459 - Enable the WebIDL parser to be used stand-alone for syntax checking; r=khuey
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 24 Aug 2012 14:51:45 -0400
changeset 105369 1bdc56a180bc2b8c588b4982103c3a7e6b6baa3e
parent 105368 258f0a5ed7f552678ee55dd15cf2cae62b1ad402
child 105370 1142882b6c0b58eb019ca0514d32552de9efbb09
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewerskhuey
bugs785459
milestone17.0a1
Bug 785459 - Enable the WebIDL parser to be used stand-alone for syntax checking; r=khuey
dom/bindings/parser/WebIDL.py
--- a/dom/bindings/parser/WebIDL.py
+++ b/dom/bindings/parser/WebIDL.py
@@ -1,16 +1,18 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 """ A WebIDL parser. """
 
 from ply import lex, yacc
 import re
+import os
+import traceback
 
 # Machinery
 
 def parseInt(literal):
     string = literal
     sign = 0
     base = 0
 
@@ -3673,8 +3675,45 @@ class Parser(Tokenizer):
 
     def reset(self):
         return Parser(lexer=self.lexer)
 
     # Builtin IDL defined by WebIDL
     _builtins = """
         typedef unsigned long long DOMTimeStamp;
     """
+
+def main():
+    # Parse arguments.
+    from optparse import OptionParser
+    usageString = "usage: %prog [options] files"
+    o = OptionParser(usage=usageString)
+    o.add_option("--cachedir", dest='cachedir', default=None,
+                 help="Directory in which to cache lex/parse tables.")
+    o.add_option("--verbose-errors", action='store_true', default=False,
+                 help="When an error happens, display the Python traceback.")
+    (options, args) = o.parse_args()
+
+    if len(args) < 1:
+        o.error(usageString)
+
+    fileList = args
+    baseDir = os.getcwd()
+
+    # Parse the WebIDL.
+    parser = Parser(options.cachedir)
+    try:
+        for filename in fileList:
+            fullPath = os.path.normpath(os.path.join(baseDir, filename))
+            f = open(fullPath, 'rb')
+            lines = f.readlines()
+            f.close()
+            print fullPath
+            parser.parse(''.join(lines), fullPath)
+        parser.finish()
+    except WebIDLError as e:
+        if options.verbose_errors:
+            traceback.print_exc()
+        else:
+            print e
+
+if __name__ == '__main__':
+    main()