Ohm-Management - Projektarbeit B-ME
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.

token-translator.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @fileoverview Translates tokens between Acorn format and Esprima format.
  3. * @author Nicholas C. Zakas
  4. */
  5. /* eslint no-underscore-dangle: 0 */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. // none!
  11. //------------------------------------------------------------------------------
  12. // Private
  13. //------------------------------------------------------------------------------
  14. // Esprima Token Types
  15. var Token = {
  16. Boolean: "Boolean",
  17. EOF: "<end>",
  18. Identifier: "Identifier",
  19. Keyword: "Keyword",
  20. Null: "Null",
  21. Numeric: "Numeric",
  22. Punctuator: "Punctuator",
  23. String: "String",
  24. RegularExpression: "RegularExpression",
  25. Template: "Template",
  26. JSXIdentifier: "JSXIdentifier",
  27. JSXText: "JSXText"
  28. };
  29. /**
  30. * Converts part of a template into an Esprima token.
  31. * @param {AcornToken[]} tokens The Acorn tokens representing the template.
  32. * @param {string} code The source code.
  33. * @returns {EsprimaToken} The Esprima equivalent of the template token.
  34. * @private
  35. */
  36. function convertTemplatePart(tokens, code) {
  37. var firstToken = tokens[0],
  38. lastTemplateToken = tokens[tokens.length - 1];
  39. var token = {
  40. type: Token.Template,
  41. value: code.slice(firstToken.start, lastTemplateToken.end)
  42. };
  43. if (firstToken.loc) {
  44. token.loc = {
  45. start: firstToken.loc.start,
  46. end: lastTemplateToken.loc.end
  47. };
  48. }
  49. if (firstToken.range) {
  50. token.start = firstToken.range[0];
  51. token.end = lastTemplateToken.range[1];
  52. token.range = [token.start, token.end];
  53. }
  54. return token;
  55. }
  56. /**
  57. * Contains logic to translate Acorn tokens into Esprima tokens.
  58. * @param {Object} acornTokTypes The Acorn token types.
  59. * @param {string} code The source code Acorn is parsing. This is necessary
  60. * to correct the "value" property of some tokens.
  61. * @constructor
  62. */
  63. function TokenTranslator(acornTokTypes, code) {
  64. // token types
  65. this._acornTokTypes = acornTokTypes;
  66. // token buffer for templates
  67. this._tokens = [];
  68. // track the last curly brace
  69. this._curlyBrace = null;
  70. // the source code
  71. this._code = code;
  72. }
  73. TokenTranslator.prototype = {
  74. constructor: TokenTranslator,
  75. /**
  76. * Translates a single Esprima token to a single Acorn token. This may be
  77. * inaccurate due to how templates are handled differently in Esprima and
  78. * Acorn, but should be accurate for all other tokens.
  79. * @param {AcornToken} token The Acorn token to translate.
  80. * @param {Object} extra Espree extra object.
  81. * @returns {EsprimaToken} The Esprima version of the token.
  82. */
  83. translate: function(token, extra) {
  84. var type = token.type,
  85. tt = this._acornTokTypes;
  86. if (type === tt.name) {
  87. token.type = Token.Identifier;
  88. // TODO: See if this is an Acorn bug
  89. if (token.value === "static") {
  90. token.type = Token.Keyword;
  91. }
  92. if (extra.ecmaVersion > 5 && (token.value === "yield" || token.value === "let")) {
  93. token.type = Token.Keyword;
  94. }
  95. } else if (type === tt.semi || type === tt.comma ||
  96. type === tt.parenL || type === tt.parenR ||
  97. type === tt.braceL || type === tt.braceR ||
  98. type === tt.dot || type === tt.bracketL ||
  99. type === tt.colon || type === tt.question ||
  100. type === tt.bracketR || type === tt.ellipsis ||
  101. type === tt.arrow || type === tt.jsxTagStart ||
  102. type === tt.incDec || type === tt.starstar ||
  103. type === tt.jsxTagEnd || type === tt.prefix ||
  104. (type.binop && !type.keyword) ||
  105. type.isAssign) {
  106. token.type = Token.Punctuator;
  107. token.value = this._code.slice(token.start, token.end);
  108. } else if (type === tt.jsxName) {
  109. token.type = Token.JSXIdentifier;
  110. } else if (type.label === "jsxText" || type === tt.jsxAttrValueToken) {
  111. token.type = Token.JSXText;
  112. } else if (type.keyword) {
  113. if (type.keyword === "true" || type.keyword === "false") {
  114. token.type = Token.Boolean;
  115. } else if (type.keyword === "null") {
  116. token.type = Token.Null;
  117. } else {
  118. token.type = Token.Keyword;
  119. }
  120. } else if (type === tt.num) {
  121. token.type = Token.Numeric;
  122. token.value = this._code.slice(token.start, token.end);
  123. } else if (type === tt.string) {
  124. if (extra.jsxAttrValueToken) {
  125. extra.jsxAttrValueToken = false;
  126. token.type = Token.JSXText;
  127. } else {
  128. token.type = Token.String;
  129. }
  130. token.value = this._code.slice(token.start, token.end);
  131. } else if (type === tt.regexp) {
  132. token.type = Token.RegularExpression;
  133. var value = token.value;
  134. token.regex = {
  135. flags: value.flags,
  136. pattern: value.pattern
  137. };
  138. token.value = "/" + value.pattern + "/" + value.flags;
  139. }
  140. return token;
  141. },
  142. /**
  143. * Function to call during Acorn's onToken handler.
  144. * @param {AcornToken} token The Acorn token.
  145. * @param {Object} extra The Espree extra object.
  146. * @returns {void}
  147. */
  148. onToken: function(token, extra) {
  149. var that = this,
  150. tt = this._acornTokTypes,
  151. tokens = extra.tokens,
  152. templateTokens = this._tokens;
  153. /**
  154. * Flushes the buffered template tokens and resets the template
  155. * tracking.
  156. * @returns {void}
  157. * @private
  158. */
  159. function translateTemplateTokens() {
  160. tokens.push(convertTemplatePart(that._tokens, that._code));
  161. that._tokens = [];
  162. }
  163. if (token.type === tt.eof) {
  164. // might be one last curlyBrace
  165. if (this._curlyBrace) {
  166. tokens.push(this.translate(this._curlyBrace, extra));
  167. }
  168. return;
  169. }
  170. if (token.type === tt.backQuote) {
  171. // if there's already a curly, it's not part of the template
  172. if (this._curlyBrace) {
  173. tokens.push(this.translate(this._curlyBrace, extra));
  174. this._curlyBrace = null;
  175. }
  176. templateTokens.push(token);
  177. // it's the end
  178. if (templateTokens.length > 1) {
  179. translateTemplateTokens();
  180. }
  181. return;
  182. } else if (token.type === tt.dollarBraceL) {
  183. templateTokens.push(token);
  184. translateTemplateTokens();
  185. return;
  186. } else if (token.type === tt.braceR) {
  187. // if there's already a curly, it's not part of the template
  188. if (this._curlyBrace) {
  189. tokens.push(this.translate(this._curlyBrace, extra));
  190. }
  191. // store new curly for later
  192. this._curlyBrace = token;
  193. return;
  194. } else if (token.type === tt.template || token.type === tt.invalidTemplate) {
  195. if (this._curlyBrace) {
  196. templateTokens.push(this._curlyBrace);
  197. this._curlyBrace = null;
  198. }
  199. templateTokens.push(token);
  200. return;
  201. }
  202. if (this._curlyBrace) {
  203. tokens.push(this.translate(this._curlyBrace, extra));
  204. this._curlyBrace = null;
  205. }
  206. tokens.push(this.translate(token, extra));
  207. }
  208. };
  209. //------------------------------------------------------------------------------
  210. // Public
  211. //------------------------------------------------------------------------------
  212. module.exports = TokenTranslator;