Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
craigulliott committed Oct 31, 2016
0 parents commit c68c39a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# gulp-kidscript_pegjs

This gulp plugin will generate a pegjs parser based on various opinions around how our grammer is written and organized

93 changes: 93 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';

var gutil = require('gulp-util');
var through = require('through2');
var PluginError = gutil.PluginError;
var File = gutil.File;

var pegjs = require('pegjs');

// Consts
const PLUGIN_NAME = 'gulp-codeverse-pegjs';

module.exports = function (fileName) {
var grammarSource = [];
var headerSource;
var combinedFile;
var latestFile;

var processContents = function(file, enc, cb) {

if (file.isNull()) {
// return empty file
return cb(null, file);
}
// we don't handle streams
else if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Streams not supported');
}
//
else if (file.isBuffer()) {
let baseName = path.basename(file.path);
let extension = baseName.split('.')[1];
// .peg files are grammar
if (extension === 'peg') {
grammarSource.push(file.contents.toString());
}
// the .js file is the header
else if (extension === 'js') {
headerSource = file.contents.toString();
}
else {
throw new PluginError(PLUGIN_NAME, 'Unsupported file ' + baseName);
}
}
//
else {
throw new PluginError(PLUGIN_NAME, 'Unknown file process');
}

// keep track of the latest file
latestFile = file;

cb(null, file);

};

var endStream = function(cb) {

// clone everything except contents from the latest file to ensure valid path
var combinedFile = latestFile.clone({contents: false});
combinedFile.path = path.join(latestFile.base, fileName);

// combine the source, note the extra line break is required to pick up the first rule below
var combinedSource = '\n' + grammarSource.join('\n');

// get a list of all the rules from the combinedSource
let allRuleNames = combinedSource.match(/\n([A-Za-z0-9_-])+[\r\n ]*\=/g).map((match) => {
return match.replace(/[\r\n =]/g, '');
});

// add the javascript header to the front of the file;
combinedSource = headerSource + '\n' + combinedSource;

// create the peg parser
let compiledParserSource = pegjs.generate(combinedSource, {
allowedStartRules: allRuleNames,
output: 'source',
trace: false
});

// finalize the source to make it importable and not linted
compiledParserSource = '// jscs:disable\nexport default ' + compiledParserSource + ';'

// populate the new file with the compiled peg parser
var fileContents = new Buffer(compiledParserSource);
combinedFile.contents = fileContents;

this.push(combinedFile);
cb();
};

return through.obj(processContents, endStream);
};
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "gulp-kidscript-pegjs",
"version": "0.0.1",
"description": "A gulp-plugin to generate parsers based on PEGJS grammars.",
"main": "index.js",
"author": "Craig Ulliott <[email protected]>",
"dependencies": {
"gulp-util": "^3.0.6",
"pegjs": "^0.10.0",
"through2": "^2.0.1"
}
}

0 comments on commit c68c39a

Please sign in to comment.