README.md
changeset 56 d48cf08cf448
equal deleted inserted replaced
55:e76930ea6464 56:d48cf08cf448
       
     1 ## What is YML?
       
     2 
       
     3 Well, it's the idea not to need to define a grammar first when you want to use a Domain Specific Language. For that purpose, YML is being translated into XML. Let's make an example.
       
     4 
       
     5 Everything which comes close to a C like language, parses without a grammar definition:
       
     6 
       
     7 This:
       
     8 
       
     9     template< class T > T max(T a, T b);
       
    10 
       
    11 Parses to:
       
    12 
       
    13     <?xml version='1.0' encoding='UTF-8'?>
       
    14     <template>
       
    15       <generic>
       
    16         <class/>
       
    17         <T/>
       
    18       </generic>
       
    19       <T>
       
    20         <max>
       
    21           <parm>
       
    22             <T/>
       
    23             <a/>
       
    24           </parm>
       
    25           <parm>
       
    26             <T/>
       
    27             <b/>
       
    28           </parm>
       
    29         </max>
       
    30       </T>
       
    31     </template>
       
    32 
       
    33 Instead of defining grammars, you test out and play around until the results are matching your needs. If the resulting tree does not fit what you're expecting, change it by patching the grammar with `decl`:
       
    34 
       
    35 This:
       
    36 
       
    37     module A {
       
    38         interface B {
       
    39             attribute long n;
       
    40         };
       
    41     };
       
    42 
       
    43 Parses to:
       
    44 
       
    45     <?xml version='1.0' encoding='UTF-8'?>
       
    46     <module>
       
    47       <A>
       
    48         <interface>
       
    49           <B>
       
    50             <attribute>
       
    51               <long>
       
    52                 <n/>
       
    53               </long>
       
    54             </attribute>
       
    55           </B>
       
    56         </interface>
       
    57       </A>
       
    58     </module>
       
    59 
       
    60 This does not look like what we want. So we tell YML that we have a module name after the module, an interface name after the interface and type and name after the attribute:
       
    61 
       
    62 This:
       
    63 
       
    64     decl module @name;
       
    65     decl interface @name;
       
    66     decl attribute @type @name;
       
    67 
       
    68     module A {
       
    69         interface B {
       
    70             attribute long n;
       
    71         };
       
    72     };
       
    73 
       
    74 Parses to:
       
    75 
       
    76     <?xml version='1.0' encoding='UTF-8'?>
       
    77     <module name="A">
       
    78       <interface name="B">
       
    79         <attribute type="long" name="n"/>
       
    80       </interface>
       
    81     </module>
       
    82 
       
    83 What can I do with YML?
       
    84 
       
    85 With YML you can:
       
    86 
       
    87   * use a C-like DSL without writing a grammar first
       
    88   * generate code out of this DSL using YSLT
       
    89   * generate code out of UML using YSLT on XMI
       
    90   * generate code out of any XML based language like SVG using YSLT
       
    91   * define a wiki like language in just a few lines like YHTML does
       
    92   * replace bad designed and complicated XML languages with simpler C-like ones
       
    93   * ... and much more.
       
    94 
       
    95 How it works: Replacing angle brackets with some Python
       
    96 
       
    97 Just writing down what I wanted to have instead of XML for a sample:
       
    98 
       
    99     <list name="List of goods">
       
   100         <head>
       
   101             <columTitle>
       
   102                 Goods
       
   103             </columnTitle>
       
   104             <columnTitle>
       
   105                 Price
       
   106             </columnTitle>
       
   107         </head>
       
   108         <row>
       
   109             <value>
       
   110                 Beer
       
   111             </value>
       
   112             <value>
       
   113                 20
       
   114             </value>
       
   115         </row>
       
   116         <row>
       
   117             <value>
       
   118                 Wine
       
   119             </value>
       
   120             <value>
       
   121                 30
       
   122             </value>
       
   123         </row>
       
   124     </list>
       
   125 
       
   126 Something like that should be more easy, say, like this:
       
   127 
       
   128     list "List of goods" {
       
   129         head title "Goods", title "Price";
       
   130         row value "Beer", value 20;
       
   131         row value "Wine", value 30;
       
   132     }
       
   133 
       
   134 ### Y Languages
       
   135 
       
   136 The latter is what I call an Y language – a language specified in YML. How could this be achieved? Well, what's to do? To have the required information, how to build XML from the script above, we need:
       
   137 
       
   138   * the information, that “list of goods” is an attribute named `name`, while `Goods` is the text value of a tag
       
   139   * `title` shout be written out as `columnTitle`
       
   140 
       
   141 How to do that? Let's invent a simple definition language for that information:
       
   142 
       
   143     decl list(name);
       
   144     decl title alias columnTitle;