Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » DSL with python grammar
DSL with python grammar [message #1839957] Fri, 02 April 2021 06:30
wings cu is currently offline wings cuFriend
Messages: 14
Registered: January 2021
Junior Member
I am now writing a DLS with part of python grammar.
I have found a xtext example with Whitespace-Aware Languages(https://www.eclipse.org/Xtext/documentation/307_special_languages.html#whitespace-aware-languages)
but it uses xbase and I don't need it.
When I use xtext synthetic tokens, my xtext project cannot analyse python correctly.
This is my xtext grammar:
grammar org.pythonDSL.PythonDSL

hidden(WS)

generate pythonDSL "http://www.pythonDSL.org/PythonDSL"

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



PythonDSL:
    (elements+=AbstractElement)*;
 
AbstractElement:
    type=(Stmt|Config);
 
QualifiedName:
    ID ('.' ID)*;
    
Comment:
	name=(ML_COMMENT|SL_COMMENT)
;

Import:
    'import' importedNamespace=QualifiedNameWithWildcard;

Config:
	{Config} 'config' configs=Dictionary;

KeyValue:
	name=QualifiedValue ':' value=QualifiedValue;

QualifiedNameWithWildcard:
    QualifiedName '.*'?;

QualifiedIndex:
INT|STRING;

Dictionary:
	{Dictionary} '{' (elements+=KeyValue)+ '}';

Set:
	{Set} '{' (elements+=QualifiedValue)* '}';

List:
	{List} '[' (elements+=QualifiedValue)* ']';

QualifiedValue:
STRING|'True'|'False'|INT;

VarDec:
name=ID('['index=QualifiedIndex']')? '=' value=QualifiedValue;

Stmt:
type=(Exp|Import) NEXT_LINE | type=If | type=Comment;

If:
	'if' (not='not')? condition=Exp ':' NEXT_LINE
	 (BEGIN ( ifStmts+=Stmt)* END)
	(=> elseIfClause+=ElseIf)*
	(=> elseClause=Else)?
;

ElseIf:
	'elif'  condition=Exp ':' NEXT_LINE
	 (BEGIN ( elseIfStmts+=Stmt)* END)
;

Else:
	{Else} 'else' NEXT_LINE (BEGIN ( elseStmts+=Stmt)* END)
;

Exp:
	left=Equal
;

Ret:
	'return' name=ID;

Var:
	name=ID;


Val:
	name=QualifiedValue | type=(Dictionary|List|Set);

Dot returns Exp:
	left=Array ({Dot.left=current} op='.'right=Array)*;

Array returns Exp:
	left=Primary ({Array.left=current} op='['right=Primary']')*;

Primary returns Exp:
   op='(' left=Equal ')' |left=Var|left=Val|left=FuncCall;

ArgList:
	{ArgList} (args+=Equal)? (',' args+=Equal)*;

FuncCall:
	name=Var (op='(' right=ArgList ')');

Equal returns Exp:
	left=AddAndSub ({Euqal.left=current} op='=' right=AddAndSub)*;

AddAndSub returns Exp:
	left=MulAndDiv ({AddAndSub.left=current} op=('+'|'-') right=MulAndDiv)*;

MulAndDiv returns Exp:
	left=Dot ({MulAndDiv.left=current} op=('*'|'/') right=Dot)*;

terminal BEGIN: 'synthetic:BEGIN';  // increase indentation
terminal END: 'synthetic:END';      // decrease indentation

terminal ID:
    '^'?('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
 
terminal INT returns ecore::EInt:
    ('0'..'9')+;
 
terminal STRING:
    '"' ( '\\'('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') | !('\\'|'"') )* '"' |
    "'" ( '\\'('b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\') | !('\\'|"'") )* "'";
 
terminal ML_COMMENT:
    "'''" -> "'''";
 
terminal SL_COMMENT:
    '#' !('\n'|'\r')* ('\r'? '\n')?;
 
terminal WS:
    (' '|'\t')+;
/*    
terminal TAB:
	('\t')+
;
    */
terminal NEXT_LINE:
	('\n'|'\r')
;

terminal ANY_OTHER:
    .;



This is example python code:
driver.implicitly_wait(20)
if a.b:
	el.click()
elif c:
	'''les '''
	e.go()
elif m:
	if lake:
		pik()

	get()
else:
	e1.click()

[Updated on: Fri, 02 April 2021 08:56]

Report message to a moderator

Previous Topic:Two dsl in one project
Next Topic:Creating terminal STRING rule with start and end tokens?
Goto Forum:
  


Current Time: Fri May 14 11:41:05 GMT 2021

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

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

Back to the top