// If you want to keep instance data over several steps, e.g. in // a read-evaluate-print-loop, you should use the createInstance() method // as below in the little calculator example from Alex' Thesis // 1) Define your grammar ometa StatefulCalculator <: Parser { var = spaces letter:x -> x, num = num:n digit:d -> (n*10 + d*1) | digit:d -> (d*1), primaryExpr = spaces var:x -> self.vars[x] | spaces num:n -> n | "(" expr:r ")" -> r, mulExpr = mulExpr:x "*" primaryExpr:y -> ( x * y ) | mulExpr:x "/" primaryExpr:y -> ( x / y ) | primaryExpr, addExpr = addExpr:x "+" mulExpr:y -> ( x + y ) | addExpr:x "-" mulExpr:y -> ( x - y ) | mulExpr, expr = var:x "=" expr:r -> (self.vars[x] = r) | addExpr, doit = (expr:r)* spaces end -> r } StatefulCalculator.initialize = function() { this.vars = {}; } // 2) --- Create a Calculator instance var sc = StatefulCalculator.createInstance(); // 3) --- Now this instance preserves its state in successive matchAll() calls sc.matchAll( "x=1", "doit"); /* stores x */ sc.vars.x /* you see, it's available */ sc.matchAll( "x=x+1", "doit"); /* do something with x */ sc.vars.x