FileDocCategorySizeDatePackage
Grammar.javaAPI DocGlassfish v2 API9862Wed Aug 30 15:34:16 BST 2006persistence.antlr.preprocessor

Grammar

public class Grammar extends Object

Fields Summary
protected String
name
protected String
fileName
protected String
superGrammar
protected String
type
protected IndexedVector
rules
protected IndexedVector
options
protected String
tokenSection
protected String
preambleAction
protected String
memberAction
protected Hierarchy
hier
protected boolean
predefined
protected boolean
alreadyExpanded
protected boolean
specifiedVocabulary
protected String
superClass
if not derived from another grammar, might still specify a non-ANTLR class to derive from like this "class T extends Parser(MyParserClass);"
protected String
importVocab
protected String
exportVocab
protected Tool
antlrTool
Constructors Summary
public Grammar(Tool tool, String name, String superGrammar, IndexedVector rules)


             
        this.name = name;
        this.superGrammar = superGrammar;
        this.rules = rules;
        this.antlrTool = tool;
    
Methods Summary
public voidaddOption(persistence.antlr.preprocessor.Option o)

        if (options == null) {	// if not already there, create it
            options = new IndexedVector();
        }
        options.appendElement(o.getName(), o);
    
public voidaddRule(persistence.antlr.preprocessor.Rule r)

        rules.appendElement(r.getName(), r);
    
public voidexpandInPlace()
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.StringgetFileName()

        return fileName;
    
public java.lang.StringgetName()

        return name;
    
public persistence.antlr.collections.impl.IndexedVectorgetOptions()

        return options;
    
public persistence.antlr.collections.impl.IndexedVectorgetRules()

        return rules;
    
public persistence.antlr.preprocessor.GrammargetSuperGrammar()

        if (superGrammar == null) return null;
        Grammar g = (Grammar)hier.getGrammar(superGrammar);
        return g;
    
public java.lang.StringgetSuperGrammarName()

        return superGrammar;
    
public java.lang.StringgetType()

        return type;
    
public voidinherit(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 voidinherit(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 voidinherit(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 booleanisPredefined()

        return predefined;
    
public voidsetFileName(java.lang.String f)

        fileName = f;
    
public voidsetHierarchy(persistence.antlr.preprocessor.Hierarchy hier)

        this.hier = hier;
    
public voidsetMemberAction(java.lang.String a)

        memberAction = a;
    
public voidsetOptions(persistence.antlr.collections.impl.IndexedVector options)

        this.options = options;
    
public voidsetPreambleAction(java.lang.String a)

        preambleAction = a;
    
public voidsetPredefined(boolean b)

        predefined = b;
    
public voidsetTokenSection(java.lang.String tk)

        tokenSection = tk;
    
public voidsetType(java.lang.String t)

        type = t;
    
public java.lang.StringtoString()

        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();