All files / cli vsl.js

0% Statements 0/108
0% Branches 0/68
0% Functions 0/12
0% Lines 0/108
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                                                                                                                                                                                                                                                                                                                                                                                     
#!/usr/bin/env node
import VSLParser from '../vsl/parser/vslparser';
import VSLTokenizer from '../vsl/parser/vsltokenizer';
 
import VSLPreprocessor from '../vsl/transform/transformers/vslpreprocessor';
import VSLTransformer from '../vsl/transform/transformers/vsltransformer';
 
import readline from 'readline';
import util from 'util';
import colors from 'colors';
import { readFileSync } from 'fs';
import minimist from 'minimist';
import pjson from '../../package.json';
 
const print = console.log,
    stdin = process.stdin,
    argv = minimist(process.argv.slice(2),  {
        alias: {
            help: 'h',
            parserrepl: 'p',
            tokenizerrepl: 't',
            transformrepl: 'r',
            code: 'c'
        },
        booleans: [
        ],
        default: {
        }
    }),
    tokenizer = new VSLTokenizer();
 
let code = '',
    parser = new VSLParser(),
    rl;
 
stdin.setEncoding('utf-8');
 
function display (output) {
    print(util.inspect(output, {showHidden: false, depth: null, colors: true}));
}
 
function feed (code) {
    let result;
    if ((result = parser.feed(code + '\n'))) {
        return result;
    } else {
        if (result === null)
            return;
        print('Syntax Error'.red + ': ');
        print(parser.error.message);
    }
}
 
function exit () {
    process.exit();
}
 
function help () {
    print(`VSL: Versatile Scripting Language, v${pjson.version}
Usage: vsl PATH_TO_VSL_FILE
 
Options:
    -h, --help          Show help
 
Debug REPLs:
    -p, --parserrepl    Start parser REPL
    -t, --tokenizerrepl Start tokenizer REPL
    -r, --transformrepl Start parser -> VSLTransformer REPL
    -c, --code CODE     Parse CODE`) || exit();
}
 
if (argv.help)
    help();
 
if (argv._.length)
    display(feed(readFileSync(argv._[0]))) || exit();
 
if (argv.code)
    display(feed(argv.code)) || exit();
 
if (argv.p || argv.t || argv.r)
    rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
 
if (argv.r) {
    function prompt() {
        rl.setPrompt('vsl:transform> '.red.bold);
        rl.prompt();
    }
    prompt();
    let feeding = false;
    rl.on('line', function (input) {
        if (input === 'exit')
    		exit(rl.close());
        if (feeding === true) {
            let result = feed(input);
            if (typeof result === 'undefined') {
                feeding = false;
                prompt();
            } else if (result.length > 0) {
                feeding = false;
                console.log(result[0].toString());
                prompt();
            } else
                rl.prompt();
            return;
        }
        parser = new VSLParser();
    	let result = feed(input);
    	
        new VSLPreprocessor().queue(result);
        new VSLTransformer().queue(result);
 
    	
    	if (typeof result === 'undefined')
    	    return prompt();
    	// check if got any tokens at all
    	// i.e. skip if all comments or no input
    	if (result.tokens && result.length < 1) {
    	    feeding = true;
    	    rl.setPrompt('>>>>>>>>>>>>>> '.bold);
    	    return rl.prompt();
    	}
    	console.log(result[0].toString());
        prompt();
    });
}
 
if (argv.p) {
    function prompt() {
        rl.setPrompt('vsl:parser> '.red.bold);
        rl.prompt();
    }
    prompt();
    let feeding = false;
    rl.on('line', function (input) {
        if (input === 'exit')
    		exit(rl.close());
        if (feeding === true) {
            let result = feed(input);
            if (typeof result === 'undefined') {
                feeding = false;
                prompt();
            } else if (result.length > 0) {
                feeding = false;
                display(result);
                prompt();
            } else
                rl.prompt();
            return;
        }
        parser = new VSLParser();
    	let result = feed(input);
    	if (typeof result === 'undefined')
    	    return prompt();
    	// check if got any tokens at all
    	// i.e. skip if all comments or no imput
    	if (result.tokens && result.length < 1) {
    	    feeding = true;
    	    rl.setPrompt('>>>>>>>>>>> '.bold);
    	    return rl.prompt();
    	}
    	display(result);
        prompt();
    });
}
 
if (argv.t) {
    rl.setPrompt('vsl:tokenizer> '.red.bold);
    rl.prompt();
    rl.on('line', function(input) {
        if (input === 'exit')
    		rl.close() || exit();
    	display(tokenizer.tokenize(input));
        rl.prompt();
    });
}
 
if (!argv.p && !argv.t && !argv.r) {
    stdin.on('readable', function () {
        let chunk = process.stdin.read();
        if (chunk)
            code += chunk;
        else if (!code)
            help();
        while ((chunk = process.stdin.read()))
            code += chunk;
    });
    stdin.on('end', function () {
        display(feed(code));
        exit();
    });
}
 
function newline () {
    console.log();
}
 
process.on('exit', newline);
process.on('SIGINT', newline);
process.on('uncaughtException', newline);