1 package antlr.preprocessor; 2 3 9 10 import antlr.collections.impl.IndexedVector; 11 12 import java.util.Enumeration ; 13 import java.io.*; 14 15 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; 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; } 40 String expandedFileName = nameForExpandedGrammarFile(this.getName()); 41 42 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 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 String s = h + o; 83 for (Enumeration e = grammars.elements(); e.hasMoreElements();) { 84 Grammar g = (Grammar)e.nextElement(); 85 s += g; 86 } 87 return s; 88 } 89 } 90 | Popular Tags |