@aljazmc/syntax 0.0.0-stage → 0.4.0

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.
@@ -0,0 +1,156 @@
1
+ /** Blocks have offsets into the source and 1-based lines, inlines offsets into their block's text. */
2
+ interface Base<T extends string> {
3
+ type: T;
4
+ start: number;
5
+ end: number;
6
+ }
7
+ interface BlockBase<T extends string> extends Base<T> {
8
+ /** The first and last line with content, blank lines around are not part of a block. */
9
+ line: number;
10
+ endLine: number;
11
+ }
12
+ export interface Root extends BlockBase<"Root"> {
13
+ /** Whether the source starts with a byte order mark. */
14
+ bom: boolean;
15
+ /** The raw front matter, with its `---` fences, when parsing it is enabled. */
16
+ frontmatter: Frontmatter | null;
17
+ children: Block[];
18
+ }
19
+ export interface Frontmatter extends BlockBase<"Frontmatter"> {
20
+ raw: string;
21
+ }
22
+ export interface Heading extends BlockBase<"Heading"> {
23
+ level: number;
24
+ /** `Title\n=====` rather than `# Title`. */
25
+ setext: boolean;
26
+ /** The content, without the markers and the indentation of each line. */
27
+ text: string;
28
+ /** The `===` or `---` line of setext headings, trimmed. */
29
+ underline: string;
30
+ children: Inline[];
31
+ }
32
+ export interface Paragraph extends BlockBase<"Paragraph"> {
33
+ /** The lines joined by `\n`, without the indentation of each line. */
34
+ text: string;
35
+ /** The indentation of each line, after the prefixes of containers. */
36
+ indents: string[];
37
+ /** What precedes each line in the source, like the indentation of a list item or `> `. */
38
+ prefixes: string[];
39
+ /** Whether each line continues the paragraph lazily, without the prefixes of its containers. */
40
+ lazy: boolean[];
41
+ children: Inline[];
42
+ }
43
+ export interface ThematicBreak extends BlockBase<"ThematicBreak"> {
44
+ /** The line as written, without indentation, like `* * *`. */
45
+ raw: string;
46
+ }
47
+ export interface CodeBlock extends BlockBase<"CodeBlock"> {
48
+ /** The opening fence, like "```" or "~~~~", or "" for indented code. */
49
+ fence: string;
50
+ /** The columns the opening fence is indented by. */
51
+ indent: number;
52
+ /** The info string, trimmed. */
53
+ info: string;
54
+ /** The lines of code, after the container prefixes, indented code without its four columns. */
55
+ lines: string[];
56
+ /** What precedes each line in the source, like the indentation of a list item or `> `. */
57
+ prefixes: string[];
58
+ /** Whether a fenced block has its closing fence. */
59
+ closed: boolean;
60
+ }
61
+ export interface HtmlBlock extends BlockBase<"HtmlBlock"> {
62
+ /** The CommonMark start condition, 1 to 7. */
63
+ kind: number;
64
+ lines: string[];
65
+ }
66
+ export interface Definition extends BlockBase<"Definition"> {
67
+ /** The indentation before the label. */
68
+ indent: string;
69
+ label: string;
70
+ destination: string;
71
+ /** The title with its quotes or parentheses, if any. */
72
+ title: string | null;
73
+ }
74
+ export interface BlockQuote extends BlockBase<"BlockQuote"> {
75
+ children: Block[];
76
+ }
77
+ export interface List extends BlockBase<"List"> {
78
+ ordered: boolean;
79
+ /** `-`, `*` or `+`, or the delimiter of ordered lists, `.` or `)`. */
80
+ delimiter: string;
81
+ /** Whether blank lines separate no items and no blocks of an item. */
82
+ tight: boolean;
83
+ children: ListItem[];
84
+ }
85
+ export interface ListItem extends BlockBase<"ListItem"> {
86
+ /** The marker as written, like `-` or `01.`. */
87
+ marker: string;
88
+ /** The number of ordered items. */
89
+ number: number | null;
90
+ /** Columns before the marker. */
91
+ indent: number;
92
+ /** Spaces after the marker as written, before the content, at least 1 unless the item is empty. */
93
+ spaces: number;
94
+ /** Task list items, `[ ]` or `[x]`, have their state, the box stays in the first paragraph. */
95
+ checked: boolean | null;
96
+ children: Block[];
97
+ }
98
+ export type Alignment = "left" | "center" | "right" | null;
99
+ export interface Table extends BlockBase<"Table"> {
100
+ alignments: Alignment[];
101
+ /** The header first, then the body rows, without the delimiter row. */
102
+ rows: TableRow[];
103
+ }
104
+ export interface TableRow extends BlockBase<"TableRow"> {
105
+ cells: TableCell[];
106
+ }
107
+ export interface TableCell extends Base<"TableCell"> {
108
+ text: string;
109
+ children: Inline[];
110
+ }
111
+ export type Block = BlockQuote | CodeBlock | Definition | Heading | HtmlBlock | List | Paragraph | Table | ThematicBreak;
112
+ export interface Text extends Base<"Text"> {
113
+ /** As written, with escapes and entities. */
114
+ raw: string;
115
+ }
116
+ export interface Break extends Base<"Break"> {
117
+ /** A hard break, two spaces or a backslash before the line ending. */
118
+ hard: boolean;
119
+ raw: string;
120
+ }
121
+ export interface Code extends Base<"Code"> {
122
+ raw: string;
123
+ }
124
+ /** `*a*`, `**a**` and `~~a~~` with their delimiters, like `_` or `**`. */
125
+ export interface Emphasis extends Base<"Emphasis"> {
126
+ delimiter: string;
127
+ children: Inline[];
128
+ }
129
+ export interface Strong extends Base<"Strong"> {
130
+ delimiter: string;
131
+ children: Inline[];
132
+ }
133
+ export interface Strikethrough extends Base<"Strikethrough"> {
134
+ delimiter: string;
135
+ children: Inline[];
136
+ }
137
+ export interface Link extends Base<"Link" | "Image"> {
138
+ children: Inline[];
139
+ /** From `[text](` to the end. */
140
+ tail: string;
141
+ /** Inline links have a destination, others a label, like `[a][b]`, `[a][]` and `[a]`. */
142
+ reference: "full" | "collapsed" | "shortcut" | null;
143
+ /** The label of references, with its brackets. */
144
+ label: string | null;
145
+ destination: string;
146
+ title: string | null;
147
+ }
148
+ export interface Autolink extends Base<"Autolink"> {
149
+ raw: string;
150
+ }
151
+ export interface Html extends Base<"Html"> {
152
+ raw: string;
153
+ }
154
+ export type Inline = Autolink | Break | Code | Emphasis | Html | Link | Strikethrough | Strong | Text;
155
+ export type Node = Root | Frontmatter | Block | ListItem | TableRow | TableCell | Inline;
156
+ export {};
@@ -0,0 +1,15 @@
1
+ import type { Root } from "./ast.js";
2
+ export interface MarkdownOptions {
3
+ /** GitHub Flavored Markdown: tables, task list items and strikethrough. */
4
+ gfm?: boolean;
5
+ /** A `---` fenced block of YAML at the very start of the file. */
6
+ frontmatter?: boolean;
7
+ }
8
+ /** Parses Markdown blocks line by line, like CommonMark's reference parser. */
9
+ export default class Blocks {
10
+ #private;
11
+ constructor(source: string, options?: MarkdownOptions);
12
+ parse(frontmatter?: boolean): Root;
13
+ /** Splits a table row into its cells, on pipes that are not escaped. */
14
+ static cells(row: string): string[];
15
+ }
@@ -0,0 +1,7 @@
1
+ import type { Inline } from "./ast.js";
2
+ /** Parses the inline content of paragraphs, headings and table cells. */
3
+ export default class Inlines {
4
+ #private;
5
+ constructor(labels: Set<string>, gfm: boolean);
6
+ parse(text: string): Inline[];
7
+ }
@@ -0,0 +1,10 @@
1
+ export declare const OPEN_TAG = "<[A-Za-z][A-Za-z0-9-]*(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*(?:[^\"'=<>`\\x00-\\x20]+|'[^']*'|\"[^\"]*\"))?)*\\s*/?>";
2
+ export declare const CLOSE_TAG = "</[A-Za-z][A-Za-z0-9-]*\\s*[>]";
3
+ export default class Scan {
4
+ /** Normalizes a link label for matching, like `[Foo Bar]` and `[foo bar]`. */
5
+ static label(label: string): string;
6
+ /** A link destination, `<...>` or without spaces and with balanced parentheses. */
7
+ static destination(text: string, from: number): [number, string] | undefined;
8
+ /** The position after a link title in quotes or parentheses. */
9
+ static title(text: string, from: number): number | undefined;
10
+ }
@@ -0,0 +1,47 @@
1
+ import type { Node, NodeOf, NodeType } from "../ast.js";
2
+ import Scanner, { type Token } from "../scanner.js";
3
+ export interface Options {
4
+ jsx: boolean;
5
+ typescript: boolean;
6
+ }
7
+ interface Snapshot {
8
+ position: number;
9
+ token: Token;
10
+ previous: number;
11
+ }
12
+ type Fields<T extends NodeType> = Omit<NodeOf<T>, "end" | "start" | "type">;
13
+ export default abstract class BaseParser {
14
+ protected readonly source: string;
15
+ protected readonly scanner: Scanner;
16
+ protected readonly options: Options;
17
+ protected token: Token;
18
+ /** End of the last consumed token. */
19
+ protected previous: number;
20
+ /** Whether `in` is a binary operator here (not in a for-in head). */
21
+ protected allowIn: boolean;
22
+ constructor(source: string, options: Options);
23
+ abstract parseAssignment(): Node;
24
+ get comments(): Scanner["comments"];
25
+ protected next(): Token;
26
+ /** Replaces the current token with a rescanned one. */
27
+ protected replace(token: Token): void;
28
+ /** Continues scanning from a position, discarding the current token. */
29
+ protected rescan(position: number): void;
30
+ protected is(value: string, token?: Token): boolean;
31
+ protected isName(token?: Token): boolean;
32
+ protected eat(value: string): boolean;
33
+ protected expect(value: string): Token;
34
+ /** Peeks the token after the current one. */
35
+ protected peek(): Token;
36
+ protected snapshot(): Snapshot;
37
+ protected restore({ position, token, previous }: Snapshot): void;
38
+ /** Runs a speculative parse, undoing it when it fails. */
39
+ protected attempt<T>(parse: () => T): T | undefined;
40
+ protected finish<T extends NodeType>(type: T, start: number, fields: Fields<T>): NodeOf<T>;
41
+ /** Consumes a semicolon or accepts automatic semicolon insertion. */
42
+ protected semicolon(): void;
43
+ protected canInsertSemicolon(): boolean;
44
+ protected unexpected(message?: string): never;
45
+ protected raise(message: string, position: number): never;
46
+ }
47
+ export {};
@@ -0,0 +1,29 @@
1
+ import type { BlockStatement, Decorator, Node } from "../ast.js";
2
+ import TypeParser from "./types.js";
3
+ /** Expressions, patterns, functions, classes and JSX. */
4
+ export default abstract class ExpressionParser extends TypeParser {
5
+ #private;
6
+ abstract parseBlock(): BlockStatement;
7
+ parseExpression(): Node;
8
+ parseAssignment(): Node;
9
+ /** Converts an object or array literal used as an assignment target into a pattern. */
10
+ toPattern(node: Node): Node;
11
+ /** Parses with `in` allowed or not, restoring the previous setting. */
12
+ protected withIn<T>(allow: boolean, parse: () => T): T;
13
+ parseLeftHandSide(): Node;
14
+ parseArguments(): Node[];
15
+ parsePropertyKey(): {
16
+ key: Node;
17
+ computed: boolean;
18
+ };
19
+ parseFunction(start: number, async: boolean, expression: boolean): Node;
20
+ parseParameters(): Node[];
21
+ /** Parses a binding target with optional `?`, type annotation and default. */
22
+ parseBindingElement(): Node;
23
+ parseBindingTarget(): Node & {
24
+ optional?: boolean;
25
+ typeAnnotation?: Node | null;
26
+ };
27
+ parseDecorators(): Decorator[];
28
+ parseClass(start: number, decorators: Decorator[], expression: boolean, abstract: boolean, declare: boolean): Node;
29
+ }
@@ -0,0 +1,11 @@
1
+ import type { BlockStatement, Node, Program, VariableDeclaration } from "../ast.js";
2
+ import ExpressionParser from "./expressions.js";
3
+ /** Statements, modules and TypeScript declarations: the complete parser. */
4
+ export default class Parser extends ExpressionParser {
5
+ #private;
6
+ parseProgram(): Program;
7
+ /** Parses a whole document as one expression, for JSON. */
8
+ parseDocument(): Node;
9
+ parseBlock(): BlockStatement;
10
+ parseVariables(start: number, declare: boolean): VariableDeclaration;
11
+ }
@@ -0,0 +1,30 @@
1
+ import type { Identifier, Node, TemplateElement, TypeArguments, TypeParameters } from "../ast.js";
2
+ import BaseParser from "./base.js";
3
+ /** Parses TypeScript types, and the literals and names they share with expressions. */
4
+ export default abstract class TypeParser extends BaseParser {
5
+ #private;
6
+ abstract parseParameters(): Node[];
7
+ abstract parsePropertyKey(): {
8
+ key: Node;
9
+ computed: boolean;
10
+ };
11
+ parseIdentifier(): Identifier;
12
+ parseString(): Node & {
13
+ type: "StringLiteral";
14
+ };
15
+ /** Parses a template, with parts parsed by the given parser. */
16
+ protected parseTemplateParts(part: () => Node): {
17
+ quasis: TemplateElement[];
18
+ parts: Node[];
19
+ };
20
+ parseTypeAnnotation(): Node | null;
21
+ parseType(): Node;
22
+ parseReturnType(): Node;
23
+ parseTypeParameters(): TypeParameters | null;
24
+ parseTypeArguments(): TypeArguments;
25
+ parseEntityName(): Node;
26
+ /** Parses `{ ... }` members of an interface or a type literal. */
27
+ parseTypeMembers(): Node[];
28
+ /** Parses `[key: T]: U` after optional modifiers. */
29
+ protected parseIndexSignature(start: number, readonly: boolean, isStatic: boolean): Node | undefined;
30
+ }
@@ -0,0 +1,48 @@
1
+ export type TokenType = "eof" | "invalid" | "jsxText" | "name" | "number" | "private" | "punct" | "regex" | "string" | "template";
2
+ export interface Token {
3
+ type: TokenType;
4
+ value: string;
5
+ start: number;
6
+ end: number;
7
+ /** Whether a line break precedes the token. */
8
+ newline: boolean;
9
+ }
10
+ export interface Comment {
11
+ type: "Block" | "Line";
12
+ /** Raw text, delimiters included. */
13
+ value: string;
14
+ start: number;
15
+ end: number;
16
+ }
17
+ export declare class ParseError extends SyntaxError {
18
+ readonly position: number;
19
+ constructor(message: string, position: number);
20
+ }
21
+ /**
22
+ * Splits source into tokens on demand. The parser decides how ambiguous
23
+ * characters are read: `/` as regex, `}` as template continuation, JSX text.
24
+ */
25
+ export default class Scanner {
26
+ #private;
27
+ readonly source: string;
28
+ readonly comments: Comment[];
29
+ constructor(source: string);
30
+ get position(): number;
31
+ /** Moves back (or forward) and forgets comments scanned beyond. */
32
+ reset(position: number): void;
33
+ next(): Token;
34
+ /** Rescans a `/` or `/=` token as a regular expression literal. */
35
+ regex(token: Token): Token;
36
+ /** Rescans `>` as the longest of `>>>=`, `>>>`, `>>=`, `>>`, `>=`. */
37
+ greater(token: Token): Token;
38
+ /** Rescans `<` as the longest of `<<=`, `<<`, `<=`, since types nest `<<T>() => T>`. */
39
+ less(token: Token): Token;
40
+ /** Scans a template part, from a backtick or a closing `}`, up to `${` or a backtick. */
41
+ template(start: number, newline?: boolean): Token;
42
+ /** Scans JSX text up to `{` or `<`. */
43
+ jsxText(start: number): Token;
44
+ /** Rescans a name token to include JSX's `-` characters. */
45
+ jsxName(token: Token): Token;
46
+ /** Rescans a quoted JSX attribute value, which has no escapes. */
47
+ jsxString(token: Token): Token;
48
+ }
@@ -0,0 +1,7 @@
1
+ import type { Node } from "./ast.js";
2
+ /** Generic traversal of syntax trees, child nodes in source order. */
3
+ export default class Walker {
4
+ static isNode(value: unknown): value is Node;
5
+ /** Child nodes by start, outer nodes before inner ones starting with them. */
6
+ static children(node: Node): Node[];
7
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@aljazmc/syntax",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.4.0",
4
+ "description": "Shared, dependency-free JavaScript, TypeScript, JSX, JSON and Markdown parser for @aljazmc packages.",
5
+ "author": "aljazmc",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/aljazmc/aljazmc.git",
11
+ "directory": "shared/syntax"
12
+ },
13
+ "engines": {
14
+ "node": "^22.18.0 || ^24.2.0 || ^25 || ^26"
15
+ },
16
+ "types": "./bin/types/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./bin/types/index.d.ts",
20
+ "default": "./bin/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "publishConfig": {
25
+ "exports": {
26
+ ".": {
27
+ "types": "./bin/types/index.d.ts",
28
+ "default": "./bin/index.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ }
32
+ },
33
+ "devDependencies": {
34
+ "@jest/globals": "^30.5.2",
35
+ "@types/node": "^26.6.5"
36
+ }
6
37
  }