简单谓词 DSL
在我正在编写的 .NET 程序的一部分中,我希望用户能够在运行时编写一个仅返回 true 或 false 的简单函数体的脚本。用户需要常用的基本逻辑运算符、嵌套逻辑运算的能力(a && (b || c))以及一小组运算符“==”“!=”“包含”“不包含” 。换句话说,我让用户编写一个函数,该函数是运行时过滤器的谓词。
我意识到像 IronPython 这样的东西很容易满足要求,但是他们可以做很多其他(危险的)事情,但我不希望他们能够做。
人们会建议我用什么来做这样的事情?
In one part of a .NET program I am writing I'd like a user to be able to script a simple function body at runtime that returns true or false only. The user would need the usual basic logical operators, the ability to nest logical operations (a && (b || c)), and a small set of operators "==" "!=" "contains" "notcontains". In other words I am letting the user write a function that is the predicate of a filter at runtime.
I realise something like IronPython easily fits the bill however there are plenty of other (dangerous) things they could do that I dont want them to be able to.
What would people recommend I use for something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的语法确实很简单,我建议您考虑编写自己的解析器和一个小型字节码解释器。开发成本并不像您想象的那么高,并且有一些有用的工具。
Coco/R 是一个解析器生成器,它可以采用语言语法并为您生成解析器和扫描器,这应该让您开始使用一个简单的解释器:http://www.ssw.uni-linz.ac.at/coco/
有一个名为 Taste 的示例解析器和解释器可以帮助您入门。
If your grammar really is simple, I recommend looking into writing your own parser and a small bytecode interpreter. The development cost isn't as high as you would think, and there are several helpful tools.
Coco/R is a parser-generator that can take a language grammar and produce a parser and scanner for you, that should get you started with a simple interpreter: http://www.ssw.uni-linz.ac.at/coco/
There is a sample parser and interpreter called Taste that can help you get started.