BVB Source Codes

reactide Show tokentype.js Source code

Return Download reactide: download tokentype.js Source code - Download reactide Source code - Type:.js
  1. // ## Token types
  2.  
  3. // The assignment of fine-grained, information-carrying type objects
  4. // allows the tokenizer to store the information it has about a
  5. // token in a way that is very cheap for the parser to look up.
  6.  
  7. // All token type variables start with an underscore, to make them
  8. // easy to recognize.
  9.  
  10. // The `beforeExpr` property is used to disambiguate between regular
  11. // expressions and divisions. It is set on all token types that can
  12. // be followed by an expression (thus, a slash after them would be a
  13. // regular expression).
  14. //
  15. // The `startsExpr` property is used to check if the token ends a
  16. // `yield` expression. It is set on all token types that either can
  17. // directly start an expression (like a quotation mark) or can
  18. // continue an expression (like the body of a string).
  19. //
  20. // `isLoop` marks a keyword as starting a loop, which is important
  21. // to know when parsing a label, in order to allow or disallow
  22. // continue jumps to that label.
  23.  
  24. export class TokenType {
  25.   constructor(label, conf = {}) {
  26.     this.label = label
  27.     this.keyword = conf.keyword
  28.     this.beforeExpr = !!conf.beforeExpr
  29.     this.startsExpr = !!conf.startsExpr
  30.     this.isLoop = !!conf.isLoop
  31.     this.isAssign = !!conf.isAssign
  32.     this.prefix = !!conf.prefix
  33.     this.postfix = !!conf.postfix
  34.     this.binop = conf.binop || null
  35.     this.updateContext = null
  36.   }
  37. }
  38.  
  39. function binop(name, prec) {
  40.   return new TokenType(name, {beforeExpr: true, binop: prec})
  41. }
  42. const beforeExpr = {beforeExpr: true}, startsExpr = {startsExpr: true}
  43.  
  44. // Map keyword names to token types.
  45.  
  46. export const keywords = {}
  47.  
  48. // Succinct definitions of keyword token types
  49. function kw(name, options = {}) {
  50.   options.keyword = name
  51.   return keywords[name] = new TokenType(name, options)
  52. }
  53.  
  54. export const types = {
  55.   num: new TokenType("num", startsExpr),
  56.   regexp: new TokenType("regexp", startsExpr),
  57.   string: new TokenType("string", startsExpr),
  58.   name: new TokenType("name", startsExpr),
  59.   eof: new TokenType("eof"),
  60.  
  61.   // Punctuation token types.
  62.   bracketL: new TokenType("[", {beforeExpr: true, startsExpr: true}),
  63.   bracketR: new TokenType("]"),
  64.   braceL: new TokenType("{", {beforeExpr: true, startsExpr: true}),
  65.   braceR: new TokenType("}"),
  66.   parenL: new TokenType("(", {beforeExpr: true, startsExpr: true}),
  67.   parenR: new TokenType(")"),
  68.   comma: new TokenType(",", beforeExpr),
  69.   semi: new TokenType(";", beforeExpr),
  70.   colon: new TokenType(":", beforeExpr),
  71.   dot: new TokenType("."),
  72.   question: new TokenType("?", beforeExpr),
  73.   arrow: new TokenType("=>", beforeExpr),
  74.   template: new TokenType("template"),
  75.   ellipsis: new TokenType("...", beforeExpr),
  76.   backQuote: new TokenType("`", startsExpr),
  77.   dollarBraceL: new TokenType("${", {beforeExpr: true, startsExpr: true}),
  78.  
  79.   // Operators. These carry several kinds of properties to help the
  80.   // parser use them properly (the presence of these properties is
  81.   // what categorizes them as operators).
  82.   //
  83.   // `binop`, when present, specifies that this operator is a binary
  84.   // operator, and will refer to its precedence.
  85.   //
  86.   // `prefix` and `postfix` mark the operator as a prefix or postfix
  87.   // unary operator.
  88.   //
  89.   // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
  90.   // binary operators with a very low precedence, that should result
  91.   // in AssignmentExpression nodes.
  92.  
  93.   eq: new TokenType("=", {beforeExpr: true, isAssign: true}),
  94.   assign: new TokenType("_=", {beforeExpr: true, isAssign: true}),
  95.   incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}),
  96.   prefix: new TokenType("prefix", {beforeExpr: true, prefix: true, startsExpr: true}),
  97.   logicalOR: binop("||", 1),
  98.   logicalAND: binop("&&", 2),
  99.   bitwiseOR: binop("|", 3),
  100.   bitwiseXOR: binop("^", 4),
  101.   bitwiseAND: binop("&", 5),
  102.   equality: binop("==/!=", 6),
  103.   relational: binop("</>", 7),
  104.   bitShift: binop("<</>>", 8),
  105.   plusMin: new TokenType("+/-", {beforeExpr: true, binop: 9, prefix: true, startsExpr: true}),
  106.   modulo: binop("%", 10),
  107.   star: binop("*", 10),
  108.   slash: binop("/", 10),
  109.   starstar: new TokenType("**", {beforeExpr: true}),
  110.  
  111.   // Keyword token types.
  112.   _break: kw("break"),
  113.   _case: kw("case", beforeExpr),
  114.   _catch: kw("catch"),
  115.   _continue: kw("continue"),
  116.   _debugger: kw("debugger"),
  117.   _default: kw("default", beforeExpr),
  118.   _do: kw("do", {isLoop: true, beforeExpr: true}),
  119.   _else: kw("else", beforeExpr),
  120.   _finally: kw("finally"),
  121.   _for: kw("for", {isLoop: true}),
  122.   _function: kw("function", startsExpr),
  123.   _if: kw("if"),
  124.   _return: kw("return", beforeExpr),
  125.   _switch: kw("switch"),
  126.   _throw: kw("throw", beforeExpr),
  127.   _try: kw("try"),
  128.   _var: kw("var"),
  129.   _const: kw("const"),
  130.   _while: kw("while", {isLoop: true}),
  131.   _with: kw("with"),
  132.   _new: kw("new", {beforeExpr: true, startsExpr: true}),
  133.   _this: kw("this", startsExpr),
  134.   _super: kw("super", startsExpr),
  135.   _class: kw("class"),
  136.   _extends: kw("extends", beforeExpr),
  137.   _export: kw("export"),
  138.   _import: kw("import"),
  139.   _null: kw("null", startsExpr),
  140.   _true: kw("true", startsExpr),
  141.   _false: kw("false", startsExpr),
  142.   _in: kw("in", {beforeExpr: true, binop: 7}),
  143.   _instanceof: kw("instanceof", {beforeExpr: true, binop: 7}),
  144.   _typeof: kw("typeof", {beforeExpr: true, prefix: true, startsExpr: true}),
  145.   _void: kw("void", {beforeExpr: true, prefix: true, startsExpr: true}),
  146.   _delete: kw("delete", {beforeExpr: true, prefix: true, startsExpr: true})
  147. }
  148.  
downloadtokentype.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top