Skip to content

Instantly share code, notes, and snippets.

@pepa65
Last active June 26, 2017 10:24
Show Gist options
  • Save pepa65/7e0db6d5e5f9a0027f0984e7f3caf6d0 to your computer and use it in GitHub Desktop.
Save pepa65/7e0db6d5e5f9a0027f0984e7f3caf6d0 to your computer and use it in GitHub Desktop.
Create grammar check with pegjs
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const parser = require('./grammar.js').parse;
function main() {
const args = require('process').argv;
if (args.length != 3) {
console.error("Only 1 argument needed: file-path")
return 1;
}
const filename = args[2];
fs.readFile(filename, {encoding: 'utf8'}, function(err, data) {
if (err) {
console.log("Error reading file", filename);
console.error(err);
return 1;
}
try {
let output = parser(data);
console.log(output);
}
catch (e) {
console.error(e.message);
}
});
}
main();
page = header description examples eof {"OK"}
header = command nl underline nl nl
description = (descriptor nolowercase any dot nl)+
examples = example (nl example)*
example = example_description nl example_command
example_description = nolowercase any colon nl
example_command = fourspace any nl
command = [a-zA-Z0-9_][-a-zA-Z0-9_ ]*
underline = '='+
any = [a-z ()-,-.={}/_0-9A-Z]*
nolowercase = [^a-z]
descriptor = '> '
fourspace = ' '
dot = .
colon = ':'
nl = [\n]
eof = !.
@pepa65
Copy link
Author

pepa65 commented Jun 26, 2017

Do chmod +x grammar and compile your grammar like: pegjs grammar.peg grammar.js and after that you can deploy like: grammar FILENAME (where FILENAME is the location of the file to be checked).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment