7. Technical Examples
Source: Applying minilanguages: http://www.faqs.org/docs/artu/ch08s02.html
<?xml version="1.0"?>
<xsl:stylesheetversion="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:apply-templates select="*|text()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
cat thesis.ms | chem | tbl | refer | grap | pic | eqn | groff -Tps > thesis.ps
<?xml version="1.0"?>
<GTK-Interface>
<widget>
<class>GtkWindow</class>
<name>HelloWindow</name>
<border_width>5</border_width>
<Signal>
<name>destroy</name>
<handler>gtk_main_quit</handler>
</Signal>
<title>Hello</title>
<type>GTK_WINDOW_TOPLEVEL</type>
<position>GTK_WIN_POS_NONE</position>
<allow_shrink>True</allow_shrink>
<allow_grow>True</allow_grow>
<auto_shrink>False</auto_shrink>
<widget>
<class>GtkButton</class>
<name>Hello World</name>
<can_focus>True</can_focus>
<label>Hello World</label>
</widget>
</widget>
</GTK-Interface>
# Poll this site first each cycle.
poll pop.provider.net proto pop3
user "jsmith" with pass "secret1" is "smith" here
user jones with pass "secret2" is "jjones" here with options keep
# Poll this site second, unless Lord Voldemort zaps us first.
poll billywig.hogwarts.com with proto imap:
user harry_potter with pass "floo" is harry_potter here
# Poll this site third in the cycle.
# Password will be fetched from ~/.netrc
poll mailhost.net with proto imap:
user esr is esr here
Glade
Troff
XSLT
fetchmail
Regex
"x.z?z{1,3}y"
SQL
SELECT * FROM TABLE
WHERE NAME LIKE '%SMI'
ORDER BY NAME
DSLs 2010 - 7
14. HAI CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
UP VAR!!1
VISIBLE VAR
IZ VAR BIGGER THAN 10?
KTHXBYE
IM OUTTA YR LOOP
KTHXBYE
Developer producing LOLCODE
15. And in the end...
nobody understands
each other
16. DSL: a potential solution?
1616
• Use a more expressive language than a
general purpose one
• Share a common metaphor of
understanding between developers and
subject matter experts
• Have domain experts help with the design
of the business logic of an application
• Avoid cluttering business code with
too much boilerplate technical code
• Cleanly separate business logic from
application code
• Let business rules have their own lifecycle
17. 17
Copyright 2010 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Towards more readibility (1)
20%
17
18. 18
Copyright 2010 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Towards more readibility (2)
80%
56. ...GPars
import static org.gparallelizer.dataflow.DataFlow.thread
final def x = new DataFlowVariable()
final def y = new DataFlowVariable()
final def z = new DataFlowVariable()
thread {
z << ~x + ~y
println "Result: ${~z}"
}
thread {
x << 10
}
thread {
y << 5
}
No race-conditions
No deadlocks
No live-locks
Completely deterministic programs
BEAUTIFUL code
DSLs 2010 - 56
115. AST Transformations
• With metaprogramming, Groovy‟s able to
modify the behaviour of programs... at
runtime
• AST Transformations provide a compile-
time approach to modify programs
– AST: Abstract Syntax Tree
– Ability to change what‟s being compiled at compile-
time!
• No runtime impact!
• Lets you change the semantics of your programs!
• Nice way of implementing patterns and removing boiler-plate
technical code
• Two kinds of transformations: global & local
DSLs 2010 - 115
123. EasyB Preprocessing...
• EasyB supports either of these:
• But we would like this:
given ("some data") {
println '... setting expectations'
}
Source: http://hamletdarcy.blogspot.com/2010/02/groovy-antlr-plugins-for-better-dsls.html
given "some data", {
println '... setting expectations'
}
given "some data" {
println '... setting expectations'
}
DSLs 2010 - 123
But also consider Command Expressions (GEP-3) for Groovy1.8+.
124. ...EasyB Preprocessing...
String addCommas(text) {
def pattern =
~/(.*)(given|when|then) "([^"]*(.[^"]*)*)" {(.*)/
def replacement = /$1$2 "$3", {$4/
(text =~ pattern).replaceAll(replacement)
}
Source: http://hamletdarcy.blogspot.com/2010/02/groovy-antlr-plugins-for-better-dsls.html
DSLs 2010 - 124
Adds the comma in where required before Groovy sees it.
125. ...EasyB Preprocessing
class SourceModifierParserPlugin extends AntlrParserPlugin {
Reduction parseCST(SourceUnit sourceUnit, Reader reader)
throws CompilationFailedException {
def text = addCommas(reader.text)
StringReader stringReader = new StringReader(text)
super.parseCST(sourceUnit, stringReader)
}
}
def parserPluginFactory = new ParserPluginFactory() {
ParserPlugin createParserPlugin() {
new SourceModifierParserPlugin()
}
}
def conf = new CompilerConfiguration(pluginFactory: parserPluginFactory)
def binding = ...
def shell = new GroovyShell(binding, conf)
Source: http://hamletdarcy.blogspot.com/2010/02/groovy-antlr-plugins-for-better-dsls.html
DSLs 2010 - 125
But use with restraint as error messages will be with
respect to converted source code not original.
128. ...GParsec
• satisfyC: combinator consumes a single input when its
predicate succeeds
• altC (alt3C, altCs): is the choice combinator. Given two
parsers it only looks at its second alternative if the first has
not consumed any input - regardless of the final value
• seqC (seq3C, seqCs): is the sequencing combinator. It runs
two parsers in succession and if successful, returns the result
of the two parsers
• noneOrMoreC, oneOrMoreC: applies a parser zero or more
times to an input stream. The result from each application of
the parser are returned in a list
• optionalC: The combinator optionalC may succeed in
parsing some input. It always returns success.
DSLs 2010 - 128
183. Some Finishing Guidelines
• Language and program evolve together …
In the end your program will look as if the
language had been designed for it … you
end up with code which is clear, small,
and efficient.
– Paul Graham
• Don‟t think just about Coding up your DSL
– But also about its target audience, its evolution,
required tool support and its testability
DSLs 2010 - 183