Skip to content

Commit

Permalink
(chore) split out emitter from TokenTree
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Feb 29, 2020
1 parent b516458 commit 1b820c0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ https://highlightjs.org/
*/

import deepFreeze from './vendor/deep_freeze';
import TokenTree from './lib/token_tree';
import TokenTreeEmitter from './lib/token_tree';
import HTMLRenderer from './lib/html_renderer';
import * as regex from './lib/regex';
import * as utils from './lib/utils';
Expand Down Expand Up @@ -322,7 +322,7 @@ const HLJS = function(hljs) {
var top = continuation || language;
var continuations = {}; // keep continuations for sub-languages
var result;
var emitter = new TokenTree();
var emitter = new TokenTreeEmitter();
processContinuations();
var mode_buffer = '';
var relevance = 0;
Expand Down Expand Up @@ -395,7 +395,7 @@ const HLJS = function(hljs) {
languageSubset = languageSubset || options.languages || Object.keys(languages);
var result = {
relevance: 0,
emitter: new TokenTree(),
emitter: new TokenTreeEmitter(),
value: escape(code)
};
var second_best = result;
Expand Down
77 changes: 49 additions & 28 deletions src/lib/token_tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default class TokenTree {
class TokenTree {
constructor() {
this.rootNode = { children: [] };
this.stack = [ this.rootNode ];
Expand All @@ -8,31 +8,12 @@ export default class TokenTree {
return this.stack[this.stack.length - 1];
}

get root() { return this.rootNode };

add(node) {
this.top.children.push(node);
}

addKeyword(text, kind) {
if (text === "") { return; }

this.openNode(kind);
this.addText(text);
this.closeNode();
}

addText(text) {
if (text === "") { return; }

this.add(text);
}

addSublanguage({rootNode}, name) {
let node = rootNode;
node.kind = name;
node.sublanguage = true;
this.add(node);
}

openNode(kind) {
let node = { kind, children: [] };
this.add(node);
Expand All @@ -52,12 +33,8 @@ export default class TokenTree {
return JSON.stringify(this.rootNode, null, 4);
}

finalize() {
return;
}

walk(builder) {
return TokenTree._walk(builder, this.rootNode);
return this.constructor._walk(builder, this.rootNode);
}

static _walk(builder, node) {
Expand All @@ -73,7 +50,7 @@ export default class TokenTree {

static _collapse(node) {
if (!node.children) {
return
return;
}
if (node.children.every(el => typeof el === "string")) {
node.text = node.children.join("")
Expand All @@ -86,3 +63,47 @@ export default class TokenTree {
}
}
}

/**
Currently this is all private API, but this is the minimal API necessary
that an Emitter must implement to fully support the parser.
API:
- addKeyword(text, kind)
- addText(text)
- addSublanguage(emitter, subLangaugeName)
- finalize()
*/
export default class TokenTreeEmitter extends TokenTree {
constructor() {
super();
}

addKeyword(text, kind) {
if (text === "") { return; }

this.openNode(kind);
this.addText(text);
this.closeNode();
}

addText(text) {
if (text === "") { return; }

this.add(text);
}

addSublanguage(emitter, name) {
let node = emitter.root;
node.kind = name;
node.sublanguage = true;
this.add(node);
}

finalize() {
return;
}

}

0 comments on commit 1b820c0

Please sign in to comment.