Açıklama Yok

esparse.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env node
  2. /*
  3. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  4. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  16. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*jslint sloppy:true node:true rhino:true */
  24. var fs, esprima, fname, content, options, syntax;
  25. if (typeof require === 'function') {
  26. fs = require('fs');
  27. esprima = require('esprima');
  28. } else if (typeof load === 'function') {
  29. try {
  30. load('esprima.js');
  31. } catch (e) {
  32. load('../esprima.js');
  33. }
  34. }
  35. // Shims to Node.js objects when running under Rhino.
  36. if (typeof console === 'undefined' && typeof process === 'undefined') {
  37. console = { log: print };
  38. fs = { readFileSync: readFile };
  39. process = { argv: arguments, exit: quit };
  40. process.argv.unshift('esparse.js');
  41. process.argv.unshift('rhino');
  42. }
  43. function showUsage() {
  44. console.log('Usage:');
  45. console.log(' esparse [options] file.js');
  46. console.log();
  47. console.log('Available options:');
  48. console.log();
  49. console.log(' --comment Gather all line and block comments in an array');
  50. console.log(' --loc Include line-column location info for each syntax node');
  51. console.log(' --range Include index-based range for each syntax node');
  52. console.log(' --raw Display the raw value of literals');
  53. console.log(' --tokens List all tokens in an array');
  54. console.log(' --tolerant Tolerate errors on a best-effort basis (experimental)');
  55. console.log(' -v, --version Shows program version');
  56. console.log();
  57. process.exit(1);
  58. }
  59. if (process.argv.length <= 2) {
  60. showUsage();
  61. }
  62. options = {};
  63. process.argv.splice(2).forEach(function (entry) {
  64. if (entry === '-h' || entry === '--help') {
  65. showUsage();
  66. } else if (entry === '-v' || entry === '--version') {
  67. console.log('ECMAScript Parser (using Esprima version', esprima.version, ')');
  68. console.log();
  69. process.exit(0);
  70. } else if (entry === '--comment') {
  71. options.comment = true;
  72. } else if (entry === '--loc') {
  73. options.loc = true;
  74. } else if (entry === '--range') {
  75. options.range = true;
  76. } else if (entry === '--raw') {
  77. options.raw = true;
  78. } else if (entry === '--tokens') {
  79. options.tokens = true;
  80. } else if (entry === '--tolerant') {
  81. options.tolerant = true;
  82. } else if (entry.slice(0, 2) === '--') {
  83. console.log('Error: unknown option ' + entry + '.');
  84. process.exit(1);
  85. } else if (typeof fname === 'string') {
  86. console.log('Error: more than one input file.');
  87. process.exit(1);
  88. } else {
  89. fname = entry;
  90. }
  91. });
  92. if (typeof fname !== 'string') {
  93. console.log('Error: no input file.');
  94. process.exit(1);
  95. }
  96. try {
  97. content = fs.readFileSync(fname, 'utf-8');
  98. syntax = esprima.parse(content, options);
  99. console.log(JSON.stringify(syntax, null, 4));
  100. } catch (e) {
  101. console.log('Error: ' + e.message);
  102. process.exit(1);
  103. }