Last active
November 7, 2016 18:45
-
-
Save silkentrance/bd77c42a57be9ac66603 to your computer and use it in GitHub Desktop.
Miscellaneous PEGJS grammars, a CLI testing frontend and input data
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 /usr/bin/node | |
// usage ./cli [grammar.js] unit.ypo | |
// default grammar is ypo.js | |
var fs = require('fs'); | |
var path = require('path'); | |
var util = require('util'); | |
var file, grammar; | |
console.log(process.argv.length); | |
console.log(process.argv); | |
if (process.argv.length > 3) | |
{ | |
grammar = path.resolve(process.argv[2]); | |
file = path.resolve(process.argv[3]); | |
} | |
else | |
{ | |
grammar = path.resolve('ypo-minimal-notfailing.js'); | |
file = path.resolve(process.argv[2]); | |
} | |
console.log('using grammar: ' + path.basename(grammar)); | |
console.log('using input file: ' + path.basename(file)); | |
var parser = require(grammar); | |
var f = fs.readFileSync(file); | |
try | |
{ | |
var unit = parser.parse(f.toString()); | |
console.log(unit); | |
console.log('---------> translations'); | |
var translations = unit.translations || [unit]; | |
if (translations) | |
{ | |
console.log(translations); | |
if (translations.length) | |
{ | |
console.log('---------> variations'); | |
console.log(translations[0].variations); | |
for (var index = 0; index < translations[0].variations.length; index++) | |
{ | |
console.log(translations[0].variations[index].text); | |
} | |
} | |
} | |
} | |
catch (err) | |
{ | |
console.log(util.inspect(err, {depth:10})); | |
} | |
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
#@ | |
variation1 | |
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
Translation | |
= NL* | |
context:VariationContext? | |
variations:Variation+ | |
{ | |
return { | |
type : 'translation', | |
location : location(), | |
context: context, | |
variations: variations | |
}; | |
} | |
Variation | |
= NL* | |
context:VariationContext? | |
NL* | |
text:Text+ | |
NL | |
{ | |
return { | |
type : 'variation', | |
location : location(), | |
context: context, | |
text:text | |
}; | |
} | |
VariationContext | |
= NL* '#' SYM_CTX value:CONTEXTID | |
{ | |
return { | |
type: 'context', | |
location: location(), | |
value: value | |
}; | |
} | |
CONTEXTID "contextid" | |
= WS? value:$QNAME EOL | |
{ | |
return value; | |
} | |
Text "text" | |
= value:$CHAR+ NL | |
{ | |
return value; | |
} | |
EOL "end of line" | |
= WS* NL | |
SYM_CTX | |
= '@' | |
QNAME | |
= [a-zA-Z0-9]+ | |
CHAR | |
= [^\r\n\f\\] | |
WS | |
= ' ' | |
/ '\t' | |
NL | |
= '\n' | |
/ '\r\n' | |
/ '\r' | |
/ '\f' | |
EOF | |
= !. |
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
Translation | |
= NL* | |
context:VariationContext? | |
variations:Variation+ | |
{ | |
return { | |
type : 'translation', | |
location : location(), | |
context: context, | |
variations: variations | |
}; | |
} | |
Variation | |
= NL* | |
context:VariationContext? | |
NL* | |
text:Text+ | |
NL | |
{ | |
return { | |
type : 'variation', | |
location : location(), | |
context: context, | |
text:text | |
}; | |
} | |
VariationContext | |
= NL* '#' SYM_CTX value:CONTEXTID | |
{ | |
return { | |
type: 'context', | |
location: location(), | |
value: value | |
}; | |
} | |
CONTEXTID "contextid" | |
= WS? value:$QNAME EOL | |
{ | |
return value; | |
} | |
Text "text" | |
= value:$(!('#' SYM_CTX) $CHAR+) NL | |
{ | |
return value; | |
} | |
EOL "end of line" | |
= WS* NL | |
SYM_CTX | |
= '@' | |
QNAME | |
= [a-zA-Z0-9]+ | |
CHAR | |
= [^\r\n\f\\] | |
WS | |
= ' ' | |
/ '\t' | |
NL | |
= '\n' | |
/ '\r\n' | |
/ '\r' | |
/ '\f' | |
EOF | |
= !. |
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
Translation | |
= NL* | |
context:VariationContext? | |
{ | |
return { | |
type : 'translation', | |
location : location(), | |
context: context | |
}; | |
} | |
VariationContext | |
= NL* '#' SYM_CTX value:CONTEXTID | |
{ | |
return { | |
type: 'context', | |
location: location(), | |
value: value | |
}; | |
} | |
CONTEXTID "contextid" | |
= WS? value:$QNAME EOL | |
{ | |
return value; | |
} | |
EOL "end of line" | |
= WS* NL | |
SYM_CTX | |
= '@' | |
QNAME | |
= [a-zA-Z0-9]+ | |
CHAR | |
= [^\r\n\f\\] | |
WS | |
= ' ' | |
/ '\t' | |
NL | |
= '\n' | |
/ '\r\n' | |
/ '\r' | |
/ '\f' | |
EOF | |
= !. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In order to use this for verification of pegjs/pegjs#410
clone,
chmod 755 cli.js
,pegjs --trace [ypo|ypo-minimal-notfailing|ypo-minimal-failing-1].pegjs
and then run
./cli [grammar] full.ypo