@aljazmc/syntax 0.0.0-stage → 0.3.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,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.3.0",
4
+ "description": "Shared, dependency-free JavaScript, TypeScript, JSX and JSON 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
  }