a zip code crypto-currency system good for red ONLY

SourceNode.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const base64VLQ = require("./base64-vlq");
  7. const getNumberOfLines = require("./helpers").getNumberOfLines;
  8. const getUnfinishedLine = require("./helpers").getUnfinishedLine;
  9. const LINE_MAPPING = ";AACA";
  10. class SourceNode {
  11. constructor(generatedCode, source, originalSource, startingLine) {
  12. this.generatedCode = generatedCode;
  13. this.originalSource = originalSource;
  14. this.source = source;
  15. this.startingLine = startingLine || 1;
  16. this._numberOfLines = getNumberOfLines(this.generatedCode);
  17. this._endsWithNewLine = generatedCode[generatedCode.length - 1] === "\n";
  18. }
  19. clone() {
  20. return new SourceNode(this.generatedCode, this.source, this.originalSource, this.startingLine);
  21. }
  22. getGeneratedCode() {
  23. return this.generatedCode;
  24. }
  25. addGeneratedCode(code) {
  26. this.generatedCode += code;
  27. this._numberOfLines += getNumberOfLines(code);
  28. this._endsWithNewLine = code[code.length - 1] === "\n";
  29. }
  30. getMappings(mappingsContext) {
  31. if(!this.generatedCode)
  32. return "";
  33. const lines = this._numberOfLines;
  34. const sourceIdx = mappingsContext.ensureSource(this.source, this.originalSource);
  35. let mappings = "A"; // generated column 0
  36. if(mappingsContext.unfinishedGeneratedLine)
  37. mappings = "," + base64VLQ.encode(mappingsContext.unfinishedGeneratedLine);
  38. mappings += base64VLQ.encode(sourceIdx - mappingsContext.currentSource); // source index
  39. mappings += base64VLQ.encode(this.startingLine - mappingsContext.currentOriginalLine); // original line index
  40. mappings += "A"; // original column 0
  41. mappingsContext.currentSource = sourceIdx;
  42. mappingsContext.currentOriginalLine = this.startingLine + lines - 1;
  43. const unfinishedGeneratedLine = mappingsContext.unfinishedGeneratedLine = getUnfinishedLine(this.generatedCode)
  44. mappings += Array(lines).join(LINE_MAPPING);
  45. if(unfinishedGeneratedLine === 0) {
  46. mappings += ";";
  47. } else {
  48. if(lines !== 0) {
  49. mappings += LINE_MAPPING;
  50. }
  51. mappingsContext.currentOriginalLine++;
  52. }
  53. return mappings;
  54. }
  55. mapGeneratedCode(fn) {
  56. throw new Error("Cannot map generated code on a SourceMap. Normalize to SingleLineNode first.");
  57. }
  58. getNormalizedNodes() {
  59. var results = [];
  60. var currentLine = this.startingLine;
  61. var generatedCode = this.generatedCode;
  62. var index = 0;
  63. var indexEnd = generatedCode.length;
  64. while(index < indexEnd) {
  65. // get one generated line
  66. var nextLine = generatedCode.indexOf("\n", index) + 1;
  67. if(nextLine === 0) nextLine = indexEnd;
  68. var lineGenerated = generatedCode.substr(index, nextLine - index);
  69. results.push(new SingleLineNode(lineGenerated, this.source, this.originalSource, currentLine));
  70. // move cursors
  71. index = nextLine;
  72. currentLine++;
  73. }
  74. return results;
  75. }
  76. merge(otherNode) {
  77. if(otherNode instanceof SourceNode) {
  78. return this.mergeSourceNode(otherNode);
  79. } else if(otherNode instanceof SingleLineNode) {
  80. return this.mergeSingleLineNode(otherNode);
  81. }
  82. return false;
  83. }
  84. mergeSourceNode(otherNode) {
  85. if(this.source === otherNode.source &&
  86. this._endsWithNewLine &&
  87. this.startingLine + this._numberOfLines === otherNode.startingLine) {
  88. this.generatedCode += otherNode.generatedCode;
  89. this._numberOfLines += otherNode._numberOfLines;
  90. this._endsWithNewLine = otherNode._endsWithNewLine;
  91. return this;
  92. }
  93. return false;
  94. }
  95. mergeSingleLineNode(otherNode) {
  96. if(this.source === otherNode.source &&
  97. this._endsWithNewLine &&
  98. this.startingLine + this._numberOfLines === otherNode.line &&
  99. otherNode._numberOfLines <= 1) {
  100. this.addSingleLineNode(otherNode);
  101. return this;
  102. }
  103. return false;
  104. }
  105. addSingleLineNode(otherNode) {
  106. this.generatedCode += otherNode.generatedCode;
  107. this._numberOfLines += otherNode._numberOfLines
  108. this._endsWithNewLine = otherNode._endsWithNewLine;
  109. }
  110. }
  111. module.exports = SourceNode;
  112. const SingleLineNode = require("./SingleLineNode"); // circular dependency