source-parser.cjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. const primitives_js_1 = require("../primitives.cjs");
  6. const util_js_1 = require("../util.cjs");
  7. function getParser({
  8. startLine = 0,
  9. markers = primitives_js_1.Markers
  10. } = {}) {
  11. let block = null;
  12. let num = startLine;
  13. return function parseSource(source) {
  14. let rest = source;
  15. const tokens = (0, util_js_1.seedTokens)();
  16. [tokens.lineEnd, rest] = (0, util_js_1.splitCR)(rest);
  17. [tokens.start, rest] = (0, util_js_1.splitSpace)(rest);
  18. if (block === null && rest.startsWith(markers.start) && !rest.startsWith(markers.nostart)) {
  19. block = [];
  20. tokens.delimiter = rest.slice(0, markers.start.length);
  21. rest = rest.slice(markers.start.length);
  22. [tokens.postDelimiter, rest] = (0, util_js_1.splitSpace)(rest);
  23. }
  24. if (block === null) {
  25. num++;
  26. return null;
  27. }
  28. const isClosed = rest.trimRight().endsWith(markers.end);
  29. if (tokens.delimiter === '' && rest.startsWith(markers.delim) && !rest.startsWith(markers.end)) {
  30. tokens.delimiter = markers.delim;
  31. rest = rest.slice(markers.delim.length);
  32. [tokens.postDelimiter, rest] = (0, util_js_1.splitSpace)(rest);
  33. }
  34. if (isClosed) {
  35. const trimmed = rest.trimRight();
  36. tokens.end = rest.slice(trimmed.length - markers.end.length);
  37. rest = trimmed.slice(0, -markers.end.length);
  38. }
  39. tokens.description = rest;
  40. block.push({
  41. number: num,
  42. source,
  43. tokens
  44. });
  45. num++;
  46. if (isClosed) {
  47. const result = block.slice();
  48. block = null;
  49. return result;
  50. }
  51. return null;
  52. };
  53. }
  54. exports.default = getParser;
  55. //# sourceMappingURL=source-parser.cjs.map