I'm using xbase
grammar xolang.Xolang with org.eclipse.xtext.xbase.Xbase
and an
XBlockExpression
where I expect to write my code
When write an expression like
val c = 1 + 1
Then I get the following error
The method +(int) is undefined
The same happens with Strings. In fact it happens with every binary operator: ==, < , >, ... Now I know that xbase supports operator overloading, maybe that has something to do with it? (Maybe I still have to define somewhere that you can add numbers?)
I solved my problem.
You need to add the xbase.lib jarfile (the right version of course) to the buildpath of the project that uses your DSL.
If you wonder how you can easily add the xbase lib in Eclipse: Right Click on your Project that makes use of your DSL, select Build-Path->Add Libraries and choose XTend Library. The XTend Library includes the correct XBase Lib.
Related
I am building a Xtext DSL and I want to embed Xbase expressions in some specific places to interpret part of my models using the Xbase interpreter, but I am not able to have method completion in the generated editor.
I reused the examples provided here: https://www.eclipse.org/Xtext/documentation/201_sevenlang_introduction.html, and manage to integrate Xbase as part of my grammar. Keyword completion proposal is working fine (i.e. do, for, while ...), but I can't find a way to have completion for Java/Xbase methods (e.g. newArrayList, or myArray.add(X)).
Clarification from comments below: if I write var x = newArrayList in the editor the method is not styled in italic but I don't have any error either.
This is a sample version of the grammar I am using:
grammar org.xtext.example.common2.Common2 with org.eclipse.xtext.xbase.Xbase
generate common2 "http://www.xtext.org/example/common2/Common2"
import "http://www.eclipse.org/xtext/xbase/Xbase"
Test returns Test:
{Test}
'test'
expressions+=Script
;
Script returns XBlockExpression:
{Script}
'{'
(expressions+=XExpressionOrVarDeclaration ';'?)*
'}'
;
I found out that if I change my grammar to the following one I can have the completion as expected:
grammar org.xtext.example.common2.Common2 with org.eclipse.xtext.xbase.Xbase
generate common2 "http://www.xtext.org/example/common2/Common2"
import "http://www.eclipse.org/xtext/xbase/Xbase"
Test returns XBlockExpression:
{Test}
'test'
expressions+=Script
;
Script returns XBlockExpression:
{Script}
'{'
(expressions+=XExpressionOrVarDeclaration ';'?)*
'}'
;
My guess is that all the tree must be composed of instances of XExpression to enable the completion, but I don't understand why? To me Test should not be a subclass of XBlockExpression (in my real-world use case Test has additional attributes/references), but it should contain an XBlockExpression.
Is there a way to achieve this? Any help/resource to look at would be much appreciated
Note
I already checked this SO question How to embed XBase expressions in an Xtext DSL, I already have xbase.lib in my build path.
I want to delete statements,like "#include" in C source file. Therefore how can I match such statements using clang AST matchers or using clang's provided API for C++ ?
Unfortunately, it's not possible because #includes are preprocessor directives and not statements. They are not part of the AST and that's why AST matchers are of no use.
You might want to use clang's Preprocessor mechanism of callbacks. First you need define your callback, which should be an instance of PPCallbacks, override method InclusionDirective, and register your callback using addPPCallbacks.
I hope this information will be helpful to you in solving your problem. Happy hacking with Clang!
I am trying to learn a bit about Vala and wanted to create a Calculator to test how Gtk worked. The problem is that I coded everything around the supposition that there would be a way to parse a string that contained the required operations. Something like this:
string operation = "5+2/3*4"
I have done this with Python and it is as simple as using the compilers parser. I understand Python is math oriented, but I thought that perhaps there would be Vala library waiting for me as an answer... I haven't found it if it does exist, but as I was looking at the string documentation, I noticed this part:
/* Strings prefixed with '#' are string templates. They can evaluate
* embedded variables and expressions prefixed with '$'.
* Since Vala 0.7.8.
*/
string name = "Dave";
println (#"Good morning, $name!");
println (#"4 + 3 = $(4 + 3)");
So... I thought that maybe there was a way to make it work that way, maybe something like this:
stdout.printf(#"$(operation)")
I understand that this is not an accurate supposition as it will just substitute the variable and require a further step to actually evaluate it.
Right now the two main doubts I am having are: a) Is there a library function capable of doing this? and b) Is it possible to work out a solution using string templates?
Here's something I found that would do the work. I used the C++ libmatheval library, for this I first required a vapi file to bind it to Vala. Which I found here. There are a lot of available vapi files under the project named vala-extra-apis, and they are recognized in GNOME's Vala List of Bindings although they are not included at install.
You could parse the expression using libvala (which is part of the compiler).
The compiler creates a CodeContext and runs the Vala parser over a (or several) .vala file(s).
You could then create your own CodeVisitor decendant class that visits the necessary nodes of the parse tree and evaluates expressions.
As far as I can see there is no expression evaluator that does this, yet. That is because normally vala code is translated to C code and the C compiler then does compile time expression evaluation or the finished executable does the run time evaluation.
Python is different, because it is primarily a scripting language and has evaluation build directly into the runtime / interpreter.
I'm developing a xtext-based language which should refer to objects defined in a vendor-specific file format.
E.g. this file format defines messages, my language shall define Rules that work with these messages. Of course i want to use xtext features e.g. to autocomplete/validate message names, attributes etc.
Not sure if that is a good idea, but I came up with the following:
Use one xtext project to describe the file format
Add a dependency for this project to my DSL project, import the file format grammar to my grammar
import the description files via importURI
FileFormat grammar:
grammar com.example.xtext.fileformat.FileFormat;
generate fileformat "http://xtext.example.com/fileformat/FileFormat"
[...]
DSL grammar:
grammar com.example.xtext.dsl.DSL;
import "http://xtext.example.com/fileformat/FileFormat" AS ff;
Model:
rules += Rule*;
Rule: ImportFileRule | SampleRule;
ImportFileRule: "IMPORT" importURI=STRING "AS" name=ID ";";
SampleRule: "FORWARD" msg=[ff::Message] ";"
First of all: This works fine.
Now, different imported files may define messages with colliding names,
and I want to use fully qualified names for messages anyways.
The prefix for the message names should be defined in my DSL, e.g. the name of the ImportFileRule.
So I would like to use something like:
IMPORT "first-incredibly-long-filename-with-version-and-stuff.ff" AS first;
IMPORT "second-incredibly-long-filename-with-version-and-stuff.ff" AS second;
FORWARD first.msg_1; // references to msg_1 in first file
FORWARD second.msg_1; // references to msg_1 in second file
Unfortunately I don't see a easy way to achieve this with xtext.
At the moment I'm using a ID for the namespace qualifier and custom ProposalProvider/Validator classes,
which is ugly in detail and bypasses the EMF index, becoming slow with files of 1000 messages and 50000 attributes...
Would there be a right way to do it?
Was it a good idea to use xtext to parse the definition files in the first place?
I have two ideas what to check.
Xtext has a specific global scope provider called ImportedNameSpaceAwareScopeProvider. By using an overridden version of this, you could specify other headers to consider.
Check the implementation of the xtext grammar itself, as it supports such a feature with EPackage imports. I am not exactly sure, how it operates, but should work this way.
Finally, I ended up using the SimpleNamesFragment, ImportURIScopingFragment and a custom ScopeProvider derived from AbstractDeclarativeScopeProvider.
That way, I had to implement ScopeProvider methods for quiet a few rules but was much more flexible in using my "namespace prefix".
E.g. it is simple to implement syntaxes like
FORWARD FROM first: msg_01, msg_02;
I want to make my Application configuration as F# file wich will be compiled to dll. (like XMonad configuration with xmonad.hs on haskell). I found it more interesting and just better way then using XML serialization.
So is there any way how can I compile single file (or more) to Library with some configuration like
module RNExcel.Repository
open RNExcel.Model
type ExpenseReportRepository() =
member x.GetAll() =
seq{ yield {Name="User1"
Role="someRole"
Password = "123321"
ExpenseLineItems =
[{ExpenseType="Item1"
ExpenseAmount="50"};
{ExpenseType="Item2"
ExpenseAmount="50"}]}
yield {Name="User2"
Role="Estimator"
Password = "123123"
ExpenseLineItems =
[{ExpenseType="Item1"
ExpenseAmount="50"};
{ExpenseType="Item2"
ExpenseAmount="125"}]} }
my idea was to run shell .... and msbuild the project , but I don't think it will works for every user with .net 4
Check out the F# Power Pack, specifically the FSharp.CodeDom library. You can use this library to compile F# code at run-time and, with a little Reflection thrown in, can likely achieve your goal of code-as-configuration with minimal fuss.
I think that using CodeDOM provider from the PowerPack as Ben suggested is a way to go. I'd just like to add a few things (and it didn't fit into the comment box).
To parse and compile the F# code with the configuration, you need just to compile the source file that the users write using F# PowerPack. The compilation part of PowerPack is complete and works just fine. It invokes the F# compiler under the cover and gives you the compiled assembly back. The only problem is that the users of your application will need to have F# compiler installed (and not just the redist).
The incomplete part of F# CodeDOM provider is generating F# code from CodeDOM trees (because the trees were not designed to support F#), but that's not needed in this case.