/* abc - The AspectBench Compiler * Copyright (C) 2010 Eric Bodden * * This compiler is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This compiler is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this compiler, in the file LESSER-GPL; * if not, write to the Free Software Foundation, Inc., * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ %header {: package abc.ja.cjp.parse; import abc.ja.cjp.jrag.*; :}; %embed {: class Events extends Parser.Events { public void reportError(Symbol token, String msg) { polyglot.util.Position pos; if (token.getId() == Terminals.EOF) { pos = new polyglot.util.Position(fileName, token.getLine(token.getStart()) - 1); msg = "Unexpected end of file."; } else pos = new polyglot.util.Position(fileName, token.getLine(token.getStart()), token.getColumn(token.getStart())); abc.main.Main.v().getAbcExtension().reportError( polyglot.util.ErrorInfo.SYNTAX_ERROR, msg, pos ); } public void syntaxError(Symbol token) { reportError(token, "Unexpected token " + Terminals.NAMES[token.getId()]); } public void scannerError(Scanner.Exception e) { abc.main.Main.v().getAbcExtension().reportError( polyglot.util.ErrorInfo.LEXICAL_ERROR, e.getMessage(), new polyglot.util.Position(fileName, e.line, e.column) ); } public void unexpectedTokenRemoved(Symbol token) { //reportError(token, "Removed unexpected token " + Terminals.NAMES[token.getId()]); } public void missingTokenInserted(Symbol token) { //reportError(token, "Inserted missing token"); } public void misspelledTokenReplaced(Symbol token) { /* StringBuffer s = new StringBuffer(); s.append(" *** Syntactic error: replaced unexpected token with " ); if (token.value != null) { s.append("\"" + token.value + "\""); } else { s.append(Terminals.NAMES[token.getId()]); } reportError(token, s.toString()); */ } public void errorPhraseRemoved(Symbol token) { //reportError(token, " *** Syntactic error: removed error phrase"); } } { report = new Events(); // Use error handler in parser } String fileName; protected java.util.Collection errors = new ArrayList(); public CompilationUnit parse(java.io.InputStream is, String fileName, polyglot.util.ErrorQueue error_queue) throws java.io.IOException, beaver.Parser.Exception { CompilationUnit cu; errors = new ArrayList(); try { this.fileName = fileName; abc.ja.parse.JavaScanner scanner = new abc.ja.parse.JavaScanner(new abc.ja.parse.Unicode(is), fileName, error_queue); cu = (CompilationUnit)parse(scanner); } catch(Parser.Exception e) { // build empty compilation unit for failed error recovery cu = new CompilationUnit(); } for(java.util.Iterator iter = errors.iterator(); iter.hasNext(); ) { Problem p = (Problem)iter.next(); p.setFileName(fileName); cu.addParseError(p); } return cu; } :};