How can we invoke a Java UDF in XText Grammar? - java

I am working on a DSL wherein I am supposed to call Java Functions I have written. How can they be called in XText Grammar definition rules ?
Example
Sample.xtext
Data:
'Data'':'
(objectRules += ObjectRule)+ //Call to Java Function here
;
I am writng the grammar and I want to invoke Java Function to perform further processing like pasting a block of code when the Rule is encountered.

Please refer to the documentation on Xbase if you want to use Java from your Xtext languages.
The pattern would be something like this:
grammar org.acme.MyDsl with org.eclipse.xtext.xbase.Xbase
generate ..
MyConcept:
operation=ID '(' ')' body = XBlockExpression
;
This would allow things like
myOperation() {
System.out.println("")
}

Related

How to implement imports within an xtext grammar

I want to be able to create libraries with blocks in them, import the library in a separate file and then use the blocks from the library in a blockUse statement.
I have created the following grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Program:
(imports+=Import)*
(libraries+=Library)*
(customBlocks+= Block)*
(blockUses+= BlockUse)*
;
Import:
'import' importedNamespace=QualifiedNameWithWildcard
;
QualifiedNameWithWildcard:
QualifiedName '.*'?
;
QualifiedName:
ID ('.' ID)*
;
Library:
'Library' name=QualifiedName
(blocks+=Block)+
'EndLibrary'
;
Block:
'block' name=ID
;
BlockUse:
'show' block=[Block|QualifiedName]
;
My end goal is that I could create two files, one with a library, and one using blocks from that library like this:
File 1:
Library lib1
block block1
block block2
EndLibrary
File 2:
import lib1.*
show block1
I believe the bulk of my confusion comes from the meaning behind importedNamespace and using cross references
These are some of the examples I have already tried to follow:
http://www.eclipse.org/Xtext/documentation/102_domainmodelwalkthrough.html#add-imports
http://blog2.vorburger.ch/2013/05/xtext-dsl-with-epackage-namespace.html
https://blogs.itemis.com/en/in-five-minutes-to-transitive-imports-within-a-dsl-with-xtext
imported namespaces shall not be references. thus you either have to change this
Import:
'import' importedNamespace=QualifiedNameWithWildcard
;
or you need to adapt the calculation of the namespace resolvers e.g. in org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getImportedNamespace(EObject) and use the nodemodel to retrieve the import text there.
Make sure the project has Xtext Builder and Xtext Nature and Build Automatically is enabled and you did a clean build.

ANTLR error 134

I'm trying to build Abstract Syntax Tree for Java in Python with antlr4 package.
I've downloaded Java grammar from
https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4
I want to use that grammar file to produce JavaLexer and JavaParser for Python2.
When I say
"$ antlr4 -Dlanguage=Python2 Java8.g4"
an error occured.That error is
error(134): Java8.g4:73:0: symbol type conflicts with generated code in target language or runtime
NOTE: I've deleted parts with Character.isJavaIdentifierPart(). Because these lines is not proper for python and i will use just ASCII.
Python has built-in function called type. Antlr4 prints an error for line 73 of the grammar:
type
: primitiveType
| referenceType
;
Looks like there is a name conflict and you have to rename type to something else in your grammar.

Velocity parser crashes when parsing java code template

When trying to use a java source code as template for Velocity, it crashes at this line of the template:
/* #see panama.form.Validator#validate(java.lang.Object) */
with this Exception:
Exception in thread "main" org.apache.velocity.exception.ParseErrorException: Lexical error, Encountered: "l" (108), after : "." at *unset*[line 23, column 53]
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1301)
at org.apache.velocity.runtime.RuntimeInstance.evaluate(RuntimeInstance.java:1265)
at org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:199)
Apparently it takes the #validate for a macro and crashes when it tries to parse the arguments for the macro. Is there anything one could do about this?
I'm using Velocity 1.7.
Edit
I know I could escape the # characters in the template files, but there are quite a number of them which also might change now and then, so I would prefer a way that would not require manual changes on the files.
First option
Try this solution from here: Escaping VTL Directives
VTL directives can be escaped with the backslash character ("\") in a manner similar to valid VTL references.
## #include( "a.txt" ) renders as <contents of a.txt>
#include( "a.txt" )
## \#include( "a.txt" ) renders as #include( "a.txt" )
\#include( "a.txt" )
## \\#include ( "a.txt" ) renders as \<contents of a.txt>
\\#include ( "a.txt" )
Second option
You have this tool [EscapeTool][2].
Tool for working with escaping in Velocity templates.
It provides methods to escape outputs for Java, JavaScript, HTML, XML and SQL. Also provides methods to render VTL characters that otherwise needs escaping.
Third option:
You may also try this workaround, I didn't use it but it should work:
You can at the beginning read your template as a String and then pre-parse it. For example replace all # with \#, or add to the beginning of file
#set( $H = '#' )
$H$H
see this answer: How to escape a # in velocity And then from that pre-parsed String create Template by using this answer: How to use String as Velocity Template?

Need help compilig an ANTLR grammar file

im new with ANTLR and I don´t know how to compile my grammar.
I´m using the v4.4 of ANTLR with a .g4 file that contains:
grammar JayGrammar;
program: KEYWORD_VOI KEYWORD_MAI SEPARATOR_PAB SEPARATOR_PCD (declarations statements);
declarations: (declaration)*;
declaration: (type identifiers);
type: (KEYWORD_INT | KEYWORD_BOO);
identifiers: (IDENTIFIER)*;
statements: (statement)*;
statement: (block | assignment | ifstatement | whilestatementk);
block: SEPARATOR_LAB statements SEPARATOR_LCD;
assignment: (IDENTIFIER OPERATOR_IGU expression);
ifstatement: KEYWORD_IF SEPARATOR_PAB expression SEPARATOR_PCD statement (KEYWORD_ELS statement)?;
whilestatementk: KEYWORD_WHI SEPARATOR_PAB expression SEPARATOR_PCD statement;
expression: conjunction ((OPERATOR_O) conjunction)*;
conjunction: relation ((OPERATOR_Y) relation)*;
relation: addition ((OPERATOR_REL) addition)*;
addition: term ((OPERATOR_SUM|OPERATOR_RES) term)*;
term: negation ((OPERATOR_POR|OPERATOR_DIV) negation)*;
negation:(OPERATOR_NO) factor;
factor: IDENTIFIER|LITERAL|SEPARATOR_PAB expression SEPARATOR_PCD;
INPUTELEMENT: (WHITESPACE|TOKEN);
WHITESPACE: (' '|'\t'|'\r'|'\n'|'\f');
TOKEN: (IDENTIFIER|KEYWORD_BOO|KEYWORD_ELS|KEYWORD_IF|KEYWORD_MAI|KEYWORD_VOI|KEYWORD_WHI|LITERAL
|SEPARATOR_COM|SEPARATOR_LAB|SEPARATOR_LCD|SEPARATOR_PAB|SEPARATOR_PCD|SEPARATOR_PYC
|OPERATOR_REL|OPERATOR_DIV|OPERATOR_IGU|OPERATOR_NO|OPERATOR_O|OPERATOR_POR|OPERATOR_RES|OPERATOR_SUM|OPERATOR_Y);
LITERAL: (BOOLEAN INTEGER);
KEYWORD_BOO: BOOLEAN;
KEYWORD_ELS:'else';
KEYWORD_IF: 'if';
KEYWORD_INT: 'int';
KEYWORD_MAI: 'main';
KEYWORD_VOI: 'void';
KEYWORD_WHI: 'while';
BOOLEAN: ('true'|'false');
INTEGER: (DIGIT+);
IDENTIFIER: (LETTER (LETTER| DIGIT)*);
DIGIT: ('0'..'9')+;
LETTER: ('a'..'z'|'A'..'Z')+;
SEPARATOR_PAB: '(';
SEPARATOR_PCD: ')';
SEPARATOR_LAB: '{';
SEPARATOR_LCD: '}';
SEPARATOR_PYC: ';';
SEPARATOR_COM: ',';
OPERATOR_IGU: ('=');
OPERATOR_SUM: ('+');
OPERATOR_RES: ('-');
OPERATOR_POR: ('*');
OPERATOR_DIV: ('/');
OPERATOR_REL: ('<'|'<='|'>'|'>='|'=='|'!=');
OPERATOR_Y: ('&&');
OPERATOR_O: ('||');
OPERATOR_NO: ('!');
I'll be really glad if someone can tell me how to create the lexer and the parser with the extension ".java" and then compile them to create the classes.
I´ve been using NetBeans but i´m really confused, If there is another IDE I can use or anything else, please help me.
This is explained in the Antlr4 "Getting Started" page.
Doing it by hand is as simple as this:
$ antlr4 YourGrammar.g4
$ javac YourGrammar*.java
... assuming that you have installed and configured a Java JDK and Antlr.
There are a number of ways to use Antrlr with the Netbeans IDE:
Just write / generate an Ant build script, and then modify it to include rules for running antlr4 etcetera. With allows you to incorporate Antlr into your builds without any IDE-specific integration.
Follow the instructions here on integrating Antlr into Netbeans,
Install and use the Antlrworks 2 plugin.

Optimization of Pig Script

I have written a ‘Pig Script’ which is processing Sequence files given as input.
It is working fine but there is one problem mentioned below.
I have repetitive statements in my pig script, as shown below:
Filtered_Data _1= FILTER BagName BY ($0 matches 'RegEx-1');
Filtered_Data_2 = FILTER BagName BY ($0 matches 'RegEx-2');
Filtered_Data_3 = FILTER BagName BY ($0 matches 'RegEx-3');
So on…
Question :
So is there any way by which I can have above statement written once and
then loop through all possible “RegEx” and substitute in Pig script.
For Example:
Filtered_Data _X = FILTER BagName BY ($0 matches 'RegEx'); ( have this statement once )
( loop through all possible RegEx and substitute value in the statement )
Right now I am calling Pig script from a shell script, so any way from shell script will be also be welcome or even Java wrapper...
Thanks in advance.
Happy Pigging!!!!

Categories

Resources