a zip code crypto-currency system good for red ONLY

parser.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var assert = require("assert");
  2. var types = require("./types");
  3. var n = types.namedTypes;
  4. var b = types.builders;
  5. var isObject = types.builtInTypes.object;
  6. var isArray = types.builtInTypes.array;
  7. var isFunction = types.builtInTypes.function;
  8. var Patcher = require("./patcher").Patcher;
  9. var normalizeOptions = require("./options").normalize;
  10. var fromString = require("./lines").fromString;
  11. var attachComments = require("./comments").attach;
  12. var util = require("./util");
  13. exports.parse = function parse(source, options) {
  14. options = normalizeOptions(options);
  15. var lines = fromString(source, options);
  16. var sourceWithoutTabs = lines.toString({
  17. tabWidth: options.tabWidth,
  18. reuseWhitespace: false,
  19. useTabs: false
  20. });
  21. var comments = [];
  22. var program = options.parser.parse(sourceWithoutTabs, {
  23. loc: true,
  24. locations: true,
  25. range: options.range,
  26. comment: true,
  27. onComment: comments,
  28. tolerant: options.tolerant,
  29. ecmaVersion: 6,
  30. sourceType: 'module'
  31. });
  32. // If the source was empty, some parsers give loc.{start,end}.line
  33. // values of 0, instead of the minimum of 1.
  34. util.fixFaultyLocations(program, lines);
  35. program.loc = program.loc || {
  36. start: lines.firstPos(),
  37. end: lines.lastPos()
  38. };
  39. program.loc.lines = lines;
  40. program.loc.indent = 0;
  41. // Expand the Program node's .loc to include all comments, since
  42. // typically its .loc.start and .loc.end will coincide with those of
  43. // the first and last statements, respectively, excluding any comments
  44. // that fall outside that region.
  45. var trueProgramLoc = util.getTrueLoc(program, lines);
  46. program.loc.start = trueProgramLoc.start;
  47. program.loc.end = trueProgramLoc.end;
  48. if (program.comments) {
  49. comments = program.comments;
  50. delete program.comments;
  51. }
  52. // In order to ensure we reprint leading and trailing program
  53. // comments, wrap the original Program node with a File node.
  54. var file = program;
  55. if (file.type === "Program") {
  56. var file = b.file(program);
  57. file.loc = {
  58. lines: lines,
  59. indent: 0,
  60. start: lines.firstPos(),
  61. end: lines.lastPos()
  62. };
  63. } else if (file.type === "File") {
  64. program = file.program;
  65. }
  66. // Passing file.program here instead of just file means that initial
  67. // comments will be attached to program.body[0] instead of program.
  68. attachComments(
  69. comments,
  70. program.body.length ? file.program : file,
  71. lines
  72. );
  73. // Return a copy of the original AST so that any changes made may be
  74. // compared to the original.
  75. return new TreeCopier(lines).copy(file);
  76. };
  77. function TreeCopier(lines) {
  78. assert.ok(this instanceof TreeCopier);
  79. this.lines = lines;
  80. this.indent = 0;
  81. }
  82. var TCp = TreeCopier.prototype;
  83. TCp.copy = function(node) {
  84. if (isArray.check(node)) {
  85. return node.map(this.copy, this);
  86. }
  87. if (!isObject.check(node)) {
  88. return node;
  89. }
  90. util.fixFaultyLocations(node, this.lines);
  91. var copy = Object.create(Object.getPrototypeOf(node), {
  92. original: { // Provide a link from the copy to the original.
  93. value: node,
  94. configurable: false,
  95. enumerable: false,
  96. writable: true
  97. }
  98. });
  99. var loc = node.loc;
  100. var oldIndent = this.indent;
  101. var newIndent = oldIndent;
  102. if (loc) {
  103. // When node is a comment, we set node.loc.indent to
  104. // node.loc.start.column so that, when/if we print the comment by
  105. // itself, we can strip that much whitespace from the left margin
  106. // of the comment. This only really matters for multiline Block
  107. // comments, but it doesn't hurt for Line comments.
  108. if (node.type === "Block" || node.type === "Line" ||
  109. this.lines.isPrecededOnlyByWhitespace(loc.start)) {
  110. newIndent = this.indent = loc.start.column;
  111. }
  112. loc.lines = this.lines;
  113. loc.indent = newIndent;
  114. }
  115. var keys = Object.keys(node);
  116. var keyCount = keys.length;
  117. for (var i = 0; i < keyCount; ++i) {
  118. var key = keys[i];
  119. if (key === "loc") {
  120. copy[key] = node[key];
  121. } else {
  122. copy[key] = this.copy(node[key]);
  123. }
  124. }
  125. this.indent = oldIndent;
  126. return copy;
  127. };