Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,16 @@ walk(ast, (node) => {
})
```

### `tokenize_cb(source, on_token)`
### `tokenize(source, on_token)`

Calls `on_token(type, start, end, line, column)` for every token. Whitespace and comments are emitted as tokens (`TOKEN_WHITESPACE`, `TOKEN_COMMENT`). This is about 35% faster than iterating `tokenize()`, because no generator or token objects are allocated.
Calls `on_token(type, start, end, line, column)` for every token. Whitespace and comments are emitted as tokens (`TOKEN_WHITESPACE`, `TOKEN_COMMENT`).

```typescript
tokenize_cb('a { color: red }', (type, start, end, line, column) => {
tokenize('a { color: red }', (type, start, end, line, column) => {
console.log(type, start, end)
})
```

In 0.20.0 this becomes `tokenize()`, replacing the iterator form below.

### `tokenize(source, on_comment?)` (deprecated)

A generator of tokens; comments are skipped and reported through `on_comment`. Deprecated: use `tokenize_cb()` now, it becomes `tokenize()` in 0.20.0.

```typescript
for (const token of tokenize('a { color: red }')) {
console.log(token.type, token.start, token.end)
}
```

### Node types, type guards and `clone()`

Type guards such as `is_declaration()` narrow a node to its specific type. `node.clone({ deep?, locations? })` returns a mutable plain-object copy of a node.
Expand Down
9 changes: 1 addition & 8 deletions benchmark/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
parse_value,
parse_dimension,
tokenize,
tokenize_cb,
walk,
traverse,
STYLE_RULE,
Expand All @@ -35,13 +34,7 @@ for (const [name, file] of Object.entries(files)) {
const css = fs.readFileSync(path.resolve('benchmark', file), 'utf-8')

bench.add(`tokenize|${name}`, () => {
for (const _token of tokenize(css)) {
// iterate
}
})

bench.add(`tokenize_cb|${name}`, () => {
tokenize_cb(css, () => {})
tokenize(css, () => {})
})

bench.add(`parse|${name}`, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { parse_atrule_prelude } from './parse-atrule-prelude'
export { parse_declaration } from './parse-declaration'
export { parse_value } from './parse-value'
export { parse_dimension } from './parse-dimension'
export { tokenize, tokenize_cb } from './tokenize'
export { tokenize } from './tokenize'
export { walk, traverse, walk_declarations, walk_selectors, walk_values, SKIP, BREAK } from './walk'
export {
is_custom,
Expand Down
20 changes: 5 additions & 15 deletions src/tokenize.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from 'vitest'
import { Lexer, tokenize, tokenize_cb } from './tokenize'
import { Lexer, tokenize } from './tokenize'
import { is_ident_start } from './char-types'
import {
TOKEN_IDENT,
Expand Down Expand Up @@ -833,20 +833,10 @@ describe('Lexer', () => {
})
})

describe('tokenize_cb', () => {
test('reports the same tokens as the iterator tokenize()', () => {
const css = 'a { color: red; margin: 0 }'
const from_cb: Token[] = []
tokenize_cb(css, (type, start, end, line, column) => {
from_cb.push({ type, start, end, line, column })
})
expect(from_cb.length).toBeGreaterThan(0)
expect(from_cb).toEqual(Array.from(tokenize(css)))
})

describe('tokenize', () => {
test('emits comments as TOKEN_COMMENT with position', () => {
const tokens: Token[] = []
tokenize_cb('a{/* hi */}', (type, start, end, line, column) =>
tokenize('a{/* hi */}', (type, start, end, line, column) =>
tokens.push({ type, start, end, line, column }),
)
const comments = tokens.filter((t) => t.type === TOKEN_COMMENT)
Expand All @@ -861,7 +851,7 @@ describe('tokenize_cb', () => {

test('tracks lines across multi-line comments', () => {
const tokens: Token[] = []
tokenize_cb('/* a\nb */ x', (type, start, end, line, column) =>
tokenize('/* a\nb */ x', (type, start, end, line, column) =>
tokens.push({ type, start, end, line, column }),
)
expect(tokens.at(-1)).toMatchObject({ type: TOKEN_IDENT, line: 2, column: 6 })
Expand All @@ -875,7 +865,7 @@ describe('tokenize_cb', () => {

test('calls nothing for empty source', () => {
let calls = 0
tokenize_cb('', () => calls++)
tokenize('', () => calls++)
expect(calls).toBe(0)
})
})
32 changes: 1 addition & 31 deletions src/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,43 +1118,13 @@ export type OnToken = (

/**
* Tokenize CSS source code, calling `on_token` for every token.
* Faster than the iterator-based `tokenize()` because it allocates no generator or token objects.
* @param source - The CSS source code to tokenize
* @param on_token - Called with `(type, start, end, line, column)` for each token
* @deprecated Renamed to `tokenize()` in 0.20.0, where the callback form replaces the iterator form.
*/
export function tokenize_cb(source: string, on_token: OnToken): void {
export function tokenize(source: string, on_token: OnToken): void {
const lexer = new Lexer(source, undefined, true)
let type: TokenType
while ((type = lexer.next_token()) !== TOKEN_EOF) {
on_token(type, lexer.token_start, lexer.token_end, lexer.token_line, lexer.token_column)
}
}

/**
* Tokenize CSS source code
* @param source - The CSS source code to tokenize
* @param on_comment - Optional callback for comment tokens
* @yields CSS tokens
* @deprecated The iterator form is replaced in 0.20.0 by a callback-based `tokenize(source, on_token)`. Use `tokenize_cb()` today for the same behavior.
*/
export function* tokenize(
source: string,
on_comment?: (info: CommentInfo) => void,
): Generator<Token, void, undefined> {
const lexer = new Lexer(source, on_comment)

while (true) {
const type = lexer.next_token()
if (type === TOKEN_EOF) {
break
}
yield {
type,
start: lexer.token_start,
end: lexer.token_end,
line: lexer.token_line,
column: lexer.token_column,
}
}
}
Loading