@stackpress/idea-parser
Version:
Parses ideas to AST and readable JSON.
106 lines (105 loc) • 4.11 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 TypeTree_js_1 = __importDefault(require("./TypeTree.js"));
const definitions_js_1 = require("../definitions.js");
class ModelTree extends TypeTree_js_1.default {
static definitions(lexer) {
super.definitions(lexer);
lexer.define('ModelWord', (code, index) => (0, definitions_js_1.scan)('_ModelWord', /^model/, code, index));
return lexer;
}
static parse(code, start = 0) {
return new ModelTree().parse(code, start);
}
model() {
const type = this._lexer.expect('ModelWord');
this._lexer.expect('whitespace');
const id = this._lexer.expect('CapitalIdentifier');
const final = this._lexer.optional('!');
this._lexer.expect('whitespace');
const properties = [];
this.dotry(() => {
properties.push(this.parameter());
this.noncode();
});
this.noncode();
this._lexer.expect('{');
this.noncode();
const columns = [];
this.dotry(() => {
columns.push(this.property());
});
this._lexer.expect('}');
return {
type: 'VariableDeclaration',
kind: 'model',
mutable: !Boolean(final),
start: type.start,
end: this._lexer.index,
declarations: [{
type: 'VariableDeclarator',
start: type.start,
end: this._lexer.index,
id,
init: {
type: 'ObjectExpression',
start: type.start,
end: this._lexer.index,
properties: [
{
type: 'Property',
kind: 'init',
start: type.start,
end: this._lexer.index,
method: false,
shorthand: false,
computed: false,
key: {
type: 'Identifier',
start: type.start,
end: type.end,
name: 'attributes',
},
value: {
type: 'ObjectExpression',
start: type.start,
end: type.end,
properties
}
},
{
type: 'Property',
kind: 'init',
start: type.start,
end: this._lexer.index,
method: false,
shorthand: false,
computed: false,
key: {
type: 'Identifier',
start: type.start,
end: type.end,
name: 'columns',
},
value: {
type: 'ObjectExpression',
start: type.start,
end: type.end,
properties: columns
}
}
]
}
}]
};
}
parse(code, start = 0) {
this._lexer.load(code, start);
return this.model();
}
}
exports.default = ModelTree;
;