| Limiting cross-reference to a single file [message #1758920] |
Tue, 04 April 2017 11:30  |
Eclipse User |
|
|
|
Hi all, I'm using the below grammar at the moment:
Model:
(
package=PackageDeclaration
imports+=ImportDeclaration*
types+=Type*
)?
;
PackageDeclaration:
'namespace' name=FQN
;
ImportDeclaration:
'use' name=FQN
;
TypeType:
Primitive | TypeRef
;
TypeRef:
ref=[Type | FQN]
;
Type:
FunctionType | ClassType | EnumType
;
FunctionType:
'func' name=ID ':' returnType=TypeType LBRACE
RBRACE
;
ClassType:
'class' name=ID ('parent' superType=TypeRef)? ('inherits' templates+=TypeRef*)? LBRACE
RBRACE
;
EnumType:
'enum' name=ID LBRACE
values+=ID*
RBRACE
;
/* Terminals and strings. */
Primitive:
name=('int' |
'long' |
'short' |
'float' |
'double' |
'str' |
'char' |
'bool' |
'obj' |
'byte' |
'nil')
;
FQN:
ID ('.' ID)*
;
terminal LBRACE: '{' ;
terminal RBRACE: '}' ;
When I run this, I can happily create something like the following:
func TestFunction: RandomType {
}
But oh no! I get an error, because RandomType doesn't exist. Fair enough, so I'm easily able to define RandomType in the file. This is what I want to happen.
What actually happens, is that I can open up a completely different file, and define RandomType, and the other file will automatically remove the error, because it found a RandomType.
The problem is, my program is able to see across multiple files, which I don't want to happen, unless someone uses the ImportDeclaration function which I have also got in the grammar above. Is there a simple way I can limit the cross-referencing capabilities of Xtext?
Thanks for any and all help.
[Updated on: Tue, 04 April 2017 11:31] by Moderator
|
|
|
|
|
|
|
|
| Re: Limiting cross-reference to a single file [message #1758941 is a reply to message #1758936] |
Tue, 04 April 2017 13:30   |
Eclipse User |
|
|
|
Funnily enough sorry, the sample model is this model. I posted them as seperate questions as they refer to different issues entirely.
As for an example in practice, one might do the following:
File 1: first_file.ext
namespace random.folders // see how this is a FQN?
use random.folders.second_file // also a FQN
class ChildClass parent second_file.ParentClass {
// ChildClass MUST be an ID, not a FQN,
// however parent can be either an ID or a FQN.
// By definition, the FQN's include ID's anyway.
// There is no actual definition of ParentClass in this file,
// so I would rather be referencing it like so, instead of just typing:
// "parent ParentClass". Because at the moment, it accepts that
// as valid, which if you compare that to a regular language like Java,
// "ParentClass" does not exist in this file, so in reality, it SHOULDN't be correct.
// the entire "parent" thing here, is a foreign reference.
}
File 2: second_file.ext
namespace random.folders
class ParentClass {
// Again, "ParentClass" is an ID, not a FQN.
}
// Anything below here is just to represent LOCAL references.
class Test { }
class TestChild parent Test { } // Notice how the parent is technically an ID because its local?
The same thing goes for enums as it does for the class. Names are ID's, parents (which the enum doesnt have) are FQN's.
Hope your able to understand my lack of proper articulation. I appreciate your help.
[Updated on: Tue, 04 April 2017 13:34] by Moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.09890 seconds