JavaScript:SpiderMonkey:Parser API: Difference between revisions

Jump to navigation Jump to search
more cleanup of JS API
(fixed up types a little bit)
(more cleanup of JS API)
Line 3: Line 3:
=== Design ===
=== Design ===


A general parsing API could treat the visitor interface from [http://code.google.com/p/es-lab/wiki/JsonMLASTFormat JsonMLAST] as a factory with a parametric type. In Java-like pseudo-types:
A general parsing API could treat the visitor interface from [http://code.google.com/p/es-lab/wiki/JsonMLASTFormat JsonMLAST] as a [http://en.wikipedia.org/wiki/Builder_pattern builder] with a parametric type. In Java-like pseudo-types:


<pre>
<pre>
Line 18: Line 18:
<pre>
<pre>
class Parser<A> {
class Parser<A> {
     Parser(factory : Visitor<A>?);
     Parser(builder : Visitor<A>?);


     parse(src : string,
     parse(src : string,
Line 30: Line 30:
This would allow the <code>parse</code> method to use the visitor as a way of injecting the parsed nodes into whatever type ''A'' the client wants. Making the argument optional makes it easy to default to the JsonMLAST format.
This would allow the <code>parse</code> method to use the visitor as a way of injecting the parsed nodes into whatever type ''A'' the client wants. Making the argument optional makes it easy to default to the JsonMLAST format.


It's a little messy (wrt types) to reuse the visitor interface for this, since the JsonMLAST visitor expects JsonMLAST values as arguments, whereas this use expects the type ''A'' as arguments. So it's maybe better to have a parallel "Factory" interface that's the same as the visitor interface but with createXXXX methods instead.
It's a little messy (wrt types) to reuse the visitor interface for this, since the JsonMLAST visitor expects JsonMLAST values as arguments, whereas this use expects the type ''A'' as arguments. So it's maybe better to have a parallel "Builder" interface that's the same as the visitor interface but with buildXXXX methods instead:
 
<pre>
interface Builder<A> {
    buildThisExpr(attr : {}) : A;
    buildReturnStmt(attr : {},
                    expr : A) : A;
    /* etc. */
}
</pre>
 


Should also implement a JsonMLAST processor that takes a visitor:
Should also implement a JsonMLAST processor that takes a visitor:


<pre>
<pre>
processJsonMLAST(ast : JsonMLAST,
processAST(ast : JsonMLAST,
                visitor : Visitor<A>) : A;
          visitor : Visitor<A>) : A;
</pre>
</pre>


This could be implemented in pure JS, at least initially. (It'll be ugly without pattern matching, though.)
This could be implemented in pure JS, at least initially. (It'll be ugly without pattern matching, though.)
I dislike the verbosity of the JsonMLAST name. Maybe just AST is good enough.


=== Plan ===
=== Plan ===
34

edits

Navigation menu