2012-10-15 58 views
3

我試圖創建Ruby的內部DSL,我還沒有開始編碼它,但我想知道,如果這句法在Ruby中是可能的:這是Ruby中的內部DSL語法嗎?

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?

+3

有可能,是的。所以你應該開始編碼。 –

+0

:P只是想確保語法不是無效的Ruby。 –

+0

那麼,我希望你能夠在出現問題時能夠彎曲語法要求:) –

回答

2

這是不可能的。問題出在這些結構上:

:class to 

您不能將參數傳遞給符號,只能傳遞給方法。

+0

似乎是這樣,Jorg。 –

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,然後用。

+1

沒有把它作爲可接受的答案,以防某人有一個更具可讀性或更接近最初提議的DSL的替代方案。 –

+0

用ruby -c進行檢查,感謝@Frederic_Cheung –