@stackpress/idea-parser
Version:
Parses ideas to AST and readable JSON.
75 lines (74 loc) • 2.38 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const definitions_js_1 = require("../definitions.js");
const AbstractTree_js_1 = __importDefault(require("./AbstractTree.js"));
class EnumTree extends AbstractTree_js_1.default {
static definitions(lexer) {
super.definitions(lexer);
lexer.define('EnumWord', (code, index) => (0, definitions_js_1.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(definitions_js_1.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
};
}
}
exports.default = EnumTree;
;