www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ParserCobinator like scala in D

reply =?ISO-2022-JP?B?GyRCSH5HTzVXOVQbKEI=?= <youxkei gmail.com> writes:
--90e6ba6e82d2d7aa7904923d2094
Content-Type: text/plain; charset=ISO-8859-1

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?

--90e6ba6e82d2d7aa7904923d2094
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div><div>Since I want parser to make a Scripting Engine, I wrote ParserCom=
binator which is popular in Scala in D.</div><div>However, when we devotedl=
y write parser using ParserCombinator, the source code is very dirty.</div>
<div>Like this</div><div><span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre">	</span>convert!(parseSeq!(parseOption(parseChar!(&#39;a&#39;)), par=
seChar!(&#39;b&#39;)),</div><div><span class=3D"Apple-tab-span" style=3D"wh=
ite-space:pre">	</span>funciton(~~~~){~~~~~})</div>
<div><br></div><div>We can easily convert PEG into parser by using ParserCo=
mbinator,</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=3D"Appl=
e-tab-span" style=3D"white-space:pre">	</span>enum peg =3D q{</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>foo&=
lt;int&gt; =3D (&#39;0&#39; | &#39;1&#39;) &gt;&gt; (char c) {</div><div><s=
pan class=3D"Apple-tab-span" style=3D"white-space:pre">			</span>return c -=
 &#39;0&#39;;</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>};</=
div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}=
</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span=
mixin(defs(peg).value);</div>

a matter of convenience.</div><div><br></div><div>Template Library: <a hre= f=3D"http://ideone.com/3rKF4">http://ideone.com/3rKF4</a></div><div>ParserC= ombinator: <a href=3D"http://ideone.com/YlGP2">http://ideone.com/YlGP2</a><= /div> <div>PEGParser: <a href=3D"http://ideone.com/vkTyh">http://ideone.com/vkTyh= </a></div><div>Sample(Expression of four arithmetic operations): <a href=3D= "http://ideone.com/0uc3t">http://ideone.com/0uc3t</a></div><div><br></div><= div> What do you think about this?</div></div> --90e6ba6e82d2d7aa7904923d2094--
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