Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to read specific file from configuration?(reading a file)
How to read specific file from configuration? [message #1022576] Fri, 22 March 2013 06:41 Go to next message
funky funky is currently offline funky funkyFriend
Messages: 3
Registered: March 2013
Junior Member
I have a file that contains a list of config files. Files from the list need to have the same grammar file.

As an example, I have a file <myfile.txt> that has these contents:

CONFIG_PATH="D:\options.config";
...
<<some other configurations here>>
...


I have created a grammar file that can get the value of the CONFIG_PATH. But.. How can I read the contents of file specified in CONFIG_PATH <options.config>?
And <options.config> should be using the same grammar file with <myfile.txt>. Basically, the intention is that it should behave similar to "import" in Java or "#include" in C language. Is this possible with xtext?

How can I read the <options.config> file?

Also, there are some settings that I want to be defined in <myfile.txt> only, and should not be defined in <options.config>. Is there a way to get the filename or file extension where the setting is defined? (e.g. CONFIG_PATH should only be in a *.txt file)

Any suggestion is much appreciated.
Re: How to read specific file from configuration? [message #1022937 is a reply to message #1022576] Fri, 22 March 2013 21:03 Go to previous messageGo to next message
Steve Kallestad is currently offline Steve KallestadFriend
Messages: 62
Registered: March 2013
Member
If you use importURI= in your grammar it happens automagically. The docs have a working example grammar near the beginning - either the 5 minute example or the 15 minute one.
Re: How to read specific file from configuration? [message #1023750 is a reply to message #1022937] Mon, 25 March 2013 04:00 Go to previous messageGo to next message
funky funky is currently offline funky funkyFriend
Messages: 3
Registered: March 2013
Junior Member
Hi Steve,

Thank you for that information.

I've looked in the docs here and I have added a new rule in my grammar file.

Config_path: 
   'CONFIG_PATH' '=' importURI = STRING
;


In my myfile.txt, I added this line:
CONFIG_PATH="../temp/config.path"


I was expecting it to work, but after executing, I'm getting an error mismatched input '""' expecting RULE_STRING.

I tried to change the double quotes to single quotes and I was able to execute it successfully. But when I check the generated file, the configuration inside config.path is not generated. I was expecting it to automagically read the config.path file. Or is there some settings/code I still need to do?

Re: How to read specific file from configuration? [message #1023790 is a reply to message #1023750] Mon, 25 March 2013 07:00 Go to previous messageGo to next message
Steve Kallestad is currently offline Steve KallestadFriend
Messages: 62
Registered: March 2013
Member
You know...

I followed the same tutorial.

I was going to present what I'm doing, but I decided to test it a little more thoroughly first.

Apparently, in my project, imports are happening automatically for any file contained in the project regardless of whether I have an import statement or not.

But any file not included in the same project is not being imported, regardless of how I phrase the import statement or the grammar.

I'm sorry, but I'm not much help here, and quite frankly I'm very quickly growing tired of the documentation. Simple things like this should not be difficult to get a firm grasp on.

Re: How to read specific file from configuration? [message #1023794 is a reply to message #1023790] Mon, 25 March 2013 07:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

first: the error look like an ambigous grammar.
second: for the import uri scoping you have to enable

fragment = scoping.ImportURIScopingFragment {}
fragment = exporting.SimpleNamesFragment {}


and disable

//fragment = scoping.ImportNamespacesScopingFragment {}
//fragment = exporting.QualifiedNamesFragment {}
//fragment = types.TypesGeneratorFragment {}
//fragment = xbase.XbaseGeneratorFragment {}


this: this only influences visibility. what is general there is only the stuff in the project.
(aka the container and the visible containers of it)

for further reading have a look http://blogs.itemis.de/stundzig/archives/773

if you have the feeling that something is missing in the docs please feel free to file a specific enhancement request.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to read specific file from configuration? [message #1023845 is a reply to message #1023794] Mon, 25 March 2013 09:44 Go to previous messageGo to next message
Steve Kallestad is currently offline Steve KallestadFriend
Messages: 62
Registered: March 2013
Member
Christian Dietrich wrote on Mon, 25 March 2013 03:15
Hi,

first: the error look like an ambigous grammar.
second: for the import uri scoping you have to enable

fragment = scoping.ImportURIScopingFragment {}
fragment = exporting.SimpleNamesFragment {}


and disable

//fragment = scoping.ImportNamespacesScopingFragment {}
//fragment = exporting.QualifiedNamesFragment {}
//fragment = types.TypesGeneratorFragment {}
//fragment = xbase.XbaseGeneratorFragment {}


this: this only influences visibility. what is general there is only the stuff in the project.
(aka the container and the visible containers of it)

for further reading have a look http://blogs.itemis.de/stundzig/archives/773

if you have the feeling that something is missing in the docs please feel free to file a specific enhancement request.

~Christian


The results from this are kind of funny.

If I change that code fragment, not only do I get automatic unwanted and undeclared imports from the same project, but from any project the entire workspace.

I'd be able to manage this problem if I could implement a scope provider that was actually aware of which resource it was grabbing data from, but even though the information is clearly available there's no way that I can see of getting to it.

My frustration with the documentation is that something simple like importing files should be covered clearly and it's not. There's a section on importing that covers one particular use case embedded in a tutorial, but
1) it's easy for a new user to glance over that tutorial,
2) there's a lot more to it than is covered in the tutorial,
3) so a new user is relegated to searching forums and googling
4) that frequently results in examples for other versions of xtext which may or may not be applicable
5) not only that, but the links for information in files generated by xtext are all 404s
and finally
6) I've run into this at literally every turn while learning xtext.

It's not something that can be covered in a bug report. My only recourse is to curse whatever it was that made me decide I needed an IDE to go with a simple scripting language.

I've been around here for two or three weeks now. You'd think that would be enough time for me to answer a straightforward and common use case question that I should have covered myself in the first few days, but all that my attempt resulted in was finding a glaring bug in my own project.

[Updated on: Mon, 25 March 2013 09:50]

Report message to a moderator

Re: How to read specific file from configuration? [message #1023870 is a reply to message #1023845] Mon, 25 March 2013 10:44 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
So you want more tutorials / xtext by example and less reference doku?
if there are dead links please file a bug for that


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to read specific file from configuration? [message #1031892 is a reply to message #1023794] Tue, 02 April 2013 10:40 Go to previous message
funky funky is currently offline funky funkyFriend
Messages: 3
Registered: March 2013
Junior Member
Hi Christian,

I have solved the error regarding the RULE_STRING.

And I have commented out the lines you mentioned.. but still xtext seems its not able to read the "import" file.

Here's my mwe file. Is there something I need to add/remove?
 // Java API to access grammar elements (required by several other fragments)
                fragment = grammarAccess.GrammarAccessFragment {}
    
                // generates Java API for the generated EPackages
                fragment = ecore.EcoreGeneratorFragment {
                // referencedGenModels = "
                //  platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel,
                //  platform:/resource/org.eclipse.xtext.common.types/model/JavaVMTypes.genmodel
                // "
                }
    
                // Serializer 2.0
                fragment = serializer.SerializerFragment {
                	generateStub = false
                }
                
                // the serialization component (1.0)
                // fragment = parseTreeConstructor.ParseTreeConstructorFragment {}
    
                // a custom ResourceFactory for use with EMF
                fragment = resourceFactory.ResourceFactoryFragment {
                    fileExtensions = file.extensions
                }
    
                // The antlr parser generator fragment.
                fragment = parser.antlr.XtextAntlrGeneratorFragment {
                //  options = {
                //      backtrack = true
                //  }
                }
    
                // java-based API for validation
                fragment = validation.JavaValidatorFragment {
                //    composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
                //    composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
                }
    
                // scoping and exporting API
                fragment = scoping.ImportURIScopingFragment {}
                fragment = exporting.SimpleNamesFragment {}
    
                // scoping and exporting API
                // fragment = scoping.ImportNamespacesScopingFragment {}
                // fragment = exporting.QualifiedNamesFragment {}
                fragment = builder.BuilderIntegrationFragment {}
    
                // generator API
                fragment = generator.GeneratorFragment {
                    generateMwe = false
                    generateJavaMain = true
                }
    
                // formatter API
                fragment = formatting.FormatterFragment {}
    
                // labeling API
                fragment = labeling.LabelProviderFragment {}
    
                // outline API
                fragment = outline.OutlineTreeProviderFragment {}
                fragment = outline.QuickOutlineFragment {}
    
                // quickfix API
                fragment = quickfix.QuickfixProviderFragment {}
    
                // content assist API
                fragment = contentAssist.JavaBasedContentAssistFragment {}
    
                // generates a more lightweight Antlr parser and lexer tailored for content assist
                fragment = parser.antlr.XtextAntlrUiGeneratorFragment {}
    
                // generates junit test support classes into Generator#pathTestProject
                fragment = junit.Junit4Fragment {}
    
                // project wizard (optional)
                // fragment = projectWizard.SimpleProjectWizardFragment {
                //      generatorProjectName = "${projectName}"
                //      modelFileExtension = file.extensions
                // }
    
                // rename refactoring
                fragment = refactoring.RefactorElementNameFragment {}
    
                // provides the necessary bindings for java types integration
                // fragment = types.TypesGeneratorFragment {}
    
                // generates the required bindings only if the grammar inherits from Xbase
                // fragment = xbase.XbaseGeneratorFragment {}
    
                // provides a preference page for template proposals
                fragment = templates.CodetemplatesGeneratorFragment {}
    
                // provides a compare view
                fragment = compare.CompareFragment {
                     fileExtensions = file.extensions



By the way,I agree with Steve. Although the documentation is great, it would be better to have specific tutorials also that would make using xtext heavenly. Smile (Something similar to perldocdotperldotorg will be much appreciated)

Another question of mine, is it possible to get the filename of the file that xtext is currently reading? Myfile.txt is not a constant filename, so I would like to be able to know if I am reading from Myfile.txt or the one defined in the config_path.

Thank you for the support, I appreciate it.
Previous Topic:Get referenced object in another model (cyclic resolution or no corresponding inode)
Next Topic:Multiple Validation Classes -- Dynamic
Goto Forum:
  


Current Time: Thu Apr 18 00:57:17 GMT 2024

Powered by FUDForum. Page generated in 0.02327 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top