KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > parsers > acc > ToolParserWrapper


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core.parsers.acc;
20
21 import java.io.Reader JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java_cup.runtime.Symbol;
27 import org.apache.log4j.Logger;
28 import org.objectweb.jac.core.parsers.acc.ToolParser;
29
30 public class ToolParserWrapper extends ToolParser
31 {
32     static Logger logger = Logger.getLogger("acc.parser");
33
34     public ToolParserWrapper() {
35         this.blockKeywords = new HashSet JavaDoc();
36     }
37
38     public ToolParserWrapper(Set JavaDoc blockKeywords) {
39         this.blockKeywords = blockKeywords;
40     }
41
42     Set JavaDoc blockKeywords;
43     public void addBlockKeywords(Collection JavaDoc keywords) {
44         blockKeywords.addAll(blockKeywords);
45     }
46     public void setBlockKeywords(Set JavaDoc keywords) {
47         blockKeywords.addAll(keywords);
48     }
49
50     /**
51      * Parses some input from a reader.
52      * @param input reader to parse from
53      * @param streamName name of the stream to read from
54      * @return a List of SyntaxElement
55      */

56     public NonTerminal parse(Reader JavaDoc input, String JavaDoc streamName)
57     {
58         AccScanner lexer = new AccScanner(input,streamName, blockKeywords);
59         setScanner(lexer);
60         // Parse the input expression
61
logger.debug("Parsing "+streamName+" keywords="+blockKeywords+"...");
62         Vector JavaDoc methods = null;
63         try {
64             syntaxElements = (NonTerminal)parse().value;
65             // System.out.println(methods);
66
logger.debug(streamName+" parsed");
67         } catch (Exception JavaDoc e) {
68             //lexer.printState();
69
logger.warn("Parser error in "+streamName+" : "+e);
70             // e.printStackTrace();
71
}
72         return syntaxElements;
73     }
74
75     NonTerminal syntaxElements;
76     public NonTerminal getSyntaxElements() {
77         return syntaxElements;
78     }
79
80     public void report_error(String JavaDoc message, Object JavaDoc info) {
81         logger.debug(message+" at character "+((Symbol)info).left+
82                      "("+((AccScanner)getScanner()).getLine()+")");
83     }
84
85     /**
86      * Returns the Terminal at a given position, or null
87      * @param position the position of the requested Terminal
88      */

89     public Terminal getTerminalAt(int position) {
90         if (syntaxElements!=null)
91             return syntaxElements.getTerminalAt(position);
92         else
93             return null;
94     }
95
96     /**
97      * Gets the "deepest" element at a given position, or null
98      * @param position the position of the requested SyntaxElement
99      */

100     public SyntaxElement getSyntaxElementAt(int position) {
101         if (syntaxElements!=null)
102             return syntaxElements.getSyntaxElementAt(position);
103         else
104             return null;
105     }
106
107     /**
108      * Gets the "deepest" element at a given position with a given
109      * name, or null
110      * @param position the position of the requested SyntaxElement
111      * @param name searched name
112      */

113     public SyntaxElement getSyntaxElementAt(int position, String JavaDoc name) {
114         if (syntaxElements!=null) {
115             SyntaxElement element = syntaxElements.getSyntaxElementAt(position);
116             if (element!=null)
117                 return element.findParent(name);
118             else
119                 return null;
120         } else
121             return null;
122     }
123 }
124
Popular Tags