Commit f9d44d66 authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(parser): new event `token` emitted for each lexer token

parent e6c1df31
......@@ -18,6 +18,18 @@ title: Events
Emitted after after configuration is ready but before DOM is initialized.
### `token`
```typescript
interface TokenEvent {
location: Location;
type: TokenType;
data?: any;
}
```
Emitted for each lexer token during parsing.
## Document
### `dom:load`
......
......@@ -95,10 +95,20 @@ export class Engine<T extends Parser = Parser> {
return this.report.save(sources);
}
/**
* Returns a list of all events generated while parsing the source.
*
* For verbosity, token events are ignored (use [[dumpTokens]] to inspect
* token stream).
*/
public dumpEvents(source: Source[]): EventDump[] {
const parser = this.instantiateParser();
const lines: EventDump[] = [];
parser.on("*", (event, data) => {
/* ignore token events as it becomes to verbose */
if (event === "token") {
return;
}
lines.push({ event, data });
});
source.forEach((src) => parser.parseHtml(src));
......
import { ConfigData } from "../config";
import { Location } from "../context";
import { DOMTree, DynamicValue, HtmlElement } from "../dom";
import { TokenType } from "../lexer";
import { Rule } from "../rule";
/**
......@@ -19,6 +20,14 @@ export interface ConfigReadyEvent extends Event {
rules: { [ruleId: string]: Rule };
}
/**
* Token event.
*/
export interface TokenEvent extends Event {
type: TokenType;
data?: any;
}
/**
* Event emitted when starting tags are encountered.
*/
......@@ -166,6 +175,7 @@ export interface DOMReadyEvent extends Event {
export interface TriggerEventMap {
"config:ready": ConfigReadyEvent;
token: TokenEvent;
"tag:start": TagStartEvent;
"tag:end": TagEndEvent;
"tag:ready": TagReadyEvent;
......@@ -181,6 +191,7 @@ export interface TriggerEventMap {
export interface ListenEventMap {
"config:ready": ConfigReadyEvent;
token: TokenEvent;
"tag:open": TagOpenEvent;
"tag:start": TagStartEvent;
"tag:close": TagCloseEvent;
......
......@@ -43,7 +43,7 @@ class ExposedParser extends Parser {
}
describe("parser", () => {
const ignoredEvents = ["dom:load", "dom:ready", "whitespace"];
const ignoredEvents = ["dom:load", "dom:ready", "token", "whitespace"];
let events: any[];
let parser: ExposedParser;
......
......@@ -484,7 +484,16 @@ export class Parser {
}
private next(tokenStream: TokenStream): IteratorResult<Token> {
return tokenStream.next();
const it = tokenStream.next();
if (!it.done) {
const token = it.value;
this.trigger("token", {
location: token.location,
type: token.type,
data: token.data ? Array.from(token.data) : undefined,
});
}
return it;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment