-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c68c39a
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |