Multiple Entities within an Entity [message #1805957] |
Mon, 29 April 2019 07:24  |
Eclipse User |
|
|
|
* DISCLAIMER: I am new to xText so this question might seem trivial *
I am trying to develop a DSL within xText for creating Functional Models of software programs. I tried setting up in initial version of this DSL which describes basic functional flows within a program (describing the functional flows using if/else statements and basic operations which I have enumerated in my DSL). The thing that I am stuck at is with defining the IfElseStatements entity. This entity should be able to model multiple if statements first, after which it is ended with an else statement (so just describing a basic if/else statement). However, when I compile the DSL and try to use it in xText, the editor tells me that I have to define an else statement directly after I have created an if statement (so it doesn't allow to create multiple if statements first). I am curious to your thoughts on why this doesn't work as aspected. If you need any more information, I will of course provide this.
grammar org.xtext.example.mydsl.TestProgramDSL with org.eclipse.xtext.common.Terminals
Model:
'functionName' name = STRING
functions = NestedFunctions
;
NestedFunctions:
nestedFunctions += Functions+
;
Functions:
libraryFunctions += libraryFunctionsEnum
ifElseStatements += IfElseStatements+
;
IfElseStatements:
'if'
IfStatements += IfStatements+
'else'
elseFunctions += libraryFunctionsEnum+
;
IfStatements:
conditions += Conditions+
ifFunctions += libraryFunctionsEnum+
;
//*Eventually filled with details from class diagram, but for now we manually fill it for the sake of testing.
enum libraryFunctionsEnum:
createAccount='createInstance'|
login='login'|
hasCode= 'encrypt'|
display='display'
;
|
|
|
|
Re: Multiple Entities within an Entity [message #1806395 is a reply to message #1806351] |
Wed, 08 May 2019 08:23  |
Eclipse User |
|
|
|
@Christian thanks a lot for your answer. I already figured it out myself by now, but I still much appreciate your answer. You are right indeed (the 'if' keyword was set to be only specified once). I rewrote the DSL and added comments to it (see below). It now works as it should. Thanks!
grammar org.xtext.example.mydsl.FinalDsl with org.eclipse.xtext.common.Terminals
generate finalDsl "http://www.xtext.org/example/mydsl/FinalDsl"
Model:
'functionName' name = STRING
functions += FunctionElements*
;
// Function elements of which the model exists. The model can contain
// library functions, for loops, and if/else statements.
FunctionElements:
(
functions += libraryFunctionsEnum |
forLoops += ForLoops |
ifElseStatements += IfElseStatements
)
;
// IfElse Statements requiring if statements and optionally followed by
// one else statement.
IfElseStatements:
ifStatements += IfStatements
(elseStatement = ElseStatement)?
;
// If statements requiring conditions and optionally followed by
// library functions or for loops.
IfStatements:
'if'
conditions = Conditions
(ifFunctions += libraryFunctionsEnum | forLoops += ForLoops)
;
// Else statement requiring one or multiple library functions.
ElseStatement:
'else' elseFunctions += libraryFunctionsEnum
;
// For loops requiring one condition and followed by zero or more
// library functions
ForLoops:
'for'
conditions = Conditions
libraryFunctions += libraryFunctionsEnum*
;
//*Eventually filled with details from class diagram, but for now we manually fill it for the sake of testing.
enum libraryFunctionsEnum:
createAccount='createInstance'|
login='login'|
hasCode= 'encrypt'|
display='display'
;
Conditions:
STRING
operator=logicalOperators
STRING
;
enum logicalOperators:
greaterThan='>'|
smallerThan='<'|
greaterOrEqualThan='=>'|
smallerOrEqualThan='<='|
equalTo='=='
;
[Updated on: Wed, 08 May 2019 08:24] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.26996 seconds