0
Domain Specific LanguagesHiro YoshiokaMarch 15th, 2013Rakuten Tower 2, Shinagawa, Tokyo, Japan
Chapter 4, Internal DSL•  Unlike	  external	  DSLs,	  you	  don’t	  need	  to	  learn	     about	  grammars	  and	  langua...
4.1 Fluent and Command-Query APIs•  Method	  Chaining	  	  	   d1	  =	  new	  Disk(150,	  Disk.UNKNOWN_SPEED,	  null);	  P...
4.1 Fluent and Command-Query APIs•  Func]on	  Sequence	  computer()	  processor();	  	  cores(2);	  	  speed(2500);	  	  i...
4.1 Fluent and Command-Query APIs                                    5
4.1 Fluent and Command-Query APIs•  Command-­‐query	  separa]on	     •  Command:	  may	  change	  state,	  but	  not	     ...
4.2 The Need for a Parsing Layer•  Expression	  Builder	     •  input:	  fluent	  interface	     •  output:	  a	  sequence	...
4.3 Using Functions•  func]on	  –	  most	  successful	  packaging,	  (also	     called	  subrou]ne,	  procedure,	  method)...
4.3 Using Functions•  Method	  Chaining	  and	  Func]on	  Sequence	     •  Scope	  of	  the	  func]ons	     •  Func]on	  S...
4.3 Using Functions                         •  nested	  func]on	                                   •  combines	  func]ons	...
4.3 Using Functions•  nested	  func]on	     •  the	  hierarchic	  structure	  of	  the	        configura]on	  is	  echoed	 ...
4.3 Using Functions                                  •  hybrid	  computer(	                                       •  It	  ...
4.3 Using Functions•  hybrid	     •  problems	  –	  punctua]onal	  confusion	  	        •  comma,	  periods,	  semicolons,...
4.4 Literal Collections•  literal	  list	  -­‐-­‐	  Java	  or	  C#	  computer(	  	  processor	  (...),	  	  disk(...),	  	...
4.4 Literal Collections•  literal	  map	     •  named	  parameters	     •  symbol	         •  immutable,	  symbol	  lookup...
4.4 Literal Collections•  symbol	  -­‐-­‐	  Ruby	  computer(processor(:cores	  =>	  2,	  :type	  =>	  :i386),	  	  	  	  	...
4.5 Using Grammars to Choose Internal                      ElementsStructure	                   BNF	                      ...
4.6 Closures•  lambdas,	  blocks,	  anonymous	  func]ons	  #ruby...	  ComputerBuilder.build	  do	  |c|	  	  	  c.processor...
4.6 Closures          •  Nested	  Closures	               •  inline	  nes]ng	               •  deferred	  evalua]on	      ...
4.7 Parse Tree Manipulation•  parse	  tree	  manipula]on	                                                              4.7...
4.8 Annotation•  Auribute	  in	  C#	    class	  Person	    	  	  	  	  validates_length_of	  :last_name,	  :maximum	  =>	 ...
4.9 Literal Extension•  Method	  Chaining	  •  chain	  begins	  on	  a	  literal	  integer,	  	     e.g.	  5.days.ago	    ...
4.10 Reducing the Syntactic Noise•  Textual	  Polishing	                                         23
4.11 Dynamic Reception•  handle	  an	  unexpected	  call	  by	  run]me	  to	  a	     special	  method.	  programmers	  can...
4.12 Providing Some Type Checking•  sta]c	  type	  checking	  or	  not	  •  type	  checking	  at	  compile	  ]me	  or	  ru...
Yoshioka	                 26
Upcoming SlideShare
Loading in...5
×

DSL - Domain Specific Languages, Chapter 4, Internal DSL

592

Published on

Domain Specific Languages, by Martin Fowler
http://martinfowler.com/books/dsl.html
Chapter 4, Internal DSL

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
592
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "DSL - Domain Specific Languages, Chapter 4, Internal DSL"

  1. 1. Domain Specific LanguagesHiro YoshiokaMarch 15th, 2013Rakuten Tower 2, Shinagawa, Tokyo, Japan
  2. 2. Chapter 4, Internal DSL•  Unlike  external  DSLs,  you  don’t  need  to  learn   about  grammars  and  language  parsing.  •  Constrain  –  host  language  expression   •  Ruby   •  Lisp  •  fluent  interface  vs  API   2
  3. 3. 4.1 Fluent and Command-Query APIs•  Method  Chaining       d1  =  new  Disk(150,  Disk.UNKNOWN_SPEED,  null);  Processor  p  =  new  Processor(2,  2500,  Processor.Type.i386);  Disk  Disk  d2  =  new  Disk(75,  7200,  Disk.Interface.SATA);  return  new  Computer(p,  d1,  d2);      Method  Chaining  computer()    .processor()    .cores(2)    .speed(2500)    .i386()    .disk()    .size(150)    .disk()    .size(75)    .speed(7200)    .sata()    .end();       3
  4. 4. 4.1 Fluent and Command-Query APIs•  Func]on  Sequence  computer()  processor();    cores(2);    speed(2500);    i386();    disk();    size(150);    disk();    size(75);    speed(7200);    sata();       4
  5. 5. 4.1 Fluent and Command-Query APIs 5
  6. 6. 4.1 Fluent and Command-Query APIs•  Command-­‐query  separa]on   •  Command:  may  change  state,  but  not   return  value   •  Query:  does  not  change  state   •  Name  –  without  context  •  Fluent:  Name  –  context  is  important   6
  7. 7. 4.2 The Need for a Parsing Layer•  Expression  Builder   •  input:  fluent  interface   •  output:  a  sequence  of  command-­‐query  API   •  Seman]c  model  •  Separa]ng  the  Seman]c  model  from   Expression  Builders   •  You  can  test  them  independently.   7
  8. 8. 4.3 Using Functions•  func]on  –  most  successful  packaging,  (also   called  subrou]ne,  procedure,  method)  computer();    processor();      cores(2);      speed(2500);      i386();    disk();    size(150);    disk();    size(75);    speed(7200);    sata();       8
  9. 9. 4.3 Using Functions•  Method  Chaining  and  Func]on  Sequence   •  Scope  of  the  func]ons   •  Func]on  Sequence:  ensure  the  func]ons   resolve  properly   •  global  func]on  –  complica]ng   namespace  and  introducing  global   variables  for  parsing  data.   •  Context  Variables   •  Method  Chaining  –  avoids  global   •  object  scoping  –  avoid  globalness,   extensibility   •  nested  func]on  –  avoid  context   variables   9
  10. 10. 4.3 Using Functions •  nested  func]on   •  combines  func]ons  by  making  func]on   calls  arguments  in  higher  level  func]on   calls.  computer(   •  reflects  the  logical  syntax  tree  of  the  DSL   •  change  in  evalua]on  order    processor(          cores(2),          speed(2500),          i386  ),   •  “(“,  “)”,  “,”  are  explicit.  noise  –  Lisp    disk(          size(150)   •  (third(second(first)))  **  I  found  typo    ),  disk(          size(75),          speed(7200),        SATA      )  );   10
  11. 11. 4.3 Using Functions•  nested  func]on   •  the  hierarchic  structure  of  the   configura]on  is  echoed  by  the  language   constructs  themselves     •  reflects  the  logical  syntax  tree  of  the  DSL   •  evalua]on  order   •  arguments  are  iden]fied  by  posi]on  rather   than  name   11
  12. 12. 4.3 Using Functions •  hybrid  computer(   •  It  uses  Func]on  Sequence,  each  computer      processor()   func]on  uses  Nested  Func]on,  each          .cores(2)          .speed(2500)   processor  and  disk  is  using  Method          .type(i386),   Chaining.      disk()          .size(150),   •  Advantages:        disk()   •  Func]on  Sequence:  each  computer          .size(75)          .speed(7200)   defini]on  well  separated.          .iface(SATA)   •  Nested  Func]on:  eliminates  a  Context  );  computer(   Variable.      processor()            .cores(4)   •  Method  Chaining:  mul]ple  op]onal  );   arguments   12
  13. 13. 4.3 Using Functions•  hybrid   •  problems  –  punctua]onal  confusion     •  comma,  periods,  semicolons,  …   13
  14. 14. 4.4 Literal Collections•  literal  list  -­‐-­‐  Java  or  C#  computer(    processor  (...),    disk(...),    disk(...)    );      -­‐-­‐  Ruby  computer  [    processor(...),    disk(...),    disk(...)    ]       14
  15. 15. 4.4 Literal Collections•  literal  map   •  named  parameters   •  symbol   •  immutable,  symbol  lookup   •  :symbol  (ruby  syntax)  -­‐-­‐  Ruby  computer(processor(:cores  =>  2,  :type  =>  :i386),                                        disk(:size  =>  150),                                        disk(:size  =>  75,  :speed  =>  7200,  :interface  =>  :sata))    -­‐-­‐  Ruby  2.0  has  a  keyword  arguments       15
  16. 16. 4.4 Literal Collections•  symbol  -­‐-­‐  Ruby  computer(processor(:cores  =>  2,  :type  =>  :i386),                                        disk(:size  =>  150),                                        disk(:size  =>  75,  :speed  =>  7200,  :interface  =>  :sata))       16
  17. 17. 4.5 Using Grammars to Choose Internal ElementsStructure   BNF   Consider  Mandatory  list   parent  ::=  first  second  third     Nested  Func+on  (357)    Op]onal  list       parent  ::=  first  maybeSecond?   Method  Chaining  (373),   maybeThird?     Literal  Map  (419)    Homogenous  bag   parent  ::=  child*     Literal  List  (417),     Func+on  Sequence  (351)    Hetrogenous  bag     parent  ::=  (this  |  that  |  theOther)*     Method  Chaining    Set     n/a     Literal  Map     17
  18. 18. 4.6 Closures•  lambdas,  blocks,  anonymous  func]ons  #ruby...  ComputerBuilder.build  do  |c|      c.processor  do  |p|          p.cores  2          p.i386          p.speed  2.2      end      c.disk  do  |d|          d.size  150        end      c.disk  do  |d|        d.size  75        d.speed  7200        d.sata      end  end   18
  19. 19. 4.6 Closures •  Nested  Closures   •  inline  nes]ng   •  deferred  evalua]on   •  limited-­‐scope  variables  #ruby...  ComputerBuilder.build  do  |c|      c.processor  do  |p|          p.cores  2          p.i386          p.speed  2.2      end      c.disk  do  |d|          d.size  150        end      c.disk  do  |d|        d.size  75        d.speed  7200        d.sata      end  end   19
  20. 20. 4.7 Parse Tree Manipulation•  parse  tree  manipula]on   4.7 Parse Tree Manipulatio BinaryExpression Left Right MemberAccess ConstantExpression Member Expression Value Age aPerson 18 Figure 4.2 A parse tree representation of aPerson.Age > 18 query in another query language, such as SQL. This is essentially what 20 .NET
  21. 21. 4.8 Annotation•  Auribute  in  C#   class  Person          validates_length_of  :last_name,  :maximum  =>  30   21
  22. 22. 4.9 Literal Extension•  Method  Chaining  •  chain  begins  on  a  literal  integer,     e.g.  5.days.ago   •  add  methods  to  external  library  classes   •  Cons:  methods  globally   22
  23. 23. 4.10 Reducing the Syntactic Noise•  Textual  Polishing   23
  24. 24. 4.11 Dynamic Reception•  handle  an  unexpected  call  by  run]me  to  a   special  method.  programmers  can  override   the  method  to  do  other  things.   •  method_missing  in  Ruby,   •  doesNotUnderstand  in  Smalltalk  •  e.g.,  Rails  Ac]ve  record’s  dynamic  finders   24
  25. 25. 4.12 Providing Some Type Checking•  sta]c  type  checking  or  not  •  type  checking  at  compile  ]me  or  run  ]me  •  modern  IDEs  provide  excellent  support  based   on  sta]c  typing   25
  26. 26. Yoshioka   26
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×