Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Cross referencing pre-defined "terminals"
icon9.gif  Cross referencing pre-defined "terminals" [message #1758809] Mon, 03 April 2017 08:28 Go to next message
Eclipse UserFriend
Hi all.

I'm working on a DSL and have come across a problem.
I want to have syntax that goes off of this:

namespace fully.qualified.name

public class Test parent SuperClass inherits ImplementType {
        int i;
        Test j;
        fully.qualified.name.Test k;
}


The class portion works fine, except when I define a variable, the bottom two work fine, except int will fail.

Below is my DSL so far:

Model:
	package=PackageDeclaration
	imports+=(ImportDeclaration)*
	types+=Type*
;

PackageDeclaration:
  	'namespace' name=FQN
;

ImportDeclaration:
	'use' name=FQN
;

FQN: ID ('.' ID)*;

TypeType:
	Primitive | FQN
;

Primitive:
	'int' |
	'float'
;

Type:
	ClassType | EnumType
;

Field:
	VariableDefinition | JavaStatement
;

ClassType:
	modifiers+=Modifier* 'class' name=ID
	('parent' superType=[Type | FQN])?
	('inherits' implementations+=[Type | FQN]*)?
	LBRACE fields+=Field* RBRACE
;

EnumType:
	modifiers+=Modifier* 'enum' name=ID LBRACE values+=ID* RBRACE
;

VariableDefinition:
	modifiers+=Modifier* type=[Type | TypeType] name=ID
;

JavaStatement:
	'java' statement=STRING
;

Modifier:
	'public' |
	'static'
;

terminal LBRACE: '{';
terminal RBRACE: '}';


The important part is
type=[Type | TypeType]
where the variable rule makes cross-reference to either a Type, a Fully Qualified Name, or a primitive that doesn't need previous defining.

This however fails, and I have no idea how to solve it. So my question is, how can I make cross-reference to types, fully qualified names AND primitives without defining them initially, in Xtext?
Re: Cross referencing pre-defined "terminals" [message #1758841 is a reply to message #1758809] Mon, 03 April 2017 12:08 Go to previous messageGo to next message
Eclipse UserFriend
Hi you cannot cross reference terminals

=> you need something like

AbstractTypeRef: PrimType | TypeRef;
PrimType: type=Primitive;
TypeRef: ref=[Type|QualifiedName];
Re: Cross referencing pre-defined "terminals" [message #1758921 is a reply to message #1758841] Tue, 04 April 2017 11:34 Go to previous messageGo to next message
Eclipse UserFriend
Hey.
Thanks for the help, I seem to partially have it working. I have asked another question here: https://www.eclipse.org/forums/index.php/m/1758920/#msg_1758920 about referencing things. The grammar I've got going at the moment is as so:

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: '}' ;


Except now my issue is, is that qualified names will not work when I reference them in the FunctionType. If you try it, it will say:

Couldn't resolve reference to Type 'foo.foo'.

Any ideas?

[Updated on: Tue, 04 April 2017 11:35] by Moderator

Re: Cross referencing pre-defined "terminals" [message #1758938 is a reply to message #1758921] Tue, 04 April 2017 13:24 Go to previous messageGo to next message
Eclipse UserFriend
Can you please share a complete sample model file
Re: Cross referencing pre-defined "terminals" [message #1758977 is a reply to message #1758938] Tue, 04 April 2017 20:39 Go to previous message
Eclipse UserFriend
Since this goes over both the questions on the forum, The uploaded grammar model file counts for both questions.
Previous Topic:With operator in Xbase?
Next Topic:Xtext multiple parameter to a method possibility
Goto Forum:
  


Current Time: Sat Jul 05 12:16:36 EDT 2025

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

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

Back to the top