You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.8 KiB

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