I'm thinking of an approach to writing a DSL with a much cleaner syntax. I'll 
try to put something together.

-------------------------------------
Marius Danciu<marius.dan...@gmail.com> wrote:

All,

I just want to see if there is any interest in the approach discussed here.
As you know Lift has some interesting support for building JavaScript
constructs from Scala code usig JsExp, JsCmd etc classes. I used quite a lot
this support and it's great but if your JS code that you want to send down
to the browser (say as an Ajax or Comet partial update response) gets a bit
more complicated then constructing the JS fragment leads IMO to some
cumbersome Scala code. I found myselft in quite a few situation to use JsRaw
to write the JavaScript fragment in order for the code reader to understand
what JavaScript code will be generated. But of course with JsRaw we put
everything into a String so I'm not a big fan of this approach. So I started
to define a JavaScript like "DSL" that IMO is closer to JavaScript form.
Attached is a source code smaple of how this looks like, so for instance we
can have something like:


val js = JsFunc('myFunc, 'param1, 'param2) {
    JsIf('param1 __< 30) {
        Var('home) := Wrap(234 __- 3) __/ 2 `;`
        Var('someArray) := JsArray(1, 2, 3, 4, 5) `;`
        'myFunc(1, 2, "do it", 'home) `;`
        $("#myID") >> 'attr("value", "123") `;`
      } ~
      JsForEach(Var('i) in 'someArray) {
        'console >> 'log("Hi there " __+ 'i) `;`
      } ~
      JsAnonFunc('arg1, 'arg2) {
       'alert("Anonymous function " __+ 'arg1 __+ 'arg2)
      }(1, 2) `;`
    }

    println(js.toJs)

this yields the following JavaScript code:


function myFunc( param1, param2 ) {
if (param1 < 30) {
var home = ( 234 - 3 ) / 2;
var someArray = [ 1, 2, 3, 4, 5 ];
myFunc(1, 2, "do it", home);
$("#myID").attr("value", "123");
}
for (var i in someArray) {
console.log("Hi there " + i);
}
function ( arg1, arg2 ) {
alert("Anonymous function " + arg1 + arg2)
}(1, 2);
}


... ok I just droped nonsense code in there for exemplification. A few
words:

1. JsIf, JsForEach describe JavaScript if and for(each) statements
2. Functions like __<, __>, ... __+, __- are function that alows definition
of boolean and/or algebraic expressions.
3. Wrap just wraps an expression into ()
4. Var defined a variable
5 := defines an assignment
6. JsFunc declares a JS function
7. JsAnonFunc declares an anonymous function
8. 'myFunc(1, 2, "do it", 'home)  is simply a javascript function invocation
by providing 4 parameter.
9. ~ is just a function that chains statements that don;t necessarily end in
;


Do you think that something like this would be usable in Lift?

Br's,
Marius

--

You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to lift...@googlegroups.com.
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.


--

You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to lift...@googlegroups.com.
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.


Reply via email to