@aljazmc/syntax 0.6.1 → 0.7.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.
@@ -4,6 +4,8 @@ export default class Parser {
4
4
  #private;
5
5
  /** Parses a stylesheet, or only values from `start` to `end`, recovering from errors when asked. */
6
6
  constructor(source: string, start?: number, end?: number, recover?: boolean);
7
+ /** Where the parser is, the start of its current token. */
8
+ get position(): number;
7
9
  parse(): Stylesheet;
8
10
  /** Values of a range of the source, like the argument of a pseudo class. */
9
11
  static values(source: string, start: number, end: number): Value[];
@@ -51,8 +51,8 @@ export interface AttributeValue extends Base<"AttributeValue"> {
51
51
  quote: '"' | "'" | "";
52
52
  }
53
53
  /**
54
- * A run of text, without the whitespace around it, which is trivia like in
55
- * Biome. A blank line ends a text, the next one starts after it.
54
+ * A run of text, without the whitespace around it, which is trivia. A
55
+ * blank line ends a text, the next one starts after it.
56
56
  */
57
57
  export interface Text extends Base<"Text"> {
58
58
  /** The text as written, character references are not decoded. */
@@ -2,11 +2,13 @@ import type { Root } from "./ast.js";
2
2
  /** The known tag a name matches, exactly like SVG's `feBlend`, or lower-cased like HTML's `DIV`. */
3
3
  export declare function tagOf(name: string): string | null;
4
4
  /**
5
- * Parses HTML like Biome: elements are closed explicitly, only void ones may
5
+ * Parses HTML: elements are closed explicitly, only void ones may
6
6
  * have no closing tag, and whitespace and comments are trivia between nodes.
7
7
  */
8
8
  export default class Parser {
9
9
  #private;
10
10
  constructor(source: string);
11
+ /** Where the parser is. */
12
+ get position(): number;
11
13
  parse(): Root;
12
14
  }
@@ -1,2 +1,2 @@
1
- /** Tag names Biome 2.6.6 knows, SVG ones in camel case like `feBlend`. */
1
+ /** Known tag names, SVG ones in camel case like `feBlend`. */
2
2
  export declare const tagNames: ReadonlySet<string>;
@@ -4,7 +4,8 @@ import type { Root as HtmlRoot } from "./html/ast.js";
4
4
  import type { Root } from "./markdown/ast.js";
5
5
  import { type MarkdownOptions } from "./markdown/blocks.js";
6
6
  import type { Options } from "./parser/base.js";
7
- import type { Comment } from "./scanner.js";
7
+ import { type Comment } from "./scanner.js";
8
+ import type { Stream } from "./yaml/ast.js";
8
9
  export type * from "./ast.js";
9
10
  export type * as Css from "./css/ast.js";
10
11
  export type * as Html from "./html/ast.js";
@@ -14,6 +15,7 @@ export type * as Markdown from "./markdown/ast.js";
14
15
  export type { Comment, Token } from "./scanner.js";
15
16
  export { ParseError } from "./scanner.js";
16
17
  export { default as Walker } from "./walker.js";
18
+ export type * as Yaml from "./yaml/ast.js";
17
19
  export type { MarkdownOptions, Options };
18
20
  export interface Parsed {
19
21
  program: Program;
@@ -31,6 +33,8 @@ export declare function language(file: string): Language | undefined;
31
33
  export declare function isMarkdown(file: string): boolean;
32
34
  /** Whether a file is HTML, by its extension. */
33
35
  export declare function isHtml(file: string): boolean;
36
+ /** Whether a file is YAML, by its extension. */
37
+ export declare function isYaml(file: string): boolean;
34
38
  /** Whether a file is CSS, by its extension. */
35
39
  export declare function isCss(file: string): boolean;
36
40
  /**
@@ -43,7 +47,12 @@ export declare function parseCss(source: string, options?: {
43
47
  }): Stylesheet;
44
48
  /** Parses the component values of a range of CSS, like a `style` attribute value. */
45
49
  export declare function parseCssValues(source: string, start?: number, end?: number): import("./css/ast.js").Value[];
46
- /** Parses HTML like Biome, throwing a ParseError at the first error. */
50
+ /**
51
+ * Parses YAML, with line feeds as line endings, into documents, throwing a
52
+ * ParseError at the first error.
53
+ */
54
+ export declare function parseYaml(source: string): Stream;
55
+ /** Parses HTML, throwing a ParseError at the first error. */
47
56
  export declare function parseHtml(source: string): HtmlRoot;
48
57
  /** Parses CommonMark, with GitHub's tables, task lists and strikethrough unless `gfm` is false. */
49
58
  export declare function parseMarkdown(source: string, options?: MarkdownOptions): Root;
@@ -1,4 +1,8 @@
1
- /** Converts offsets to 1-based lines and columns. */
1
+ /**
2
+ * Converts offsets to 1-based lines and columns. Like Biome, columns count
3
+ * characters, so a character outside the BMP, like an emoji, is one column,
4
+ * not the two UTF-16 code units it takes.
5
+ */
2
6
  export default class Lines {
3
7
  #private;
4
8
  constructor(text: string);
@@ -9,6 +9,8 @@ export interface MarkdownOptions {
9
9
  export default class Blocks {
10
10
  #private;
11
11
  constructor(source: string, options?: MarkdownOptions);
12
+ /** Where the parser is, the start of its current line. */
13
+ get position(): number;
12
14
  parse(frontmatter?: boolean): Root;
13
15
  /** Splits a table row into its cells, on pipes that are not escaped. */
14
16
  static cells(row: string): string[];
@@ -15,6 +15,8 @@ export default abstract class BaseParser {
15
15
  protected readonly scanner: Scanner;
16
16
  protected readonly options: Options;
17
17
  protected token: Token;
18
+ /** Where the parser is, the start of its current token. */
19
+ get position(): number;
18
20
  /** End of the last consumed token. */
19
21
  protected previous: number;
20
22
  /** Whether `in` is a binary operator here (not in a for-in head). */
@@ -0,0 +1,140 @@
1
+ /** A `#` comment, its value starting with the `#`. */
2
+ export interface Comment {
3
+ start: number;
4
+ end: number;
5
+ value: string;
6
+ }
7
+ /** A comment on a line of its own, after a blank line or not. */
8
+ export interface CommentLine {
9
+ comment: Comment;
10
+ blank: boolean;
11
+ }
12
+ interface Base {
13
+ start: number;
14
+ end: number;
15
+ /** Tag and anchor, like `!!str &a`, the tag first, separated by a single space. */
16
+ properties?: string;
17
+ /** The comment after properties standing alone on their line. */
18
+ propertiesComment?: Comment;
19
+ /** Comments on their own lines between such properties and the node. */
20
+ leading?: CommentLine[];
21
+ /** The comment after the node on its last line. */
22
+ comment?: Comment;
23
+ }
24
+ /** A plain, single or double quoted scalar. */
25
+ export interface Scalar extends Base {
26
+ type: "Scalar";
27
+ style: "plain" | "single" | "double";
28
+ /** Its lines without their indentation and trailing spaces, quotes included, blank lines empty. */
29
+ lines: string[];
30
+ }
31
+ /** A literal `|` or folded `>` scalar. */
32
+ export interface BlockScalar extends Base {
33
+ type: "BlockScalar";
34
+ /** Like `|`, `>-` or `|2+`. */
35
+ header: string;
36
+ /** The explicit indentation indicator, 0 without one. */
37
+ indicator: number;
38
+ /** The indentation of the node the scalar belongs to, -1 for a document's own. */
39
+ parent: number;
40
+ /** The indentation of its content. */
41
+ indentation: number;
42
+ /** Its content lines as written, with their indentation. */
43
+ lines: string[];
44
+ }
45
+ export interface Alias extends Base {
46
+ type: "Alias";
47
+ /** Like `*a`. */
48
+ name: string;
49
+ }
50
+ /** An entry of a flow collection, a pair like `a: 1` or a single node, which is its key. */
51
+ export interface FlowItem {
52
+ key: Node | null;
53
+ value: Node | null;
54
+ /** Whether it has a `:`. */
55
+ pair: boolean;
56
+ /** Comments after it, before the next one. */
57
+ comments: Comment[];
58
+ }
59
+ export interface FlowMapping extends Base {
60
+ type: "FlowMapping";
61
+ items: FlowItem[];
62
+ /** Comments before the first item. */
63
+ opening: Comment[];
64
+ }
65
+ export interface FlowSequence extends Base {
66
+ type: "FlowSequence";
67
+ items: FlowItem[];
68
+ opening: Comment[];
69
+ }
70
+ /** What block mapping entries and sequence items share. */
71
+ interface Entry {
72
+ /** Comments on their own lines before it, at its indentation. */
73
+ leading: CommentLine[];
74
+ /** Whether a blank line comes before it, after its leading comments. */
75
+ blank: boolean;
76
+ value: Node | null;
77
+ /** The comment after its indicator, `:` or `-`, when the value is on the lines after it. */
78
+ comment?: Comment;
79
+ /** Comments on their own lines between the indicator and a value on the lines after it. */
80
+ between: CommentLine[];
81
+ /** Whether a blank line comes before such a value, after those comments. */
82
+ betweenBlank: boolean;
83
+ /** Comments indented deeper than it after its value, which is not a block scalar. */
84
+ inner: CommentLine[];
85
+ }
86
+ export interface MappingEntry extends Entry {
87
+ key: Node | null;
88
+ /** Whether the key is written after `?`. */
89
+ explicit: boolean;
90
+ /** The comment after `?` when the key is on the lines after it. */
91
+ keyComment?: Comment;
92
+ /** Comments on their own lines around an explicit key. */
93
+ keyBetween: CommentLine[];
94
+ }
95
+ export type SequenceItem = Entry;
96
+ export interface Mapping extends Base {
97
+ type: "Mapping";
98
+ entries: MappingEntry[];
99
+ /** Comments at its indentation after its last entry. */
100
+ endComments: CommentLine[];
101
+ }
102
+ export interface Sequence extends Base {
103
+ type: "Sequence";
104
+ items: SequenceItem[];
105
+ endComments: CommentLine[];
106
+ }
107
+ export type Node = Scalar | BlockScalar | Alias | FlowMapping | FlowSequence | Mapping | Sequence;
108
+ /** A comment or a directive, like `%YAML 1.2`, before a document starts. */
109
+ export interface Prelude {
110
+ text: string;
111
+ blank: boolean;
112
+ }
113
+ export interface Document {
114
+ type: "Document";
115
+ start: number;
116
+ end: number;
117
+ prelude: Prelude[];
118
+ /** Whether it starts with `---`. */
119
+ explicitStart: boolean;
120
+ /** Whether a blank line comes before `---`, after the prelude. */
121
+ startBlank: boolean;
122
+ /** The comment after `---`. */
123
+ startComment?: Comment;
124
+ /** Comments before its node, after `---`. */
125
+ leading: CommentLine[];
126
+ /** Whether a blank line comes before its node, after those comments. */
127
+ blank: boolean;
128
+ root: Node | null;
129
+ /** Comments after its node. */
130
+ trailing: CommentLine[];
131
+ /** Whether it ends with `...`. */
132
+ explicitEnd: boolean;
133
+ /** The comment after `...`. */
134
+ endComment?: Comment;
135
+ }
136
+ export interface Stream {
137
+ type: "Stream";
138
+ documents: Document[];
139
+ }
140
+ export {};
@@ -0,0 +1,11 @@
1
+ import type { Stream } from "./ast.js";
2
+ /**
3
+ * Parses YAML, with line feeds as line endings, into documents of block
4
+ * and flow nodes, keeping comments and blank lines where they are.
5
+ */
6
+ export default class YamlParser {
7
+ #private;
8
+ position: number;
9
+ constructor(source: string);
10
+ parse(): Stream;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aljazmc/syntax",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "Shared, dependency-free JavaScript, TypeScript, JSX, JSON, Markdown, HTML and CSS parser for @aljazmc packages.",
5
5
  "author": "aljazmc",
6
6
  "license": "MIT",
@@ -31,7 +31,6 @@
31
31
  }
32
32
  },
33
33
  "devDependencies": {
34
- "@jest/globals": "^30.5.2",
35
34
  "@types/node": "^26.6.6"
36
35
  }
37
36
  }