ANTLR4 Tokenizing a Huge Set of Keywords - antlr4

I want to embed some known identifier names into my grammar e.g. the class names of my project are known and I want to tell the lexer what identifiers are known keywords that actually belongs to the class-name token. But since I have a long list of class names (hundreds of names), I don't want to create a class-name lexer rule by listing all the known class name keywords in the rule, that will make my grammar file too large.
Is it possible to place my keywords into a separate file? One possibility I am thinking about is to place the keywords in a java class that will be subclassed by the generated lexer class. In that case, my lexer's semantic predicate can just call a method in custom lexer superclass to verify if the input token matches my long list of names. And my long list can be placed inside that superclass src code.
However, in the ANTLR4 book it says grammar options 'superClass' for combined grammar only set the parser's superclass. How can I set my lexer's superclass if I still want to use combined grammar. Or is there any other better method to put my long list of keywords into a separate "keyword file".

If you want each keyword to have its own token type, you can do the following:
Add a tokens{} block to the grammar to create tokens for each keyword. This ensures unique token types are created for each of your keywords.
tokens {
Keyword1,
Keyword2,
...
}
Create a separate class MyLanguageKeywords similar to the following:
private static final Map<String, Integer> KEYWORDS =
new HashMap<String, Integer>();
static {
KEYWORDS.put("keyword1", MyLanguageParser.Keyword1);
KEYWORDS.put("keyword2", MyLanguageParser.Keyword2);
...
}
public static int getKeywordOrIdentifierType(String text) {
Integer type = KEYWORDS.get(text);
if (type == null) {
return MyLanguageParser.Identifier;
}
return type;
}
Add an Identifier lexer rule to your grammar that handles keywords and identifiers.
Identifier
: [a-zA-Z_] [a-zA-Z0-9_]*
{_type = MyLanguageKeywords.getKeywordOrIdentifierType(getText());}
;

Related

IntelliJ Live Template for dart named constructor: lists class' fields

I want to generate a custom named constructor in Dart.
I have many dto class to implement and each should provide a named constructor like: ClassName.fromMap().
For example for this class:
class Student {
final String name;
final int age;
}
The generated constructor should be:
Student.fromMap(Map<String, dynamic> map) :
name = map['name'],
age = map['age'];
How can I retrieve the list of the field of my current class as strings? Is that even possibile?
Of course I can have a variable number of fields.
My template looks like:
$CLASS$.fromMap(Map<String, dynamic> map) :
$INITIALIZATION_LIST$
binding $CLASS$ to dartClassName().
Now I'd like to bind $INITIALIZATION_LIST$ to something like:
getClassFieldList().forEach((fieldName) => "$fieldName = map['$fieldName']")
Can I achieve something like that?
There is no way to retrieve a list of Dart class fields using predefined live template functions. You can try developing your own template macro for this. See https://intellij-support.jetbrains.com/hc/en-us/community/posts/206201699-create-a-new-expression-for-a-live-template-for-actionscript for some hints.
Existing live template functions implementations can be found at https://github.com/JetBrains/intellij-community/tree/master/platform/lang-impl/src/com/intellij/codeInsight/template/macro.
You can also try using Structural Search and Replace instead of live template

Xtext DSL editor referring to the Java classes defined in my model

I have set of java classes and I want to refer them in the DSL which I am trying to edit using Xtext grammar. e.g. There is a class Employee :
class Employee{
String name;
double salary;
String organisation;
String status;
}
Now in my DSL I wish to use them. Hence when user is trying the name he should get these tree attributes as help. e.g. DSL i will like to trite like this:
employee.salary > 100000 then employee.status='GOLD'
My question is when i am typing the name such as employee than all the three attributes should be available to user as context help.

Is it possible to change keyword for cross referencing between grammar rules/objects in Xtext?

When I want to make cross referencing between grammar rules in Xtext work, I need to use keyword name for that. E.g.:
Constant:
name=NAME_TERMINAL "=" number=Number;
ConstUsage:
constant=[Constant | NAME_TERMINAL];
Is it possible to change this word to another one (e.g. id) ? I need it e.g. in case when I have rule, which uses parameter name for something else.
you can use a custom implementation of IQualifiedNameProvider e.g. by subclassing DefaultDeclarativeQualifiedNameProvider.
public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider{
QualifiedName qualifiedName(Element e) {
Package p = (Package) e.eContainer();
return QualifiedName.create(p.getName(), e.getId());
}
}
see https://dietrich-it.de/xtext/2011/07/16/iqualifiednameproviders-in-xtext-2-0.html for the complete example

Impex Export: Colon in multivalue attribute is escaped by double backslash - How to remove this behavior?

Hybris: 6.3.0.0-SNAPSHOT (the behavior is the same with 6.3.0.21)
When exporting impex, we noticed a difference when exporting a non-multivalue Type attribute versus exporting a multivalue Type attribute.
When exporting String attribute data without colon, a non-multivalue attribute can be exported as Experts, while a multivalue attribute can be exported as Experts|Hybris.
When exporting Type with String attribute data with colons (e.g. URL), the colon is escaped with a double backslash (for multivalue only). A non-multivalue attribute can be exported as https://experts.hybris.com, while a multivale attribute can be exported as https\://experts.hybris.com if there is only 1 value or as https\://experts.hybris.com|https\://help.hybris.com if there are 2 values.
How can I stop the export from escaping the colon? Is there a method I can override to change this behavior? I would like to change the result to https://experts.hybris.com|https://help.hybris.com or to "https://experts.hybris.com"|"https://help.hybris.com".
Business Case: We want to copy the URL from the exported impex, but the URL contains double backslashes. The exported impex is not meant to be reimported.
Notes #`: The URLs are stored in a collection (e.g. Product.newAttribute, where newAttribute is a collection of custom types which has a String). So, the Impex header looks something like "INSERT_UPDATE Product;newAttribute(data)"
Notes #2: (UPDATE: Didn't work) Currently, I'm checking if it's possible with a CSVCellDecorator; this is for import only.
Notes #3: Currently, I'm checking if it's possible with AbstractSpecialValueTranslator.
For this specific case, I created a new translator, extending AbstractValueTranslator. Then, I implemented the exportValue method, joining the string data (which are URLs), without escaping them.
public String exportValue(final Object value) throws JaloInvalidParameterException
{
String joinedString = "";
if (value instanceof Collection)
{
final Collection valueCollection = (Collection) value;
if (!valueCollection.isEmpty())
{
final ArrayList<CustomType> list = (ArrayList<CustomType>) valueCollection;
final StringJoiner joiner = new StringJoiner("|");
for (final CustomType customType : list)
{
// data is a URL
joiner.add(customType.getData());
}
// value would be something like "https://experts.hybris.com|https://help.hybris.com"
joinedString = joiner.toString();
}
}
return joinedString;
}
Reference:
Customization: https://help.hybris.com/1808/hcd/ef51040168d743879c015b7de232ce40.html
I think that might not be possible, since the colon is used to separate keys for referenced types. As in
...;catalogVersion(catalog(id),version);...
...;myCatalog:Staged;...
Why not run search/replace on the result?

Declare a constant class field

I'm supposed to declare a class field for a federal tax rate, which is a constant, with a value .07. How can I achieve this?
Since you didn't specify a language, I'll throw in a Java solution
public class YourClass {
public static final double TAX_RATE = 0.07;
}
In Java naming convention, you name your constants with all upper casing and use _ to separate words instead of the normal camel casing.
the final keyword, is to make it, well, final - meaning unchangeable
Anything not withing a block like a constructor or a method is considered a field.

Resources