ANTLR4 - DotGenerator example [duplicate] - antlr4

This question already has answers here:
ANTLR4 parse tree to DOT using DOTGenerator
(2 answers)
Closed 4 years ago.
Where I can find example how to use org.antlr.v4.tool.DotGenerator in ANTLR4?
As I understand, it replaces DOTTreeGenerator in ANTLR4.

I am also interested in an answer to your question and didn't find a fully convincing one yet.
Assuming that you are interested in Displaying the ParseTree here is an alternative way to at least get a visual representation:
/**
* show the given Tree Viewer
* #param tv
*/
public int showTreeViewer(TreeViewer tv) {
JPanel panel = new JPanel();
tv.setScale(2);
panel.add(tv);
return JOptionPane.showConfirmDialog(null, panel, "ParseTree",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
// http://stackoverflow.com/questions/30134121/drawing-parse-tree-in-antlr4-using-java/30137407#30137407
ParseTree tree=rulesContext;
List<String> ruleNames=Arrays.asList(parser.getRuleNames());
// http://stackoverflow.com/questions/34832518/antlr4-dotgenerator-example
TreeViewer tv=new TreeViewer(ruleNames,tree);
showTreeViewer(tv);

Related

Replace string with another string across few folders, leaving a variable in a replaced string intact

I have 3 unrelated java selenium repos which utilise extensively a following block of code to get text from list of elements:
getVisibleElements(someElement)
.stream()
.map(WebElement::getText)
.toList();
I have written a method in a base class that is extended by another classes, so I don't have to write such verbose code every time I want to extract text from elements:
/**
* Get text from multiple elements
*
* #param locator element
* #param T either {#code By} or {#code List<WebElement>}.
* #return List of elements' text
*/
protected final <T> List<String> getTexts(T locator) {
return getVisibleElements(locator)
.stream()
.map(WebElement::getText)
.toList();
}
if anyone wanted to know this is getVisibleElements(T locator) implementation, but it's a bit offtopic:
#SuppressWarnings("unchecked")
protected final <T> List<WebElement> getVisibleElements(T locator) {
return wait.until(isBy(locator)
? ExpectedConditions.visibilityOfAllElementsLocatedBy((By) locator)
: ExpectedConditions.visibilityOfAllElements((List<WebElement>) locator));
}
where wait is an instance of WebDriverWait.
Now, I would like to write some script (I was thinking Python or Perl), which would take folders, where these repos are, as input and simply replace all these code blocks with my new short method, leaving only method argument intact, so, for example:
List<String> tileTexts = getVisibleElements(attachmentFileNameDiv)
.stream()
.map(WebElement::getText)
.toList();
becomes:
List<String> tileTexts = getTexts(attachmentFileNameDiv);
I could of course use IDE replace feature and go thru all the files, however I would like to do it in a more efficient way. I have unfortunately no knowlegde of scripting and if someone helped me to write such script, or showed perhaps some tool that could help with string processing I could use this knowledge and use it to further refacor my repos.

Change class style by JavaScript [duplicate]

This question already has answers here:
Changing a CSS rule-set from Javascript
(9 answers)
Closed 3 years ago.
I use in the head of a page a style tag with definition of the styles used by elements in the body using the ‘class’ attribute.
Example:
.itemColor { color:red; }
Is it possible, with javascript, to change the value of the color property in the classes defined in the tag style?
For your example, you would first select your item, and then change its style:
myitem = document.getElementById("itemColor") // searches in all children and grandhildren of document
myitem.style.color = "blue";
You can do the same for the elements in getElementsByClassName and getElementsByTagName, which are HTML collections.
With the following, you can override all elements' properties having said class.
myitems = document.getElementsByClassName("itemColor") // HTMLCollection
[].forEach.call(myitems, function(subitem){
subitem.style.color = "blue";
}
);
console.log(myitem.style) to see all attributes you can change.

Groovy - Set your own property of an Integer

I just started to learn Groovy and wondering if you can set your own property for an integer. For example,
def a = 34.5.plus(34.34)
def b = 5.64.minus(3.43)
def c = 12.64.multiply(33.43)
In the above there are certain methods like plus minus and multiply
What should I do if I want to define some of my own methods for integers like that.
I searched Google but couldn't find much about it.
Sure, you can just add methods to the metaClass of Integer.
Here's an example:
Integer.metaClass.zeds = { -> 'z' * delegate }
assert 3.zeds() == 'zzz'
You can also add methods to a single instance of integer should you wish to, ie:
Integer num = 4
num.metaClass.halved = { -> delegate / 2.0 }
assert num.halved() == 2.0
You can also add methods to classes via Extension Methods a good explanation of which can be found over here
It should be noted (as you originally tagged this question as Java) that obviously, Java code will have no knowledge of these things, as it doesn't know about the metaClass
Use groovy meta programming, this allows you to create dynamic method creation atruntime in the class that you want to place in .
bydefault if a method is not found methodmissing exception throws , this is where groovy allows you add method at runtime for more reference use the below comprehensive link
http://groovy-lang.org/metaprogramming.html
If this answer helps , dont forget to click answered.

C# use reflection to get method name [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get methodinfo from a method reference C#
This is most likely something simple but so far I have not come up with anything on how to do this.
I want to be able to get the name of a method in two different ways. Please note I want a method name, not a property name.
1) Inside of a class like ClassA<T>, looking like:
var name = GetMethodName(x => x.MethodA);
2) Outside of a class, looking like:
var name = GetMethodName<ClassA<object>>(x => x.MethodA);
var name = GetMethodName<ClassB>(x => x.MethodB);
How might I do this exactly?
Thanks!
You don't need lambdas (x => x.MethodA, etc). That's just confusing the issue (and hiding the method of interest: the MethodA bit would be hidden from your GetMethodName method).
Instead, you can use reflection to get a MethodInfo object, which then has a Name property.
For example:
MethodInfo sm = typeof(SomeClass).GetMethod("SomeMethod");
string methodName = sm.Name;
Here methodName will be the string "SomeMethod". (Of course, in this simple case we've used the class name to get the MethodInfo object, so it's somewhat circular and we might as well have just used the hard-coded "SomeMethod" string instead!)

Make ReSharper reformat code according to StyleCop rule SA1206: The 'static' keyword must come before the 'other' keyword in the element declaration

I suspect I should create a pattern in ReSharper / Options / Languages / C# / Formatting Style / Type Membership Layout for this. I am currently using the default pattern and I would like some help from someone who is good at them.
I want this to be WRONG:
public new static Age Empty {
get {
return empty;
}
set {
empty = value;
}
}
And this to be right:
public static new Age Empty {
get {
return empty;
}
set {
empty = value;
}
}
In other words, I want static to come before other keywords, like new. Currently ReSharper 5.1 does it the "wrong" way.
ReSharper now has the possibility to configure the arrangement of modifiers.
Open the ReSharper options and go to Code Editing | C# | Code Style. In the Modifiers section Modifiers order you can reorder the arrangement according to your needs (see ReSharper online help: Arranging Modifiers).
To satisfy StyleCop's rule SA1206 move the static modifier above the new modifier:
It is impossible.

Resources