Skip to content

Commit e2ca1b6

Browse files
committed
feat!(core/ast): custom debugger and ast refractor
1 parent 1869a58 commit e2ca1b6

File tree

10 files changed

+60
-70
lines changed

10 files changed

+60
-70
lines changed

ast/ast.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Rule } from "./node.ts";
2+
3+
type Type = "stylesheet";
4+
5+
export interface AST {
6+
type: Type;
7+
stylesheet: {
8+
rules: Rule[];
9+
};
10+
}

ast/mod.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./ast.ts"
2+
export * from "./node.ts"
3+
export * from "./token.ts"

ast/node.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Rule {
2+
type: "rule";
3+
selectors: string[];
4+
declarations: Decl[];
5+
}
6+
7+
export interface Decl {
8+
type?: string;
9+
name?: string;
10+
value?: string;
11+
}

ast/token.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface Position {
2+
line: number;
3+
col: number;
4+
}
5+
6+
export interface Token {
7+
type?: string;
8+
start?: Position;
9+
end?: Position;
10+
position?: {
11+
start?: Position;
12+
end?: Position;
13+
};
14+
prefix?: string;
15+
name?: string;
16+
text?: string;
17+
value?: string;
18+
}

lexer/lexer.ts renamed to core/lexer/lexer.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
var DEBUG = false; // `true` to print debugging info.
2-
var TIMER = false; // `true` to time calls to `lex()` and print the results.
3-
4-
import dbg from "../debug.js";
5-
import { Token } from "../types.ts";
1+
import dbg from "../../debug.js";
2+
import { Token } from "../../types.ts";
63

74
let debug = dbg("lex");
85

@@ -187,7 +184,7 @@ export function lex(css: string): Token[] {
187184
col: column,
188185
};
189186

190-
DEBUG && debug("addToken:", JSON.stringify(token, null, 2));
187+
debug("addToken:", JSON.stringify(token, null, 2));
191188

192189
tokens.push(token);
193190

@@ -222,10 +219,10 @@ export function lex(css: string): Token[] {
222219
since the total number of cases is very low.
223220
*/
224221

225-
TIMER && (start = Date.now());
222+
start = Date.now();
226223

227224
while (ch = getCh()) {
228-
DEBUG && debug(ch, getState());
225+
debug(ch, getState());
229226

230227
// column += 1;
231228

@@ -686,7 +683,7 @@ export function lex(css: string): Token[] {
686683
}
687684
}
688685

689-
TIMER && debug("ran in", (Date.now() - start) + "ms");
686+
debug("ran in", (Date.now() - start) + "ms");
690687

691688
return tokens;
692689
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
var DEBUG = false; // `true` to print debugging info.
2-
var TIMER = false; // `true` to time calls to `parse()` and print the results.
3-
4-
import dbg from "../debug.js";
1+
import dbg from "../../debug.js";
52
let debug = dbg("parse");
63

74
import { lex } from "../lexer/lexer.ts";
8-
import { AST, Token } from "../types.ts";
5+
import { AST, Token } from "../../types.ts";
96

107
var _comments: boolean; // Whether comments are allowed.
118
var _depth: number; // Current block nesting depth.
@@ -36,14 +33,14 @@ export function parse(css: string | any[], options: any): AST {
3633
var rules = [];
3734
var token;
3835

39-
TIMER && (start = Date.now());
36+
start = Date.now();
4037

4138
while ((token = next())) {
4239
rule = parseToken(token);
4340
rule && rules.push(rule);
4441
}
4542

46-
TIMER && debug("ran in", (Date.now() - start) + "ms");
43+
debug("ran in", (Date.now() - start) + "ms");
4744

4845
return {
4946
type: "stylesheet",
@@ -95,7 +92,7 @@ function astNode(token: Token, overrd?: any): Token {
9592
};
9693
}
9794

98-
DEBUG && debug("astNode:", JSON.stringify(node, null, 2));
95+
debug("astNode:", JSON.stringify(node, null, 2));
9996

10097
return node;
10198
}
@@ -107,7 +104,7 @@ function astNode(token: Token, overrd?: any): Token {
107104
*/
108105
function next() {
109106
var token = _tokens.shift();
110-
DEBUG && debug("next:", JSON.stringify(token, null, 2));
107+
debug("next:", JSON.stringify(token, null, 2));
111108
return token;
112109
}
113110

@@ -249,7 +246,7 @@ function parseToken(token: any): any {
249246
return parseAtGroup(token);
250247
}
251248

252-
DEBUG && debug("parseToken: unexpected token:", JSON.stringify(token));
249+
debug("parseToken: unexpected token:", JSON.stringify(token));
253250
}
254251

255252
// -- Parse Helper Functions ---------------------------------------------------

debug.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
function debug(label) {
2-
return _debug.bind(null, label);
2+
return _debug.bind(null, label);
33
}
44

55
function _debug(label) {
6+
if (!window.DEBUG) return;
67
var args = [].slice.call(arguments, 1);
78
args.unshift("[" + label + "]");
89
Deno.core.print(args.join(" ") + "\n");

example.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { parse } from "./core/parser/parser.ts";
2+
var ast = parse("p { color: black; }", {});
3+
console.log(ast);

test.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

types.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)