3

我正在尝试为 Ruby 创建一个内部 DSL,我还没有开始编写它,我想知道这种语法在 Ruby 中是否可行:

IF more_than: 1, :class smells to: "god class", sense: HIGH and has "no temp attributes", sense: 0.5
THEN problem "some problem"
WITH ponderation percent: 10

并且:

IF more_than: 1, :class
{
    smells to: 'god class', sense: 2
    and
    (
        has 'no temp attributes', sense: 0.5 or
        smells to: 'extensive coupling', sense: 1.5 or
        has 'refused parent Bequest', sense: HIGH or
        smells to: 'tradition breaker', sense: LOW
    )
    and
    (
        has :Method
        {
            smells to: 'extensive coupling', sense: 1.5
        }
    )
}
THEN
{ 
    problem "abusive conceptualization"
}
WITH
{
    ponderation percent: 10
}

更新:: D 我仍在定义 DSL 的要求,这是我的出发点,我正在考虑自定义解析器或 Ruby。你会说什么是更好的主意,为它创建一个自定义解析器或使用 Ruby?

4

2 回答 2

2

这是不可能的。问题在于这些结构:

:class to

您不能将参数传递给符号,只能传递给方法。

于 2012-10-15T09:17:17.440 回答
1

这是一个有效的 ruby​​ 语法的替代版本:

IF CLASS Student {
    smells to: 'god class', sense: 2 and
    has 'no temp attributes', sense: 0.5 or
    smells to: 'extensive coupling', sense: 1.5 or
    has 'refused parent bequest', sense: HIGH or
    smells to: 'tradition breaker', sense: LOW and
    has_method {
        smells to: 'extensive coupling', sense: 1.5
    }
}
THEN {
    problem 'abusive conceptualization'
}
WITH {
    ponderation percent: 10
}

更新:我一直在检查可能性,可以使用大写的 IF、THEN、WITH。

于 2012-10-15T08:52:50.830 回答