cst.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. var cstScalar = require('./cst-scalar.js');
  3. var cstStringify = require('./cst-stringify.js');
  4. var cstVisit = require('./cst-visit.js');
  5. /** The byte order mark */
  6. const BOM = '\u{FEFF}';
  7. /** Start of doc-mode */
  8. const DOCUMENT = '\x02'; // C0: Start of Text
  9. /** Unexpected end of flow-mode */
  10. const FLOW_END = '\x18'; // C0: Cancel
  11. /** Next token is a scalar value */
  12. const SCALAR = '\x1f'; // C0: Unit Separator
  13. /** @returns `true` if `token` is a flow or block collection */
  14. const isCollection = (token) => !!token && 'items' in token;
  15. /** @returns `true` if `token` is a flow or block scalar; not an alias */
  16. const isScalar = (token) => !!token &&
  17. (token.type === 'scalar' ||
  18. token.type === 'single-quoted-scalar' ||
  19. token.type === 'double-quoted-scalar' ||
  20. token.type === 'block-scalar');
  21. /* istanbul ignore next */
  22. /** Get a printable representation of a lexer token */
  23. function prettyToken(token) {
  24. switch (token) {
  25. case BOM:
  26. return '<BOM>';
  27. case DOCUMENT:
  28. return '<DOC>';
  29. case FLOW_END:
  30. return '<FLOW_END>';
  31. case SCALAR:
  32. return '<SCALAR>';
  33. default:
  34. return JSON.stringify(token);
  35. }
  36. }
  37. /** Identify the type of a lexer token. May return `null` for unknown tokens. */
  38. function tokenType(source) {
  39. switch (source) {
  40. case BOM:
  41. return 'byte-order-mark';
  42. case DOCUMENT:
  43. return 'doc-mode';
  44. case FLOW_END:
  45. return 'flow-error-end';
  46. case SCALAR:
  47. return 'scalar';
  48. case '---':
  49. return 'doc-start';
  50. case '...':
  51. return 'doc-end';
  52. case '':
  53. case '\n':
  54. case '\r\n':
  55. return 'newline';
  56. case '-':
  57. return 'seq-item-ind';
  58. case '?':
  59. return 'explicit-key-ind';
  60. case ':':
  61. return 'map-value-ind';
  62. case '{':
  63. return 'flow-map-start';
  64. case '}':
  65. return 'flow-map-end';
  66. case '[':
  67. return 'flow-seq-start';
  68. case ']':
  69. return 'flow-seq-end';
  70. case ',':
  71. return 'comma';
  72. }
  73. switch (source[0]) {
  74. case ' ':
  75. case '\t':
  76. return 'space';
  77. case '#':
  78. return 'comment';
  79. case '%':
  80. return 'directive-line';
  81. case '*':
  82. return 'alias';
  83. case '&':
  84. return 'anchor';
  85. case '!':
  86. return 'tag';
  87. case "'":
  88. return 'single-quoted-scalar';
  89. case '"':
  90. return 'double-quoted-scalar';
  91. case '|':
  92. case '>':
  93. return 'block-scalar-header';
  94. }
  95. return null;
  96. }
  97. exports.createScalarToken = cstScalar.createScalarToken;
  98. exports.resolveAsScalar = cstScalar.resolveAsScalar;
  99. exports.setScalarValue = cstScalar.setScalarValue;
  100. exports.stringify = cstStringify.stringify;
  101. exports.visit = cstVisit.visit;
  102. exports.BOM = BOM;
  103. exports.DOCUMENT = DOCUMENT;
  104. exports.FLOW_END = FLOW_END;
  105. exports.SCALAR = SCALAR;
  106. exports.isCollection = isCollection;
  107. exports.isScalar = isScalar;
  108. exports.prettyToken = prettyToken;
  109. exports.tokenType = tokenType;