Last active
June 26, 2017 10:24
-
-
Save pepa65/7e0db6d5e5f9a0027f0984e7f3caf6d0 to your computer and use it in GitHub Desktop.
Create grammar check with pegjs
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
#!/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(); |
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
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 = !. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).