This page is no longer maintained — Please continue to the home page at www.scala-lang.org

a combinator parser for the Scala language?

5 replies
James.Strachan
Joined: 2009-07-08,
User offline. Last seen 2 years 25 weeks ago.

Scala's combinator parsers are awesome. I can see it being pretty
handy for folks creating external DSLs with parser combinators to be
able to reuse Scala's parser to reuse bits of the Scala language as
and when required in their DSL. (I've a real use case in Scalate BTW
:).

I've googled around trying to find a combinator parser for the Scala
language but drawn a blank so far. I wonder if anyone had seen one?
Seems the real scala compiler uses some similar kinds of classes (some
cut and pasted for legacy reasons?) but doesn't reuse the
scala.util.parsing.combinator package from what I can tell and I don't
see an easy way of reusing the two things together. I spotted one in
the old scalax library too, but that doesn't use the combinator parser
library either.

Has anyone managed to get the real Scala parser to work inside an
embedded DSL using the scala.util.parsing.combinator package - or
found a Scala parser implemented in Scala's combinator parser library?
Currently I'm only after a Scanner for the scala language that can be
easily used with Scala's parser combinators; but longer term it'd be
cool to just work direct with Scala AST...

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: a combinator parser for the Scala language?

On Wed, Apr 14, 2010 at 12:26:32PM +0100, James Strachan wrote:
> I've googled around trying to find a combinator parser for the Scala
> language but drawn a blank so far. I wonder if anyone had seen one?

I wrote one last summer.

http://github.com/paulp/scala-lang-combinators

It's not 100% there and it's abandonware right now, but I'd be surprised
if there's anything closer to done. If you wanted to finish it I'd be
thrilled. At the time I stopped working on it I was fully parsing all
but a few files in trunk which involved XML.

James.Strachan
Joined: 2009-07-08,
User offline. Last seen 2 years 25 weeks ago.
Re: a combinator parser for the Scala language?

On 14 April 2010 13:22, Paul Phillips wrote:
> On Wed, Apr 14, 2010 at 12:26:32PM +0100, James Strachan wrote:
>> I've googled around trying to find a combinator parser for the Scala
>> language but drawn a blank so far. I wonder if anyone had seen one?
>
> I wrote one last summer.
>
>  http://github.com/paulp/scala-lang-combinators
>
> It's not 100% there and it's abandonware right now, but I'd be surprised
> if there's anything closer to done.  If you wanted to finish it I'd be
> thrilled.  At the time I stopped working on it I was fully parsing all
> but a few files in trunk which involved XML.

Awesome stuff Paul - I'll take a look, many thanks!

Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.
Re: a combinator parser for the Scala language?

 Did you consider writing Scalate as an internal DSL with a compiler plugin ?
It seems that you would get to reuse Scalac's parser, type system and a lot of heavy lifting.
I don't know how practical it would be since I have not yet played with Scalac
plugins, I'm waiting for meta programming support à la AST Transform (à la Groovy),
Paul Philips once said he had something like it on the back burner ;-).
I like the idea of plugin enhanced internal DSLs as an alternative to external DSLs,
It sounds good in theory, could it be as good in practice ?

 BTW, I like what I see in Scalate, It is certainly enriches the Scala platform.

 Cheers

On Wed, Apr 14, 2010 at 8:28 AM, James Strachan <james [dot] strachan [at] gmail [dot] com> wrote:
On 14 April 2010 13:22, Paul Phillips <paulp [at] improving [dot] org> wrote:
> On Wed, Apr 14, 2010 at 12:26:32PM +0100, James Strachan wrote:
>> I've googled around trying to find a combinator parser for the Scala
>> language but drawn a blank so far. I wonder if anyone had seen one?
>
> I wrote one last summer.
>
>  http://github.com/paulp/scala-lang-combinators
>
> It's not 100% there and it's abandonware right now, but I'd be surprised
> if there's anything closer to done.  If you wanted to finish it I'd be
> thrilled.  At the time I stopped working on it I was fully parsing all
> but a few files in trunk which involved XML.

Awesome stuff Paul - I'll take a look, many thanks!

--
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

James.Strachan
Joined: 2009-07-08,
User offline. Last seen 2 years 25 weeks ago.
Re: a combinator parser for the Scala language?

2010/4/14 Maxime Lévesque :
>  Did you consider writing Scalate as an internal DSL with a compiler plugin
> ?

Yes, we thought of that. Scaml/Haml is whitespace sensitive which
doen't translate well to an internal DSL - so we'd have to switch
indentation for {} or () which would make it much noisier and no
longer be HAML really. Haml's amazing DRYness and conciseness stems
from using indentation and the first character to determine elements,
div IDs, CSS style names or expressions etc. I'm generally not a fan
of indentation based languages - but it works very well for Scaml/Haml
and markup. Similarly SSP would be hard to do nicely; escaping into
and out of pages of text is quite noisy inside the Scala language with
""" and + etc.

For templating it seems the typesafe builder is maybe the neatest
approach as an internal DSL...

def indexPage = html {
body {
h1("someTitle")
p("the time is now " + new java.util.Date)
}
}

I guess we could look at a compiler plugin to integrate Scalate's
external DSL & code generator into the regular Scala compile step if
folks embed a template inside a class. e.g.

import org.fusesource.scalate.Template
import org.fusesource.scalate.scaml.Scaml

class Foo {
val template: Template = Scaml("""
%h1 someTitle

%p
the time is now
= new java.util.Date
""")

...
}

the compiler plugin could compile that Scaml template into bytecode as
part of the step of comping Foo I guess? i.e. using """ quoted
strings for the template in a Scala source file and embedding the
external DSL in there

> It seems that you would get to reuse Scalac's parser, type system and a lot
> of heavy lifting.

We're currently reusing scalac to generate the bytecode and to compile
the 'scala bits' in Scalate - so we're really doing as little as we
can in template-land and just defer to the Scala language for all the
heavy lifting. While Scaml/Haml is a bit more complex, SSP is really
simple, its really just about cutting a few holes in a page of text to
put some Scala expressions and statements in there which scalac
compiles.

e.g. here's the parser for Ssp which supports both JSP style and
Velocity style directives. I'm still amazed how small and concise
scala code can be...
http://github.com/scalate/scalate/blob/master/scalate-core/src/main/scal...

> I don't know how practical it would be since I have not yet played with
> Scalac
> plugins, I'm waiting for meta programming support à la AST Transform (à la
> Groovy),
> Paul Philips once said he had something like it on the back burner ;-).
> I like the idea of plugin enhanced internal DSLs as an alternative to
> external DSLs,
> It sounds good in theory, could it be as good in practice ?

It certainly sounds interesting; I'm sure there's lots of cool stuff
we can do with internal DSLs and compiler plugins.

What kind of thing did you have in mind for a template language as an
internal DSL?

>  BTW, I like what I see in Scalate, It is certainly enriches the Scala
> platform.

Thanks!

Stephen Haberman
Joined: 2009-07-17,
User offline. Last seen 42 years 45 weeks ago.
Re: a combinator parser for the Scala language?

> For templating it seems the typesafe builder is maybe the neatest
> approach as an internal DSL...
>
> def indexPage = html {
> body {
> h1("someTitle")
> p("the time is now " + new java.util.Date)
> }
> }

Wonder if you could use code generation for that--e.g. feed an arbitrary
DTD/schema file into "generate-my-builder.scala" and it pops out a bunch
of case classes that would let you write in the above style.

I think scalaxb.org is doing something very close to this; no idea if
it can consume the full HTML DTD yet, nor if the case classes it
generates can be composed in the builder pattern.

- Stephen

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland