【发布时间】:2019-05-08 13:07:47
【问题描述】:
我正在 xText 中创建一个 DSL,用于对应用程序的功能行为进行建模。我的目标是将资源需求(例如 CPU 周期数、硬盘上的写入操作)与我想用 DSL 建模的功能行为结合起来。 DSL 是用 Eclipse IDE 用 xText 编写的。可以在下面找到包含 cmets 的 DSL 语法。
现在它是一个非常简单的 DSL 来模拟功能行为;结合 if/else 和 for 语句并向它们添加 libraryFunctions。我自己想出了后一个术语。它用于指代作为我的功能行为的基本步骤的操作(例如登录、加密、显示;您可以将它们视为编程语言中的方法)。现在我想扩展我的 DSL,使其能够引用 Java 项目的源代码。我创建了一个类似于带有登录屏幕和创建帐户屏幕的基本程序的小型 Java 程序(参见下面的类图)。为了使用 DSL 对该程序的功能行为进行建模,我希望能够参考 Java 程序源代码的某些细节,以便我可以直接从源代码中提取这些细节并在 DSL 中使用它。例如;假设我想引用 Java 程序中使用的某些方法。现在我的 DSL 中有简单的枚举“libraryFunctionsEnum”,但如果我能以某种方式直接引用 Java 程序源代码中使用的方法会很好(这样当我编译 DSL 并使用它时,xText编辑器会自动提供我可以参考的可用方法列表。
我尝试使用 ecore 模型来转换我的 Java 项目的类图并将它们集成到 xText 中,但我觉得我有点不知所措。我还研究了 xBase 和 xTend(旨在使 xText 与 Java 更具有互操作性的两种语言),但到目前为止,我发现它们更侧重于从 xText 模型自动生成 Java 源代码。我想用另一种方式来做(参考来自外部项目的 Java 源代码,以便我可以在我的 DSL 中使用这些引用)。我不知道我上面提到的方法(ecore、xBase、xTend)是否是实现我想要的正确方法。如果您有更好的想法或解释,我很高兴听到它!
顺便说一句,我还是 xText 和 DSL 建模/DSL 开发的新手。我可能忘记了一些重要的细节/解释。如果您遗漏了什么,请告诉我。
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='=='
;
Java 类图:
【问题讨论】: