Home | History | Annotate | Download | only in functional
      1 /* Test Infrastructure */
      2 
      3 function loadFile(fileName, encoding) {
      4     var f = new java.io.File(fileName),
      5         size = f.length(),
      6         isr,
      7         fis = new java.io.FileInputStream(f);
      8     if (encoding) {
      9         isr = new java.io.InputStreamReader(fis, encoding);
     10     } else {
     11         isr = new java.io.InputStreamReader(fis);
     12     }
     13 
     14     /* Should use the ternary version of isr.read here, but can't figure
     15      * out how to create a Java char array from JS. . .
     16      * @todo
     17      */
     18     var charCode, data=[];
     19     while ((charCode = isr.read()) >= 0) {
     20         data.push(String.fromCharCode(charCode));
     21     }
     22     return data.join("");
     23 }
     24 
     25 eval(loadFile("../../lib/antlr3-all.js"));
     26 eval(loadFile("../../lib/antlr3-cli.js"));
     27 eval(loadFile("PythonLexer.js"));
     28 eval(loadFile("PythonParser.js"));
     29 eval(loadFile("rhino-python.extensions"));
     30 
     31 /* Parser Extensions */
     32 
     33 var output = [];
     34 function xlog(msg) {
     35     output.push(msg);
     36 }
     37 
     38 function MyLexer() {
     39     MyLexer.superclass.constructor.apply(this, arguments);
     40 }
     41 ANTLR.lang.extend(MyLexer, PythonLexer, {
     42     nextToken: function() {
     43         // keep track of this token's position in line because Python is
     44         // whitespace sensitive
     45         this.startPos = this.getCharPositionInLine();
     46         return MyLexer.superclass.nextToken.call(this);
     47     }
     48 });
     49 MyLexer.prototype.emitErrorMessage = function(msg) {xlog(msg);}
     50 PythonParser.prototype.emitErrorMessage = function(msg) {xlog(msg);}
     51 
     52 /* Test */
     53 
     54 function parse(text) {
     55     try {
     56         var input = new ANTLR.runtime.ANTLRStringStream(text);
     57         var lexer = new MyLexer(input);
     58         var tokens = new ANTLR.runtime.CommonTokenStream(lexer);
     59         tokens.discardOffChannelTokens=true;
     60         var indentedSource = new PythonTokenSource(tokens);
     61         tokens = new ANTLR.runtime.CommonTokenStream(indentedSource);
     62         var parser = new PythonParser(tokens);
     63         parser.file_input();
     64     } catch (e) {
     65         xlog(e.toString());
     66     } finally {
     67     }
     68 }
     69 
     70 var input = loadFile("rhino-python.input");
     71 var expected = loadFile("rhino-python.output");
     72 parse(input);
     73 var actual = output.join("\n")+"\n";
     74 if (actual==expected) {
     75     print("Test Passed!");
     76 } else {
     77     print("Test Failed!");
     78     print(actual);
     79     print(expected);
     80 }
     81