001 /* 002 Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public 015 License along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 017 USA */ 018 019 package org.objectweb.jac.core.parsers.acc; 020 021 import java.io.Reader; 022 import java.util.Collection; 023 import java.util.HashSet; 024 import java.util.Set; 025 import java.util.Vector; 026 import java_cup.runtime.Symbol; 027 import org.apache.log4j.Logger; 028 import org.objectweb.jac.core.parsers.acc.ToolParser; 029 030 public class ToolParserWrapper extends ToolParser 031 { 032 static Logger logger = Logger.getLogger("acc.parser"); 033 034 public ToolParserWrapper() { 035 this.blockKeywords = new HashSet(); 036 } 037 038 public ToolParserWrapper(Set blockKeywords) { 039 this.blockKeywords = blockKeywords; 040 } 041 042 Set blockKeywords; 043 public void addBlockKeywords(Collection keywords) { 044 blockKeywords.addAll(blockKeywords); 045 } 046 public void setBlockKeywords(Set keywords) { 047 blockKeywords.addAll(keywords); 048 } 049 050 /** 051 * Parses some input from a reader. 052 * @param input reader to parse from 053 * @param streamName name of the stream to read from 054 * @return a List of SyntaxElement 055 */ 056 public NonTerminal parse(Reader input, String streamName) 057 { 058 AccScanner lexer = new AccScanner(input,streamName, blockKeywords); 059 setScanner(lexer); 060 // Parse the input expression 061 logger.debug("Parsing "+streamName+" keywords="+blockKeywords+"..."); 062 Vector methods = null; 063 try { 064 syntaxElements = (NonTerminal)parse().value; 065 // System.out.println(methods); 066 logger.debug(streamName+" parsed"); 067 } catch (Exception e) { 068 //lexer.printState(); 069 logger.warn("Parser error in "+streamName+" : "+e); 070 // e.printStackTrace(); 071 } 072 return syntaxElements; 073 } 074 075 NonTerminal syntaxElements; 076 public NonTerminal getSyntaxElements() { 077 return syntaxElements; 078 } 079 080 public void report_error(String message, Object info) { 081 logger.debug(message+" at character "+((Symbol)info).left+ 082 "("+((AccScanner)getScanner()).getLine()+")"); 083 } 084 085 /** 086 * Returns the Terminal at a given position, or null 087 * @param position the position of the requested Terminal 088 */ 089 public Terminal getTerminalAt(int position) { 090 if (syntaxElements!=null) 091 return syntaxElements.getTerminalAt(position); 092 else 093 return null; 094 } 095 096 /** 097 * Gets the "deepest" element at a given position, or null 098 * @param position the position of the requested SyntaxElement 099 */ 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 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 }