This repository was archived by the owner on Oct 9, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (80 loc) · 2.79 KB
/
Copy pathindex.js
File metadata and controls
100 lines (80 loc) · 2.79 KB
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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var path = require('path');
var PluginError = gutil.PluginError;
var File = gutil.File;
var pegjs = require('pegjs');
// Consts
const PLUGIN_NAME = 'gulp-kidscript-pegjs';
module.exports = function (fileName, options) {
options = options || {};
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();
};
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 ]+|(\/\/[^\r\n=]+))*\=/g).map((match) => {
return match.replace(/[\r\n =]|(\/\/[^\r\n=]+)/g, '');
});
// add the javascript header to the front of the file;
combinedSource = headerSource + '\n' + combinedSource;
if (options.print) {
console.log('\n\n Combined Grammar:\n' + combinedSource + '\n\n');
}
// create the peg parser
let compiledParserSource = pegjs.generate(combinedSource, {
allowedStartRules: allRuleNames,
output: 'source',
trace: !!options.trace
});
// 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;
// add this new file
this.push(combinedFile);
cb();
};
return through.obj(processContents, endStream);
};