Skip to content

Commit edb241f

Browse files
committed
feat: lexer types
1 parent 1e8707b commit edb241f

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# css-parser
2-
CSS3 Parser implementation for Deno
1+
# `css_parser`
2+
CSS Lexer & Parser implementation for Deno

debug.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function debug(label) {
55
function _debug(label) {
66
var args = [].slice.call(arguments, 1);
77
args.unshift('[' + label + ']');
8-
process.stderr.write(args.join(' ') + '\n');
8+
Deno.core.print(args.join(' ') + '\n');
99
}
1010

1111
export default debug;

lexer.js renamed to lexer.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default lex;
1313
* @param {String} css CSS
1414
* @returns {Array} lexical tokens
1515
*/
16-
function lex(css) {
17-
var start; // Debug timer start.
16+
function lex(css: string) {
17+
var start = 0; // Debug timer start.
1818

1919
var buffer = ''; // Character accumulator
2020
var ch; // Current character
@@ -24,11 +24,11 @@ function lex(css) {
2424
var line = 1; // Current source line number
2525
var state = 'before-selector'; // Current state
2626
var stack = [state]; // State stack
27-
var token = {}; // Current token
28-
var tokens = []; // Token accumulator
27+
var token: any = {}; // Current token
28+
var tokens: any[] = []; // Token accumulator
2929

3030
// Supported @-rules, in roughly descending order of usage probability.
31-
var atRules = [
31+
var atRules: any = [
3232
'media',
3333
'keyframes',
3434
{ name: '-webkit-keyframes', type: 'keyframes', prefix: '-webkit-' },
@@ -65,7 +65,7 @@ function lex(css) {
6565
* @param {Number} [index=0] Index to return.
6666
* @returns {String} state
6767
*/
68-
function getState(index) {
68+
function getState(index?: number): string {
6969
return index ? stack[stack.length - 1 - index] : state;
7070
}
7171

@@ -76,7 +76,7 @@ function lex(css) {
7676
* @param {String} str The string to look for.
7777
* @returns {Boolean} Whether the string was found.
7878
*/
79-
function isNextString(str) {
79+
function isNextString(str: string): boolean {
8080
var start = cursor + 1;
8181
return (str === css.slice(start, start + str.length));
8282
}
@@ -88,7 +88,7 @@ function lex(css) {
8888
* @param {String} str The substring to look for.
8989
* @returns {Number|false} The position, or `false` if not found.
9090
*/
91-
function find(str) {
91+
function find(str: string): number | boolean {
9292
var pos = css.slice(cursor).indexOf(str);
9393

9494
return pos > 0 ? pos : false;
@@ -100,7 +100,7 @@ function lex(css) {
100100
* @param {String} ch Character.
101101
* @returns {Boolean} Whether the character is next.
102102
*/
103-
function isNextChar(ch) {
103+
function isNextChar(ch: string): boolean {
104104
return ch === peek(1);
105105
}
106106

@@ -111,7 +111,7 @@ function lex(css) {
111111
* @param {Number} [offset=1] Cursor offset.
112112
* @returns {String} Character.
113113
*/
114-
function peek(offset) {
114+
function peek(offset: number): string {
115115
return css[cursor + (offset || 1)];
116116
}
117117

@@ -120,7 +120,7 @@ function lex(css) {
120120
*
121121
* @returns {String} The removed state.
122122
*/
123-
function popState() {
123+
function popState(): string | undefined {
124124
var removed = stack.pop();
125125
state = stack[stack.length - 1];
126126

@@ -133,7 +133,7 @@ function lex(css) {
133133
* @param {String} newState The new state.
134134
* @returns {Number} The new stack length.
135135
*/
136-
function pushState(newState) {
136+
function pushState(newState: string): number {
137137
state = newState;
138138
stack.push(state);
139139

@@ -146,7 +146,7 @@ function lex(css) {
146146
* @param {String} newState The new state.
147147
* @returns {String} The replaced state.
148148
*/
149-
function replaceState(newState) {
149+
function replaceState(newState: string): string {
150150
var previousState = state;
151151
stack[stack.length - 1] = state = newState;
152152

@@ -159,7 +159,7 @@ function lex(css) {
159159
*
160160
* @param {Number} [n=1] Number of characters to skip.
161161
*/
162-
function skip(n) {
162+
function skip(n?: number) {
163163
if ((n || 1) == 1) {
164164
if (css[cursor] == '\n') {
165165
line++;
@@ -169,13 +169,13 @@ function lex(css) {
169169
}
170170
cursor++;
171171
} else {
172-
var skipStr = css.slice(cursor, cursor + n).split('\n');
172+
var skipStr = css.slice(cursor, cursor + (n || 0)).split('\n');
173173
if (skipStr.length > 1) {
174174
line += skipStr.length - 1;
175175
column = 1;
176176
}
177177
column += skipStr[skipStr.length - 1].length;
178-
cursor = cursor + n;
178+
cursor = cursor + (n || 0);
179179
}
180180
}
181181

@@ -201,7 +201,7 @@ function lex(css) {
201201
*
202202
* @param {String} type Token type.
203203
*/
204-
function initializeToken(type) {
204+
function initializeToken(type: any) {
205205
token = {
206206
type: type,
207207
start: {
@@ -530,7 +530,7 @@ function lex(css) {
530530
// difficult to represent in the AST.
531531
var pos = find('*/');
532532

533-
if (pos) {
533+
if (pos && typeof pos !== "boolean") {
534534
skip(pos + 1);
535535
}
536536
} else {

test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parse from "./parser.js";
1+
import parse from "./lexer.ts";
22

33
var ast = parse('p { color: black; }');
44
console.log(ast)

0 commit comments

Comments
 (0)