flow.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. export var tokenize = initializeFlow
  2. import assert from 'assert'
  3. import codes from '../character/codes.mjs'
  4. import markdownLineEnding from '../character/markdown-line-ending.mjs'
  5. import types from '../constant/types.mjs'
  6. import content from '../tokenize/content.mjs'
  7. import spaceFactory from '../tokenize/factory-space.mjs'
  8. import blank from '../tokenize/partial-blank-line.mjs'
  9. function initializeFlow(effects) {
  10. var self = this
  11. var initial = effects.attempt(
  12. // Try to parse a blank line.
  13. blank,
  14. atBlankEnding,
  15. // Try to parse initial flow (essentially, only code).
  16. effects.attempt(
  17. this.parser.constructs.flowInitial,
  18. afterConstruct,
  19. spaceFactory(
  20. effects,
  21. effects.attempt(
  22. this.parser.constructs.flow,
  23. afterConstruct,
  24. effects.attempt(content, afterConstruct)
  25. ),
  26. types.linePrefix
  27. )
  28. )
  29. )
  30. return initial
  31. function atBlankEnding(code) {
  32. assert(
  33. code === codes.eof || markdownLineEnding(code),
  34. 'expected eol or eof'
  35. )
  36. if (code === codes.eof) {
  37. effects.consume(code)
  38. return
  39. }
  40. effects.enter(types.lineEndingBlank)
  41. effects.consume(code)
  42. effects.exit(types.lineEndingBlank)
  43. self.currentConstruct = undefined
  44. return initial
  45. }
  46. function afterConstruct(code) {
  47. assert(
  48. code === codes.eof || markdownLineEnding(code),
  49. 'expected eol or eof'
  50. )
  51. if (code === codes.eof) {
  52. effects.consume(code)
  53. return
  54. }
  55. effects.enter(types.lineEnding)
  56. effects.consume(code)
  57. effects.exit(types.lineEnding)
  58. self.currentConstruct = undefined
  59. return initial
  60. }
  61. }