problem for define dsl [message #902737] |
Mon, 20 August 2012 04:21  |
Eclipse User |
|
|
|
Hi,
I'm working on a DSL but i have a problem with my grammar
I defined for DoLoop the keyword 'do' and 'loop'
i realised test with xtend but i see that keyword 'loop' it's not obligatory (test is ok without keyword 'loop')
I don't understand why
Maybe is not a good define
My dsl:
CompilationUnit:
instructions+=Instruction*;
Instruction:
ControleStructure|VariableDeclaration
|VariableAssignment|SymbolRef
;
ControleStructure:
DoLoop
;
DoLoop:
{DoLoop}
'do'
instructions+=Instruction*
'loop'
;
VariableDeclaration returns Symbol:
'dcl' name = ID type=Type/*( | ('like' variable=[VariableDeclaration |Type]))*/
('endloc')?('[,S]')?
;
Type:
ShortNumericType | NumericType | StringType | BCDType
;
ShortNumericType:
{ShortNumericType}'#';
NumericType:
{NumericType}'%';
StringType:
{StringType}'$'('='size=INT)?
;
BCDType:
{BCDType}('='size=INT)?;
VariableAssignment:
dcl=[Symbol]'='expr=Expression
;
Expression :
CompareOperator;
CompareOperator returns Expression:
ConditionalOR(( /* {Inclusion.left=current} '='|*/{Equality.left=current}'=='|{NoInclusion.left=current}'!='|{Different.left=current}'<>'|{Inferrior.left=current}'<'|{Superior.left=current}'>'|{SuperiorOrEqual.left=current}'>='|{InferriorOrEqual.left=current}'<=') right=ConditionalOR)*;
ConditionalOR returns Expression:
Addition (({ConditionalOR.left=current} 'or'|{ConditionalOX.left=current} 'ox'|{ConditionalAND.left=current} 'and'|{ConditionalNOT.left=current} 'not') right=Addition)*;
Addition returns Expression:
Multiplication (({Plus.left=current} '+' | {Minus.left=current} '-') right=Multiplication)*;
Multiplication returns Expression:
PrimaryExpression (({Multi.left=current} '*' | {Div.left=current} '/') right=PrimaryExpression)*;
PrimaryExpression returns Expression:
NumericInt|NumericReal|StringLiteral
|SymbolRef
| '(' Expression ')'
;
SymbolRef:
CallOrVariableSymbolRef
;
CallOrVariableSymbolRef:
('call')? symbol=[Symbol|ID] ("(" (arguments+=Expression)? ("," arguments+=Expression)* ")")?
;
StringLiteral:
stringValue=STRING
;
NumericInt:
value=INT;
NumericReal:
value=REAL;
test with xtend:
@RunWith(typeof(XtextRunner))
@InjectWith(typeof(LanguageInjectorProvider))
class test {
@Inject extension ParseHelper<CompilationUnit>
@Test
def void testSegmentLoopDeclaration()
{
val compilationUnit =
'''
program "MyProgramme"
segment 0
dcl a%
do
a=a+1
eseg 0
end
'''.parse
val instruction= compilationUnit.instructions.get(0)
val variable= instruction as Symbol
Assert::assertEquals("a", variable.name)
val num= variable.type
Assert::assertNotNull(num)
Assert::assertTrue(num instanceof NumericType)
val numericType= num as NumericType
Assert::assertNotNull(numericType)
val controleStructure=compilationUnit.instructions.get(1) as ControleStructure
val doLoop= controleStructure as DoLoop
Assert::assertNotNull(doLoop)//test should be false
val variableAssignement=doLoop.instructions.get(0) as VariableAssignment
val plus=variableAssignement.expr as Plus
Assert::assertNotNull(plus)
val variableOperande = plus.left as SymbolRef
val variable2 = variableOperande as CallOrVariableSymbolRef
Assert::assertEquals('a',variable2.symbol.name)
val numeric= plus.right as NumericInt
Assert::assertEquals(1,numeric.value)
}
}
But that's not working and I doubt that this is the right way. I'm new to Xtext and so this question might be quite stupid. Maybe somebody has an idea or can point me into the right direction? Thanks in advance!
Best regards,
Jérôme
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.30853 seconds