@stackpress/idea-parser
Version:
Parses ideas to AST and readable JSON.
45 lines (44 loc) • 1.4 kB
JavaScript
import { scan } from '../definitions.js';
import AbstractTree from './AbstractTree.js';
export default class PluginTree extends AbstractTree {
static definitions(lexer) {
super.definitions(lexer);
lexer.define('PluginWord', (code, index) => scan('_PluginWord', /^plugin/, code, index));
return lexer;
}
static parse(code, start = 0) {
return new this().parse(code, start);
}
parse(code, start = 0) {
this._lexer.load(code, start);
return this.plugin();
}
plugin() {
const type = this._lexer.expect('PluginWord');
this._lexer.expect('whitespace');
const id = this._lexer.expect('String');
this._lexer.expect('whitespace');
const init = this._lexer.expect('Object');
return {
type: 'VariableDeclaration',
kind: 'plugin',
start: type.start,
end: this._lexer.index,
declarations: [
{
type: 'VariableDeclarator',
start: id.start,
end: this._lexer.index,
id: {
type: 'Identifier',
start: id.start,
end: id.end,
name: id.value
},
init
}
]
};
}
}
;