@stackpress/idea-parser
Version:
Parses ideas to AST and readable JSON.
69 lines (68 loc) • 2.07 kB
JavaScript
import { scalar, scan } from '../definitions.js';
import AbstractTree from './AbstractTree.js';
export default class EnumTree extends AbstractTree {
static definitions(lexer) {
super.definitions(lexer);
lexer.define('EnumWord', (code, index) => scan('_EnumWord', /^enum/, 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.enum();
}
enum() {
const type = this._lexer.expect('EnumWord');
this._lexer.expect('whitespace');
const id = this._lexer.expect('CapitalIdentifier');
this._lexer.expect('whitespace');
this._lexer.expect('{');
this.noncode();
const props = [];
this.dotry(() => {
props.push(this.property());
});
this._lexer.expect('}');
return {
type: 'VariableDeclaration',
kind: 'enum',
start: type.start,
end: this._lexer.index,
declarations: [
{
type: 'VariableDeclarator',
start: id.start,
end: this._lexer.index,
id,
init: {
type: 'ObjectExpression',
start: type.start,
end: this._lexer.index,
properties: props
}
}
]
};
}
property() {
const key = this._lexer.expect('UpperIdentifier');
this._lexer.expect('whitespace');
const value = this._lexer.expect(scalar);
this._lexer.expect('whitespace');
this.noncode();
return {
type: 'Property',
kind: 'init',
start: key.start,
end: value.end,
method: false,
shorthand: false,
computed: false,
key,
value
};
}
}
;