• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Test Infrastructure */
2
3function 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
25eval(loadFile("../../lib/antlr3-all.js"));
26eval(loadFile("../../lib/antlr3-cli.js"));
27eval(loadFile("PythonLexer.js"));
28eval(loadFile("PythonParser.js"));
29eval(loadFile("rhino-python.extensions"));
30
31/* Parser Extensions */
32
33var output = [];
34function xlog(msg) {
35    output.push(msg);
36}
37
38function MyLexer() {
39    MyLexer.superclass.constructor.apply(this, arguments);
40}
41ANTLR.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});
49MyLexer.prototype.emitErrorMessage = function(msg) {xlog(msg);}
50PythonParser.prototype.emitErrorMessage = function(msg) {xlog(msg);}
51
52/* Test */
53
54function 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
70var input = loadFile("rhino-python.input");
71var expected = loadFile("rhino-python.output");
72parse(input);
73var actual = output.join("\n")+"\n";
74if (actual==expected) {
75    print("Test Passed!");
76} else {
77    print("Test Failed!");
78    print(actual);
79    print(expected);
80}
81