2021-05-02 17:42:04 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
2021-05-10 16:55:19 -04:00
|
|
|
import debug from 'debug';
|
2021-05-02 17:42:04 -04:00
|
|
|
import { resolve, parse } from 'path';
|
2021-05-06 23:05:36 -04:00
|
|
|
import { readFileSync, readdirSync } from 'fs';
|
2021-05-02 17:42:04 -04:00
|
|
|
import nearley from 'nearley';
|
|
|
|
|
import compile from 'nearley/lib/compile.js';
|
|
|
|
|
import generate from 'nearley/lib/generate.js';
|
|
|
|
|
import nearleyGrammar from 'nearley/lib/nearley-language-bootstrapped.js';
|
|
|
|
|
import moo from 'moo';
|
|
|
|
|
const grammarFile = 'grammar.ne';
|
2021-05-06 23:05:36 -04:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
const { get, set } = _;
|
2021-05-02 17:42:04 -04:00
|
|
|
import Module from './Module.js';
|
|
|
|
|
import System from './System.js';
|
2021-05-10 16:55:19 -04:00
|
|
|
import tokens from './tokens.js';
|
2021-05-02 17:42:04 -04:00
|
|
|
|
2021-05-06 09:33:28 -04:00
|
|
|
// globals inside grammar context
|
|
|
|
|
import minify from './minify.js';
|
2021-05-02 17:42:04 -04:00
|
|
|
|
2021-05-10 16:55:19 -04:00
|
|
|
const log = {
|
|
|
|
|
ast: debug('vogue:ast'),
|
|
|
|
|
modules: debug('vogue:modules'),
|
|
|
|
|
debug: debug('vogue:debug'),
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 09:33:28 -04:00
|
|
|
Object.defineProperty(Array.prototype, 'empty', {
|
|
|
|
|
get() {
|
|
|
|
|
return this.length === 0;
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-05-02 17:42:04 -04:00
|
|
|
|
|
|
|
|
function createParser() {
|
2021-05-06 23:05:36 -04:00
|
|
|
// Parse the grammar source into an AST
|
|
|
|
|
const grammarParser = new nearley.Parser(nearleyGrammar);
|
|
|
|
|
grammarParser.feed(readFileSync(grammarFile).toString());
|
|
|
|
|
const grammarAst = grammarParser.results[0]; // TODO check for errors
|
|
|
|
|
|
|
|
|
|
// Compile the AST into a set of rules
|
|
|
|
|
const grammarInfoObject = compile(grammarAst, {});
|
|
|
|
|
// Generate JavaScript code from the rules
|
|
|
|
|
const grammarJs = generate(grammarInfoObject, "grammar");
|
|
|
|
|
|
2021-05-10 16:55:19 -04:00
|
|
|
const lexer = moo.compile(tokens);
|
2021-05-06 23:05:36 -04:00
|
|
|
|
|
|
|
|
// lexer.__proto__.formatError = function(token, message) {
|
|
|
|
|
// if (token == null) {
|
|
|
|
|
// // An undefined token indicates EOF
|
|
|
|
|
// var text = this.buffer.slice(this.index)
|
|
|
|
|
// var token = {
|
|
|
|
|
// text: text,
|
|
|
|
|
// offset: this.index,
|
|
|
|
|
// lineBreaks: text.indexOf('\n') === -1 ? 0 : 1,
|
|
|
|
|
// line: this.line,
|
|
|
|
|
// col: this.col,
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// var start = Math.max(0, token.offset - token.col + 1)
|
|
|
|
|
// var eol = token.lineBreaks ? token.text.indexOf('\n') : token.text.length
|
|
|
|
|
// var firstLine = this.buffer.substring(start, token.offset + eol)
|
|
|
|
|
// message += " at line " + token.line + " col " + token.col + ":\n\n"
|
|
|
|
|
// message += " " + firstLine + "\n"
|
|
|
|
|
// message += " " + Array(token.col).join(" ") + "^"
|
|
|
|
|
// return message
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Pretend this is a CommonJS environment to catch exports from the grammar.
|
|
|
|
|
const module = { exports: {} };
|
|
|
|
|
eval(grammarJs);
|
|
|
|
|
|
|
|
|
|
const grammar = module.exports;
|
|
|
|
|
return new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
|
2021-05-02 17:42:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const systemLocation = resolve(process.argv[2]);
|
|
|
|
|
const entry = process.argv[3];
|
|
|
|
|
|
2021-05-06 23:05:36 -04:00
|
|
|
(async () => {
|
|
|
|
|
// TODO simplify this line gaddam
|
2021-05-08 23:06:31 -04:00
|
|
|
const modules =
|
|
|
|
|
(await Promise.all(
|
|
|
|
|
readdirSync(systemLocation)
|
|
|
|
|
.map(v => resolve(systemLocation, v))
|
|
|
|
|
.map(parseModule)
|
|
|
|
|
)).reduce((acc, val) => {
|
|
|
|
|
set(acc, val.name.full, val);
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
|
2021-05-06 23:05:36 -04:00
|
|
|
const sys = new System(modules);
|
|
|
|
|
})()
|
|
|
|
|
|
|
|
|
|
async function parseModule(location) {
|
2021-05-02 17:42:04 -04:00
|
|
|
const parser = createParser();
|
|
|
|
|
const contents = readFileSync(location).toString();
|
|
|
|
|
const name = parse(location).name;
|
2021-05-08 23:06:31 -04:00
|
|
|
const module = new Module();
|
|
|
|
|
|
2021-05-06 09:33:28 -04:00
|
|
|
// parser.reportError = function(token) {
|
|
|
|
|
// return JSON.stringify(token, null, 2);
|
2021-05-06 23:05:36 -04:00
|
|
|
// var message = this.lexer.formatError(token, 'invalid syntax') + '\n';
|
|
|
|
|
// message += 'Unexpected ' + (token.type ? token.type + ' token: ' : '');
|
|
|
|
|
// message +=
|
|
|
|
|
// JSON.stringify(token.value !== undefined ? token.value : token) + '\n';
|
|
|
|
|
// return message;
|
|
|
|
|
// };
|
2021-05-08 23:06:31 -04:00
|
|
|
|
|
|
|
|
parser.feed(contents);
|
2021-05-02 17:42:04 -04:00
|
|
|
parser.finish();
|
|
|
|
|
const parsed = parser.results[0];
|
|
|
|
|
|
2021-05-10 16:55:19 -04:00
|
|
|
log.ast('='.repeat(80));
|
|
|
|
|
log.ast(location);
|
|
|
|
|
log.ast(parsed);
|
2021-05-10 15:45:36 -04:00
|
|
|
|
2021-05-02 17:42:04 -04:00
|
|
|
module.name.last = name;
|
|
|
|
|
module.name.full = name;
|
2021-05-06 23:05:36 -04:00
|
|
|
try {
|
|
|
|
|
|
2021-05-08 23:06:31 -04:00
|
|
|
// move this whole loop ass bitch into module.
|
2021-05-06 23:05:36 -04:00
|
|
|
for (const item of parsed) {
|
2021-05-08 23:06:31 -04:00
|
|
|
if ('name' in item && item.name in module.identifiers)
|
|
|
|
|
throw new Error('Identifier ' + item.name + ' already declared!');
|
|
|
|
|
|
|
|
|
|
module.identifiers[item.name] = item.type;
|
|
|
|
|
|
|
|
|
|
if (item.type === 'link') {
|
|
|
|
|
module.links
|
|
|
|
|
[item.required ? 'required' : 'optional']
|
|
|
|
|
[item.array ? 'arrays' : 'single']
|
|
|
|
|
.push(item.name);
|
|
|
|
|
|
|
|
|
|
} else if (item.type === 'namespace') {
|
|
|
|
|
module.name.space = item.namespace;
|
|
|
|
|
module.name.full = module.name.space + '.' + module.name.last;
|
|
|
|
|
|
|
|
|
|
} else if (item.type === 'restore') {
|
|
|
|
|
module.functions.restore = item.block;
|
|
|
|
|
|
|
|
|
|
} else if (item.type === 'function') {
|
|
|
|
|
module.functions[item.name] = item.block;
|
|
|
|
|
|
|
|
|
|
} else if (item.type === 'import') {
|
|
|
|
|
const imported = await import(item.importName);
|
|
|
|
|
if('default' in imported) module.imports[item.name] = imported.default;
|
|
|
|
|
else module.imports[item.name] = imported;
|
|
|
|
|
|
|
|
|
|
} else if (item.type === 'variable') {
|
2021-05-09 19:42:03 -04:00
|
|
|
module.variables[item.persist ? 'cold' : 'warm'].push(item.name);
|
2021-05-02 17:42:04 -04:00
|
|
|
}
|
2021-05-06 23:05:36 -04:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
2021-05-02 17:42:04 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-08 23:06:31 -04:00
|
|
|
return module;
|
2021-05-02 17:42:04 -04:00
|
|
|
|
|
|
|
|
}
|