File: PythonTreeTester.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (104 lines) | stat: -rw-r--r-- 3,119 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.python.antlr;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.runtime.tree.Tree;
import org.antlr.runtime.tree.TreeAdaptor;

/**
 * A walker producing a <code>PythonTree</code> AST.
 */
public class PythonTreeTester {

    public enum Block { MODULE, INTERACTIVE, EXPRESSION };

    private boolean _parseOnly;
    private Block _block;

    public PythonTreeTester() {
        setParseOnly(false);
        setBlock(Block.MODULE);
    }

    public PythonTree parse(String[] args) throws Exception {
        PythonTree result = null;
        //ErrorHandler eh = new ListErrorHandler();
        ErrorHandler eh = new FailFastHandler();
        CharStream input = new ANTLRFileStream(args[0]);
        PythonLexer lexer = new PythonLexer(input);
        lexer.setErrorHandler(eh);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PythonTokenSource indentedSource = new PythonTokenSource(tokens, args[0]);
        tokens = new CommonTokenStream(indentedSource);
        PythonParser parser = new PythonParser(tokens);
        parser.setErrorHandler(eh);
        parser.setTreeAdaptor(new PythonTreeAdaptor());
        PythonTree r = null;
        switch (_block) {
        case MODULE :
            r = parser.file_input().tree;
            break;
        case INTERACTIVE :
            r = parser.single_input().tree;
            break;
        case EXPRESSION :
            r = parser.eval_input().tree;
                break;
        }
        if (args.length > 1) {
            System.out.println(r.toStringTree());
        }
        if (!isParseOnly()) {
            /*
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(r);
            nodes.setTokenStream(tokens);
            PythonWalker walker = new PythonWalker(nodes);
            walker.setErrorHandler(eh);
            switch (_block) {
            case MODULE :
                result = walker.module();
                break;
            case INTERACTIVE :
                result = walker.interactive();
                break;
            case EXPRESSION :
                result = walker.expression();
                break;
            }

            if (args.length > 1) {
                System.out.println(result.toStringTree());
            }
            */
        }
        return result;
    }

    /**
     * If set to <code>true</code>, only <code>PythonParser</code> is
     * called.
     * 
     * @param parseOnly
     */
    public void setParseOnly(boolean parseOnly) {
        _parseOnly = parseOnly;
    }

    public boolean isParseOnly() {
        return _parseOnly;
    }

    public void setBlock(Block block) {
        _block = block;
    }

    public Block getBlock() {
        return _block;
    }

}