Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parsing single line commands
Parsing single line commands [message #1408886] Sat, 16 August 2014 11:09 Go to next message
Earnest Banbury is currently offline Earnest BanburyFriend
Messages: 4
Registered: August 2014
Junior Member
I have an existing file format (not CSV), for which I want to create an editor. In this format every command is on its own line. The first character on each line is used to discriminate between commands. Is there a way to stop Xtext from parsing a line once a command has been found? Or are there other ways to deal with parsing to the end of a line?
Re: Parsing single line commands [message #1409844 is a reply to message #1408886] Tue, 19 August 2014 05:43 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
Just some ideas from a newbie:

Have a look how the multi-line Comment is specified in the terminals package.
I think you have to change the standard so the newline is no terminal any more and not hidden.
There must be some examples in this forum like: https://www.eclipse.org/forums/index.php/mv/msg/596731/1186656/#msg_1186656

Uli

[Updated on: Tue, 19 August 2014 05:44]

Report message to a moderator

Re: Parsing single line commands [message #1409879 is a reply to message #1409844] Tue, 19 August 2014 07:57 Go to previous messageGo to next message
Earnest Banbury is currently offline Earnest BanburyFriend
Messages: 4
Registered: August 2014
Junior Member
I have tried something like that, but my grammar is very ambiguous, unless it is parsed one line at a time. Xtext insists on parsing from the middle of lines and finds additional matches, that I don't want. I have looked through the forum and the internet for several days, but I couldn't find a good answer.
Re: Parsing single line commands [message #1409919 is a reply to message #1409879] Tue, 19 August 2014 09:54 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
Earnest Banbury wrote on Tue, 19 August 2014 03:57
I have tried something like that, but my grammar is very ambiguous, unless it is parsed one line at a time. Xtext insists on parsing from the middle of lines and finds additional matches, that I don't want. I have looked through the forum and the internet for several days, but I couldn't find a good answer.


Parsing from the middle of the line ??
I don't think so if your grammar is something like:

Model: records+= Record;
Record : recordtype=ID (restOfRecord=TextUpToTheEOL)?;
TextUpToTheEOL: !('\n'|'\r')* ('\r'? '\n')?;

It's just a sketch, I will have a closer look at it when I have some time,
Uli

[Updated on: Tue, 19 August 2014 09:58]

Report message to a moderator

Re: Parsing single line commands [message #1409929 is a reply to message #1409919] Tue, 19 August 2014 10:28 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
experimenting with the idea, it looks like there are some tricky bits in the details.

Perhaps someone with more experience can help with a tip ?


Not sure if the following link will be helpful: http://stackoverflow.com/questions/13992686/matching-a-text-in-line-by-line-file-with-xtext

or a blog from 2009: http://blogs.itemis.de/leipzig/archives/498

[Updated on: Tue, 19 August 2014 11:35]

Report message to a moderator

Re: Parsing single line commands [message #1409983 is a reply to message #1408886] Tue, 19 August 2014 13:36 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
Here is the grammar:
###################################
grammar oneliner.oncsv.Onecsv
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate onecsv "http://www.oncsv.oneliner/Onecsv"
Csv: rows+=Row (NL+ rows+=Row)* NL*;
Row: rowline=ENTRY;
terminal ENTRY: (!('\r'|'\n'))*;
terminal NL: ('\r')?'\n';
#################################

and the generator looks like:
############################################
package oneliner.oncsv.generator

import oneliner.oncsv.onecsv.Csv
import oneliner.oncsv.onecsv.Row
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator

class OnecsvGenerator implements IGenerator {

override void doGenerate(Resource resource, IFileSystemAccess fsa) {
fsa.generateFile('Onecsv.0.CODE', makeTheCode(resource.contents.head as Csv))
}
def makeTheCode(Csv sm) '''
«FOR Row modul : sm.rows»
«modul.rowline»
«ENDFOR»
'''
}
########################################

The example Input
###################
ab test or so tt

testline

1;5 6 ,7 ,8,9,10
Aber, mal; Test 12;
####################

will be generated to (empty lines are suppressed):
#####################
ab test or so tt
testline
1;5 6 ,7 ,8,9,10
Aber, mal; Test 12;
#####################

[Updated on: Tue, 19 August 2014 13:39]

Report message to a moderator

Re: Parsing single line commands [message #1410004 is a reply to message #1409983] Tue, 19 August 2014 14:41 Go to previous messageGo to next message
Earnest Banbury is currently offline Earnest BanburyFriend
Messages: 4
Registered: August 2014
Junior Member
Thanks for the effort. I tried your solution, but it doesn't work in my case.
The format I'm trying to parse, has a number at the beginning, that specifies the type of command. Then follow a number of parameters separated by white space. Every command has a different number of parameters. Parameters can be integer, float or String. And there can be only one command per line.

0 param1 1 2
1 1 2.0 -2 someText


I know it's trivial to parse with Java, but that doesn't give me an editor.
Re: Parsing single line commands [message #1410031 is a reply to message #1410004] Tue, 19 August 2014 16:02 Go to previous messageGo to next message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
Ok, so all you have to do is to provide
one grammar specification for each command number (like KeyValuePair1, KeyValuePair2, ...
on top, you bundle it as in this example the KeyValuePair:
###############################
KeyValuePair: (KeyValuePair1 | KeyValuePair2 | KeyValuePair3 );
KeyValuePair1: name=ID '=' value=STRING;
KeyValuePair2: 'reffield--'name=ID '=' reffield=[Field|QualifiedName];
KeyValuePair3: 'refentity--'name=ID '=' refentity=[Entity];
QualifiedName: ID ('.' ID)*;
##################################################################


in the generator, it is handled like:
##################################################################
for (KeyValuePair kvp : mf.placeholderPairs) {
LxtCore::replaceLxtWorkText(retKvpRealName(kvp),retKvpValue(kvp))
}

// returning the "real" names of the placeholders
def dispatch retKvpRealName(KeyValuePair1 kvp1) { return(kvp1.name) }
def dispatch retKvpRealName(KeyValuePair2 kvp2) { return("reffield--"+kvp2.name) }
def dispatch retKvpRealName(KeyValuePair3 kvp3) { return("refentity--"+kvp3.name) }

// returning the values for replacement
def dispatch retKvpValue(KeyValuePair1 kvp1) { return(kvp1.value) }
def dispatch retKvpValue(KeyValuePair2 kvp2) {
return(kvp2.reffield.name + "." + genParentname(kvp2.reffield.eContainer()))
}
def dispatch retKvpValue(KeyValuePair3 kvp3) { return(kvp3.refentity.name) }

def genParentname(EObject container) '''«switch container {
Entity: {
container.name
}
}» '''
##################################################################
So what's the problem?
Re: Parsing single line commands [message #1410241 is a reply to message #1410031] Wed, 20 August 2014 07:29 Go to previous messageGo to next message
Earnest Banbury is currently offline Earnest BanburyFriend
Messages: 4
Registered: August 2014
Junior Member
So here is what I tried using your input:

grammar de.banbury.ldraw.LDraw hidden (WS)

import "<http:>//www.eclipse.org/emf/2002/Ecore" as ecore

generate lDraw "<http:>//www.banbury.de/ldraw/LDraw"

Model:
	lines+=Line (NL+ lines+=Line)* NL*;
	
Line:
	Comment |
	PartRef
;

Comment:
	'0' TEXT+
;

PartRef:
	'1' INT+
;

terminal INT:
	'-'?('0'..'9')+
;

terminal WS :
	(' '|'\t')+;
	
terminal TEXT: (!('\r'|'\n'))*;

terminal NL:
	'\r'?'\n'
;


It looks correct to me, but it doesn't work. I get a 'no viable input' error.
Re: Parsing single line commands [message #1410258 is a reply to message #1410241] Wed, 20 August 2014 08:27 Go to previous message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
think the brackets are missing in Line definition, should be:

Line:
(Comment |
PartRef)
;

But maybe some expert will find more ...

[Updated on: Wed, 20 August 2014 08:50]

Report message to a moderator

Previous Topic:Grammar element with cross-reference
Next Topic:Correct way to build with xtext?
Goto Forum:
  


Current Time: Fri Mar 29 11:38:27 GMT 2024

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

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

Back to the top