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

Hierarchy

public class Hierarchy extends Object

Fields Summary
protected Grammar
LexerRoot
protected Grammar
ParserRoot
protected Grammar
TreeParserRoot
protected Hashtable
symbols
protected Hashtable
files
protected Tool
antlrTool
Constructors Summary
public Hierarchy(Tool tool)


       
        this.antlrTool = tool;
        LexerRoot = new Grammar(tool, "Lexer", null, null);
        ParserRoot = new Grammar(tool, "Parser", null, null);
        TreeParserRoot = new Grammar(tool, "TreeParser", null, null);
        symbols = new Hashtable(10);
        files = new Hashtable(10);

        LexerRoot.setPredefined(true);
        ParserRoot.setPredefined(true);
        TreeParserRoot.setPredefined(true);

        symbols.put(LexerRoot.getName(), LexerRoot);
        symbols.put(ParserRoot.getName(), ParserRoot);
        symbols.put(TreeParserRoot.getName(), TreeParserRoot);
    
Methods Summary
public voidaddGrammar(persistence.antlr.preprocessor.Grammar gr)

        gr.setHierarchy(this);
        // add grammar to hierarchy
        symbols.put(gr.getName(), gr);
        // add grammar to file.
        GrammarFile f = getFile(gr.getFileName());
        f.addGrammar(gr);
    
public voidaddGrammarFile(persistence.antlr.preprocessor.GrammarFile gf)

        files.put(gf.getName(), gf);
    
public voidexpandGrammarsInFile(java.lang.String fileName)

        GrammarFile f = getFile(fileName);
        for (Enumeration e = f.getGrammars().elements(); e.hasMoreElements();) {
            Grammar g = (Grammar)e.nextElement();
            g.expandInPlace();
        }
    
public persistence.antlr.preprocessor.GrammarfindRoot(persistence.antlr.preprocessor.Grammar g)

        if (g.getSuperGrammarName() == null) {		// at root
            return g;
        }
        // return root of super.
        Grammar sg = g.getSuperGrammar();
        if (sg == null) return g;		// return this grammar if super missing
        return findRoot(sg);
    
public persistence.antlr.preprocessor.GrammarFilegetFile(java.lang.String fileName)

        return (GrammarFile)files.get(fileName);
    
public persistence.antlr.preprocessor.GrammargetGrammar(java.lang.String gr)

        return (Grammar)symbols.get(gr);
    
public persistence.antlr.ToolgetTool()

        return antlrTool;
    
public static java.lang.StringoptionsToString(persistence.antlr.collections.impl.IndexedVector options)

        String s = "options {" + System.getProperty("line.separator");
        for (Enumeration e = options.elements(); e.hasMoreElements();) {
            s += (Option)e.nextElement() + System.getProperty("line.separator");
        }
        s += "}" +
            System.getProperty("line.separator") +
            System.getProperty("line.separator");
        return s;
    
public voidreadGrammarFile(java.lang.String file)

        Reader grStream = new BufferedReader(new FileReader(file));
        addGrammarFile(new GrammarFile(antlrTool, file));

        // Create the simplified grammar lexer/parser
        PreprocessorLexer ppLexer = new PreprocessorLexer(grStream);
        ppLexer.setFilename(file);
        Preprocessor pp = new Preprocessor(ppLexer);
		pp.setTool(antlrTool);
        pp.setFilename(file);

        // populate the hierarchy with class(es) read in
        try {
            pp.grammarFile(this, file);
        }
        catch (TokenStreamException io) {
            antlrTool.toolError("Token stream error reading grammar(s):\n" + io);
        }
        catch (ANTLRException se) {
            antlrTool.toolError("error reading grammar(s):\n" + se);
        }
    
public voidsetTool(persistence.antlr.Tool antlrTool)

        this.antlrTool = antlrTool;
    
public booleanverifyThatHierarchyIsComplete()
Return true if hierarchy is complete, false if not

        boolean complete = true;
        // Make a pass to ensure all grammars are defined
        for (Enumeration e = symbols.elements(); e.hasMoreElements();) {
            Grammar c = (Grammar)e.nextElement();
            if (c.getSuperGrammarName() == null) {
                continue;		// at root: ignore predefined roots
            }
            Grammar superG = c.getSuperGrammar();
            if (superG == null) {
                antlrTool.toolError("grammar " + c.getSuperGrammarName() + " not defined");
                complete = false;
                symbols.remove(c.getName()); // super not defined, kill sub
            }
        }

        if (!complete) return false;

        // Make another pass to set the 'type' field of each grammar
        // This makes it easy later to ask a grammar what its type
        // is w/o having to search hierarchy.
        for (Enumeration e = symbols.elements(); e.hasMoreElements();) {
            Grammar c = (Grammar)e.nextElement();
            if (c.getSuperGrammarName() == null) {
                continue;		// ignore predefined roots
            }
            c.setType(findRoot(c).getName());
        }

        return true;