XTEXT 2 Customized Rename Refactoring Example - xtext

Is anyone out there who has an example of a customized rename refactoring example in xtext ?
I guess it has to be similar to the customized syntax highlighting, binding some classes override some implementations and then crawl trough the EObjects you want to rename.
But i don't know where to start, has anyone an idea ? Or is there even someone who has allready implemented a customized rename refactoring in xtext ?
kind regards,
Example: If i do rename, the ruleName of a Rule, i also want to rename the ruleReferenceName of the RuleReference
Rule:
ruleName=(RuleName)':' ruleContent=RuleContent ';'
;
RuleContent:
ruleReferences+=RuleReference
;
RuleReference:
ruleReferenceName=RuleName (cardinality=Cardinality)?
;
RuleName:
value=RuleReferenceNameTerminal
;

I guess what i first planned to do isn't intended by the xtext rename refactoring. So I took again a closer look at the crossreference concept. I tried rename refactoring through crossreferencing earlier, but stumbled across the fact that i didn't had an "ID" Terminal defined. What solved my issue was to let the crossreference know which terminal rule it should use and to set the name-attribute at the right place.
Here is what the grammar should look like to have the rename refactoring work like i wanted it to(note the square brackets and the name attribute). No binding and overriding needed at all.
Rule:
ruleName=(RuleName)':' ruleContent=RuleContent ';'
;
RuleContent:
ruleReferences+=RuleReference
;
RuleReference:
ruleReferenceName=[RuleName | RuleReferenceNameTerminal] (cardinality=Cardinality)?
;
RuleName:
name=RuleReferenceNameTerminal
;
It is important to know that the " | " between the square brackets is not an alternative.

Related

Antlr Indirect Left Recursion

I've seen this question asked multiple times, and also seen people "solve" it... but it either confused me or didn't solve my specific situation:
Here's approximately what's going on:
block: statement*;
statement: <bunch of stuff> | expressionStatement;
expression_statement: <more stuff, for example> | method_invoke;
method_invoke: expression LEFT_PAREN <args...> RIGHT_PAREN block;
expression: <bunch of stuff> | expression_statement;
Everything inside of the expression_statement that starts with an expression uses indirect left recursion that I do not know how to fix while still being able to use those syntaxes as statements so they'll be usable in blocks(It is possible to do something like Print("hello world");
On it's own(a statement), but also do something like int c = a + b.getValue() as a part of an expression(an expression)...
How would I handle it differently?
If you need more info please let me know and I'll try my best to provide
I knew that to solve Indirect Left-Recursion I'd have to duplicate one or more of the rules... I hoped there'd be a better way to handle it then what is written online and also said here, but there isn't. I ended up doing that and it worked, thank you

How to implode an embedded choice of alternative symbols?

I have this syntax definition
syntax RuleData
= rule_data: ID+ RulePart '-\>' (Command|RulePart)+ Message? Newlines
;
Which for the most part doesn't cause me an issue, the only problem is that I'm not sure how to implode (Command|RulePart)+, I looked through the Rascal docs but I didn't find anything on how to define "Union" types.
This is what my ADT looks like currently
data RULEDATA
= rule_data(list[str] prefix, list[RULEPART] left, list[???] right, list[str] message, str)
;
The ??? is the bit where it could either be a RulePart (which, for the sake of simplicity, is a list[str]) or a Command (which is a str).
Turns out I was overcomplicating the whole thing. Instead of trying to have a union of types I simply added an additional construction to RULEPART that could accommodate COMMAND. I think I might have been also initially confused about some of the issues because I had bugs in other parts of the code that I was misinterpreting as being caused by this problem.
data RULEDATA
= rule_data(list[str] prefix, list[RULEPART] left, list[RULEPART] right, list[str] message, str)
;
data RULEPART
= part(list[RULECONTENT] contents)
| command(str command)
;

How to 'subtract' lexer rules in ANTLR?

Let's say I have two rules like the below:
printable_characters : '\u0020' .. '\uFFEF' ;
newline_characters : '\n' | '\r' ;
Now let's say that I want to make a new rule called printable_no_newlines. I would like to do this by subtracting newline_characters from printable_characters like so:
printable_no_newlines : printable_characters - newline_characters ;
That syntax doesn't work in ANTLR3 but does anyone know what the best way would be to emulate this without re-typing the entire rule?
I don't think this is possible. I'm also skeptical that it would do what you want: for example, your printable_new_newlines would include "foo\nbar", since it matches printable_characters, but does not match newline_characters (as that only matches one-character strings).

Help with Shift/Reduce conflict - Trying to model (X A)* (X B)*

Im trying to model the EBNF expression
("declare" "namespace" ";")* ("declare" "variable" ";")*
I have built up the yacc (Im using MPPG) grammar, which seems to represent this, but it fails to match my test expression.
The test case i'm trying to match is
declare variable;
The Token stream from the lexer is
KW_Declare
KW_Variable
Separator
The grammar parse says there is a "Shift/Reduce conflict, state 6 on KW_Declare". I have attempted to solve this with "%left PrologHeaderList PrologBodyList", but neither solution works.
Program : Prolog;
Prolog : PrologHeaderList PrologBodyList;
PrologHeaderList : /*EMPTY*/
| PrologHeaderList PrologHeader;
PrologHeader : KW_Declare KW_Namespace Separator;
PrologBodyList : /*EMPTY*/
| PrologBodyList PrologBody;
PrologBody : KW_Declare KW_Variable Separator;
KW_Declare KW_Namespace KW_Variable Separator are all tokens with values "declare", "naemsapce", "variable", ";".
It's been a long time since I've used anything yacc-like, but here are a couple of suggestions that may or may not help.
It seems that you need a 2-token lookahead in this situation. The parser gets to the last PrologHeader, and it has to decide whether the next construct is a PrologHeader or a PrologBody, and it can't tell that from the KW_Declare. If there's a directive to increase lookahead in this situation, it will probably solve the problem.
You could also introduce context into your actions: rather than define PrologHeaderList and PrologBodyList, define PrologRuleList and have the actions throw an error if a header appears after a body. Ugly, but sometimes you have to do it: what appears simple in a grammar may not be simple in the generated parser.
A hackish approach might be to combine the tokens: rather than KW_Declare and KW_Variable, have your lexer recognize the space and use KW_Declare_Variable. Since both are keywords, you're not going to run into namespace collision problems.
The grammar at the top is regular so IIRC you can plot it out as a DFA (or a NDA and convert it to a DFA) and then convert the DFA to a grammar. It's bean a while so I'll leave the work as an exercise for the reader.

Create a Print Function

I'm learning Bison and at this time the only thing that I do was the rpcalc example, but now I want to implement a print function(like printf of C), but I don't know how to do this and I'm planning to have a syntax like this print ("Something here");, but I don't know how to build the print function and I don't know how to create that ; as a end of line. Thanks for your help.
You first need to ask yourself:
What are the [sub-]parts of my 'print ("something");' syntax ?
Once you identify these parts, "simply" describe them in the form of grammar syntax rules, along with applicable production rules. And then let Bison generate the parser for you; that's about it.
To put you on your way:
The semi-column is probably a element you will use to separate statemements (such a one "call" to print from another).
'print' itself is probably a keyword, or preferably a native function name of your language.
The print statement appears to take a literal string as [one of] its arguments. a literal string starts and ends with a double quote (and probably allow for escaped quotes within itself)
etc.
The bolded and italic expressions above are some of the entities (the 'symbols' in parser lingo) you'll likely need to define in the syntax for your language. For that you'll use Bison grammar rules, such as
stmt : print_stmt ';' | input_stmt ';'| some_other_stmt ';' ;
prnt_stmt : print '(' args ')'
{ printf( $3 ); }
;
args : arg ',' args;
...
Since the question asked about the semi-column, maybe some confusion was from the different uses thereof; see for example above how the ';' belong to your language's syntax whereby the ; (no quotes) at the end of each grammar rule are part of Bison's language.
Note: this is of course a simplistic implementation, aimed at showing the essential. Also the Bison syntax may be a tat off (been there / done it, but a long while back ;-) I then "met" ANTLR never to return to Bison, although I do see how its lightweight and fully self contained nature can make it appropriate in some cases)

Resources