@cssints/css-grammar 0.0.0-stage → 0.1.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.
package/dist/token.mjs ADDED
@@ -0,0 +1,112 @@
1
+ //#region src/token.ts
2
+ /**
3
+ * Per token type, by the name of `token.<name>`:
4
+ * - `syntax`: the `@property` syntax, `<syntax>`, checked by the twin and by `ValidSyntax`;
5
+ * - `sentinels`: the typed-value types the token stands for in a property value (`⟨length⟩`, api-typed.ts);
6
+ * - `probes`: literals of the type that a build-time check puts in place of the token (one per sentinel), since the
7
+ * recognizer reads text and a token is `var(--name)`;
8
+ * - `grammar`: the grammar of a value when it is not `<syntax>` (a list of shadows);
9
+ * - `property`: the `@property` syntax when `<syntax>` is not one `@property` takes: `*`, any value, which a browser
10
+ * does not check or animate (an easing function, a shadow).
11
+ */
12
+ const TOKEN_TYPES = {
13
+ color: {
14
+ syntax: "color",
15
+ sentinels: ["color"],
16
+ probes: ["red"]
17
+ },
18
+ length: {
19
+ syntax: "length",
20
+ sentinels: ["length"],
21
+ probes: ["1px"]
22
+ },
23
+ percentage: {
24
+ syntax: "percentage",
25
+ sentinels: ["percentage"],
26
+ probes: ["1%"]
27
+ },
28
+ lengthPercentage: {
29
+ syntax: "length-percentage",
30
+ sentinels: ["length", "percentage"],
31
+ probes: ["1px", "1%"]
32
+ },
33
+ number: {
34
+ syntax: "number",
35
+ sentinels: ["number"],
36
+ probes: ["1"]
37
+ },
38
+ integer: {
39
+ syntax: "integer",
40
+ sentinels: ["integer"],
41
+ probes: ["1"]
42
+ },
43
+ angle: {
44
+ syntax: "angle",
45
+ sentinels: ["angle"],
46
+ probes: ["1deg"]
47
+ },
48
+ time: {
49
+ syntax: "time",
50
+ sentinels: ["time"],
51
+ probes: ["1s"]
52
+ },
53
+ customIdent: {
54
+ syntax: "custom-ident",
55
+ sentinels: ["custom-ident"],
56
+ probes: ["x"]
57
+ },
58
+ resolution: {
59
+ syntax: "resolution",
60
+ sentinels: ["resolution"],
61
+ probes: ["1dppx"]
62
+ },
63
+ url: {
64
+ syntax: "url",
65
+ sentinels: ["url"],
66
+ probes: ["url(x)"]
67
+ },
68
+ image: {
69
+ syntax: "image",
70
+ sentinels: ["image"],
71
+ probes: ["linear-gradient(red, blue)"]
72
+ },
73
+ transformList: {
74
+ syntax: "transform-list",
75
+ sentinels: ["transform-list"],
76
+ probes: ["translateX(1px)"]
77
+ },
78
+ easingFunction: {
79
+ syntax: "easing-function",
80
+ sentinels: ["easing-function"],
81
+ probes: ["ease"],
82
+ property: "*"
83
+ },
84
+ shadow: {
85
+ syntax: "shadow",
86
+ sentinels: ["shadow"],
87
+ probes: ["0 0 red"],
88
+ grammar: "<shadow>#",
89
+ property: "*"
90
+ }
91
+ };
92
+ /**
93
+ * The grammar of a value of a token type.
94
+ * @param {string} syntax the `syntax` of the token type
95
+ * @returns {string} `<syntax>`, or the type's `grammar`
96
+ */
97
+ const grammarOf = (syntax) => Object.values(TOKEN_TYPES).find((t) => t.syntax === syntax)?.grammar ?? `<${syntax}>`;
98
+ /**
99
+ * DTCG `$type` to token type (createTokens, docs/API.md "Themes"). `fontWeight` is a number: its keywords become numbers.
100
+ * The other DTCG types (`fontFamily`, `strokeStyle`, `border`, `transition`, `gradient`, `typography`) are an error.
101
+ */
102
+ const DTCG_TYPES = {
103
+ color: "color",
104
+ dimension: "length",
105
+ number: "number",
106
+ duration: "time",
107
+ fontWeight: "number",
108
+ cubicBezier: "easingFunction",
109
+ shadow: "shadow"
110
+ };
111
+ //#endregion
112
+ export { DTCG_TYPES, TOKEN_TYPES, grammarOf };
@@ -0,0 +1,48 @@
1
+ //#region src/twin-core.d.ts
2
+ type Tok = {
3
+ t: "type";
4
+ v: string;
5
+ lo?: string;
6
+ hi?: string;
7
+ } | {
8
+ t: "prop";
9
+ v: string;
10
+ } | {
11
+ t: "kw";
12
+ v: string;
13
+ } | {
14
+ t: "fn";
15
+ v: string;
16
+ } | {
17
+ t: "range";
18
+ lo: string;
19
+ hi: string;
20
+ } | {
21
+ t: "p";
22
+ v: string;
23
+ } | {
24
+ t: "lit";
25
+ v: string;
26
+ } | {
27
+ t: "eof";
28
+ };
29
+ export declare function next(s: string): [Tok, string];
30
+ export type Node = ["kw", string] | ["type", string, string?, string?] | ["prop", string] | ["fn", string, Node] | ["lit", string] | ["alt", Node[]] | ["or", Node[]] | ["and", Node[]] | ["seq", Node[]] | ["opt", Node] | ["star", Node] | ["plus", Node] | ["list", Node] | ["rep", Node, string, string] | ["bang", Node];
31
+ export declare function parseGrammar(s: string): Node;
32
+ export declare const spaced: (q: string) => string;
33
+ export declare const isNative: (name: string) => boolean;
34
+ interface Ctx {
35
+ len: number;
36
+ far: number;
37
+ expected: Set<string>;
38
+ pend?: boolean;
39
+ ch?: readonly string[];
40
+ why?: [at: number, text: string];
41
+ fix?: [at: number, written: string, canonical: string];
42
+ }
43
+ export declare function consume(n: Node, s: string, ctx: Ctx, label?: string): Set<string>;
44
+ export declare const magnitude: (t: string) => number;
45
+ export declare const nullable: (n: Node) => boolean;
46
+ export declare function matches(syntax: string, s: string, pending?: boolean): boolean;
47
+ export declare function valid(p: string, s: string, syntax?: string | undefined): string;
48
+ //#endregion