state.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {tokenizer, SourceLocation, tokTypes as tt, Node, lineBreak, isNewLine} from "../index"
  2. // Registered plugins
  3. export const pluginsLoose = {}
  4. export class LooseParser {
  5. constructor(input, options = {}) {
  6. this.toks = tokenizer(input, options)
  7. this.options = this.toks.options
  8. this.input = this.toks.input
  9. this.tok = this.last = {type: tt.eof, start: 0, end: 0}
  10. if (this.options.locations) {
  11. let here = this.toks.curPosition()
  12. this.tok.loc = new SourceLocation(this.toks, here, here)
  13. }
  14. this.ahead = [] // Tokens ahead
  15. this.context = [] // Indentation contexted
  16. this.curIndent = 0
  17. this.curLineStart = 0
  18. this.nextLineStart = this.lineEnd(this.curLineStart) + 1
  19. this.inAsync = false
  20. // Load plugins
  21. this.options.pluginsLoose = options.pluginsLoose || {}
  22. this.loadPlugins(this.options.pluginsLoose)
  23. }
  24. startNode() {
  25. return new Node(this.toks, this.tok.start, this.options.locations ? this.tok.loc.start : null)
  26. }
  27. storeCurrentPos() {
  28. return this.options.locations ? [this.tok.start, this.tok.loc.start] : this.tok.start
  29. }
  30. startNodeAt(pos) {
  31. if (this.options.locations) {
  32. return new Node(this.toks, pos[0], pos[1])
  33. } else {
  34. return new Node(this.toks, pos)
  35. }
  36. }
  37. finishNode(node, type) {
  38. node.type = type
  39. node.end = this.last.end
  40. if (this.options.locations)
  41. node.loc.end = this.last.loc.end
  42. if (this.options.ranges)
  43. node.range[1] = this.last.end
  44. return node
  45. }
  46. dummyNode(type) {
  47. let dummy = this.startNode()
  48. dummy.type = type
  49. dummy.end = dummy.start
  50. if (this.options.locations)
  51. dummy.loc.end = dummy.loc.start
  52. if (this.options.ranges)
  53. dummy.range[1] = dummy.start
  54. this.last = {type: tt.name, start: dummy.start, end: dummy.start, loc: dummy.loc}
  55. return dummy
  56. }
  57. dummyIdent() {
  58. let dummy = this.dummyNode("Identifier")
  59. dummy.name = "✖"
  60. return dummy
  61. }
  62. dummyString() {
  63. let dummy = this.dummyNode("Literal")
  64. dummy.value = dummy.raw = "✖"
  65. return dummy
  66. }
  67. eat(type) {
  68. if (this.tok.type === type) {
  69. this.next()
  70. return true
  71. } else {
  72. return false
  73. }
  74. }
  75. isContextual(name) {
  76. return this.tok.type === tt.name && this.tok.value === name
  77. }
  78. eatContextual(name) {
  79. return this.tok.value === name && this.eat(tt.name)
  80. }
  81. canInsertSemicolon() {
  82. return this.tok.type === tt.eof || this.tok.type === tt.braceR ||
  83. lineBreak.test(this.input.slice(this.last.end, this.tok.start))
  84. }
  85. semicolon() {
  86. return this.eat(tt.semi)
  87. }
  88. expect(type) {
  89. if (this.eat(type)) return true
  90. for (let i = 1; i <= 2; i++) {
  91. if (this.lookAhead(i).type == type) {
  92. for (let j = 0; j < i; j++) this.next()
  93. return true
  94. }
  95. }
  96. }
  97. pushCx() {
  98. this.context.push(this.curIndent)
  99. }
  100. popCx() {
  101. this.curIndent = this.context.pop()
  102. }
  103. lineEnd(pos) {
  104. while (pos < this.input.length && !isNewLine(this.input.charCodeAt(pos))) ++pos
  105. return pos
  106. }
  107. indentationAfter(pos) {
  108. for (let count = 0;; ++pos) {
  109. let ch = this.input.charCodeAt(pos)
  110. if (ch === 32) ++count
  111. else if (ch === 9) count += this.options.tabSize
  112. else return count
  113. }
  114. }
  115. closes(closeTok, indent, line, blockHeuristic) {
  116. if (this.tok.type === closeTok || this.tok.type === tt.eof) return true
  117. return line != this.curLineStart && this.curIndent < indent && this.tokenStartsLine() &&
  118. (!blockHeuristic || this.nextLineStart >= this.input.length ||
  119. this.indentationAfter(this.nextLineStart) < indent)
  120. }
  121. tokenStartsLine() {
  122. for (let p = this.tok.start - 1; p >= this.curLineStart; --p) {
  123. let ch = this.input.charCodeAt(p)
  124. if (ch !== 9 && ch !== 32) return false
  125. }
  126. return true
  127. }
  128. extend(name, f) {
  129. this[name] = f(this[name])
  130. }
  131. loadPlugins(pluginConfigs) {
  132. for (let name in pluginConfigs) {
  133. let plugin = pluginsLoose[name]
  134. if (!plugin) throw new Error("Plugin '" + name + "' not found")
  135. plugin(this, pluginConfigs[name])
  136. }
  137. }
  138. }