Grammar & Scoping Pattern for variables, functions [message #736199] |
Thu, 13 October 2011 15:36  |
Eclipse User |
|
|
|
Hey folks! I'm working on a new xtext2-based project to describe basic scripted actions and workflows and I've been looking all over for a pattern to model scope access of subproperties through variable references. Think objects with attributes and functions. So I might have an object called "Database" that has a child object called "Table" that has some actions that can be performed on it.
My test grammar:
Model:
(resources+=Resource
| variables+=VariableDeclaration
| actions+=ActionStatement)*
;
Resource:
'resource' name=ID '{'
(children+=Resource
| actions+=ActionDeclaration)*
'}'
;
ActionDeclaration:
'action' name=ID '(' ')'
;
VariableDeclaration:
'var' name=ID '=' resource=[Resource|QualifiedName]
;
ActionStatement:
action=[ActionDeclaration|QualifiedName]
;
QualifiedName:
ID ('.' ID)*;
This allows for models that look something like:
resource Database {
resource Table {
action selectAll()
action runReport()
}
action listTables()
}
var db = Database
My question is how to get the ActionStatement to behave how you'd expect in an object-oriented kind of way using the variable as a reference. As it sits in the grammar now, the editor will allow things like
Database.listTables()
Database.Table.selectAll()
Instead, I'd like it to do things like
db.listTables()
db.Table.selectAll()
I've been reading the documentation and the forums looking for examples of doing this with the scope API, and I briefly tried changing ActionStatement to the following:
ActionStatement:
variable=[VariableDeclaration] '.' action=[ActionDeclaration]
;
I implemented the methods "scope_ActionStatement_variable(...)" and scope_ActionStatement_action(...)" to only offer actions visible as children under defined variables. This allowed "db.listTables()", but did not allow for arbitrarily traversing child resources to get to an action lower down in the tree.
So the short of it: using the scope API or grammar changes, how do I get the editor to treat variable declarations like the resource itself in resolving qualified references to children or actions? As always, thanks so much for the help!
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06228 seconds