1 | package antlr.preprocessor; |
---|
2 | |
---|
3 | /* ANTLR Translator Generator |
---|
4 | * Project led by Terence Parr at http://www.cs.usfca.edu |
---|
5 | * Software rights: http://www.antlr.org/license.html |
---|
6 | * |
---|
7 | * $Id: //depot/code/org.antlr/release/antlr-2.7.7/antlr/preprocessor/GrammarFile.java#2 $ |
---|
8 | */ |
---|
9 | |
---|
10 | import antlr.collections.impl.IndexedVector; |
---|
11 | |
---|
12 | import java.util.Enumeration; |
---|
13 | import java.io.*; |
---|
14 | |
---|
15 | /** Stores header action, grammar preamble, file options, and |
---|
16 | * list of grammars in the file |
---|
17 | */ |
---|
18 | public class GrammarFile { |
---|
19 | protected String fileName; |
---|
20 | protected String headerAction = ""; |
---|
21 | protected IndexedVector options; |
---|
22 | protected IndexedVector grammars; |
---|
23 | protected boolean expanded = false; // any grammars expanded within? |
---|
24 | protected antlr.Tool tool; |
---|
25 | |
---|
26 | public GrammarFile(antlr.Tool tool, String f) { |
---|
27 | fileName = f; |
---|
28 | grammars = new IndexedVector(); |
---|
29 | this.tool = tool; |
---|
30 | } |
---|
31 | |
---|
32 | public void addGrammar(Grammar g) { |
---|
33 | grammars.appendElement(g.getName(), g); |
---|
34 | } |
---|
35 | |
---|
36 | public void generateExpandedFile() throws IOException { |
---|
37 | if (!expanded) { |
---|
38 | return; // don't generate if nothing got expanded |
---|
39 | } |
---|
40 | String expandedFileName = nameForExpandedGrammarFile(this.getName()); |
---|
41 | |
---|
42 | // create the new grammar file with expanded grammars |
---|
43 | PrintWriter expF = tool.openOutputFile(expandedFileName); |
---|
44 | expF.println(toString()); |
---|
45 | expF.close(); |
---|
46 | } |
---|
47 | |
---|
48 | public IndexedVector getGrammars() { |
---|
49 | return grammars; |
---|
50 | } |
---|
51 | |
---|
52 | public String getName() { |
---|
53 | return fileName; |
---|
54 | } |
---|
55 | |
---|
56 | public String nameForExpandedGrammarFile(String f) { |
---|
57 | if (expanded) { |
---|
58 | // strip path to original input, make expanded file in current dir |
---|
59 | return "expanded" + tool.fileMinusPath(f); |
---|
60 | } |
---|
61 | else { |
---|
62 | return f; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | public void setExpanded(boolean exp) { |
---|
67 | expanded = exp; |
---|
68 | } |
---|
69 | |
---|
70 | public void addHeaderAction(String a) { |
---|
71 | headerAction += a + System.getProperty("line.separator"); |
---|
72 | } |
---|
73 | |
---|
74 | public void setOptions(IndexedVector o) { |
---|
75 | options = o; |
---|
76 | } |
---|
77 | |
---|
78 | public String toString() { |
---|
79 | String h = headerAction == null ? "" : headerAction; |
---|
80 | String o = options == null ? "" : Hierarchy.optionsToString(options); |
---|
81 | |
---|
82 | StringBuffer s = new StringBuffer(10000); s.append(h); s.append(o); |
---|
83 | for (Enumeration e = grammars.elements(); e.hasMoreElements();) { |
---|
84 | Grammar g = (Grammar)e.nextElement(); |
---|
85 | s.append(g.toString()); |
---|
86 | } |
---|
87 | return s.toString(); |
---|
88 | } |
---|
89 | } |
---|