@lithent/lithent-template-parser 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/LICENSE +21 -0
- package/README.md +159 -0
- package/dist/codegen/createElement.d.ts +10 -0
- package/dist/codegen/index.d.ts +1 -0
- package/dist/compiler.d.ts +26 -0
- package/dist/docPipe/index.d.ts +40 -0
- package/dist/docPipe/scan.d.ts +6 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.mjs +2302 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.cjs +13 -0
- package/dist/index.umd.cjs.map +1 -0
- package/dist/parser/ast.d.ts +207 -0
- package/dist/parser/lexer.d.ts +109 -0
- package/dist/parser/parser.d.ts +79 -0
- package/dist/parser/tokens.d.ts +51 -0
- package/dist/transform/directives.d.ts +30 -0
- package/dist/transform/index.d.ts +14 -0
- package/dist/transform/optimize.d.ts +21 -0
- package/package.json +53 -0
- package/src/codegen/createElement.ts +661 -0
- package/src/codegen/index.ts +1 -0
- package/src/compiler.ts +70 -0
- package/src/docPipe/index.ts +104 -0
- package/src/docPipe/scan.ts +318 -0
- package/src/index.ts +16 -0
- package/src/parser/ast.ts +478 -0
- package/src/parser/lexer.ts +561 -0
- package/src/parser/parser.ts +514 -0
- package/src/parser/tokens.ts +84 -0
- package/src/playground/main.ts +206 -0
- package/src/playground/style.css +135 -0
- package/src/shims/babel-env.ts +17 -0
- package/src/tests/compiler.test.ts +434 -0
- package/src/tests/docPipe.test.ts +41 -0
- package/src/tests/helpers.ts +96 -0
- package/src/tests/lexer.test.ts +282 -0
- package/src/tests/parser.test.ts +691 -0
- package/src/transform/directives.ts +160 -0
- package/src/transform/index.ts +29 -0
- package/src/transform/optimize.ts +203 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 superlucky84
|
|
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,159 @@
|
|
|
1
|
+
# Lithent Template Parser
|
|
2
|
+
|
|
3
|
+
Lithent Template Parser converts Lithent-specific template syntax into plain
|
|
4
|
+
JavaScript `h()` calls. The compiler powers both the standalone parser APIs and
|
|
5
|
+
any tooling that needs to transform JSX-like markup into Lithent render
|
|
6
|
+
functions.
|
|
7
|
+
|
|
8
|
+
Lithent templates look familiar to JSX but are intentionally independent. They
|
|
9
|
+
offer a concise declarative layer tuned for Lithent’s render pipeline—directives
|
|
10
|
+
(`l-if`, `l-for`), fragments, and interpolation all compile away to predictable
|
|
11
|
+
`h()` calls without relying on the JSX transform.
|
|
12
|
+
|
|
13
|
+
> **Status:** Experimental. APIs may change without notice while the parser
|
|
14
|
+
> ecosystem stabilises.
|
|
15
|
+
|
|
16
|
+
## Highlights
|
|
17
|
+
|
|
18
|
+
- **End-to-end pipeline** – lexer → parser → transformer → code generator with
|
|
19
|
+
TypeScript definitions for every stage.
|
|
20
|
+
- **Document transformer** – `transformDocument()` scans an entire source file,
|
|
21
|
+
replaces inline templates, and returns code plus optional source maps.
|
|
22
|
+
- **Directive support** – `l-if`, `l-else-if`, `l-else`, `l-for`, fragments,
|
|
23
|
+
refs, interpolations, event handlers, and boolean/static/dynamic props.
|
|
24
|
+
- **Batteries included** – over 90 Vitest specs, playground, and tree-shakable
|
|
25
|
+
exports for advanced tooling.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @lithent/lithent-template-parser
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { compile } from '@lithent/lithent-template-parser';
|
|
37
|
+
|
|
38
|
+
const template = `
|
|
39
|
+
<div class="todo-list">
|
|
40
|
+
<h2>Todos ({todos.length})</h2>
|
|
41
|
+
<ul>
|
|
42
|
+
<li l-for={(todo, index) in todos}>
|
|
43
|
+
<span class="index">{index + 1}.</span>
|
|
44
|
+
<span>{todo.text}</span>
|
|
45
|
+
</li>
|
|
46
|
+
</ul>
|
|
47
|
+
</div>
|
|
48
|
+
`;
|
|
49
|
+
|
|
50
|
+
const result = compile(template);
|
|
51
|
+
console.log(result.code);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Output:
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
h('div', { class: 'todo-list' },
|
|
58
|
+
h('h2', null, 'Todos (', todos.length, ')'),
|
|
59
|
+
h('ul', null,
|
|
60
|
+
(todos).map((todo, index) =>
|
|
61
|
+
h('li', null,
|
|
62
|
+
h('span', { class: 'index' }, index + 1, '.'),
|
|
63
|
+
h('span', null, todo.text)
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Document Transformation
|
|
71
|
+
|
|
72
|
+
The `docPipe` helpers expose a higher-level API for tooling such as the Vite
|
|
73
|
+
plugin:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { transformDocument } from '@lithent/lithent-template-parser';
|
|
77
|
+
|
|
78
|
+
const source = `
|
|
79
|
+
const View = () => (
|
|
80
|
+
<section class="profile">
|
|
81
|
+
<Avatar l-if={user.avatar} src={user.avatar} />
|
|
82
|
+
<strong>{user.name}</strong>
|
|
83
|
+
<p l-else>No avatar</p>
|
|
84
|
+
</section>
|
|
85
|
+
);
|
|
86
|
+
`;
|
|
87
|
+
|
|
88
|
+
const { code, map, transformed } = transformDocument(source, {
|
|
89
|
+
source: 'profile.ltsx',
|
|
90
|
+
generateSourceMap: true,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log(transformed); // true
|
|
94
|
+
console.log(code);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The result keeps non-template code untouched while replacing all template
|
|
98
|
+
regions with generated `h()` calls. When `generateSourceMap` is `true`, the
|
|
99
|
+
returned source map is compatible with downstream tooling (e.g. Vite, Rollup).
|
|
100
|
+
|
|
101
|
+
## Template Syntax Overview
|
|
102
|
+
|
|
103
|
+
| Feature | Example |
|
|
104
|
+
| --- | --- |
|
|
105
|
+
| HTML elements | `<div class="box">{title}</div>` |
|
|
106
|
+
| Components | `<TodoList items={todos} />` |
|
|
107
|
+
| Static attributes | `<button class="primary">` |
|
|
108
|
+
| Dynamic attributes | `<div class={className} id={nodeId}>` |
|
|
109
|
+
| Boolean attributes | `<input disabled checked />` |
|
|
110
|
+
| Event handlers | `<button onClick={handleClick}>` |
|
|
111
|
+
| Text interpolation | `Hello {user.name}!` |
|
|
112
|
+
| Conditionals | `<span l-if={done}>Done</span><span l-else>Pending</span>` |
|
|
113
|
+
| Loops | `<li l-for={(todo, i) in todos}>{i}: {todo.text}</li>` |
|
|
114
|
+
| Fragments | `<><Header /><Content /></>` |
|
|
115
|
+
|
|
116
|
+
All directives can be nested and combined. Invalid syntax produces descriptive
|
|
117
|
+
errors with line and column numbers.
|
|
118
|
+
|
|
119
|
+
## CLI & Playground
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
cd packages/lithentTemplateParser
|
|
123
|
+
pnpm dev
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The Vite playground exposes a live editor that shows the generated code, AST,
|
|
127
|
+
token stream, and the rendered Lithent component. In the browser console you can
|
|
128
|
+
call `compile()`, `transformDocument()`, or `tokenize()` directly for faster
|
|
129
|
+
experimentation.
|
|
130
|
+
|
|
131
|
+
## Architecture
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
Template String
|
|
135
|
+
↓
|
|
136
|
+
Lexer (tokenize)
|
|
137
|
+
↓
|
|
138
|
+
Parser (AST)
|
|
139
|
+
↓
|
|
140
|
+
Transform (optimise + directive handling)
|
|
141
|
+
↓
|
|
142
|
+
Codegen (h() calls)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Every stage is exported so you can plug custom transforms, inspect the AST, or
|
|
146
|
+
hook into the pipeline from other tooling.
|
|
147
|
+
|
|
148
|
+
## Testing
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
pnpm --filter @lithent/lithent-template-parser test
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Vitest covers lexing, parsing, directive expansion, code generation, and
|
|
155
|
+
document transforms.
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
MIT © Lithent contributors
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RootNode } from '../parser/ast';
|
|
2
|
+
/**
|
|
3
|
+
* Generate JavaScript code from AST
|
|
4
|
+
*/
|
|
5
|
+
export declare function generate(ast: RootNode, options?: GenerateOptions): string;
|
|
6
|
+
export interface GenerateOptions {
|
|
7
|
+
templateFactory?: string;
|
|
8
|
+
templateFragmentFactory?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function formatCode(code: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createElement';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { TransformOptions } from './transform';
|
|
2
|
+
import { GenerateOptions } from './codegen';
|
|
3
|
+
/**
|
|
4
|
+
* Compile a Lithent template to JavaScript code
|
|
5
|
+
*/
|
|
6
|
+
export declare function compile(template: string, options?: CompileOptions): CompileResult;
|
|
7
|
+
export interface CompileOptions {
|
|
8
|
+
transform?: TransformOptions;
|
|
9
|
+
generate?: GenerateOptions;
|
|
10
|
+
}
|
|
11
|
+
export interface CompileResult {
|
|
12
|
+
code: string;
|
|
13
|
+
ast: any;
|
|
14
|
+
errors: CompileError[];
|
|
15
|
+
}
|
|
16
|
+
export interface CompileError {
|
|
17
|
+
message: string;
|
|
18
|
+
line: number;
|
|
19
|
+
column: number;
|
|
20
|
+
}
|
|
21
|
+
export * from './parser/tokens';
|
|
22
|
+
export * from './parser/lexer';
|
|
23
|
+
export * from './parser/parser';
|
|
24
|
+
export * from './parser/ast';
|
|
25
|
+
export * from './transform';
|
|
26
|
+
export * from './codegen';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type SourceMap } from 'magic-string';
|
|
2
|
+
import { type CompileError, type CompileOptions } from '../compiler';
|
|
3
|
+
import { scanTemplates, type TemplateMatch } from './scan';
|
|
4
|
+
export interface DocumentTransformOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Include original source name in generated source map.
|
|
7
|
+
*/
|
|
8
|
+
source?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Emit a source map when true.
|
|
11
|
+
*/
|
|
12
|
+
generateSourceMap?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Forwarded compile options for each template block.
|
|
15
|
+
*/
|
|
16
|
+
compileOptions?: CompileOptions;
|
|
17
|
+
}
|
|
18
|
+
export interface DocumentTransformError {
|
|
19
|
+
match: TemplateMatch;
|
|
20
|
+
error: CompileError;
|
|
21
|
+
}
|
|
22
|
+
export interface DocumentTransformResult {
|
|
23
|
+
code: string;
|
|
24
|
+
map: SourceMap | null;
|
|
25
|
+
/**
|
|
26
|
+
* `true` when at least one replacement succeeded.
|
|
27
|
+
*/
|
|
28
|
+
transformed: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Errors produced while compiling templates.
|
|
31
|
+
*/
|
|
32
|
+
errors: DocumentTransformError[];
|
|
33
|
+
/**
|
|
34
|
+
* All template matches discovered in the source.
|
|
35
|
+
*/
|
|
36
|
+
matches: TemplateMatch[];
|
|
37
|
+
}
|
|
38
|
+
export declare const transformDocument: (source: string, options?: DocumentTransformOptions) => DocumentTransformResult;
|
|
39
|
+
export { scanTemplates };
|
|
40
|
+
export type { TemplateMatch };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lithent Template Parser
|
|
3
|
+
* A Vue-like template parser for Lithent
|
|
4
|
+
*/
|
|
5
|
+
export { compile } from './compiler';
|
|
6
|
+
export type { CompileOptions, CompileResult, CompileError } from './compiler';
|
|
7
|
+
export * from './parser/tokens';
|
|
8
|
+
export * from './parser/lexer';
|
|
9
|
+
export * from './parser/parser';
|
|
10
|
+
export * from './parser/ast';
|
|
11
|
+
export * from './transform';
|
|
12
|
+
export * from './codegen';
|
|
13
|
+
export * from './docPipe';
|