|
|
Re: how to resolve import files when using in standalone mode [message #534330 is a reply to message #534216] |
Tue, 18 May 2010 16:54 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi,
your code looks fine. Is "a.dsl" located in the same folder as "b.dsl"?
Do you get any exceptions when loading "b.dsl"?
Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Am 18.05.10 13:51, schrieb Yp Li:
> hi,
> I am new to XText. I try to use the parser in standalone mode, however,
> it do not resole imported files automatically, how to make the parser to
> parser the imported files?
> using the Xtext entity demo as an example:
> // grammar Model :
> (imports+=Import)*
> (elements+=Type)*;
>
> Import :
> 'import' importURI=STRING;
>
> Type:
> SimpleType | Entity;
>
> SimpleType:
> 'type' name=ID;
>
> Entity :
> 'entity' name=ID ('extends' extends=[Entity])? '{'
> properties+=Property*
> '}';
> ......
>
>
> DSL files:
>
> // file a.dsl
> entity A{}....
>
> // file b.dsl
> import "a.dsl"
>
> entity B extends A{}
>
>
> // standalone code
> new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../ ");
> Injector injector = new
> MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet =
> injector.getInstance(XtextResourceSet.class);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
> Resource resource = resourceSet.getResource(
> URI.createURI("b.dsl"), true);
> Model model = (Model) resource.getContents().get(0);
> // it do not resolve a.dsl here and get null pointer error.
|
|
|
|
|
|
|
|
|
Re: how to resolve import files when using in standalone mode [message #672084 is a reply to message #671933] |
Thu, 19 May 2011 20:28 |
Johan Missing name Messages: 3 Registered: May 2011 |
Junior Member |
|
|
Hi Christian,
Thanks for your quick reply! When cleaning up the code before sending it to you, it started to work. I have no clue why, but while I am not wiser I am at least happier.
I believe the key was in a previous response to a similar question, where it was stated that the resourceset must contain all the file that are referenced.
Anyway, maybe there will be similar questions in the future so I figure I might as well show how my command line class for using xtext as standalone .
The part of the grammar that relates to imports look like this (the origin of the code is ORCC project available at Sourceforge and the language is CAL):
///////////////////////////////////////////////////////////////////////////////
// top-level entity
///////////////////////////////////////////////////////////////////////////////
AstEntity:
('package' package=QualifiedName ';')?
(imports += Import)*
(('actor' name=ID actor=AstActor) | ('unit' name=ID unit=AstUnit));
Import: 'import' importedNamespace=QualifiedNameWithWildCard ';';
QualifiedName:
ID ('.' ID)*;
QualifiedNameWithWildCard:
QualifiedName '.*'?;
====================================
Some sample code is shown below. The function "square" is imported from a separate file "funcs.cal".
File "Add.cal":
------------------------------------------
package SimpleExamples;
import SimpleExamples.funcs.*;
actor Add () int A, int B ==> int Result:
int t = square(2); //<-- THIS WERE THE IMPORTED FUNCTION IS USED.
action A:[a], B:[b] ==> Result:[a + b]
end
end
File "funcs.cal":
------------------------------------------
package SimpleExamples;
unit funcs :
function square(int i) --> int :i*i end //<-- DEFINITION OF FUNCTION TO BE EXPORTED
end
=============================================
Now, to the interesting part (well, well). The command line is expecting a list of files, i.e. something like "FrontendCli *.cal". The main part of this code relates to populating the resourceset with all the files.
public class FrontendCli {
public static void main(String[] args) {
Injector injector = new CalStandaloneSetup().createInjectorAndDoEMFRegistration();
FrontendCli fe = injector.getInstance(FrontendCli.class);
fe.compileActors(args);
}
@Inject
private XtextResourceSet resourceSet;
public void compileActors(String[] args) {
List<String> paths = new ArrayList<String>();
Multimap<String, URI> uris = new HashMultimap<String, URI>();
for (int i = 0; i<args.length; i++) {
File f = new File(args[i]);
paths.add(f.getAbsolutePath());
uris.put(paths.get(i), URI.createFileURI(paths.get(i)));
}
ContainersStateFactory containersStateFactory = new ContainersStateFactory();
IAllContainersState containersState = containersStateFactory.getContainersState(paths, uris);
resourceSet.eAdapters().add(new DelegatingIAllContainerAdapter(containersState));
Collection<URI> values = Sets.newHashSet(uris.values());
for (URI uri : values) {
resourceSet.createResource(uri);
}
boolean hasErrors = false;
Resource resource = (Resource) resourceSet.getResources().get(0);
IResourceValidator v = ((XtextResource) resource).getResourceServiceProvider().getResourceValidator();
List<Resource> resources = Lists.newArrayList(resourceSet.getResources());
for (Resource res : resources) {
List<Diagnostic> errors = res.getErrors();
if (!errors.isEmpty()) {
for (Diagnostic error : errors) {
System.err.println("Diagnostics" + error);
}
hasErrors = true;
}
try {
res.load(null);
List<Issue> result = v.validate(res, CheckMode.ALL, null);
for (Issue issue : result) {
switch (issue.getSeverity()) {
case ERROR:
System.err.println(issue.getMessage());
break;
case WARNING:
System.out.println(issue.getMessage());
break;
case INFO:
System.out.println(issue.getMessage());
break;
}
}
} catch (IOException e) {
System.err.println("Couldn't load resource (" + res.getURI() + ")" + e);
}
}
for (Resource res : resources) {
AstEntity entity = (AstEntity) res.getContents().get(0);
try {
if (!hasErrors) {
String result = new UglyPrinter(entity).compile();
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
A few of the steps in populating the resourceSet are more or less copy-past and are possible not the simplest way.
Thanks, again. Sometimes having someone to explain to is all it takes to solve the problems;-)
Ciao,
Johan
|
|
|
Powered by
FUDForum. Page generated in 0.06211 seconds