Methods Summary |
---|
public void | addOption(persistence.antlr.preprocessor.Option o)
if (options == null) { // if not already there, create it
options = new IndexedVector();
}
options.appendElement(o.getName(), o);
|
public void | addRule(persistence.antlr.preprocessor.Rule r)
rules.appendElement(r.getName(), r);
|
public void | expandInPlace()Copy all nonoverridden rules, vocabulary, and options into this grammar from
supergrammar chain. The change is made in place; e.g., this grammar's vector
of rules gets bigger. This has side-effects: all grammars on path to
root of hierarchy are expanded also.
// if this grammar already expanded, just return
if (alreadyExpanded) {
return;
}
// Expand super grammar first (unless it's a predefined or subgrammar of predefined)
Grammar superG = getSuperGrammar();
if (superG == null)
return; // error (didn't provide superclass)
if (exportVocab == null) {
// if no exportVocab for this grammar, make it same as grammar name
exportVocab = getName();
}
if (superG.isPredefined())
return; // can't expand Lexer, Parser, ...
superG.expandInPlace();
// expand current grammar now.
alreadyExpanded = true;
// track whether a grammar file needed to have a grammar expanded
GrammarFile gf = hier.getFile(getFileName());
gf.setExpanded(true);
// Copy rules from supergrammar into this grammar
IndexedVector inhRules = superG.getRules();
for (Enumeration e = inhRules.elements(); e.hasMoreElements();) {
Rule r = (Rule)e.nextElement();
inherit(r, superG);
}
// Copy options from supergrammar into this grammar
// Modify tokdef options so that they point to dir of enclosing grammar
IndexedVector inhOptions = superG.getOptions();
if (inhOptions != null) {
for (Enumeration e = inhOptions.elements(); e.hasMoreElements();) {
Option o = (Option)e.nextElement();
inherit(o, superG);
}
}
// add an option to load the superGrammar's output vocab
if ((options != null && options.getElement("importVocab") == null) || options == null) {
// no importVocab found, add one that grabs superG's output vocab
Option inputV = new Option("importVocab", superG.exportVocab + ";", this);
addOption(inputV);
// copy output vocab file to current dir
String originatingGrFileName = superG.getFileName();
String path = antlrTool.pathToFile(originatingGrFileName);
String superExportVocabFileName = path + superG.exportVocab +
persistence.antlr.CodeGenerator.TokenTypesFileSuffix +
persistence.antlr.CodeGenerator.TokenTypesFileExt;
String newImportVocabFileName = antlrTool.fileMinusPath(superExportVocabFileName);
if (path.equals("." + System.getProperty("file.separator"))) {
// don't copy tokdef file onto itself (must be current directory)
// System.out.println("importVocab file same dir; leaving as " + superExportVocabFileName);
}
else {
try {
antlrTool.copyFile(superExportVocabFileName, newImportVocabFileName);
}
catch (IOException io) {
antlrTool.toolError("cannot find/copy importVocab file " + superExportVocabFileName);
return;
}
}
}
// copy member action from supergrammar into this grammar
inherit(superG.memberAction, superG);
|
public java.lang.String | getFileName()
return fileName;
|
public java.lang.String | getName()
return name;
|
public persistence.antlr.collections.impl.IndexedVector | getOptions()
return options;
|
public persistence.antlr.collections.impl.IndexedVector | getRules()
return rules;
|
public persistence.antlr.preprocessor.Grammar | getSuperGrammar()
if (superGrammar == null) return null;
Grammar g = (Grammar)hier.getGrammar(superGrammar);
return g;
|
public java.lang.String | getSuperGrammarName()
return superGrammar;
|
public java.lang.String | getType()
return type;
|
public void | inherit(persistence.antlr.preprocessor.Option o, persistence.antlr.preprocessor.Grammar superG)
// do NOT inherit importVocab/exportVocab options under any circumstances
if (o.getName().equals("importVocab") ||
o.getName().equals("exportVocab")) {
return;
}
Option overriddenOption = null;
if (options != null) { // do we even have options?
overriddenOption = (Option)options.getElement(o.getName());
}
// if overridden, do not add to this grammar
if (overriddenOption == null) { // not overridden
addOption(o); // copy option into this grammar--not overridden
}
|
public void | inherit(persistence.antlr.preprocessor.Rule r, persistence.antlr.preprocessor.Grammar superG)
// if overridden, do not add to this grammar
Rule overriddenRule = (Rule)rules.getElement(r.getName());
if (overriddenRule != null) {
// rule is overridden in this grammar.
if (!overriddenRule.sameSignature(r)) {
// warn if different sig
antlrTool.warning("rule " + getName() + "." + overriddenRule.getName() +
" has different signature than " +
superG.getName() + "." + overriddenRule.getName());
}
}
else { // not overridden, copy rule into this
addRule(r);
}
|
public void | inherit(java.lang.String memberAction, persistence.antlr.preprocessor.Grammar superG)
if (this.memberAction != null) return; // do nothing if already have member action
if (memberAction != null) { // don't have one here, use supergrammar's action
this.memberAction = memberAction;
}
|
public boolean | isPredefined()
return predefined;
|
public void | setFileName(java.lang.String f)
fileName = f;
|
public void | setHierarchy(persistence.antlr.preprocessor.Hierarchy hier)
this.hier = hier;
|
public void | setMemberAction(java.lang.String a)
memberAction = a;
|
public void | setOptions(persistence.antlr.collections.impl.IndexedVector options)
this.options = options;
|
public void | setPreambleAction(java.lang.String a)
preambleAction = a;
|
public void | setPredefined(boolean b)
predefined = b;
|
public void | setTokenSection(java.lang.String tk)
tokenSection = tk;
|
public void | setType(java.lang.String t)
type = t;
|
public java.lang.String | toString()
StringBuffer s = new StringBuffer(10000);
if (preambleAction != null) {
s.append(preambleAction);
}
if (superGrammar == null) {
return "class " + name + ";";
}
if ( superClass!=null ) {
// replace with specified superclass not actual grammar
// user must make sure that the superclass derives from super grammar class
s.append("class " + name + " extends " + superClass + ";");
}
else {
s.append("class " + name + " extends " + type + ";");
}
s.append(
System.getProperty("line.separator") +
System.getProperty("line.separator"));
if (options != null) {
s.append(Hierarchy.optionsToString(options));
}
if (tokenSection != null) {
s.append(tokenSection + "\n");
}
if (memberAction != null) {
s.append(memberAction + System.getProperty("line.separator"));
}
for (int i = 0; i < rules.size(); i++) {
Rule r = (Rule)rules.elementAt(i);
if (!getName().equals(r.enclosingGrammar.getName())) {
s.append("// inherited from grammar " + r.enclosingGrammar.getName() + System.getProperty("line.separator"));
}
s.append(r +
System.getProperty("line.separator") +
System.getProperty("line.separator"));
}
return s.toString();
|