How to parse comments / documentation in Groovy code? - parsing

I'd like to parse the JavaDocs / GroovyDocs in my Groovy source code and build a JSON file with the parts that I'm interested in. Is there a clean way of doing this? I'd like to retrieve the class docs, field docs, method docs, etc. Example:
/** Please parse me. */
class Foo { /** And me too */ def prop }
One of the responses in the following thread was helpful in mentinoing GroovyDocTool / GroovyLexer / GroovyRecognizer, but I could really use a concrete example of how to add my own custom parsing: How to parse groovy code?
My current workaround is to attempt parsing the HTML that the "groovydoc" and "grails doc" commands generate. I'll probably try using NekoHTML to convert the HTML into well-formed XML, then use XmlSlurper. See: http://www.codercorp.com/blog/groovy/reading-html-using-groovys-xmlslurper.html

You may try ANTLR and create a parser. But this will be a brand new parser you may have to write

Related

Use Generator CodeGen in Tatsu

I have generated Model code and parser from my Grammar but I can't modify model to generated code in python.
My EBNF grammar is a script code like "C" syntax for translate file in XML or ANSI X12.
It's a language specific and I would like to generate Python code from this script with Tatsu.
I parse script but I can't success to use Parser or Model to generated Python source code.... Where i must to save Model or modify parser to generated python code ... I see tools.py ... can I copy the code to build a new code model...
Can you help me ... I start learn python and i must to implement this solution on web site with upload script and download python code.
TatSu is a parser generator. I doesn't have any provisions for generating running code from text parsed by an arbitrary grammar.
You have to write your own code generator (walk thes AST after a parse, and generate the corresponding code).

IBM Integration Bus and xsd:anyType

I'm working with IIB v9 mxsd message definitions. I'd like to define one of the XML elements to be of type xsd:anyType. However, in the list of types I can choose from, only anySimpleType and anyUri are possible (besides all other types like string, integer, etc.).
How can I get around this limitation?
The XMLNSC parser supports the entire XML Schema specification, including xs:any and xs:anyType. In IIBv9 you should create a Library and import your xsds into it. Link your Application to the Library and the XMLNSC parser will find and use the model. You do not need to specify the name of the Library in the node properties; the XSD model will be automatically available to the entire application.
You do not need to use a message set at all in IIBv9 and later versions.
The mxsd file format is used only by the MRM (not DFDL) parser.
You shouldn't use an MXSD to model your XML data, use a normal XSD.
MXSD is for modelling data for the DFDL parser, but you should use the XMLNSC parser for XML messages and define them in XSDs, in which you can use anyType.
As far as I know DFDL doesn't support anyType.

Can Yeoman generators update existing files?

So just to give you some context, I'm trying to create a generator that will create some files (based on user input of course) as well as update some existing files in the project (like adding a new route for example).
Creating the files using this.template is no problem... the question is: is there any way to do this with Yeoman without having to read the file using Node and doing some fanciful find and replace?
Ok, so I found the answer to my question.
Addy Osmani showed me where to look in this thread on twitter, and then I later found this link which shows exactly what I need.
The gist of it boils down to two functions : readFileAsString and write. Usage is as follows:
var path = "/path/to/file.html",
file = this.readFileAsString(path);
/* make modifications to the file string here */
this.write(path, file);
Edit: I've also blogged about this on my blog.
 EDIT 1
As mentionned in comments by Toilal :
The write method doesn't exists anymore, and must be replaced by writeFileFromString (arguments are also reversed) – Toilal
EDIT 2
And then, as mentionned in comments by ivoba:
this.writeFileFromString & this.readFileAsString are deprecated, github.com/yeoman/html-wiring should be used by now, things change :) – ivoba
Yeoman also provides a more elegant way of fs operations using mem-fs-editor.
You can use this.fs.copy, passing a process function as an option to do any modifications to the content:
this.fs.copy(path, newPath, {
process: function(content) {
/* Any modification goes here. Note that contents is a Buffer object */
var regEx = new RegExp('old string', 'g');
var newContent = content.toString().replace(regEx, 'new string');
return newContent;
}
});
This way you can also take advantage of mem-fs-editor features.
In combination with #sepans' excellent answer, instead of using regular expressions, one can use some parser.
From Yeoman documentation:
Tip: Update existing file's content
Updating a pre-existing file is not always a simple task. The most reliable way to do so is to parse the file AST and edit it. The main issue with this solution is that editing an AST can be verbose and a bit hard to grasp.
Some popular AST parsers are:
Cheerio for parsing HTML.
Esprima for parsing JavaScript - you might be interested in AST-Query which provide a lower level API to edit Esprima syntax tree.
For JSON files, you can use the native JSON object methods.
Parsing a code file with RegEx is perilous path, and before doing so, you should read this CS anthropological answers and grasp the flaws of RegEx parsing. If you do choose to edit existing files using RegEx rather than AST tree, please be careful and provide complete unit tests. - Please please, don't break your users' code.
More specifically, when using esprima you will most probably require also some generator such as escodegen to generate js back.
var templatePath = this.destinationPath(...);
this.fs.copy(templatePath, templatePath, {
process: function (content) {
// here use the parser
return ...
}
});
Note that you can use the same path in from, to arguments in order to replace the existing file.
On the other hand, the downside of such parsers is that in some cases it may alter the original file way too much, and although safe, this is more intrusive.
Use var text = this.fs.read(filePath) to read from a file and this.fs.write(filePath, content) to write to a file at location.
You can use the mem-fs-editor, and call the fs.append(filepath, contents, [options]) method.
e.g.
this.fs.append(this.contextRoot + "/index.html", " <p>Appended text</p>");

Code Documentation in Dart

Do we have any code documentation syntax and tool support for generating Code Documentation out of Dart Application Code, something similar to Doxygen for C/C++. I prefer to use Markdown styled syntax than doxygen-syntax.
The DartDoc tool (which uses markdown) creates API documentation (as found at api.dartlang.org )
This describes the api reference for using DartDoc in your own code
The Readme.txt here, shows how you can format your code comments to generate API doc
///I am the beerclass
class BeerClass{
///this is a beer variable
String beername;
///this is a beer method
String get getBeer => "beer for the people";
}
Just go in the Darteditor: Tools->Generate Dartdoc. You get a new directory docs and you start index.html in your browser. Your class, variable and method have now documentation.

c++ dom parsing problem

hi every body
i'm new to xercses C++dom parser.can anyone tell me
can we write our own getElementsBytagName,GetNodeValue etc function ?
how to write these function and use them in my code ?
can anybody explain me the process of Dom parsing?
xerces provides methods like getElementsBytagName so you can use them in your application.
Have a look at xerces programming guide you can find there how to parse xml file and how to get elements and attributes.

Resources