Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to emit error message if line length exceeds some threshold
How to emit error message if line length exceeds some threshold [message #917874] Thu, 20 September 2012 14:20 Go to next message
Tim Geisler is currently offline Tim GeislerFriend
Messages: 47
Registered: July 2009
Member
Hi,

in one project we have to implement a rather old-style language using Xtext where the length of lines is limited to 72 characters (seems to be designed for punched cards ... Wink.

Now, I'd like to have my Xtext-based editor to show error markers when a line exceeds 72 characters. The exceeding characters should be underlined.

We already have an external lexer for this language which handles line comments correctly.

One idea is to override the method nextToken() of the lexer. In this method, I can recognize the tokens which are outside the bounds. But, how to properly show the error markers using Xtext for these tokens?

Best regards,

Tim
Re: How to emit error message if line length exceeds some threshold [message #918236 is a reply to message #917874] Thu, 20 September 2012 21:45 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-20-09 16:20, Tim Geisler wrote:
> Hi,
>
> in one project we have to implement a rather old-style language using
> Xtext where the length of lines is limited to 72 characters (seems to be
> designed for punched cards ... ;).
>
> Now, I'd like to have my Xtext-based editor to show error markers when a
> line exceeds 72 characters. The exceeding characters should be underlined.
>
> We already have an external lexer for this language which handles line
> comments correctly.
>
> One idea is to override the method nextToken() of the lexer. In this
> method, I can recognize the tokens which are outside the bounds. But,
> how to properly show the error markers using Xtext for these tokens?
>
> Best regards,
>
> Tim

That does not sound like the right place (for many reasons).

The easiest may be to do it with a Builder that manages its own markers.
It runs on resource changes however, so if you want it to be
interactive, you need to insert the logic in the sequence that is
normally run (lexing/parsing/linking).

The place to do this is the callback that occurs "afterModelLinked"
(override the linker and call your own additional validation). You then
simply obtain the text for the root and scan for lines that are longer
than your limit.

If possible (given the language), you could help users by fixing the
problem and split the lines on save automatically. If so, the builder
may be the better option.

For example of both a validating builder, and an overridden linker that
issues errors/warnings you can take a look at cloudsmith / geppetto @
github. Its linker is called PPResourceLinker.

Hope that helps.
- henrik
Re: How to emit error message if line length exceeds some threshold [message #918740 is a reply to message #918236] Fri, 21 September 2012 09:45 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi,

overriding #afterModelLinked or #beforeModelLinked and creating
RangeBasedDiagnostics (and add them resource.getErrors) seems the right
thing to do.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 20.09.12 23:45, schrieb Henrik Lindberg:
> On 2012-20-09 16:20, Tim Geisler wrote:
>> Hi,
>>
>> in one project we have to implement a rather old-style language using
>> Xtext where the length of lines is limited to 72 characters (seems to be
>> designed for punched cards ... ;).
>>
>> Now, I'd like to have my Xtext-based editor to show error markers when a
>> line exceeds 72 characters. The exceeding characters should be
>> underlined.
>>
>> We already have an external lexer for this language which handles line
>> comments correctly.
>>
>> One idea is to override the method nextToken() of the lexer. In this
>> method, I can recognize the tokens which are outside the bounds. But,
>> how to properly show the error markers using Xtext for these tokens?
>>
>> Best regards,
>>
>> Tim
>
> That does not sound like the right place (for many reasons).
>
> The easiest may be to do it with a Builder that manages its own markers.
> It runs on resource changes however, so if you want it to be
> interactive, you need to insert the logic in the sequence that is
> normally run (lexing/parsing/linking).
>
> The place to do this is the callback that occurs "afterModelLinked"
> (override the linker and call your own additional validation). You then
> simply obtain the text for the root and scan for lines that are longer
> than your limit.
>
> If possible (given the language), you could help users by fixing the
> problem and split the lines on save automatically. If so, the builder
> may be the better option.
>
> For example of both a validating builder, and an overridden linker that
> issues errors/warnings you can take a look at cloudsmith / geppetto @
> github. Its linker is called PPResourceLinker.
>
> Hope that helps.
> - henrik
>
>
Re: How to emit error message if line length exceeds some threshold [message #918901 is a reply to message #917874] Fri, 21 September 2012 13:09 Go to previous messageGo to next message
Tim Geisler is currently offline Tim GeislerFriend
Messages: 47
Registered: July 2009
Member
Hi Sebastian, hi Henrik,

thanks for your suggestions to simplify this. Why is the linker the better place than the validator? (which would have been my first guess)

Tim
Re: How to emit error message if line length exceeds some threshold [message #921515 is a reply to message #918901] Mon, 24 September 2012 07:19 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Tim,

the #afterModelLinked hook allows you to take full control over how you
want to traverse the thing, e.g. just pick the root node and check the
line length. The validator will traverse the model externally and you'd
have to make sure that you don't validate things twice.
From my POV its more convenient to do such a thing in the linker.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 21.09.12 15:09, schrieb Tim Geisler:
> Hi Sebastian, hi Henrik,
>
> thanks for your suggestions to simplify this. Why is the linker the
> better place than the validator? (which would have been my first guess)
>
> Tim
Re: How to emit error message if line length exceeds some threshold [message #921526 is a reply to message #921515] Mon, 24 September 2012 07:33 Go to previous message
Tim Geisler is currently offline Tim GeislerFriend
Messages: 47
Registered: July 2009
Member
Hi Sebastian,

Thanks for your comments.

I implemented both validations. In the validator, one can use the acceptError method. In the linker, one has to sublass RangeBasedDiagnostic since this class is protected. In the validator it is possible for my meta model to have the validation only called once since the class of the root element is never used deeper inside in the model.

Best regards,

Tim
Previous Topic:Xtend or Xtext causes deadlock while workbench loading
Next Topic:Referencing elements of a DSL from another DSL using XText 1.0.1
Goto Forum:
  


Current Time: Thu Apr 25 16:07:17 GMT 2024

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

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

Back to the top