Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to write correct parser rules using JavaVMTypes
How to write correct parser rules using JavaVMTypes [message #754961] Sat, 05 November 2011 21:26 Go to next message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
Hello everyone,

I am working on an extension of Java in Xtext however I am having difficulties using references to the JavaVMTypes.
References work perfect for the code like:
	import java.lang.System;
	...
	java.lang.System.currentMilliseconds();
	System.currentMilliseconds();

However the editor shows errors when trying to parse code like:
	System.out.println();

The error is: "Couldn't resolve reference to JvmType 'System.out'".
My question is how do I write correct parser rules that allow references to public static fields in classes such as System.out?

Relevant parts of my grammar are as follows:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.xbase.Xtype

generate myDsl "h ttp://www.xtext.org/example/mydsl/MyDsl"

import "h ttp://www.eclipse.org/emf/2002/Ecore" as ecore
import "h ttp://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes

CompilationUnit
	:	// A compilation unit starts with an optional package definition
		package=PackageDeclaration?
		imports+=Import*
		typeDefinitions+=TypeDefinition*
	;
	
Import:
	'import' importedNamespace=QualifiedNameWithWildCard ';' ;

TypeDefinition
    :   JavaClassDeclaration
    ;

expression returns JavaExpression
    :   JavaBasicExpression
    ;

//.... the usual other expression rules

JavaUnaryExpressionNotPlusMinus returns JavaExpression
	:	{JavaUnaryExpressionNotPlusMinus} operator='~' unaryExpr=JavaUnaryExpression
	|	{JavaUnaryExpressionNotPlusMinus} operator='!' unaryExpr=JavaUnaryExpression
	|	JavaPrimaryExpression ({JavaUnaryExpressionNotPlusMinus.primary=current} selectors+=JavaSelector+)? ('++'|'--')?
	;

JavaPrimaryExpression returns JavaExpression
    :   parExpression
    |   {JavaIdentifierReference} target=IdentifierReference  //suffix=JavaIdentifierSuffix?
    ;
JavaSelector returns JavaExpression
    :   {JavaMethodCall} '.' methodName=ID arguments=JavaArguments // Method Call
    ;

IdentifierReference
:	typeRef=JvmTypeReference
;

JvmVoid returns jvmTypes::JvmVoid:
	{jvmTypes::JvmVoid} 'void'
;
JvmTypeReference returns jvmTypes::JvmParameterizedTypeReference:
    JvmParameterizedTypeReference =>({JvmGenericArrayTypeReference.componentType=current} '['']')* 
  ;
JavaVariable:
	name=ID
;
//.... other rules


I tried different adjustments to the grammar without any success.

How can I solve this issue?
Thanks in advance.

[Updated on: Sun, 06 November 2011 23:19]

Report message to a moderator

Re: How to write correct parser rules using JavaVMTypes [message #755017 is a reply to message #754961] Sun, 06 November 2011 20:57 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Ivan,

if you want to use the dot '.' notation for static fields, you'll have
to model class names as trees and customize the scoping heavily. In
Xbase we use the '::' as delimiter for static qualifiers.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

On 05.11.11 22:26, ivan2007 wrote:
> Hello everyone,
>
> I am working on an extension of Java in Xtext however I am having
> difficulties using references to the JavaVMTypes.
> References work perfect for the code like:
>
> import java.lang.System;
> ...
> java.lang.System.currentMilliseconds();
> System.currentMilliseconds();
>
> However the editor shows errors when trying to parse code like:
>
> System.out.println();
>
> The error is: "Couldn't resolve reference to JvmType 'System.out'".
> My question is how do I write correct parser rules that allow references
> to public static fields in classes such as System.out?
>
> Relevant parts of my grammar are as follows:
>
>
> grammar org.xtext.example.mydsl.MyDsl with
> org.eclipse.xtext.common.Terminals
>
> generate myDsl "h ttp://www.xtext.org/example/mydsl/MyDsl"
>
> import "h ttp://www.eclipse.org/emf/2002/Ecore" as ecore
> import "h ttp://www.eclipse.org/xtext/common/JavaVMTypes" as jvmTypes
>
> CompilationUnit
> : // A compilation unit starts with an optional package definition
> package=PackageDeclaration?
> imports+=Import*
> typeDefinitions+=TypeDefinition*
> ;
>
> Import:
> 'import' importedNamespace=QualifiedNameWithWildCard ';' ;
>
> TypeDefinition
> : JavaClassDeclaration
> ;
>
> expression returns JavaExpression
> : JavaBasicExpression
> ;
>
> //.... the usual other expression rules
>
> JavaUnaryExpressionNotPlusMinus returns JavaExpression
> : {JavaUnaryExpressionNotPlusMinus} operator='~'
> unaryExpr=JavaUnaryExpression
> | {JavaUnaryExpressionNotPlusMinus} operator='!'
> unaryExpr=JavaUnaryExpression
> | JavaPrimaryExpression
> ({JavaUnaryExpressionNotPlusMinus.primary=current}
> selectors+=JavaSelector+)? ('++'|'--')?
> ;
>
> JavaPrimaryExpression returns JavaExpression
> : parExpression
> | {JavaIdentifierReference} target=IdentifierReference
> //suffix=JavaIdentifierSuffix?
> ;
> JavaSelector returns JavaExpression
> : {JavaMethodCall} '.' methodName=ID arguments=JavaArguments // Method Call
> ;
>
> JvmVoid returns jvmTypes::JvmVoid:
> {jvmTypes::JvmVoid} 'void'
> ;
> JvmTypeReference returns jvmTypes::JvmParameterizedTypeReference:
> type=[jvmTypes::JvmType|QualifiedName]
> // ('.' ID)* // Doesn't work and leads to a ambiguous grammar
> ;
> IdentifierReference
> : typeRef=JvmTypeReference
> ;
> //.... other rules
>
>
> The obvious problems are in the expression rules namely
> JavaPrimaryExpression, JavaSelector, IdentifierReference and
> JvmTypeReference. I tried different adjustments to the grammar without
> any success.
>
> How can I solve this issue?
> Thanks in advance.
Re: How to write correct parser rules using JavaVMTypes [message #755023 is a reply to message #755017] Sun, 06 November 2011 23:18 Go to previous messageGo to next message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
Thanks Sebastian,

My language must be able to parse existing Java code so this is a requirement.
I will try to model the class names as trees.

I have another question on how to differentiate between a method invocation on a type or on a variable referring to an object.
The following code should be parsable:
  System.currentTimeMillis();
  obj.executeMethod();

How can I differentiate between the two?
Should I be able to solve this also by customize the scoping?

Changing the "IdentifierReference" rule to the following doesn't work.
IdentifierReference:	
	typeRef=JvmTypeReference | objRef=[JavaVariable]
;

Please give me some tips on the direction I should go.

~ Goran
Re: How to write correct parser rules using JavaVMTypes [message #755436 is a reply to message #755023] Tue, 08 November 2011 14:02 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Goran,

You can't using scoping and linking to help the parser, the parsing has
finished before scoping and linking starts. Your only chance is to use
the case of the identifier: the first char of a class names should be
upper case. Hence, you can define two rules, one for each case (pun
intended).

I know this is a convention only, but it should work for 99.99% of Java
code.

Hallvard

On 07.11.11 00.18, Goran wrote:
> Thanks Sebastian,
>
> My language must be able to parse existing Java code so this is a
> requirement.
> I will try to model the class names as trees.
>
> I have another question on how to differentiate between a method
> invocation on a type or on a variable referring to an object.
> The following code should be parsable:
>
> System.currentTimeMillis();
> obj.executeMethod();
>
> How can I differentiate between the two?
> Should I be able to solve this also by customize the scoping?
>
> Changing the "IdentifierReference" rule to the following doesn't work.
>
> IdentifierReference:
> typeRef=JvmTypeReference | objRef=[JavaVariable]
> ;
>
> Please give me some tips on the direction I should go.
>
> ~ Goran
>
Re: How to write correct parser rules using JavaVMTypes [message #755938 is a reply to message #755436] Thu, 10 November 2011 09:23 Go to previous message
Goran   is currently offline Goran Friend
Messages: 45
Registered: November 2011
Member
Thanks Hallvard,

I guess this is the way to go indeed.

~Goran
Previous Topic:[xtend2] instancefactory
Next Topic:Highlighting with Xtext
Goto Forum:
  


Current Time: Sat Apr 27 00:00:11 GMT 2024

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

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

Back to the top