1 package persistence.antlr.preprocessor; 2 3 8 9 import persistence.antlr.collections.impl.IndexedVector; 10 11 import java.util.Enumeration ; 12 import java.io.*; 13 14 17 public class GrammarFile { 18 protected String fileName; 19 protected String headerAction = ""; 20 protected IndexedVector options; 21 protected IndexedVector grammars; 22 protected boolean expanded = false; protected persistence.antlr.Tool tool; 24 25 public GrammarFile(persistence.antlr.Tool tool, String f) { 26 fileName = f; 27 grammars = new IndexedVector(); 28 this.tool = tool; 29 } 30 31 public void addGrammar(Grammar g) { 32 grammars.appendElement(g.getName(), g); 33 } 34 35 public void generateExpandedFile() throws IOException { 36 if (!expanded) { 37 return; } 39 String expandedFileName = nameForExpandedGrammarFile(this.getName()); 40 41 PrintWriter expF = tool.openOutputFile(expandedFileName); 43 expF.println(toString()); 44 expF.close(); 45 } 46 47 public IndexedVector getGrammars() { 48 return grammars; 49 } 50 51 public String getName() { 52 return fileName; 53 } 54 55 public String nameForExpandedGrammarFile(String f) { 56 if (expanded) { 57 return "expanded" + tool.fileMinusPath(f); 59 } 60 else { 61 return f; 62 } 63 } 64 65 public void setExpanded(boolean exp) { 66 expanded = exp; 67 } 68 69 public void addHeaderAction(String a) { 70 headerAction += a + System.getProperty("line.separator"); 71 } 72 73 public void setOptions(IndexedVector o) { 74 options = o; 75 } 76 77 public String toString() { 78 String h = headerAction == null ? "" : headerAction; 79 String o = options == null ? "" : Hierarchy.optionsToString(options); 80 81 StringBuffer s = new StringBuffer (10000); s.append(h); s.append(o); 82 for (Enumeration e = grammars.elements(); e.hasMoreElements();) { 83 Grammar g = (Grammar)e.nextElement(); 84 s.append(g.toString()); 85 } 86 return s.toString(); 87 } 88 } 89 | Popular Tags |