www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ParserCobinator like scala in D

reply =?ISO-2022-JP?B?GyRCSH5HTzVXOVQbKEI=?= <youxkei gmail.com> writes:
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?
Oct 09 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
 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?
The code looks clean enough. I suggest to use 4 spaces as indent (or a single tab). You may rewrite tuple.field[1] as tuple[1] An alias char[] str; may shorten some signatures a bit. See also Pymeta, an implementation of OMeta: http://washort.twistedmatrix.com/ Bye, bearophile
Oct 10 2010
prev sibling parent Sphere Research <contact sphere-research.com> writes:
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!(parseOpti
n(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><d
v>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><d
v>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>
 
Oct 10 2010