5 May 2011 11:09
I wanted to create the "Hello World" application for Boo.OMeta.Parser but it turned out that it is not so easy. Of course I could just parse "print 'Hello world'" but it doesn't show essential idea of the parser. Instead I came up with idea to parse something very famous from literature having sense from DSL perspective. "To be or not to be..." matched both criteries. So this is what I want to share:
return BinaryExpression(Operator: BinaryOperatorType.Assign, Left: l, Right: newStringLiteral(flatString(r)))
assignment = shakespeare_assignment | super
shakespeare_assignment = (++(~(tit),_) >> r, tit, reference >> l, ("."|"") ^ newShakespeareAssignment(l,r) )
tit = --("," | ":"| " "), "that is the"
def OMetaParseAndRun(code as string):
compiler = Boo.Lang.Compiler.BooCompiler()
compiler.Parameters.OutputWriter = StringWriter()
compiler.Parameters.Pipeline = Boo.Lang.Compiler.Pipelines.CompileToMemory()
code = """
To be, or not to be, that is the question.
output = StringWriter()
assert output.ToString() == "To be, or not to be\r\n"
The application compiles and executes the famous Shakespeare's quotation. It is demonstration of flexibility of Boo.OMeta.Parser. It also demonstrates how the extendable parser can be used as a tool for creation of external DSLs. Please notice that the standard compiler pipeline was used. The only thing was changed is that parser step was changed from standard to Boo.OMeta.Parser.
CompilingShakespeare.zip (185.26 kb)