TokenStream.cjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. 'use strict';
  2. const adoptBuffer = require('./adopt-buffer.cjs');
  3. const utils = require('./utils.cjs');
  4. const names = require('./names.cjs');
  5. const types = require('./types.cjs');
  6. const OFFSET_MASK = 0x00FFFFFF;
  7. const TYPE_SHIFT = 24;
  8. const BLOCK_OPEN_TOKEN = 1;
  9. const BLOCK_CLOSE_TOKEN = 2;
  10. const balancePair = new Uint8Array(32); // 32b of memory ought to be enough for anyone (any number of tokens)
  11. balancePair[types.Function] = types.RightParenthesis;
  12. balancePair[types.LeftParenthesis] = types.RightParenthesis;
  13. balancePair[types.LeftSquareBracket] = types.RightSquareBracket;
  14. balancePair[types.LeftCurlyBracket] = types.RightCurlyBracket;
  15. const blockTokens = new Uint8Array(32);
  16. blockTokens[types.Function] = BLOCK_OPEN_TOKEN;
  17. blockTokens[types.LeftParenthesis] = BLOCK_OPEN_TOKEN;
  18. blockTokens[types.LeftSquareBracket] = BLOCK_OPEN_TOKEN;
  19. blockTokens[types.LeftCurlyBracket] = BLOCK_OPEN_TOKEN;
  20. blockTokens[types.RightParenthesis] = BLOCK_CLOSE_TOKEN;
  21. blockTokens[types.RightSquareBracket] = BLOCK_CLOSE_TOKEN;
  22. blockTokens[types.RightCurlyBracket] = BLOCK_CLOSE_TOKEN;
  23. function boundIndex(index, min, max) {
  24. return index < min ? min : index > max ? max : index;
  25. }
  26. class TokenStream {
  27. constructor(source, tokenize) {
  28. this.setSource(source, tokenize);
  29. }
  30. reset() {
  31. this.eof = false;
  32. this.tokenIndex = -1;
  33. this.tokenType = 0;
  34. this.tokenStart = this.firstCharOffset;
  35. this.tokenEnd = this.firstCharOffset;
  36. }
  37. setSource(source = '', tokenize = () => {}) {
  38. source = String(source || '');
  39. const sourceLength = source.length;
  40. const offsetAndType = adoptBuffer.adoptBuffer(this.offsetAndType, source.length + 1); // +1 because of eof-token
  41. const balance = adoptBuffer.adoptBuffer(this.balance, source.length + 1);
  42. let tokenCount = 0;
  43. let firstCharOffset = -1;
  44. let balanceCloseType = 0;
  45. let balanceStart = source.length;
  46. // capture buffers
  47. this.offsetAndType = null;
  48. this.balance = null;
  49. balance.fill(0);
  50. tokenize(source, (type, start, end) => {
  51. const index = tokenCount++;
  52. // type & offset
  53. offsetAndType[index] = (type << TYPE_SHIFT) | end;
  54. if (firstCharOffset === -1) {
  55. firstCharOffset = start;
  56. }
  57. // balance
  58. balance[index] = balanceStart;
  59. if (type === balanceCloseType) {
  60. const prevBalanceStart = balance[balanceStart];
  61. // set reference to balance end for a block opener
  62. balance[balanceStart] = index;
  63. // pop state
  64. balanceStart = prevBalanceStart;
  65. balanceCloseType = balancePair[offsetAndType[prevBalanceStart] >> TYPE_SHIFT];
  66. } else if (this.isBlockOpenerTokenType(type)) { // check for FunctionToken, <(-token>, <[-token> and <{-token>
  67. // push state
  68. balanceStart = index;
  69. balanceCloseType = balancePair[type];
  70. }
  71. });
  72. // finalize buffers
  73. offsetAndType[tokenCount] = (types.EOF << TYPE_SHIFT) | sourceLength; // <EOF-token>
  74. balance[tokenCount] = tokenCount; // prevents false positive balance match with any token
  75. // reverse references from balance start to end
  76. // tokens
  77. // token: a ( [ b c ] d e ) {
  78. // index: 0 1 2 3 4 5 6 7 8 9
  79. // before
  80. // balance: 0 8 5 2 2 2 1 1 1 0
  81. // - > > < < < < < < -
  82. // after
  83. // balance: 9 8 5 5 5 2 8 8 1 9
  84. // > > > > > < > > < >
  85. for (let i = 0; i < tokenCount; i++) {
  86. const balanceStart = balance[i];
  87. if (balanceStart <= i) {
  88. const balanceEnd = balance[balanceStart];
  89. if (balanceEnd !== i) {
  90. balance[i] = balanceEnd;
  91. }
  92. } else if (balanceStart > tokenCount) {
  93. balance[i] = tokenCount;
  94. }
  95. }
  96. // balance[0] = tokenCount;
  97. this.source = source;
  98. this.firstCharOffset = firstCharOffset === -1 ? 0 : firstCharOffset;
  99. this.tokenCount = tokenCount;
  100. this.offsetAndType = offsetAndType;
  101. this.balance = balance;
  102. this.reset();
  103. this.next();
  104. }
  105. lookupType(offset) {
  106. offset += this.tokenIndex;
  107. if (offset < this.tokenCount) {
  108. return this.offsetAndType[offset] >> TYPE_SHIFT;
  109. }
  110. return types.EOF;
  111. }
  112. lookupTypeNonSC(idx) {
  113. for (let offset = this.tokenIndex; offset < this.tokenCount; offset++) {
  114. const tokenType = this.offsetAndType[offset] >> TYPE_SHIFT;
  115. if (tokenType !== types.WhiteSpace && tokenType !== types.Comment) {
  116. if (idx-- === 0) {
  117. return tokenType;
  118. }
  119. }
  120. }
  121. return types.EOF;
  122. }
  123. lookupOffset(offset) {
  124. offset += this.tokenIndex;
  125. if (offset < this.tokenCount) {
  126. return this.offsetAndType[offset - 1] & OFFSET_MASK;
  127. }
  128. return this.source.length;
  129. }
  130. lookupOffsetNonSC(idx) {
  131. for (let offset = this.tokenIndex; offset < this.tokenCount; offset++) {
  132. const tokenType = this.offsetAndType[offset] >> TYPE_SHIFT;
  133. if (tokenType !== types.WhiteSpace && tokenType !== types.Comment) {
  134. if (idx-- === 0) {
  135. return offset - this.tokenIndex;
  136. }
  137. }
  138. }
  139. return types.EOF;
  140. }
  141. lookupValue(offset, referenceStr) {
  142. offset += this.tokenIndex;
  143. if (offset < this.tokenCount) {
  144. return utils.cmpStr(
  145. this.source,
  146. this.offsetAndType[offset - 1] & OFFSET_MASK,
  147. this.offsetAndType[offset] & OFFSET_MASK,
  148. referenceStr
  149. );
  150. }
  151. return false;
  152. }
  153. getTokenStart(tokenIndex) {
  154. if (tokenIndex === this.tokenIndex) {
  155. return this.tokenStart;
  156. }
  157. if (tokenIndex > 0) {
  158. return tokenIndex < this.tokenCount
  159. ? this.offsetAndType[tokenIndex - 1] & OFFSET_MASK
  160. : this.offsetAndType[this.tokenCount] & OFFSET_MASK;
  161. }
  162. return this.firstCharOffset;
  163. }
  164. getTokenEnd(tokenIndex) {
  165. if (tokenIndex === this.tokenIndex) {
  166. return this.tokenEnd;
  167. }
  168. return this.offsetAndType[boundIndex(tokenIndex, 0, this.tokenCount)] & OFFSET_MASK;
  169. }
  170. getTokenType(tokenIndex) {
  171. if (tokenIndex === this.tokenIndex) {
  172. return this.tokenType;
  173. }
  174. return this.offsetAndType[boundIndex(tokenIndex, 0, this.tokenCount)] >> TYPE_SHIFT;
  175. }
  176. substrToCursor(start) {
  177. return this.source.substring(start, this.tokenStart);
  178. }
  179. isBlockOpenerTokenType(tokenType) {
  180. return blockTokens[tokenType] === BLOCK_OPEN_TOKEN;
  181. }
  182. isBlockCloserTokenType(tokenType) {
  183. return blockTokens[tokenType] === BLOCK_CLOSE_TOKEN;
  184. }
  185. getBlockTokenPairIndex(tokenIndex) {
  186. const type = this.getTokenType(tokenIndex);
  187. if (blockTokens[type] === 1) {
  188. // block open token
  189. const pairIndex = this.balance[tokenIndex];
  190. const closeType = this.getTokenType(pairIndex);
  191. return balancePair[type] === closeType ? pairIndex : -1;
  192. } else if (blockTokens[type] === 2) {
  193. // block close token
  194. const pairIndex = this.balance[tokenIndex];
  195. const openType = this.getTokenType(pairIndex);
  196. return balancePair[openType] === type ? pairIndex : -1;
  197. }
  198. return -1;
  199. }
  200. isBalanceEdge(tokenIndex) {
  201. return this.balance[this.tokenIndex] < tokenIndex;
  202. }
  203. isDelim(code, offset) {
  204. if (offset) {
  205. return (
  206. this.lookupType(offset) === types.Delim &&
  207. this.source.charCodeAt(this.lookupOffset(offset)) === code
  208. );
  209. }
  210. return (
  211. this.tokenType === types.Delim &&
  212. this.source.charCodeAt(this.tokenStart) === code
  213. );
  214. }
  215. skip(tokenCount) {
  216. let next = this.tokenIndex + tokenCount;
  217. if (next < this.tokenCount) {
  218. this.tokenIndex = next;
  219. this.tokenStart = this.offsetAndType[next - 1] & OFFSET_MASK;
  220. next = this.offsetAndType[next];
  221. this.tokenType = next >> TYPE_SHIFT;
  222. this.tokenEnd = next & OFFSET_MASK;
  223. } else {
  224. this.tokenIndex = this.tokenCount;
  225. this.next();
  226. }
  227. }
  228. next() {
  229. let next = this.tokenIndex + 1;
  230. if (next < this.tokenCount) {
  231. this.tokenIndex = next;
  232. this.tokenStart = this.tokenEnd;
  233. next = this.offsetAndType[next];
  234. this.tokenType = next >> TYPE_SHIFT;
  235. this.tokenEnd = next & OFFSET_MASK;
  236. } else {
  237. this.eof = true;
  238. this.tokenIndex = this.tokenCount;
  239. this.tokenType = types.EOF;
  240. this.tokenStart = this.tokenEnd = this.source.length;
  241. }
  242. }
  243. skipSC() {
  244. while (this.tokenType === types.WhiteSpace || this.tokenType === types.Comment) {
  245. this.next();
  246. }
  247. }
  248. skipUntilBalanced(startToken, stopConsume) {
  249. let cursor = startToken;
  250. let balanceEnd = 0;
  251. let offset = 0;
  252. loop:
  253. for (; cursor < this.tokenCount; cursor++) {
  254. balanceEnd = this.balance[cursor];
  255. // stop scanning on balance edge that points to offset before start token
  256. if (balanceEnd < startToken) {
  257. break loop;
  258. }
  259. offset = cursor > 0 ? this.offsetAndType[cursor - 1] & OFFSET_MASK : this.firstCharOffset;
  260. // check stop condition
  261. switch (stopConsume(this.source.charCodeAt(offset))) {
  262. case 1: // just stop
  263. break loop;
  264. case 2: // stop & included
  265. cursor++;
  266. break loop;
  267. default:
  268. // fast forward to the end of balanced block for an open block tokens
  269. if (this.isBlockOpenerTokenType(this.offsetAndType[cursor] >> TYPE_SHIFT)) {
  270. cursor = balanceEnd;
  271. }
  272. }
  273. }
  274. this.skip(cursor - this.tokenIndex);
  275. }
  276. forEachToken(fn) {
  277. for (let i = 0, offset = this.firstCharOffset; i < this.tokenCount; i++) {
  278. const start = offset;
  279. const item = this.offsetAndType[i];
  280. const end = item & OFFSET_MASK;
  281. const type = item >> TYPE_SHIFT;
  282. offset = end;
  283. fn(type, start, end, i);
  284. }
  285. }
  286. dump() {
  287. const tokens = new Array(this.tokenCount);
  288. this.forEachToken((type, start, end, index) => {
  289. tokens[index] = {
  290. idx: index,
  291. type: names[type],
  292. chunk: this.source.substring(start, end),
  293. balance: this.balance[index]
  294. };
  295. });
  296. return tokens;
  297. }
  298. }
  299. exports.TokenStream = TokenStream;