Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```const Parser = require('tree-sitter');
- const AgsScript = require('tree-sitter-ags-script');
- const Yargs = require("yargs");
- // move all your imports/requires to the top level unless you're doing dynamic loading, but that isn't the case here.
- const fs = require('fs');
- const parser = new Parser();
- parser.setLanguage(AgsScript);
- const argv = Yargs.scriptName("ash2doc")
- .usage('Usage: $0 <command> [options]')
- .command('mark', 'Extract .ash comments to Markdown')
- .example('$0 mark -f foo.ash', 'Turns comments in the given .ash file to markdown')
- .alias('f', 'file')
- .nargs('f', 1)
- .describe('f', 'Load a file')
- .alias('l', 'level')
- .nargs('l', 1)
- .describe('l', 'Initial header level')
- .demandOption(['f'])
- .help('h')
- .alias('h', 'help')
- .epilog('copyright 2019')
- .argv;
- const hl = () => {
- switch (argv.level) {
- case '1':
- hl = "#";
- break;
- case '2':
- hl = "##";
- break;
- case '3':
- hl = "###"
- break;
- default:
- break;
- }
- }
- const cursor = fs.readFile(argv.file, (err, data) => {
- if (err) throw err;
- return parser.parse(data.toString()).walk()
- })
- let lastComment,
- reportText,
- structName;
- function commentNode(nodeText) {
- nodeText.starsWith('///') ? nodeText.substr(3, nodeText.length - 3) : ''
- }
- function importDeclaration(nodeText) {
- const funcText = nodeText.replace('import', '').replace(';', '')
- return `${reportText}
- ${hl} ${funcText}
- ${lastComment}
- `
- }
- function handleFieldDeclarationList(c2) {
- for (var notEnd_c2 = c2.gotoFirstChild();
- notEnd_c2;
- notEnd_c2 = c2.gotoNextSibling()) {
- }
- function handleStructDeclaration(c1) {
- for (let NotEnd_c1 = c1.gotoFirstChild(); notEnd_c1; notEnd_c1 = c1.gotoNextSibling()) {
- switch (c1.nodeType) {
- case 'type_identifier':
- structName = c1.nodeText;
- reportText = `${reportText}
- ${hl} ${structName}
- `
- break;
- case 'field_declaration_list':
- handleFieldDeclarationList(c1.currentNode.walk())
- break;
- }
- }
- }
- for (var notEnd = cursor.gotoFirstChild();
- notEnd;
- notEnd = cursor.gotoNextSibling()) {
- switch (cursor.nodeType) {
- case 'comment':
- lastComment = commentNode(cursor.nodeText);
- break;
- case 'import_declaration':
- lastComment = '';
- reportText = importDeclaration(cursor.nodeText());
- break;
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement