ParserCobinator like scala in D

Sphere Research contact at sphere-research.com
Sun Oct 10 13:36:13 PDT 2010


Hi, you can aggregate all submissions on one page: ideone.com/user_login/page - just simply manage them on 'my submissions' panel, good luck!

SphereResearch Team

美馬久行 Wrote:

> Since I want parser to make a Scripting Engine, I wrote ParserCombinator
> which is popular in Scala in D.
> However, when we devotedly write parser using ParserCombinator, the source
> code is very dirty.
> Like this
> convert!(parseSeq!(parseOption(parseChar!('a')), parseChar!('b')),
> funciton(~~~~){~~~~~})
> 
> We can easily convert PEG into parser by using ParserCombinator,
> so I wrote the parser which parse a PEG and return the string of parser in
> CTFE.
> Like this
> enum peg = q{
> foo<int> = ('0' | '1') >> (char c) {
> return c - '0';
> };
> }
> mixin(defs(peg).value);
> 'defs' is the parser.
> I call the parser PEGParser as a matter of convenience.
> 
> Template Library: http://ideone.com/3rKF4
> ParserCombinator: http://ideone.com/YlGP2
> PEGParser: http://ideone.com/vkTyh
> Sample(Expression of four arithmetic operations): http://ideone.com/0uc3t
> 
> What do you think about this?
> 
> <div><div>Since I want parser to make a Scripting Engine, I wrote ParserCombinator which is popular in Scala in D.</div><div>However, when we devotedly write parser using ParserCombinator, the source code is very dirty.</div>
> <div>Like this</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>convert!(parseSeq!(parseOption(parseChar!('a')), parseChar!('b')),</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>funciton(~~~~){~~~~~})</div>
> <div><br></div><div>We can easily convert PEG into parser by using ParserCombinator,</div><div>so I wrote the parser which parse a PEG and return the string of parser in CTFE.</div><div>Like this</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>enum peg = q{</div>
> <div><span class="Apple-tab-span" style="white-space:pre">		</span>foo&lt;int&gt; = ('0' | '1') &gt;&gt; (char c) {</div><div><span class="Apple-tab-span" style="white-space:pre">			</span>return c - '0';</div>
> <div><span class="Apple-tab-span" style="white-space:pre">		</span>};</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>}</div><div><span class="Apple-tab-span" style="white-space:pre">	</span>mixin(defs(peg).value);</div>
> <div>'defs' is the parser.</div><div>I call the parser PEGParser as a matter of convenience.</div><div><br></div><div>Template Library: <a href="http://ideone.com/3rKF4">http://ideone.com/3rKF4</a></div><div>ParserCombinator: <a href="http://ideone.com/YlGP2">http://ideone.com/YlGP2</a></div>
> <div>PEGParser: <a href="http://ideone.com/vkTyh">http://ideone.com/vkTyh</a></div><div>Sample(Expression of four arithmetic operations): <a href="http://ideone.com/0uc3t">http://ideone.com/0uc3t</a></div><div><br></div><div>
> What do you think about this?</div></div>
> 



More information about the Digitalmars-d mailing list