Modifying ANTLR4 Token - antlr4

My Java.g4 grammar is generating " /**\r\n\t * A description Test. \r\n\t * \r\n\t * #see A first block tag\r\n\t * #see A second block tag\r\n\t */ " as a Token .
I'm rather interested in modifying my token permanently to " A description Test. #see A first block tag #see A second block tag " by means of TokenStreamRewrite or by some other way.
Any idea how to do it ??

The tokens are actually CommonToken instance so you could cast them to that class and call setText(..) on them. However, I believe the lexer/parser is not the right place for this kind of work. Instead do that in the following phase (e.g. when walking over the parse tree) and extract the content of your doc-comment using simple string manipulation functions. Don't overload the generated classes with work that doesn't really belong to them.

Related

Generate method or class comments in kotlin

I'm using Android Studio 3.0.1. I can hit fix doc comment to insert comment of method quickly in Java, but not effective in Kotlin. How can I solve it?
When you're typing # inside a kdoc, you'll see auto complete for #param, and after #param you'll see all parameters, and currently you cannot complete all parameters within one click.
Mention the /** */ comments are called kdoc in Kotlin, which is not javadoc.
Kotlin and especially KDoc encourage a different documentation style. As stated in this discussion:
The reason is that we found that referring to parameter names from the documentation text allows to write documentation that is more concise and easier to read compared to the traditional javadoc style where every parameter is documented in a separate tag. Therefore, we do not generate the template with parameter names by default. (D. Jemerov, Kotlin in Action Author)
Here’s an example of let, which is part of the standard library:
/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*/
#kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R

Is it possible to document a function in JSDoc that's not defined locally?

The basic pattern that I have a couple of times in my code is exports.thing = thingFromSomewhereElse, and I want to document the function members of thingFromSomewhereElse as though I had defined them myself. But I can't see how do do that without an actual thing.functionName = function() {...}. If I try to use the #callback tag, JSDoc classifies it as a type definition instead of a member. Is there a way to get it to properly display as an object member?
This can be accomplished by using the #name JSDoc tag. That tag is intended to be used to write a comment for something that would not be recognized by JSDoc. The #kind tag should also be used to indicate that the thing being documented is actually a function, so that the function-specific tags like #param and #return work properly. For the example given, the function would be documented with a comment like this:
/**
* Description of the function
* #name thing.functionName
* #kind function
* #param ...
* #return ...
*/

HTTP Outbound Gateway - Passing URI parameters

I need to invoke a REST API via Spring Integration's HTTP outbound gateway. How do I substitute the path variable with a value from payload. Payload has just one String value. The following code snippet is sending the place holder as such. Any help is appreciated.
#Bean
public MessageHandler httpGateway(#Value("http://localhost:8080/api/test-resource/v1/{parameter1}/codes") URI uri) {
HttpRequestExecutingMessageHandler httpHandler = new HttpRequestExecutingMessageHandler(uri);
httpHandler.setExpectedResponseType(Map.class);
httpHandler.setHttpMethod(HttpMethod.GET);
Map<String, Expression> uriVariableExp = new HashMap();
SpelExpressionParser parser = new SpelExpressionParser();
uriVariableExp.put("parameter1", parser.parseExpression("payload.Message"));
httpHandler.setUriVariableExpressions(uriVariableExp);
return httpHandler;
}
Let take a look what #Value is for first of all!
* Annotation at the field or method/constructor parameter level
* that indicates a default value expression for the affected argument.
*
* <p>Typically used for expression-driven dependency injection. Also supported
* for dynamic resolution of handler method parameters, e.g. in Spring MVC.
*
* <p>A common use case is to assign default field values using
* "#{systemProperties.myProp}" style expressions.
Looking to your sample there is nothing to resolve as a dependency injection value.
You can just use:
new HttpRequestExecutingMessageHandler("http://localhost:8080/api/test-resource/v1/{parameter1}/codes");
Although it doesn't matter in this case...
You code looks good, unless we don't know what your payload is.
That payload.Message expression looks odd. From big height it may mean like MyClass.getMessage(), but it can't invoke the getter because you use a property name as capitalized. If you really have there such a getter, so use it like payload.message. Otherwise, please, elaborate more. Some logs, StackTrace, the info about payload etc... It's fully unclear what is the problem.

Does anyone know where to find a SS2.0 JSDOC definition library?

Does anyone know or has a JSDOC definition library I can use for SS2.0??
The current one I have for SS1.0 looks like the one below, and I use it to plug into my IDE and get autocompletion.
/** * Loads an existing saved search. The saved search could have been created using the UI, or created using nlapiCreateSearch(type, filters, columns) in conjunction with nlobjSearch.saveSearch(title, scriptId).
*<br>API Governance: 5
* #param {String} recType [optional] - The record internal ID of the record type you are searching (for example, customer|lead|prospect|partner|vendor|contact). This parameter is case-insensitive.
* #param {String} searchId - The internal ID or script ID of the saved search. The script ID of the saved search is required, regardless of whether you specify the search type. If you do not specify the search type, you must set type to null and then set the script/search ID.
* #returns {nlobjSearch} nlobjSearch
* #since 2012.1 */
function nlapiLoadSearch(recType, searchId) { };
Not really a technical question but would come in handy for everyone.
You may consider what I did which there is no need to download other plugins. You will be able to enable the code assist if you have the copy of SS2.0 API and then use "#param" and "#type" JSDOC tag.
Then, there will be suggestion every time you type then 'CTRL' + 'SPACE".
Aside from that, your IDE will also provide description for each function.
So this is what will you do.
On your NetSuite accoun, download the SuiteScript 2.0 JavaScript files. You can do this by nagaviting at >Documents>Files>SuiteScripts>. Then at right portion of your screen, you should see links for "SuiteScript 2.0 API" and "SuiteSCript 1.0 API". Click for SS2.0 to download.
On Eclipse IDE, create a new JavaScript project for SS2.0 or include it on your existing project.
Next on the project you are working, right click then select “Properties”. Under >JavaScript>Include Path and then at “Projects” subtab, add the project where SS2.0 APIs are included.
You will now have code assist for object literal APIs of SS2.0. These are 'N/log' and 'N'util' modules.
Next to enable the code assist for object constructor APIs of SS2.0 like 'N/record' and 'N/search' modules, we should be adding "#param" JSDoc tag on each function declaration.
So if we are going to use the 'N/record', 'N/search' and 'N/error' modules in our script, we should have below sample comments before function declaration. But please take note that it should match the value inside the "{[VALUE HERE]}" tag and the module name. Also the variable name on the comment section and function declaration.
/**
* Do something.
*
* #param {record} objRec
* #param {search} objSearch
* #param {error} objError
*
*/
function doSomething(objRec, objSearch, objError)
{
//CODE HERE
}
You may also use '#type' for your variable declaration. Below is the sample code.
/**
* Do something.
*
*/
function doSomething()
{
/*** #type record**/
var recCustomerRefund = record.create(
{
type : 'customerrefund',
isDynamic : true
});
}
The way SS2 works you are essentially out of luck.
consider
define(['N/search'], function(bob){
var srch = bob.load({id:'customsearch_my_search'});
}
What is bob? Your IDE will have to be considerably smarter to know that bob is part of the search namespace. It's of course doable but less likely to work with a simple downloadable file that you can just plug into your IDE. If you just use 'search' that may make things more likely to work but now you've used up a very generic name that you cannot use elsewhere.
Also if an IDE can determine what 'bob' is now your arguments is an unordered hash so positional #params don't work anymore.
Typescript may help. I've been maintaining a Typescript definition file for SS1 at https://github.com/BKnights/KotN-Netsuite. As I start doing more with SS2.0 I may do the same for that. Then your IDE would have more of a chance:
define(['N/search'], function(bob:NSearch){...
So your Typescript aware IDE could use that for member completion and at least you'd have a compile time check on types

what does getType do in antlr4?

This question is with reference to the Cymbol code from the book (~ page 143) :
int t = ctx.type().start.getType(); // in DefPhase.enterFunctionDecl()
Symbol.Type type = CheckSymbols.getType(t);
What does each component return: "ctx.type()", "start", "getType()" ? The book does not contain any explanation about these names.
I can "kind of" understand that "ctx.type()" refers to the "type" rule, and "getType()" returns the number associated with it. But what exactly does the "start" do?
Also, to generalize this question: what is the mechanism to get the value/structure returned by a rule - especially in the context of usage in a listener?
I can see that for an ID, it is:
String name = ctx.ID().getText();
And as in above, for an enumeration of keywords it is via "start.getType()". Any other special kinds of access that I should be aware of?
Lets disassemble problem step by step. Obviously, ctx is instance of CymbolParser.FunctionDeclContext. On page 98-99 you can see how grammar and ParseTree are implemented (at least the feeling - for real implementation please see th .g4 file).
Take a look at the figure of AST on page 99 - you can see that node FunctionDeclContext has a several children, one labeled type. Intuitively you see that it somehow correspond with function return-type. This is the node you retrieve when calling CymbolParser.FunctionDeclContext::type. The return type is probably sth like TypeContext.
Note that methods without 'get' at the beginning are usually children-getters - e.g. you can access the block by calling CymbolParser.FunctionDeclContext::block.
So you got the type context of the method you got passed. You can call either begin or end on any context to get first of last Token defining the context. Simply start gets you "the first word". In this case, the first Token is of course the function return-type itsef, e.g. int.
And the last call - Token::getType returns integral representation of Token.
You can find more information at API reference webpages - Context, Token. But the best way of understanding the behavior is reading through the generated ANTLR classes such as <GrammarName>Parser etc. And to be complete, I attach a link to the book.

Resources