How does lab.antlr.org get the parser rule and alternative number? - antlr4

I have been playing with ANTLR Lab (really nice by the way) and was wondering how it is able to label the matching parser rule and alternative number. For example, below content:1, x_tag:2 and x_tag:3 are all rule names and the number after the colon is the alternative within that rule.
I have built a recognizer from my grammar but cannot see from looking at the runtime API how to access them from within a custom listener.

AFAIK, ANTLR Lab and the ANTLR IntelliJ plugin both use the RuleContextWithAltNum to set and get the alternative.
Note that it is only implemented in Java. From the documentation:
A handy class for use with options {contextSuperClass=org.antlr.v4.runtime.RuleContextWithAltNum;} that provides a backing field / impl for the outer alternative number matched for an internal parse tree node. I'm only putting into Java runtime as I'm certain I'm the only one that will really every use this.
Also see the related stackoverflow Q&A: Is there a way to know which alternative rule ANTLR parser is currently in?

Related

Build a CodeEffect Rule

Is there a way to build a ruleset in codeeffects from a string? The default way over the ASP/MVC RuleEditor is currently not an option.
You need to use RuleXML if you want to build your rules dynamically outside of our editor. It's a common way of creating rules in Code Effects. String representation is too generic to be useful in rule generation. Details on RuleXML can be found here.
Also, please take a look at SourceXML; it allows you to generate your source objects dynamically, too. This is a MUCH more flexible approach for source handling that plain .NET objects. Details can be found here.

Any way in Expression Engine to simulate Wordpress' shortcode functionality?

I'm relatively new to Expression Engine, and as I'm learning it I am seeing some stuff missing that WordPress has had for a while. A big one for me is shortcodes, since I will use these to allow CMS users to place more complex content in place with their other content.
I'm not seeing any real equivalent to this in EE, apart from a forthcoming plugin that's in private beta.
As an initial test I'm attempting to fake shortcodes by using delimited strings (e.g. #foo#) in the content field, then using a regex to pull those out and pass them to a function that can retrieve the content out of EE's database.
This brings me to a second question, which is that in looking at EE's API docs, there doesn't appear to be a simple means of retrieving the channel entries programmatically (thinking of something akin to WP's built-in get_posts function).
So my questions are:
a) Can this be done?
b) If so, is my method of approaching it reasonable? Or is there something stupidly obvious I'm missing in my approach?
To reiterate, my main objective here is to have some means of allowing people managing content to drop a code in place in their content that will be replaced with channel content.
Thanks for any advice or help you can give me.
Here's a simple example of the functionality you're looking for.
1) Start by installing Low Replace.
2) Create two Global Variables called gv_hello and gv_goodbye with the values "Hello" and "Goodbye" respectively.
3) Put this text into the body of an entry:
[say_hello]
Nice to see you.
[say_goodbye]
4) Put this into your template, wrapping the Low Replace tag around your body field.
{exp:low_replace
find="[say_hello]|[say_goodbye]"
replace="{gv_hello}|{gv_goodbye}"
multiple="yes"
}
{body}
{/exp:low_replace}
5) It should output this into your browser:
Hello
Nice to see you.
Goodbye
Obviously, this is a really simple example. You can put full blown HTML into your global variable. For example, we've used that to render a complex, interactive graphic that isn't editable but can be easily dropped into a page by any editor.
Unfortunately, due to parse order issues, EE tags won't work inside Global Variables. If you need EE tags in your short code output, you'll need to use Low Variables addon instead of Global Variables.
Continued from the comment:
Do you have examples of the kind of shortcodes you want to support/include? Because i have doubts if controlling the page-layout from a text-field or wysiwyg-field is the way to go.
If you want editors to be able to adjust layout or show/hide extra parts on the page, giving them access to some extra fields in the channel, is (imo) much more manageable and future-proof. For instance some selectfields, a relationship (or playa) field, or a matrix, to let them choose which parts to include/exclude on a page, or which entry from another channel to pull content from.
As said in the comment: i totally understand if you want to replace some #foo# tags with images or data from another field (see other answers: nsm-transplant, low_replace). But, giving an editor access to shortcodes and picking them out, is like writing a template-engine to generate ee-template code for the ee-template-engine.
Using some custom fields to let editors pick and choose parts to embed is, i think, much more manageable.
That being said, you could make a plugin to parse the shortcodes from a textareas content, and then program a lot, to fetch data from other modules you want to support. For channel entries you could build out of the channel data library by objectiveHTML. https://github.com/objectivehtml/Channel-Data
I hear you, I too miss shortcodes from WP -- though the reason they work so easily there is the ubiquity of the_content(). With the great flexibility of EE comes fewer blanket solutions.
I'd suggest looking at NSM Transplant. It should fit the bill for you.
There is also a plugin called Shortcode, which you can find here at
Devot-ee
A quote from the page:
Shortcode aims to allow for more dynamic use of content by authors and
editors, allowing for injection of reusable bits of content or even
whole pieces of functionality into any field in EE

What is the use of ".# " in groovy?

What is the use of .# in groovy? Can anyone explain me with a code snippet?
Have you seen the official documentation? It contains nice code samples.
Essentially, when you use normal . operator, you access fields indirectly, using implicitly generated getters/setters. However, .# allows you to access the field directly, skipping getter/setter.
This can be useful when you want to avoid some additional logic implemented in getter/setter and change the field directly. Violates tons of OOP principles, but the authors of Groovy found this construct to be useful.
That's the Java Field operator (according to the documentation)
There are examples in the documentation.
It is also used for accessing attributes when you are parsing XML (again, there's an example if you follow that link).

Complex Orchard Layer Rules Support

Is it possible to use more complicated layer url rule matching syntax? I want to be able to choose the layer to display based on a more regex type rule that matches the rules I have set up in my custom routing for my module.
I would like to be able to acheive something along the lines of:
url('~/my-{\w*}/something/{\w*}')
It's not available ootb, but could be pretty easy to implement yourself in a custom module (if you don't want to alter the core code).
It can be implemented as a slight modification to existing Orchard.Widgets.RuleEngine.UrlRuleProvider, so regexes would be also taken into account.
Just create an implementation of IRuleProvider, name your function as eg. 'urlregex' (so it wouldn't collide with the existing 'url', processed by UrlRuleProvider) and do all the processing stuff inside Process(RuleContext ruleContext) method. It's a very simple class to implement and would involve just a few lines of code - take a look at the default url rule provider I mentioned at the beginning.

wizard that create code in multiple programming languages

as part of our application i need to build component that will output code in php and asp
that have the same functionality ( and maybe latter jsp ) .
how can i design this kind of component to be generic as possible ?
You need to be able to create a parse tree out of all input languages.
For each output language, you will need to create a set of tree transforming grammar.
You may also require a runtime library to help translate routines that are not available in your output language.
I would personally go with the Strategy Pattern. You could have a master code maker class and instantiate it with a strategy.
PHPCodeStrategy
ASPCodeStrategy
Then each would have a method called execute maybe. Bu then you could add further strategies down the line and extend your application.
Andrew

Resources