@jesscss/jess-parser 2.0.0-alpha.9
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/LICENSE +21 -0
- package/README.md +121 -0
- package/lib/ast/grammar.d.ts +1 -0
- package/lib/cst.cjs +14 -0
- package/lib/cst.d.ts +5 -0
- package/lib/cst.js +12 -0
- package/lib/grammar.cjs +31734 -0
- package/lib/grammar.d.ts +1 -0
- package/lib/grammar.js +31733 -0
- package/lib/index.cjs +35327 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +35322 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Matthew Dean
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @jesscss/jess-parser
|
|
2
|
+
|
|
3
|
+
A parser for the [Jess](https://github.com/jesscss/jess) language, built on [parseman](https://www.npmjs.com/package/parseman). Jess is CSS extended with `$variables`, `$(…)` arithmetic, mixins, `@-compose`/`@-from` imports, and control flow (`$if`/`$for`/`$while`).
|
|
4
|
+
|
|
5
|
+
Two parser representations are available:
|
|
6
|
+
|
|
7
|
+
- **Canonical AST v2** — the default `parse()` entry constructs a `Stylesheet`
|
|
8
|
+
directly through parser-local Parseman reductions.
|
|
9
|
+
- **Explicit CST** — the `./cst` entry has **no dependency on
|
|
10
|
+
`@jesscss/core`** and parses Jess source text into a concrete syntax tree for
|
|
11
|
+
language-service/document consumers.
|
|
12
|
+
|
|
13
|
+
> **Status:** the direct AST v2 parser is in active feature closure. `parse()`
|
|
14
|
+
> either returns a complete canonical `Stylesheet` or rejects the source; it does not
|
|
15
|
+
> silently substitute an empty stylesheet for unsupported input.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install @jesscss/jess-parser
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`@jesscss/core` is an **optional** peer dependency — needed for the default
|
|
24
|
+
AST v2 `parse()` entry, not for `./cst` or `./grammar`.
|
|
25
|
+
Those explicit entries expose Parseman types and grammar values, so consumers
|
|
26
|
+
of them must also provide the package's `parseman` peer.
|
|
27
|
+
|
|
28
|
+
## Canonical AST parsing
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { parse } from '@jesscss/jess-parser'
|
|
32
|
+
|
|
33
|
+
const stylesheet = parse('$brand: #3366ff;')
|
|
34
|
+
|
|
35
|
+
stylesheet.type // 'Stylesheet'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Standalone usage (core-free)
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
import { parseJessCst } from '@jesscss/jess-parser/cst'
|
|
42
|
+
// or: import { parseJessCst } from '@jesscss/jess-parser'
|
|
43
|
+
|
|
44
|
+
const result = parseJessCst('$brand: #3366ff;')
|
|
45
|
+
|
|
46
|
+
result.ok // true
|
|
47
|
+
result.errors // ParseError[] (empty when ok)
|
|
48
|
+
result.unconsumedFrom // index of first unparsed char, or null
|
|
49
|
+
result.tree // the CST root (a StyleSheet node)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Signature:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
parseJessCst(input: string, startRule = 'Stylesheet', options?: { collapse?: boolean }): JessCstParseResult
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Pass a different `startRule` (any capitalized grammar rule) to parse a fragment.
|
|
59
|
+
|
|
60
|
+
## Public API
|
|
61
|
+
|
|
62
|
+
| Entry | Export | Purpose |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| `@jesscss/jess-parser` (`.`) | `parse`, `JessParseError` | Parse Jess directly to canonical AST v2 `Stylesheet`; malformed input throws `JessParseError` with an offset and expected facts. |
|
|
65
|
+
| `@jesscss/jess-parser` (`.`) | `parseJessCst`, `jessGrammar` | Convenience exports for the explicit language-service CST surface. |
|
|
66
|
+
| `@jesscss/jess-parser` (`.`) | `JessCstNode`, `JessCstLeaf`, `JessCstError`, `JessCstChild`, `JessCstParseResult`, `JessCstType` (types) | CST type definitions (aliases of the shared `@jesscss/css-parser/cst` types). |
|
|
67
|
+
| `@jesscss/jess-parser/cst` | `parseJessCst`, CST types | Same core-free CST parser (explicit subpath). |
|
|
68
|
+
| `@jesscss/jess-parser/grammar` | `jessGrammar` | The explicit CST/language-service grammar rule map. It is not the production compiler parser route. |
|
|
69
|
+
|
|
70
|
+
## Default CST shape
|
|
71
|
+
|
|
72
|
+
The CST is parseman's, produced by the shared `cssCstBuildHost` (from `@jesscss/css-parser`). Three kinds of node:
|
|
73
|
+
|
|
74
|
+
- **node** — `{ _tag: 'node', type, grammarType, span: { start, end }, state, children }` (`grammarType` = raw rule name; `type` = friendly public name).
|
|
75
|
+
- **leaf** — `{ _tag: 'leaf', value, span }` for terminals.
|
|
76
|
+
- **error** — `{ _tag: 'error', type, span, expected, children, state }` where recovery happened.
|
|
77
|
+
|
|
78
|
+
Spans are `[start, end)` offsets; whitespace and comments are trivia and do not appear as children.
|
|
79
|
+
|
|
80
|
+
Parsing `$brand: #3366ff;` yields:
|
|
81
|
+
|
|
82
|
+
```jsonc
|
|
83
|
+
{
|
|
84
|
+
"_tag": "node", "type": "StyleSheet", "grammarType": "Stylesheet", "span": { "start": 0, "end": 16 },
|
|
85
|
+
"children": [
|
|
86
|
+
{ "_tag": "node", "type": "VarDeclaration", "grammarType": "VarDeclaration", "span": { "start": 0, "end": 16 },
|
|
87
|
+
"children": [
|
|
88
|
+
{ "_tag": "leaf", "value": "$brand", "span": { "start": 0, "end": 6 } },
|
|
89
|
+
{ "_tag": "leaf", "value": ":", "span": { "start": 6, "end": 7 } },
|
|
90
|
+
{ "_tag": "node", "type": "Color", "grammarType": "Color", "span": { "start": 8, "end": 15 },
|
|
91
|
+
"children": [ { "_tag": "leaf", "value": "#3366ff", "span": { "start": 8, "end": 15 } } ] },
|
|
92
|
+
{ "_tag": "leaf", "value": ";", "span": { "start": 15, "end": 16 } }
|
|
93
|
+
] }
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Jess-specific grammar rules the delta adds include `VarDeclaration` (`$x: …`), `Reference` (`$x`, `$x.prop`, `$x[0]`), `Expression` / `Operation` / `Condition` (inside `$(…)`), `Mixin`, `MixinCall`, `InterpolatedSelector` (`.widget-$[side]`), and the `@-compose`/`@-export`/`@-from`/`@-use` import at-rules.
|
|
99
|
+
|
|
100
|
+
Pass `{ collapse: true }` to unwrap single-child wrapper types (`Reference`, `NamedColor`, `InterpolatedSelector`) into their child.
|
|
101
|
+
|
|
102
|
+
### Current parse coverage
|
|
103
|
+
|
|
104
|
+
`parse()` is deliberately strict: it returns a complete AST v2 `Stylesheet` only for
|
|
105
|
+
input represented by its direct grammar. Use the explicit CST result's
|
|
106
|
+
`unconsumedFrom` field when a language-service consumer needs partial-parse
|
|
107
|
+
diagnostics.
|
|
108
|
+
|
|
109
|
+
## Compiler boundary
|
|
110
|
+
|
|
111
|
+
Production Jess parsing is `parse()` or `@jesscss/plugin-jess` through a
|
|
112
|
+
`Compiler`/`Context`. The direct grammar reductions construct canonical AST facts
|
|
113
|
+
themselves; there is no BuilderHost, parser action registry, CST-to-AST bridge, or
|
|
114
|
+
second parse route. `jessGrammar` and `parseJessCst()` are retained only for
|
|
115
|
+
explicit language-service/document consumers that need CST fidelity. Published
|
|
116
|
+
parser packages expose only their macro-compiled `lib` artifacts, so a consumer
|
|
117
|
+
does not need the workspace-private recognition package.
|
|
118
|
+
|
|
119
|
+
## Part of Jess
|
|
120
|
+
|
|
121
|
+
This package is developed as part of [Jess](https://github.com/jesscss/jess). It shares its CSS base and CST machinery with `@jesscss/css-parser`, `@jesscss/less-parser`, and `@jesscss/scss-parser`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const jessAstGrammar: Record<string, import("parseman").FusedRule>;
|
package/lib/cst.cjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_grammar = require("./grammar.cjs");
|
|
3
|
+
let _jesscss_css_parser_cst = require("@jesscss/css-parser/cst");
|
|
4
|
+
//#region src/cst.ts
|
|
5
|
+
function parseJessCst(input, startRule = "Stylesheet", options) {
|
|
6
|
+
return (0, _jesscss_css_parser_cst.parseCst)(require_grammar.jessGrammar, input, startRule, options);
|
|
7
|
+
}
|
|
8
|
+
/** Incremental (`.edit()`-able) Jess document — mirrors `parseLessDoc`/`parseScssDoc`. */
|
|
9
|
+
function parseJessDoc(input, startRule = "Stylesheet") {
|
|
10
|
+
return (0, _jesscss_css_parser_cst.parseDocCst)(require_grammar.jessGrammar, input, startRule);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
exports.parseJessCst = parseJessCst;
|
|
14
|
+
exports.parseJessDoc = parseJessDoc;
|
package/lib/cst.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type CssCstNode, type CssCstParseOptions, type CssCstParseResult, type ParseDoc } from '@jesscss/css-parser/cst';
|
|
2
|
+
export declare function parseJessCst(input: string, startRule?: string, options?: CssCstParseOptions): CssCstParseResult;
|
|
3
|
+
/** Incremental (`.edit()`-able) Jess document — mirrors `parseLessDoc`/`parseScssDoc`. */
|
|
4
|
+
export declare function parseJessDoc(input: string, startRule?: string): ParseDoc<CssCstNode>;
|
|
5
|
+
export type { CssCstChild as JessCstChild, CssCstError as JessCstError, CssCstLeaf as JessCstLeaf, CssCstNode as JessCstNode, CssCstParseOptions as JessCstParseOptions, CssCstParseResult as JessCstParseResult, CssCstType as JessCstType } from '@jesscss/css-parser/cst';
|
package/lib/cst.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jessGrammar } from "./grammar.js";
|
|
2
|
+
import { parseCst, parseDocCst } from "@jesscss/css-parser/cst";
|
|
3
|
+
//#region src/cst.ts
|
|
4
|
+
function parseJessCst(input, startRule = "Stylesheet", options) {
|
|
5
|
+
return parseCst(jessGrammar, input, startRule, options);
|
|
6
|
+
}
|
|
7
|
+
/** Incremental (`.edit()`-able) Jess document — mirrors `parseLessDoc`/`parseScssDoc`. */
|
|
8
|
+
function parseJessDoc(input, startRule = "Stylesheet") {
|
|
9
|
+
return parseDocCst(jessGrammar, input, startRule);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { parseJessCst, parseJessDoc };
|