-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.ts
More file actions
97 lines (85 loc) · 2.81 KB
/
parser.ts
File metadata and controls
97 lines (85 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// =============================================================================
// LOGIC.md v1.0 - Frontmatter Parser
// =============================================================================
// Extracts YAML frontmatter and markdown body from LOGIC.md file content.
// Returns a discriminated union (ParseSuccess | ParseFailure) so callers
// can narrow safely. Schema validation belongs to Phase 4 -- this module
// only extracts and casts.
// =============================================================================
import { createRequire } from "node:module";
import type { LogicSpec } from "./types.js";
const require = createRequire(import.meta.url);
const matter = require("gray-matter") as typeof import("gray-matter");
// -----------------------------------------------------------------------------
// Result Types
// -----------------------------------------------------------------------------
/** Structured error with optional source location */
export interface ParseError {
message: string;
line?: number;
column?: number;
}
/** Successful parse -- typed data + markdown body */
export interface ParseSuccess {
ok: true;
data: LogicSpec;
content: string;
}
/** Failed parse -- one or more errors */
export interface ParseFailure {
ok: false;
errors: ParseError[];
}
/** Discriminated union returned by parse() */
export type ParseResult = ParseSuccess | ParseFailure;
// -----------------------------------------------------------------------------
// Parser
// -----------------------------------------------------------------------------
/**
* Parse a LOGIC.md file string into typed frontmatter data and markdown body.
*
* - Returns `ParseSuccess` when frontmatter is successfully extracted.
* - Returns `ParseFailure` with descriptive errors for all invalid inputs.
* - Does NOT validate against the JSON Schema (that is the validator's job).
*/
export function parse(input: string): ParseResult {
// Guard: empty or whitespace-only input
if (!input || input.trim() === "") {
return {
ok: false,
errors: [{ message: "Input is empty" }],
};
}
// Pre-check: must have frontmatter delimiters
if (!matter.test(input)) {
return {
ok: false,
errors: [
{
message: "No YAML frontmatter found. LOGIC.md files must start with `---`",
},
],
};
}
try {
const result = matter(input);
return {
ok: true,
data: result.data as LogicSpec,
content: result.content,
};
} catch (error: unknown) {
const err = error as {
reason?: string;
mark?: { line?: number; column?: number };
message?: string;
};
const message = err.reason ?? err.message ?? "Failed to parse YAML frontmatter";
const line = err.mark?.line != null ? err.mark.line + 1 : undefined;
const column = err.mark?.column;
return {
ok: false,
errors: [{ message, line, column }],
};
}
}