• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2010 Terence Parr
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *  3. The name of the author may not be used to endorse or promote products
15  *      derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.tool;
29 
30 import org.antlr.Tool;
31 import org.antlr.runtime.*;
32 import org.antlr.runtime.tree.ParseTree;
33 
34 import java.io.BufferedReader;
35 import java.io.FileReader;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.StringTokenizer;
40 
41 /** Interpret any ANTLR grammar:
42  *
43  *  java Interp file.g tokens-to-ignore start-rule input-file
44  *
45  *  java Interp C.g 'WS COMMENT' program t.c
46  *
47  *  where the WS and COMMENT are the names of tokens you want to have
48  *  the parser ignore.
49  */
50 public class Interp {
51     public static class FilteringTokenStream extends CommonTokenStream {
FilteringTokenStream(TokenSource src)52         public FilteringTokenStream(TokenSource src) { super(src); }
53         Set<Integer> hide = new HashSet<Integer>();
sync(int i)54         protected void sync(int i) {
55             super.sync(i);
56             if ( hide.contains(get(i).getType()) ) get(i).setChannel(Token.HIDDEN_CHANNEL);
57         }
setTokenTypeChannel(int ttype, int channel)58         public void setTokenTypeChannel(int ttype, int channel) {
59             hide.add(ttype);
60         }
61     }
62 
63 	// pass me a java file to parse
main(String[] args)64 	public static void main(String[] args) throws Exception {
65 		if ( args.length!=4 ) {
66 			System.err.println("java Interp file.g tokens-to-ignore start-rule input-file");
67 			return;
68 		}
69 		String grammarFileName = args[0];
70 		String ignoreTokens = args[1];
71 		String startRule = args[2];
72 		String inputFileName = args[3];
73 
74 		// TODO: using wrong constructor now
75 		Tool tool = new Tool();
76 		CompositeGrammar composite = new CompositeGrammar();
77 		Grammar parser = new Grammar(tool, grammarFileName, composite);
78 		composite.setDelegationRoot(parser);
79 		FileReader fr = new FileReader(grammarFileName);
80 		BufferedReader br = new BufferedReader(fr);
81 		parser.parseAndBuildAST(br);
82 		br.close();
83 
84 		parser.composite.assignTokenTypes();
85 		parser.composite.defineGrammarSymbols();
86 		parser.composite.createNFAs();
87 
88 		List leftRecursiveRules = parser.checkAllRulesForLeftRecursion();
89 		if ( leftRecursiveRules.size()>0 ) {
90 			return;
91 		}
92 
93 		if ( parser.getRule(startRule)==null ) {
94 			System.out.println("undefined start rule "+startRule);
95 			return;
96 		}
97 
98 		String lexerGrammarText = parser.getLexerGrammar();
99 		Grammar lexer = new Grammar(tool);
100 		lexer.importTokenVocabulary(parser);
101 		lexer.fileName = grammarFileName;
102 		lexer.setTool(tool);
103 		if ( lexerGrammarText!=null ) {
104 			lexer.setGrammarContent(lexerGrammarText);
105 		}
106 		else {
107 			System.err.println("no lexer grammar found in "+grammarFileName);
108 		}
109 		lexer.composite.createNFAs();
110 
111 		CharStream input =
112 			new ANTLRFileStream(inputFileName);
113 		Interpreter lexEngine = new Interpreter(lexer, input);
114 		FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
115 		StringTokenizer tk = new StringTokenizer(ignoreTokens, " ");
116 		while ( tk.hasMoreTokens() ) {
117 			String tokenName = tk.nextToken();
118 			tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), 99);
119 		}
120 
121 		if ( parser.getRule(startRule)==null ) {
122 			System.err.println("Rule "+startRule+" does not exist in "+grammarFileName);
123 			return;
124 		}
125 		Interpreter parseEngine = new Interpreter(parser, tokens);
126 		ParseTree t = parseEngine.parse(startRule);
127 		System.out.println(t.toStringTree());
128 	}
129 }
130