cst.js 2.8 KB

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