>b's YML 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.5 KiB

3 years ago
  1. ## What is YML?
  2. 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.
  3. Everything which comes close to a C like language, parses without a grammar definition:
  4. This:
  5. template< class T > T max(T a, T b);
  6. Parses to:
  7. <?xml version='1.0' encoding='UTF-8'?>
  8. <template>
  9. <generic>
  10. <class/>
  11. <T/>
  12. </generic>
  13. <T>
  14. <max>
  15. <parm>
  16. <T/>
  17. <a/>
  18. </parm>
  19. <parm>
  20. <T/>
  21. <b/>
  22. </parm>
  23. </max>
  24. </T>
  25. </template>
  26. 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`:
  27. This:
  28. module A {
  29. interface B {
  30. attribute long n;
  31. };
  32. };
  33. Parses to:
  34. <?xml version='1.0' encoding='UTF-8'?>
  35. <module>
  36. <A>
  37. <interface>
  38. <B>
  39. <attribute>
  40. <long>
  41. <n/>
  42. </long>
  43. </attribute>
  44. </B>
  45. </interface>
  46. </A>
  47. </module>
  48. 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:
  49. This:
  50. decl module @name;
  51. decl interface @name;
  52. decl attribute @type @name;
  53. module A {
  54. interface B {
  55. attribute long n;
  56. };
  57. };
  58. Parses to:
  59. <?xml version='1.0' encoding='UTF-8'?>
  60. <module name="A">
  61. <interface name="B">
  62. <attribute type="long" name="n"/>
  63. </interface>
  64. </module>
  65. What can I do with YML?
  66. With YML you can:
  67. * use a C-like DSL without writing a grammar first
  68. * generate code out of this DSL using YSLT
  69. * generate code out of UML using YSLT on XMI
  70. * generate code out of any XML based language like SVG using YSLT
  71. * define a wiki like language in just a few lines like YHTML does
  72. * replace bad designed and complicated XML languages with simpler C-like ones
  73. * ... and much more.
  74. How it works: Replacing angle brackets with some Python
  75. Just writing down what I wanted to have instead of XML for a sample:
  76. <list name="List of goods">
  77. <head>
  78. <columTitle>
  79. Goods
  80. </columnTitle>
  81. <columnTitle>
  82. Price
  83. </columnTitle>
  84. </head>
  85. <row>
  86. <value>
  87. Beer
  88. </value>
  89. <value>
  90. 20
  91. </value>
  92. </row>
  93. <row>
  94. <value>
  95. Wine
  96. </value>
  97. <value>
  98. 30
  99. </value>
  100. </row>
  101. </list>
  102. Something like that should be more easy, say, like this:
  103. list "List of goods" {
  104. head title "Goods", title "Price";
  105. row value "Beer", value 20;
  106. row value "Wine", value 30;
  107. }
  108. ### Y Languages
  109. 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:
  110. * the information, that “list of goods” is an attribute named `name`, while `Goods` is the text value of a tag
  111. * `title` shout be written out as `columnTitle`
  112. How to do that? Let's invent a simple definition language for that information:
  113. decl list(name);
  114. decl title alias columnTitle;