e6d018a88d
This is related to my last commit. I've updated all the JavaScript files to satisfy 'eslint-config-futagozaryuu', my eslint configuration. I'm sure I've probally missed something, but I've run all NPM scripts and Gulp tasks, fixed any bugs that cropped up, and updated some stuff (mainly related to generated messages), so as far as I can, tell this conversion is over (I know I've probally jixed it just by saying this ;P).
102 lines
1.6 KiB
JavaScript
102 lines
1.6 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
"use strict";
|
|
|
|
const fs = require( "fs" );
|
|
const peg = require( "../lib/peg" );
|
|
const options = require( "./options" );
|
|
|
|
// Helpers
|
|
|
|
function readStream( inputStream, callback ) {
|
|
|
|
let input = "";
|
|
inputStream.on( "data", data => {
|
|
|
|
input += data;
|
|
|
|
} );
|
|
inputStream.on( "end", () => {
|
|
|
|
callback( input );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
function abort( message ) {
|
|
|
|
console.error( message );
|
|
process.exit( 1 );
|
|
|
|
}
|
|
|
|
// Main
|
|
|
|
let inputStream, outputStream;
|
|
|
|
if ( options.inputFile === "-" ) {
|
|
|
|
process.stdin.resume();
|
|
inputStream = process.stdin;
|
|
inputStream.on( "error", () => {
|
|
|
|
abort( `Can't read from file "${ options.inputFile }".` );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
inputStream = fs.createReadStream( options.inputFile );
|
|
|
|
}
|
|
|
|
if ( options.outputFile === "-" ) {
|
|
|
|
outputStream = process.stdout;
|
|
|
|
} else {
|
|
|
|
outputStream = fs.createWriteStream( options.outputFile );
|
|
outputStream.on( "error", () => {
|
|
|
|
abort( `Can't write to file "${ options.outputFile }".` );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
readStream( inputStream, input => {
|
|
|
|
let location, source;
|
|
|
|
try {
|
|
|
|
source = peg.generate( input, options );
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( typeof e.location === "object" ) {
|
|
|
|
location = e.location.start;
|
|
if ( typeof location === "object" ) {
|
|
|
|
return abort( location.line + ":" + location.column + ": " + e.message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return abort( e.message );
|
|
|
|
}
|
|
|
|
outputStream.write( source );
|
|
if ( outputStream !== process.stdout ) {
|
|
|
|
outputStream.end();
|
|
|
|
}
|
|
|
|
} );
|