Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Whitespace-Aware Languages with unindented Single line comment
Whitespace-Aware Languages with unindented Single line comment [message #1748565] Thu, 24 November 2016 13:04 Go to next message
Stefan Thoeni is currently offline Stefan ThoeniFriend
Messages: 6
Registered: November 2016
Junior Member
Hello Folks,

I am currently working on a Whitespace-Aware Language (YAML-Style).

Now i would like to allow comments to have any indentation without it triggering the BLOCK_END synthetic. How would i do this properly?

e.g. in the homeautomation example i want my Grammar to accept following:
Device Window can be open, closed
 
Device Heater can be on, off, error
 
Rule 'Save energy' when Window.open then
//fire(Heater.off)
  fire(Heater.off)


Which with the untouched example project results in having the second fire(Heater.off) highlighted with the error missing RULE_BEGIN at 'fire'

Thanks for the input.

regards,
Stefan
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748692 is a reply to message #1748565] Sun, 27 November 2016 08:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10330
Registered: July 2009
Senior Member
you could try do adapt SL_COMMENT rule to eat up the newline before and not to eat up the newline after
this should at least work in the most cases

terminal SL_COMMENT:
('\n'|'\r')?'//' !('\n'|'\r')*
;
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748857 is a reply to message #1748692] Tue, 29 November 2016 12:09 Go to previous messageGo to next message
Stefan Thoeni is currently offline Stefan ThoeniFriend
Messages: 6
Registered: November 2016
Junior Member
Hi Christian,

Thanks for your response. Sadly this does not resolve the issue i have.

Referring to following home-automation example this still gives me the error "missing RULE_BEGIN at 'fire'":

Device Window can be open, closed
 
Device Heater can be on, off, error
 
Rule 'Save energy' when Window.open then
  fire(Heater.off)
// Some comment
  fire(Heater.off)


Do you have any other ideas on how to tackle this?

Regards,
Stefan

Edit: edited the example-syntax

[Updated on: Tue, 29 November 2016 12:19]

Report message to a moderator

Re: Whitespace-Aware Languages with unindented Single line comment [message #1748859 is a reply to message #1748857] Tue, 29 November 2016 12:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10330
Registered: July 2009
Senior Member
no i dont
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748918 is a reply to message #1748859] Wed, 30 November 2016 07:35 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 222
Registered: June 2013
Senior Member
I don't see your grammar, but ...
is it correct that fire(Heater.off) appears twice?
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748928 is a reply to message #1748918] Wed, 30 November 2016 09:02 Go to previous messageGo to next message
Stefan Thoeni is currently offline Stefan ThoeniFriend
Messages: 6
Registered: November 2016
Junior Member
Hi Uli,

Its the grammar of the Home-Automation example included in xText. The only thing i changed is the SL_COMMENT terminal as recommended by Christian.

For the sake of completeness:

/*******************************************************************************
 * Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
grammar org.eclipse.xtext.example.homeautomation.RuleEngine with org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
generate ruleEngine "http://www.eclipse.org/Xtext/example/RuleEngine"

Model:
	declarations+=Declaration*;
	
terminal SL_COMMENT:
  ('\n'|'\r' )  '//' !('\n'|'\r')*
;

Declaration:
	Device | Rule;

Device:
	'Device' name=ID 'can' 'be'
		(states+=State (',' states+=State)*)?;

State:
	name=ID ;

Rule:
	'Rule' description=STRING
		'when' deviceState=[State|QualifiedName]
		'then' thenPart=XBlockExpression;

// We modify the concrete syntax of two Xbase expressions and make them indentation-aware
XBlockExpression returns xbase::XExpression: 
	{xbase::XBlockExpression}
	BEGIN
		(expressions+=XExpressionOrVarDeclaration ';'?)*
	END;

XSwitchExpression returns xbase::XExpression:
	{xbase::XSwitchExpression}
	'switch' (=>('(' declaredParam=JvmFormalParameter ':') switch=XExpression ')'
		| =>(declaredParam=JvmFormalParameter ':')? switch=XExpression)
	BEGIN
		(cases+=XCasePart)*
		('default' ':' default=XExpression )?
	END;

// The following synthetic tokens are used for the indentation-aware blocks
terminal BEGIN: 'synthetic:BEGIN';  // increase indentation
terminal END: 'synthetic:END';      // decrease indentation
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748930 is a reply to message #1748928] Wed, 30 November 2016 09:07 Go to previous messageGo to next message
Stefan Thoeni is currently offline Stefan ThoeniFriend
Messages: 6
Registered: November 2016
Junior Member
So what i think would help is if i modify the SL_COMMENT terminal to consume not only the newline but all whitespaces before the comment. Something like this:

terminal SL_COMMENT:
WS? '//' !('\n'|'\r')*
;

But now when i open a .rule file in eclipse i get the error:
Failed to create the part's controls

java.lang.NullPointerException
at org.eclipse.xtext.ui.editor.toggleComments.SLCommentPrefixCalculator.caseGroup(SLCommentPrefixCalculator.java:43)
at org.eclipse.xtext.ui.editor.toggleComments.SLCommentPrefixCalculator.caseGroup(SLCommentPrefixCalculator.java:1)
at org.eclipse.xtext.util.XtextSwitch.doSwitch(XtextSwitch.java:225)
at org.eclipse.emf.ecore.util.Switch.doSwitch(Switch.java:53)
at org.eclipse.emf.ecore.util.Switch.doSwitch(Switch.java:69)
at org.eclipse.xtext.ui.editor.toggleComments.DefaultSingleLineCommentHelper.calculatePrefixes(DefaultSingleLineCommentHelper.java:54)
at org.eclipse.xtext.ui.editor.toggleComments.DefaultSingleLineCommentHelper.getDefaultPrefixes(DefaultSingleLineCommentHelper.java:48)
at org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration.getDefaultPrefixes(XtextSourceViewerConfiguration.java:249)
at org.eclipse.jface.text.source.SourceViewer.configure(SourceViewer.java:526)
at org.eclipse.ui.texteditor.AbstractTextEditor.createPartControl(AbstractTextEditor.java:3360)
.
.
.
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748949 is a reply to message #1748930] Wed, 30 November 2016 11:09 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 222
Registered: June 2013
Senior Member
... your rule only allows a single "then fire(heater.off)"

But the DSL file causing the error reads (excluding the comment) "then fire(heater.off) fire(heater.off)"
Re: Whitespace-Aware Languages with unindented Single line comment [message #1748959 is a reply to message #1748949] Wed, 30 November 2016 13:52 Go to previous messageGo to next message
Stefan Thoeni is currently offline Stefan ThoeniFriend
Messages: 6
Registered: November 2016
Junior Member
Hi Uli,

Thanks for your response. The repetition of Expressions or Variable declarations are not the problem here.

The then part of the rule 'Rule' consists of a XBlockExpression:
Rule:
	'Rule' description=STRING
		'when' deviceState=[State|QualifiedName]
		'then' thenPart=XBlockExpression;


Which is a block filled with zero or many XExpressionOrVariableDeclaration:
XBlockExpression returns xbase::XExpression: 
	{xbase::XBlockExpression}
	BEGIN
		(expressions+=XExpressionOrVarDeclaration ';'?)*
	END;


This makes this .rules file valid under the grammar:

Device Window can be open, closed
 
Device Heater can be on, off, error

Rule 'Save energy' when Window.open then
  var someDeclaration = "Anystring";
  fire(Heater.off)
  // Some Comment
  main(["mainArgs..."]);


But this is not because of the aforementioned problem considering block statements and comments:

Device Window can be open, closed
 
Device Heater can be on, off, error

Rule 'Save energy' when Window.open then
  var someDeclaration = "Anystring";
  fire(Heater.off)
// Some Comment
  main(["mainArgs..."]);


What i would like is to have comments similar to most WS aware languages such as e.g. phyton whereas this is a syntactically valid program:
x = 1
if x == 1:
    print("x is 1.")
# syntactically correct comment line
    print("some other output")


Regards,
Stefan

edit: i fixed some typos.

[Updated on: Wed, 30 November 2016 13:53]

Report message to a moderator

Re: Whitespace-Aware Languages with unindented Single line comment [message #1748973 is a reply to message #1748959] Wed, 30 November 2016 16:11 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 222
Registered: June 2013
Senior Member
OK, I understand.

So the problem is that the comment is not as much indented
as the previous statement and therefore the parser raises the "END" of the Block
Re: Whitespace-Aware Languages with unindented Single line comment [message #1749026 is a reply to message #1748973] Thu, 01 December 2016 09:32 Go to previous messageGo to next message
Stefan Thoeni is currently offline Stefan ThoeniFriend
Messages: 6
Registered: November 2016
Junior Member
Yes. That is the problem.

Now, a clean approach towards a solution would be of interest. Do you have any input?
Re: Whitespace-Aware Languages with unindented Single line comment [message #1749043 is a reply to message #1749026] Thu, 01 December 2016 12:27 Go to previous message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 222
Registered: June 2013
Senior Member
perhaps the following extension to your grammar may help?
a NL to get it started, perhaps spaces and tabs, then the // followed by the rest of the line?

terminal SL_COMMENT:
('\n'|'\r' )(' '|'\t')* '//' !('\n'|'\r')*

instead of your
terminal SL_COMMENT:
WS? '//' !('\n'|'\r')*
Previous Topic:How to get an inverse boolean assignment operator?
Next Topic:Substitute for xtext-utils unittesting?
Goto Forum:
  


Current Time: Thu Mar 30 03:03:38 GMT 2017

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

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