@stackpress/idea-parser
Version:
Parses ideas to AST and readable JSON.
100 lines (99 loc) • 3.8 kB
JavaScript
import TypeTree from './TypeTree.js';
import { scan } from '../definitions.js';
export default class ModelTree extends TypeTree {
static definitions(lexer) {
super.definitions(lexer);
lexer.define('ModelWord', (code, index) => 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();
}
}
;