@aljazmc/syntax 0.5.0 → 0.6.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.
- package/README.md +20 -2
- package/bin/index.js +1305 -16
- package/bin/types/css/ast.d.ts +231 -0
- package/bin/types/css/parser.d.ts +10 -0
- package/bin/types/css/tokens.d.ts +27 -0
- package/bin/types/html/tags.d.ts +1 -1
- package/bin/types/index.d.ts +14 -0
- package/package.json +2 -2
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/** Nodes have offsets into the source, `end` is exclusive. */
|
|
2
|
+
interface Base<T extends string> {
|
|
3
|
+
type: T;
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Stylesheet extends Base<"Stylesheet"> {
|
|
8
|
+
/** Whether the source starts with a byte order mark. */
|
|
9
|
+
bom: boolean;
|
|
10
|
+
children: Statement[];
|
|
11
|
+
/** Every `/* *\/` comment, in source order. Comments are not children. */
|
|
12
|
+
comments: Comment[];
|
|
13
|
+
/** Syntax errors a parser that recovers skipped, the items with them are left out. */
|
|
14
|
+
errors: {
|
|
15
|
+
message: string;
|
|
16
|
+
position: number;
|
|
17
|
+
}[];
|
|
18
|
+
}
|
|
19
|
+
/** What a stylesheet contains at its top level. */
|
|
20
|
+
export type Statement = Rule | AtRule | Empty;
|
|
21
|
+
/** What a block contains: declarations, and nested rules and at-rules. */
|
|
22
|
+
export type Item = Declaration | Rule | AtRule | Empty;
|
|
23
|
+
/** A rule with selectors, like `a > b { color: red }`, or a keyframe, like `from { }`. */
|
|
24
|
+
export interface Rule extends Base<"Rule"> {
|
|
25
|
+
/** The selectors before the block, nested ones may start with a combinator, null for keyframes. */
|
|
26
|
+
selectors: SelectorList | null;
|
|
27
|
+
/** The values before the block, the selectors of keyframes, like `from, 50%`. */
|
|
28
|
+
prelude: Value[];
|
|
29
|
+
block: Block;
|
|
30
|
+
}
|
|
31
|
+
/** An at-rule, like `@media screen { }` or `@import "a.css";`. */
|
|
32
|
+
export interface AtRule extends Base<"AtRule"> {
|
|
33
|
+
/** The name as written, without `@`. */
|
|
34
|
+
name: string;
|
|
35
|
+
/** The name in lower case, for matching. */
|
|
36
|
+
keyword: string;
|
|
37
|
+
/** The values between the name and the block or semicolon. */
|
|
38
|
+
prelude: Value[];
|
|
39
|
+
/** The start and end of the prelude, without the whitespace around it. */
|
|
40
|
+
preludeStart: number;
|
|
41
|
+
preludeEnd: number;
|
|
42
|
+
block: Block | null;
|
|
43
|
+
/** Ends with `;`, a statement at the end of a block or file may not. */
|
|
44
|
+
semicolon: boolean;
|
|
45
|
+
}
|
|
46
|
+
export interface Block extends Base<"Block"> {
|
|
47
|
+
children: Item[];
|
|
48
|
+
}
|
|
49
|
+
/** A `;` on its own. */
|
|
50
|
+
export type Empty = Base<"Empty">;
|
|
51
|
+
export interface Declaration extends Base<"Declaration"> {
|
|
52
|
+
/** The property as written, like `COLOR` or `--main-color`. */
|
|
53
|
+
property: string;
|
|
54
|
+
/** The end of the property name. */
|
|
55
|
+
propertyEnd: number;
|
|
56
|
+
/** The position of the `:`. */
|
|
57
|
+
colon: number;
|
|
58
|
+
/** The values after the `:`, without `!important`. */
|
|
59
|
+
value: Value[];
|
|
60
|
+
/** `!important`, as written, like `! IMPORTANT`. */
|
|
61
|
+
important: Important | null;
|
|
62
|
+
/** Ends with `;`, the last declaration of a block may not. */
|
|
63
|
+
semicolon: boolean;
|
|
64
|
+
}
|
|
65
|
+
export interface Important extends Base<"Important"> {
|
|
66
|
+
/** The keyword as written, like `important`. */
|
|
67
|
+
keyword: string;
|
|
68
|
+
}
|
|
69
|
+
/** Component values, the parts of property values and at-rule preludes. */
|
|
70
|
+
export type Value = Identifier | FunctionValue | NumberValue | Percentage | Dimension | StringValue | Url | Hash | Delimiter | Comma | Colon | Semicolon | SimpleBlock | UnicodeRange | AtKeyword;
|
|
71
|
+
export interface Identifier extends Base<"Identifier"> {
|
|
72
|
+
/** As written, escapes are not decoded. */
|
|
73
|
+
value: string;
|
|
74
|
+
}
|
|
75
|
+
export interface FunctionValue extends Base<"Function"> {
|
|
76
|
+
/** The name as written, without `(`. */
|
|
77
|
+
name: string;
|
|
78
|
+
/** The position of the `(`. */
|
|
79
|
+
open: number;
|
|
80
|
+
children: Value[];
|
|
81
|
+
/** Whether the `)` is written, a function at the end of a file may miss it. */
|
|
82
|
+
closed: boolean;
|
|
83
|
+
}
|
|
84
|
+
export interface NumberValue extends Base<"Number"> {
|
|
85
|
+
/** As written, like `+.5` or `1e3`. */
|
|
86
|
+
value: string;
|
|
87
|
+
}
|
|
88
|
+
export interface Percentage extends Base<"Percentage"> {
|
|
89
|
+
/** The number as written, without `%`. */
|
|
90
|
+
value: string;
|
|
91
|
+
}
|
|
92
|
+
export interface Dimension extends Base<"Dimension"> {
|
|
93
|
+
/** The number as written. */
|
|
94
|
+
value: string;
|
|
95
|
+
/** The unit as written, like `PX`. */
|
|
96
|
+
unit: string;
|
|
97
|
+
}
|
|
98
|
+
export interface StringValue extends Base<"String"> {
|
|
99
|
+
/** As written, with its quotes. */
|
|
100
|
+
raw: string;
|
|
101
|
+
/** Whether the string ends with its quote, a newline or the end of the file end unclosed ones. */
|
|
102
|
+
closed: boolean;
|
|
103
|
+
}
|
|
104
|
+
/** `url(…)`, with an unquoted or a quoted address. */
|
|
105
|
+
export interface Url extends Base<"Url"> {
|
|
106
|
+
/** The name as written, like `URL`. */
|
|
107
|
+
name: string;
|
|
108
|
+
/** The address, a raw text or a string, or null for `url()`. */
|
|
109
|
+
value: StringValue | UrlRaw | null;
|
|
110
|
+
/** Values after a quoted address, like `url("a" format("b"))`. */
|
|
111
|
+
modifiers: Value[];
|
|
112
|
+
}
|
|
113
|
+
/** An unquoted address in `url(…)`. */
|
|
114
|
+
export interface UrlRaw extends Base<"UrlRaw"> {
|
|
115
|
+
/** As written, without the whitespace around it. */
|
|
116
|
+
value: string;
|
|
117
|
+
}
|
|
118
|
+
export interface Hash extends Base<"Hash"> {
|
|
119
|
+
/** As written, without `#`. */
|
|
120
|
+
value: string;
|
|
121
|
+
}
|
|
122
|
+
/** A single character that is no other token, like `/`, `+`, `>` or `!`. */
|
|
123
|
+
export interface Delimiter extends Base<"Delimiter"> {
|
|
124
|
+
value: string;
|
|
125
|
+
}
|
|
126
|
+
export type Comma = Base<"Comma">;
|
|
127
|
+
export type Colon = Base<"Colon">;
|
|
128
|
+
export type Semicolon = Base<"Semicolon">;
|
|
129
|
+
/** Values in parentheses, brackets or braces. */
|
|
130
|
+
export interface SimpleBlock extends Base<"SimpleBlock"> {
|
|
131
|
+
open: "(" | "[" | "{";
|
|
132
|
+
children: Value[];
|
|
133
|
+
/** Whether the closing bracket is written. */
|
|
134
|
+
closed: boolean;
|
|
135
|
+
}
|
|
136
|
+
export interface UnicodeRange extends Base<"UnicodeRange"> {
|
|
137
|
+
/** As written, like `U+0025-00FF`. */
|
|
138
|
+
value: string;
|
|
139
|
+
}
|
|
140
|
+
export interface AtKeyword extends Base<"AtKeyword"> {
|
|
141
|
+
/** As written, without `@`. */
|
|
142
|
+
value: string;
|
|
143
|
+
}
|
|
144
|
+
export interface Comment extends Base<"Comment"> {
|
|
145
|
+
/** As written, with `/*` and `*\/`. */
|
|
146
|
+
value: string;
|
|
147
|
+
}
|
|
148
|
+
/** Selectors separated by commas. */
|
|
149
|
+
export interface SelectorList extends Base<"SelectorList"> {
|
|
150
|
+
children: Selector[];
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Compound selectors joined by combinators. The first may have a combinator
|
|
154
|
+
* too, in nested rules and in `:has()`.
|
|
155
|
+
*/
|
|
156
|
+
export interface Selector extends Base<"Selector"> {
|
|
157
|
+
children: CompoundSelector[];
|
|
158
|
+
}
|
|
159
|
+
/** Simple selectors written together, like `&div.a:hover`. */
|
|
160
|
+
export interface CompoundSelector extends Base<"CompoundSelector"> {
|
|
161
|
+
/** The combinator before it, null for the first one without a combinator. */
|
|
162
|
+
combinator: Combinator | null;
|
|
163
|
+
/** The type or universal selector, first after the nesting selectors. */
|
|
164
|
+
element: TypeSelector | UniversalSelector | null;
|
|
165
|
+
children: SubSelector[];
|
|
166
|
+
}
|
|
167
|
+
export interface Combinator extends Base<"Combinator"> {
|
|
168
|
+
/** `>`, `+`, `~`, `||`, or " " for descendants. */
|
|
169
|
+
value: ">" | "+" | "~" | "||" | " ";
|
|
170
|
+
}
|
|
171
|
+
export interface TypeSelector extends Base<"TypeSelector"> {
|
|
172
|
+
/** The namespace prefix as written, like `svg` of `svg|a` or `*`, "" for `|a`, null without `|`. */
|
|
173
|
+
namespace: string | null;
|
|
174
|
+
/** The name as written. */
|
|
175
|
+
name: string;
|
|
176
|
+
}
|
|
177
|
+
export interface UniversalSelector extends Base<"UniversalSelector"> {
|
|
178
|
+
namespace: string | null;
|
|
179
|
+
}
|
|
180
|
+
export type SubSelector = NestingSelector | IdSelector | ClassSelector | AttributeSelector | PseudoClassSelector | PseudoElementSelector;
|
|
181
|
+
export interface NestingSelector extends Base<"NestingSelector"> {
|
|
182
|
+
/** A suffix written right after the `&`, like `-a` of `&-a`. */
|
|
183
|
+
suffix: string;
|
|
184
|
+
}
|
|
185
|
+
export interface IdSelector extends Base<"IdSelector"> {
|
|
186
|
+
/** As written, without `#`. */
|
|
187
|
+
name: string;
|
|
188
|
+
}
|
|
189
|
+
export interface ClassSelector extends Base<"ClassSelector"> {
|
|
190
|
+
/** As written, without `.`. */
|
|
191
|
+
name: string;
|
|
192
|
+
}
|
|
193
|
+
export interface AttributeSelector extends Base<"AttributeSelector"> {
|
|
194
|
+
namespace: string | null;
|
|
195
|
+
/** The attribute name as written. */
|
|
196
|
+
name: string;
|
|
197
|
+
/** `=`, `~=`, `|=`, `^=`, `$=` or `*=`, null without a value. */
|
|
198
|
+
matcher: string | null;
|
|
199
|
+
/** The value, a string or an identifier as written. */
|
|
200
|
+
value: StringValue | Identifier | null;
|
|
201
|
+
/** `i` or `s`, as written. */
|
|
202
|
+
modifier: string | null;
|
|
203
|
+
}
|
|
204
|
+
export interface PseudoClassSelector extends Base<"PseudoClassSelector"> {
|
|
205
|
+
/** The name as written, without `:`. */
|
|
206
|
+
name: string;
|
|
207
|
+
/** What is between its parentheses, null without them. */
|
|
208
|
+
argument: PseudoArgument | null;
|
|
209
|
+
}
|
|
210
|
+
export interface PseudoElementSelector extends Base<"PseudoElementSelector"> {
|
|
211
|
+
/** The name as written, without `::`. */
|
|
212
|
+
name: string;
|
|
213
|
+
argument: PseudoArgument | null;
|
|
214
|
+
}
|
|
215
|
+
/** Selectors for `:is()` and the like, An+B for `:nth-child()`, values for others. */
|
|
216
|
+
export type PseudoArgument = SelectorList | Nth | PseudoValues;
|
|
217
|
+
/** An+B, optionally `of` selectors. */
|
|
218
|
+
export interface Nth extends Base<"Nth"> {
|
|
219
|
+
/** The An+B part, like `2n+1`, `even` or `-n + 3`, as written. */
|
|
220
|
+
value: string;
|
|
221
|
+
/** The coefficient and offset, null for an invalid one. */
|
|
222
|
+
a: number | null;
|
|
223
|
+
b: number | null;
|
|
224
|
+
selectors: SelectorList | null;
|
|
225
|
+
}
|
|
226
|
+
/** Other arguments of pseudo classes and elements, like of `:lang()` or `::part()`. */
|
|
227
|
+
export interface PseudoValues extends Base<"PseudoValues"> {
|
|
228
|
+
children: Value[];
|
|
229
|
+
}
|
|
230
|
+
export type Node = Stylesheet | Rule | AtRule | Block | Empty | Declaration | Important | Value | UrlRaw | Comment | SelectorList | Selector | CompoundSelector | Combinator | TypeSelector | UniversalSelector | SubSelector | Nth | PseudoValues;
|
|
231
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Stylesheet, Value } from "./ast.js";
|
|
2
|
+
/** Parses CSS into rules, at-rules, declarations and component values. */
|
|
3
|
+
export default class Parser {
|
|
4
|
+
#private;
|
|
5
|
+
/** Parses a stylesheet, or only values from `start` to `end`, recovering from errors when asked. */
|
|
6
|
+
constructor(source: string, start?: number, end?: number, recover?: boolean);
|
|
7
|
+
parse(): Stylesheet;
|
|
8
|
+
/** Values of a range of the source, like the argument of a pseudo class. */
|
|
9
|
+
static values(source: string, start: number, end: number): Value[];
|
|
10
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ParseError } from "../scanner.js";
|
|
2
|
+
import type { Comment } from "./ast.js";
|
|
3
|
+
export type TokenType = "ident" | "function" | "at" | "hash" | "string" | "url" | "number" | "percentage" | "dimension" | "unicode-range" | "whitespace" | "delim" | "colon" | "semicolon" | "comma" | "(" | ")" | "[" | "]" | "{" | "}" | "cdo" | "cdc" | "eof";
|
|
4
|
+
export interface Token {
|
|
5
|
+
type: TokenType;
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
/** The name of idents, functions, at-keywords and hashes, the number of numeric tokens, the character of delimiters. */
|
|
9
|
+
value: string;
|
|
10
|
+
/** The unit of dimensions, the name of urls. */
|
|
11
|
+
unit?: string;
|
|
12
|
+
/** Whether a string ends with its quote. */
|
|
13
|
+
closed?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const isWhitespace: (char: string | undefined) => char is "\t" | "\n" | "\f" | "\r" | " ";
|
|
16
|
+
/** Splits CSS into tokens like CSS Syntax Level 3, collecting comments. */
|
|
17
|
+
export default class Tokenizer {
|
|
18
|
+
#private;
|
|
19
|
+
readonly source: string;
|
|
20
|
+
readonly comments: Comment[];
|
|
21
|
+
/** Errors of a tokenizer that recovers from them, others throw them. */
|
|
22
|
+
readonly errors: ParseError[] | null;
|
|
23
|
+
constructor(source: string, start?: number, recover?: boolean);
|
|
24
|
+
/** Every token to the end, the last one is `eof`. */
|
|
25
|
+
tokenize(): Token[];
|
|
26
|
+
next(): Token;
|
|
27
|
+
}
|
package/bin/types/html/tags.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/** Tag names Biome 2.6.
|
|
1
|
+
/** Tag names Biome 2.6.6 knows, SVG ones in camel case like `feBlend`. */
|
|
2
2
|
export declare const tagNames: ReadonlySet<string>;
|
package/bin/types/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { Program } from "./ast.js";
|
|
2
|
+
import type { Stylesheet } from "./css/ast.js";
|
|
2
3
|
import type { Root as HtmlRoot } from "./html/ast.js";
|
|
3
4
|
import type { Root } from "./markdown/ast.js";
|
|
4
5
|
import { type MarkdownOptions } from "./markdown/blocks.js";
|
|
5
6
|
import type { Options } from "./parser/base.js";
|
|
6
7
|
import type { Comment } from "./scanner.js";
|
|
7
8
|
export type * from "./ast.js";
|
|
9
|
+
export type * as Css from "./css/ast.js";
|
|
8
10
|
export type * as Html from "./html/ast.js";
|
|
9
11
|
export { tagOf } from "./html/parser.js";
|
|
10
12
|
export { default as Lines } from "./lines.js";
|
|
@@ -29,6 +31,18 @@ export declare function language(file: string): Language | undefined;
|
|
|
29
31
|
export declare function isMarkdown(file: string): boolean;
|
|
30
32
|
/** Whether a file is HTML, by its extension. */
|
|
31
33
|
export declare function isHtml(file: string): boolean;
|
|
34
|
+
/** Whether a file is CSS, by its extension. */
|
|
35
|
+
export declare function isCss(file: string): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Parses CSS into rules, at-rules, declarations and selectors, throwing a
|
|
38
|
+
* ParseError at the first error, or with `recover` leaving out items with
|
|
39
|
+
* errors and listing the errors.
|
|
40
|
+
*/
|
|
41
|
+
export declare function parseCss(source: string, options?: {
|
|
42
|
+
recover?: boolean;
|
|
43
|
+
}): Stylesheet;
|
|
44
|
+
/** Parses the component values of a range of CSS, like a `style` attribute value. */
|
|
45
|
+
export declare function parseCssValues(source: string, start?: number, end?: number): import("./css/ast.js").Value[];
|
|
32
46
|
/** Parses HTML like Biome, throwing a ParseError at the first error. */
|
|
33
47
|
export declare function parseHtml(source: string): HtmlRoot;
|
|
34
48
|
/** Parses CommonMark, with GitHub's tables, task lists and strikethrough unless `gfm` is false. */
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aljazmc/syntax",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared, dependency-free JavaScript, TypeScript, JSX, JSON, Markdown and
|
|
3
|
+
"version": "0.6.1",
|
|
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",
|
|
7
7
|
"type": "module",
|