@jianwen-lang/parser 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/dist/cli/render.d.ts +1 -0
  4. package/dist/cli/render.js +300 -0
  5. package/dist/core/ast.d.ts +286 -0
  6. package/dist/core/ast.js +2 -0
  7. package/dist/core/block/rules/heading.d.ts +9 -0
  8. package/dist/core/block/rules/heading.js +75 -0
  9. package/dist/core/block/rules/horizontal-rule.d.ts +9 -0
  10. package/dist/core/block/rules/horizontal-rule.js +87 -0
  11. package/dist/core/block/rules/include.d.ts +9 -0
  12. package/dist/core/block/rules/include.js +64 -0
  13. package/dist/core/block/rules/index.d.ts +2 -0
  14. package/dist/core/block/rules/index.js +16 -0
  15. package/dist/core/block/rules/types.d.ts +21 -0
  16. package/dist/core/block/rules/types.js +2 -0
  17. package/dist/core/block/types.d.ts +10 -0
  18. package/dist/core/block/types.js +2 -0
  19. package/dist/core/block-parser.d.ts +3 -0
  20. package/dist/core/block-parser.js +1385 -0
  21. package/dist/core/clone.d.ts +9 -0
  22. package/dist/core/clone.js +32 -0
  23. package/dist/core/diagnostics.d.ts +12 -0
  24. package/dist/core/diagnostics.js +24 -0
  25. package/dist/core/errors.d.ts +12 -0
  26. package/dist/core/errors.js +2 -0
  27. package/dist/core/inline/rules/backtick.d.ts +4 -0
  28. package/dist/core/inline/rules/backtick.js +90 -0
  29. package/dist/core/inline/rules/bracket.d.ts +4 -0
  30. package/dist/core/inline/rules/bracket.js +721 -0
  31. package/dist/core/inline/rules/disabled.d.ts +2 -0
  32. package/dist/core/inline/rules/disabled.js +34 -0
  33. package/dist/core/inline/rules/escape.d.ts +2 -0
  34. package/dist/core/inline/rules/escape.js +11 -0
  35. package/dist/core/inline/rules/index.d.ts +2 -0
  36. package/dist/core/inline/rules/index.js +25 -0
  37. package/dist/core/inline/rules/marker-highlight.d.ts +4 -0
  38. package/dist/core/inline/rules/marker-highlight.js +56 -0
  39. package/dist/core/inline/rules/style.d.ts +4 -0
  40. package/dist/core/inline/rules/style.js +120 -0
  41. package/dist/core/inline/rules/types.d.ts +22 -0
  42. package/dist/core/inline/rules/types.js +2 -0
  43. package/dist/core/inline/types.d.ts +1 -0
  44. package/dist/core/inline/types.js +2 -0
  45. package/dist/core/inline-parser.d.ts +3 -0
  46. package/dist/core/inline-parser.js +52 -0
  47. package/dist/core/location.d.ts +9 -0
  48. package/dist/core/location.js +30 -0
  49. package/dist/core/parser.d.ts +9 -0
  50. package/dist/core/parser.js +423 -0
  51. package/dist/core/traverse.d.ts +7 -0
  52. package/dist/core/traverse.js +71 -0
  53. package/dist/html/convert.d.ts +29 -0
  54. package/dist/html/convert.js +61 -0
  55. package/dist/html/format.d.ts +1 -0
  56. package/dist/html/format.js +17 -0
  57. package/dist/html/render/blocks.d.ts +4 -0
  58. package/dist/html/render/blocks.js +433 -0
  59. package/dist/html/render/html.d.ts +8 -0
  60. package/dist/html/render/html.js +89 -0
  61. package/dist/html/render/inlines.d.ts +4 -0
  62. package/dist/html/render/inlines.js +110 -0
  63. package/dist/html/render/meta.d.ts +3 -0
  64. package/dist/html/render/meta.js +51 -0
  65. package/dist/html/render/utils.d.ts +31 -0
  66. package/dist/html/render/utils.js +213 -0
  67. package/dist/html/theme/base/css.d.ts +2 -0
  68. package/dist/html/theme/base/css.js +383 -0
  69. package/dist/html/theme/dark/css.d.ts +2 -0
  70. package/dist/html/theme/dark/css.js +108 -0
  71. package/dist/html/theme/default/colors.d.ts +13 -0
  72. package/dist/html/theme/default/colors.js +2 -0
  73. package/dist/html/theme/default/css.d.ts +2 -0
  74. package/dist/html/theme/default/css.js +6 -0
  75. package/dist/html/theme/default/runtime.d.ts +0 -0
  76. package/dist/html/theme/default/runtime.js +31 -0
  77. package/dist/html/theme/light/css.d.ts +2 -0
  78. package/dist/html/theme/light/css.js +55 -0
  79. package/dist/html/theme/preset/colors.d.ts +10 -0
  80. package/dist/html/theme/preset/colors.js +14 -0
  81. package/dist/html/theme/runtime.d.ts +0 -0
  82. package/dist/html/theme/runtime.js +31 -0
  83. package/dist/html/theme/theme.d.ts +9 -0
  84. package/dist/html/theme/theme.js +21 -0
  85. package/dist/lexer/lexer.d.ts +17 -0
  86. package/dist/lexer/lexer.js +47 -0
  87. package/dist/parser.d.ts +6 -0
  88. package/dist/parser.js +22 -0
  89. package/package.json +50 -0
@@ -0,0 +1,9 @@
1
+ import { BlockNode } from './ast';
2
+ export interface CloneOptions {
3
+ /**
4
+ * Override origin for the entire cloned subtree.
5
+ * Useful for blocks expanded from include(file) where line numbers belong to the included file.
6
+ */
7
+ origin?: string;
8
+ }
9
+ export declare function cloneBlock(block: BlockNode, options?: CloneOptions): BlockNode;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cloneBlock = cloneBlock;
4
+ const location_1 = require("./location");
5
+ function cloneBlock(block, options = {}) {
6
+ return cloneValue(block, options);
7
+ }
8
+ function cloneValue(value, options) {
9
+ if (Array.isArray(value)) {
10
+ return value.map((item) => (item === undefined ? null : cloneValue(item, options)));
11
+ }
12
+ if (!value || typeof value !== 'object') {
13
+ return value;
14
+ }
15
+ const inputObject = value;
16
+ const outputObject = {};
17
+ for (const [key, child] of Object.entries(inputObject)) {
18
+ if (child === undefined) {
19
+ continue;
20
+ }
21
+ outputObject[key] = cloneValue(child, options);
22
+ }
23
+ const location = (0, location_1.getNodeLocation)(value);
24
+ if (location) {
25
+ (0, location_1.setNodeLocation)(outputObject, location);
26
+ }
27
+ const origin = options.origin ?? (0, location_1.getNodeOrigin)(value);
28
+ if (origin) {
29
+ (0, location_1.setNodeOrigin)(outputObject, origin);
30
+ }
31
+ return outputObject;
32
+ }
@@ -0,0 +1,12 @@
1
+ import { ParseError } from './errors';
2
+ export type ParseErrorSeverity = ParseError['severity'];
3
+ export interface ParseErrorInput {
4
+ message: string;
5
+ line: number;
6
+ column?: number;
7
+ code?: string;
8
+ }
9
+ export declare function buildParseError(input: ParseErrorInput, severity: ParseErrorSeverity): ParseError;
10
+ export declare function reportParseError(errors: ParseError[], input: ParseErrorInput): void;
11
+ export declare function reportParseWarning(errors: ParseError[], input: ParseErrorInput): void;
12
+ export declare function prefixIncludeError(error: ParseError, target: string): ParseError;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildParseError = buildParseError;
4
+ exports.reportParseError = reportParseError;
5
+ exports.reportParseWarning = reportParseWarning;
6
+ exports.prefixIncludeError = prefixIncludeError;
7
+ function buildParseError(input, severity) {
8
+ return {
9
+ message: input.message,
10
+ line: input.line,
11
+ column: input.column,
12
+ severity,
13
+ code: input.code,
14
+ };
15
+ }
16
+ function reportParseError(errors, input) {
17
+ errors.push(buildParseError(input, 'error'));
18
+ }
19
+ function reportParseWarning(errors, input) {
20
+ errors.push(buildParseError(input, 'warning'));
21
+ }
22
+ function prefixIncludeError(error, target) {
23
+ return { ...error, message: `[include:${target}] ${error.message}` };
24
+ }
@@ -0,0 +1,12 @@
1
+ import { JianwenDocument } from './ast';
2
+ export interface ParseError {
3
+ message: string;
4
+ line: number;
5
+ column?: number;
6
+ severity: 'error' | 'warning';
7
+ code?: string;
8
+ }
9
+ export interface ParseResult {
10
+ ast: JianwenDocument;
11
+ errors: ParseError[];
12
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import { InlineNode } from '../../ast';
2
+ import { InlineRule, InlineRuleContext, InlineScanner } from './types';
3
+ export declare function readBacktickSegment(scanner: InlineScanner, ctx: InlineRuleContext): InlineNode | undefined;
4
+ export declare const backtickRule: InlineRule;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.backtickRule = void 0;
4
+ exports.readBacktickSegment = readBacktickSegment;
5
+ const diagnostics_1 = require("../../diagnostics");
6
+ function readBacktickSegment(scanner, ctx) {
7
+ const startLine = scanner.line;
8
+ const startColumn = scanner.column;
9
+ let tickCount = 0;
10
+ while (!scanner.eof() && scanner.peek() === '`') {
11
+ scanner.next();
12
+ tickCount += 1;
13
+ }
14
+ if (tickCount === 0) {
15
+ return undefined;
16
+ }
17
+ if (tickCount === 1) {
18
+ let value = '';
19
+ while (!scanner.eof()) {
20
+ const ch = scanner.peek();
21
+ if (ch === undefined) {
22
+ break;
23
+ }
24
+ if (ch === '`') {
25
+ scanner.next();
26
+ return { type: 'codeSpan', value };
27
+ }
28
+ scanner.next();
29
+ value += ch;
30
+ }
31
+ (0, diagnostics_1.reportParseWarning)(ctx.errors, {
32
+ message: 'Missing closing backtick for inline code span',
33
+ line: startLine,
34
+ column: startColumn,
35
+ });
36
+ return { type: 'text', value: `\`${value}` };
37
+ }
38
+ let content = '';
39
+ while (!scanner.eof()) {
40
+ const ch = scanner.peek();
41
+ if (ch === undefined) {
42
+ break;
43
+ }
44
+ if (ch === '`') {
45
+ const saveIndex = scanner.index;
46
+ const saveLine = scanner.line;
47
+ const saveColumn = scanner.column;
48
+ let matched = true;
49
+ for (let i = 0; i < tickCount; i += 1) {
50
+ const c = scanner.peek();
51
+ if (c !== '`') {
52
+ matched = false;
53
+ break;
54
+ }
55
+ scanner.next();
56
+ }
57
+ if (matched) {
58
+ const children = ctx.parseNested(content, startLine);
59
+ const node = {
60
+ type: 'highlight',
61
+ mode: 'frame',
62
+ children,
63
+ };
64
+ return node;
65
+ }
66
+ scanner.index = saveIndex;
67
+ scanner.line = saveLine;
68
+ scanner.column = saveColumn;
69
+ }
70
+ scanner.next();
71
+ content += ch;
72
+ }
73
+ (0, diagnostics_1.reportParseWarning)(ctx.errors, {
74
+ message: 'Missing closing double backticks for frame highlight',
75
+ line: startLine,
76
+ column: startColumn,
77
+ });
78
+ return { type: 'text', value: `${'`'.repeat(tickCount)}${content}` };
79
+ }
80
+ exports.backtickRule = {
81
+ flushTextBefore: true,
82
+ parse(scanner, ctx) {
83
+ const node = readBacktickSegment(scanner, ctx);
84
+ if (node) {
85
+ return { kind: 'emit', node };
86
+ }
87
+ const consumed = scanner.next();
88
+ return { kind: 'append', text: consumed ?? '' };
89
+ },
90
+ };
@@ -0,0 +1,4 @@
1
+ import { InlineNode } from '../../ast';
2
+ import { InlineRule, InlineRuleContext, InlineScanner } from './types';
3
+ export declare function parseBracketExpression(scanner: InlineScanner, ctx: InlineRuleContext): InlineNode | string | undefined;
4
+ export declare const bracketRule: InlineRule;