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