grunt-peg.js 999 B

1234567891011121314151617181920212223242526272829
  1. module.exports = function(grunt) {
  2. var PEG = require('pegjs');
  3. /**
  4. Full config looks like this:
  5. pegjs: {
  6. my_parser: {
  7. options: { // optional
  8. trackLineAndColumn: true, // default: false
  9. cache: true, // default: false
  10. export_var: 'smalltalk.parser' // default: module.exports
  11. },
  12. src: 'parser.pegjs',
  13. dest: 'parser.js',
  14. }
  15. },
  16. */
  17. grunt.registerMultiTask('peg', 'Generate JavaScript parser from PEG.js grammar description', function() {
  18. var options = this.options({
  19. cache: false,
  20. trackLineAndColumn: false,
  21. export_var: 'module.exports'
  22. });
  23. var parser = PEG.buildParser(grunt.file.read(this.data.src), options);
  24. var content = 'define("amber_vm/parser", ["./smalltalk","./nil"],function(smalltalk,nil){\n'+options.export_var + ' = ' + parser.toSource() + ';\n});';
  25. grunt.file.write(this.data.dest, content);
  26. });
  27. };