Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. ```const Parser = require('tree-sitter');
  2. const AgsScript = require('tree-sitter-ags-script');
  3. const Yargs = require("yargs");
  4. // move all your imports/requires to the top level unless you're doing dynamic loading, but that isn't the case here.
  5. const fs = require('fs');
  6.  
  7. const parser = new Parser();
  8. parser.setLanguage(AgsScript);
  9.  
  10. const argv = Yargs.scriptName("ash2doc")
  11. .usage('Usage: $0 <command> [options]')
  12. .command('mark', 'Extract .ash comments to Markdown')
  13. .example('$0 mark -f foo.ash', 'Turns comments in the given .ash file to markdown')
  14. .alias('f', 'file')
  15. .nargs('f', 1)
  16. .describe('f', 'Load a file')
  17. .alias('l', 'level')
  18. .nargs('l', 1)
  19. .describe('l', 'Initial header level')
  20. .demandOption(['f'])
  21. .help('h')
  22. .alias('h', 'help')
  23. .epilog('copyright 2019')
  24. .argv;
  25.  
  26. const hl = () => {
  27. switch (argv.level) {
  28. case '1':
  29. hl = "#";
  30. break;
  31. case '2':
  32. hl = "##";
  33. break;
  34. case '3':
  35. hl = "###"
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41.  
  42. const cursor = fs.readFile(argv.file, (err, data) => {
  43. if (err) throw err;
  44. return parser.parse(data.toString()).walk()
  45. })
  46.  
  47.  
  48. let lastComment,
  49. reportText,
  50. structName;
  51.  
  52. function commentNode(nodeText) {
  53. nodeText.starsWith('///') ? nodeText.substr(3, nodeText.length - 3) : ''
  54. }
  55.  
  56. function importDeclaration(nodeText) {
  57. const funcText = nodeText.replace('import', '').replace(';', '')
  58. return `${reportText}
  59.  
  60. ${hl} ${funcText}
  61.  
  62. ${lastComment}
  63. `
  64. }
  65.  
  66. function handleFieldDeclarationList(c2) {
  67. for (var notEnd_c2 = c2.gotoFirstChild();
  68. notEnd_c2;
  69. notEnd_c2 = c2.gotoNextSibling()) {
  70. }
  71.  
  72. function handleStructDeclaration(c1) {
  73. for (let NotEnd_c1 = c1.gotoFirstChild(); notEnd_c1; notEnd_c1 = c1.gotoNextSibling()) {
  74. switch (c1.nodeType) {
  75. case 'type_identifier':
  76. structName = c1.nodeText;
  77. reportText = `${reportText}
  78.  
  79. ${hl} ${structName}
  80.  
  81. `
  82. break;
  83. case 'field_declaration_list':
  84. handleFieldDeclarationList(c1.currentNode.walk())
  85. break;
  86. }
  87. }
  88. }
  89.  
  90. for (var notEnd = cursor.gotoFirstChild();
  91. notEnd;
  92. notEnd = cursor.gotoNextSibling()) {
  93.  
  94. switch (cursor.nodeType) {
  95. case 'comment':
  96. lastComment = commentNode(cursor.nodeText);
  97. break;
  98. case 'import_declaration':
  99. lastComment = '';
  100. reportText = importDeclaration(cursor.nodeText());
  101. break;
  102. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement