Skip to content

Instantly share code, notes, and snippets.

@silkentrance
Last active November 7, 2016 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save silkentrance/bd77c42a57be9ac66603 to your computer and use it in GitHub Desktop.
Save silkentrance/bd77c42a57be9ac66603 to your computer and use it in GitHub Desktop.
Miscellaneous PEGJS grammars, a CLI testing frontend and input data
#!/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}));
}
#@
variation1
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
= !.
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
= !.
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
= !.
@silkentrance
Copy link
Author

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

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