Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Preserving comments
Preserving comments [message #769188] Wed, 21 December 2011 14:39 Go to next message
Lasse  indg is currently offline Lasse indgFriend
Messages: 12
Registered: July 2009
Junior Member
I'd like to preserve the comments the user enters and output them in the target code.
Since my users will be java developers, I'd like to be able to use /* */ and // comments.

I have a familiar looking grammar, where I have defined all STRING tokens to be comments. That works, but I am not happy with it, since it is quite confusing that you can also enter java comments.

What is the easiest way to preserve comments?

grammar mydsl.SchemaDSL with org.eclipse.xtext.common.Terminals

generate schemaDSL <<no links>>

SchemaDSL:
	PackageDeclaration
	imports+=Import* 
	elements+=Entity*;

PackageDeclaration:
	comment=Comment?
	'namespace' name=QualifiedName alias=ValidID;

Import:
	comment=Comment?
	'import' importedNamespace=QualifiedNameWithWildCard;

Entity:
	comment=Comment?
	'element' name=ValidID ('extends' superType=Entity)? '{'
		features+=Feature*
	'}';

/* snip -- more grammar like that */

Comment: STRING;

Re: Preserving comments [message #769231 is a reply to message #769188] Wed, 21 December 2011 16:01 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2011-21-12 15:39, Lasse indg wrote:
> I'd like to preserve the comments the user enters and output them in the
> target code.
> Since my users will be java developers, I'd like to be able to use /* */
> and // comments.
>
> I have a familiar looking grammar, where I have defined all STRING
> tokens to be comments. That works, but I am not happy with it, since it
> is quite confusing that you can also enter java comments.
>
> What is the easiest way to preserve comments?

What you do not want to do is to make the comments part of the syntax
and described in the grammar (this because you probably want them
between every other element).

You have to decide on which comments you actually want to keep and how
to map them from their location in the source to something relevant in
what you generate.

In cloudsmith/geppetto at github I do something similar for parsing of
documentation comments (which look like regular comments, only that they
are placed immediately before certain documentable statements.

You can take a look at that code if interested. It assigns the parsed
documentation to the semantic objects via an adapter.

I can post more details if you are interested. (I think I posted on this
subject before in this forum too - so you may find more information if
you search for it.

Regards
- henrik
>
> grammar mydsl.SchemaDSL with org.eclipse.xtext.common.Terminals
>
> generate schemaDSL <<no links>>
>
> SchemaDSL:
> PackageDeclaration
> imports+=Import* elements+=Entity*;
>
> PackageDeclaration:
> comment=Comment?
> 'namespace' name=QualifiedName alias=ValidID;
>
> Import:
> comment=Comment?
> 'import' importedNamespace=QualifiedNameWithWildCard;
>
> Entity:
> comment=Comment?
> 'element' name=ValidID ('extends' superType=Entity)? '{'
> features+=Feature*
> '}';
>
> /* snip -- more grammar like that */
>
> Comment: STRING;
>
>
Re: Preserving comments [message #769264 is a reply to message #769231] Wed, 21 December 2011 17:13 Go to previous message
Lasse  indg is currently offline Lasse indgFriend
Messages: 12
Registered: July 2009
Junior Member
Hi,

Thank you for your reply, it didn't help me directly, but it inspired my to look a couple of new places, so I managed to solve it.

I looked at the DomainmodelJvmModelInferrer that comes with the domainmodel example.


		acceptor.accept(
			e.toClass( e.fullyQualifiedName ) [
				documentation = e.documentation


So it was just a question of replicting that behaviour in my more plain vanilla IGenerator:

class XmlGenerator implements IGenerator {
	@Inject	IEObjectDocumentationProvider documentationProvider

        // snip

	def compile(Entity e) '''
		<!-- «documentationProvider.getDocumentation(e)» -->
                <xsd:complexType name="«e.name»">
                // snip
        '''
}


After that I could remove the comment=Comment? from all the elements in my grammar.

Hope this helps someone.

/Lasse
Previous Topic:Things we lost from Xpand ?
Next Topic:Xtext editor to insert single proposals automatically without showing content assist popup
Goto Forum:
  


Current Time: Fri Apr 19 21:55:45 GMT 2024

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

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

Back to the top