From: <iam...@us...> - 2010-03-15 18:36:30
|
Revision: 1319 http://eulergui.svn.sourceforge.net/eulergui/?rev=1319&view=rev Author: iamlolive Date: 2010-03-15 18:36:23 +0000 (Mon, 15 Mar 2010) Log Message: ----------- our own version of ParseResult (from parser4j). it will work alongside the sealed=false version of parser4j.jar, that we have just put on our maven repository. Added Paths: ----------- trunk/eulergui/src/main/java/net/sf/ trunk/eulergui/src/main/java/net/sf/parser4j/ trunk/eulergui/src/main/java/net/sf/parser4j/parser/ trunk/eulergui/src/main/java/net/sf/parser4j/parser/entity/ trunk/eulergui/src/main/java/net/sf/parser4j/parser/entity/ParseResult.java Added: trunk/eulergui/src/main/java/net/sf/parser4j/parser/entity/ParseResult.java =================================================================== --- trunk/eulergui/src/main/java/net/sf/parser4j/parser/entity/ParseResult.java (rev 0) +++ trunk/eulergui/src/main/java/net/sf/parser4j/parser/entity/ParseResult.java 2010-03-15 18:36:23 UTC (rev 1319) @@ -0,0 +1,412 @@ +/* + * Copyright 2008 Luc Peuvrier + * + * This file is part of parser4j. + * + * parser4j 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 3 of the License, or + * (at your option) any later version. + * + * parser4j 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 General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with parser4j. If not, see <http://www.gnu.org/licenses/>. + */ +package net.sf.parser4j.parser.entity; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import net.sf.parser4j.parser.entity.data.NonTerminal; +import net.sf.parser4j.parser.entity.data.TerminalCharRange; +import net.sf.parser4j.parser.entity.parsenode.IParseNode; +import net.sf.parser4j.parser.entity.parsenode.data.IParseNodeData; +import net.sf.parser4j.parser.entity.parsenode.status.IParseNodeInErrorStatus; +import net.sf.parser4j.parser.entity.parsestate.ParseStack; +import net.sf.parser4j.parser.service.ParserException; +import net.sf.parser4j.parser.service.ParsingToStringUtil; + +/** + * parse result + * + * @author luc peuvrier + * + */ +public class ParseResult { + + private final String grammarFileUrl; // NOPMD + + /** true if syntax parsing error */ + private boolean parseError; + + /** file name where is syntax error */ + private String fileName; // NOPMD + + /** line number in input text file for syntax error */ + private int lineNumber = -1;// NOPMD + + /** column number in input text file for syntax error */ + private int columnNumber = -1;// NOPMD + + /** last terminal value read */ + private int lastTerminalValueRead = -1;// NOPMD + + /** expected terminal not found in input text file for syntax error */ + private final Set<TerminalCharRange> expectedTerminal = // NOPMD + /**/new TreeSet<TerminalCharRange>(); + + /** expected non terminal not found in input text file for syntax error */ + private final Set<String> expectedNonTerminal = // NOPMD + /**/new TreeSet<String>(); + + /** the error status for error cause of reduce error */ + private List<IParseNodeInErrorStatus> errorStatusList;// NOPMD + + /** line number in input text file for reduce error */ + private int reduceErrorLineNumber;// NOPMD + + /** column number in input text file for reduce error */ + private int reduceErrorColumnNumber;// NOPMD + + /** the resulting parse tree root node if no syntax error */ + private IParseNode resultParseNode; + + private ParseStack[] parseStacks; + + private ParseStack[] tokenParseStacks;// NOPMD + + private boolean[] tokenRecognition;// NOPMD + + private boolean[] stringTokenRecognition;// NOPMD + + private final Map<Integer, NonTerminal> nonTerminalByIdentifierMap;// NOPMD + + private boolean preserveParseNode; + + private final ParsingToStringUtil parsingToStringUtil;// NOPMD + + /** + * set error state at creation + * + * @param grammarFileUrl + * @param nonTerminalByIdentifierMap + */ + public ParseResult(final String grammarFileUrl, + final Map<Integer, NonTerminal> nonTerminalByIdentifierMap) { + this.grammarFileUrl = grammarFileUrl; + this.nonTerminalByIdentifierMap = nonTerminalByIdentifierMap; + parsingToStringUtil = new ParsingToStringUtil( + nonTerminalByIdentifierMap); + } + + public String getGrammarFileUrl() { + return grammarFileUrl; + } + + /** + * set the resulting parse tree root node + * + * @param resultParseNode + */ + public void setResultParseNode(final IParseNode resultParseNode) { + this.resultParseNode = resultParseNode; + } + + /** + * @param lastTerminalValueRead + * the last terminal value read to set + */ + public void setlastTerminalValueRead(final int lastTerminalValueRead) { + this.lastTerminalValueRead = lastTerminalValueRead; + } + + /** + * set line number and column number in input text file for syntax error + * + * @param fileName + * @param lineNumber + * line in file where is syntax error + * @param columnNumber + * column in file where is syntax error + * @param fileName + * file name where is syntax error + */ + public void setInFileInformation(final String fileName, + final int lineNumber, final int columnNumber) { + this.fileName = fileName; + this.lineNumber = lineNumber; + this.columnNumber = columnNumber; + } + + /** + * set syntax error state + * + * @param parseError + * true if syntax parsing error + */ + public void setParseError(final boolean parseError) { + this.parseError = parseError; + } + + /** + * set the reduce error informations + * + * @param errorStatusList + * the error status for error cause of reduce error + */ + public void setReduceErrorInfo( + final List<IParseNodeInErrorStatus> errorStatusList) { + this.errorStatusList = errorStatusList; + } + + /** + * + * @return true if error during parsing + */ + public boolean isInError() { + return parseError || isReduceError(); + } + + /** + * + * @return the resulting parse tree root node if no syntax error + */ + public IParseNode getResultParseNode() { + return resultParseNode; + } + + public IParseNodeData getUniqData() throws ParserException { + if (!resultParseNode.hasDataComputed()) { + throw new ParserException("node data not computed"); + } + return resultParseNode.getUniqData(); + } + + public IParseNodeData[] getDataByAlternative() throws ParserException { + if (!resultParseNode.hasDataComputed()) { + throw new ParserException("node data not computed"); + } + return resultParseNode.getDataByAlternative(); + } + + /** + * + * @return true if syntax parsing error + */ + public boolean isParseError() { + return parseError; + } + + /** + * + * @return true if reduce error while parsing + */ + public boolean isReduceError() { + return errorStatusList != null && !errorStatusList.isEmpty(); + } + + /** + * + * @return file name where is syntax error + */ + public String getFileName() { + return fileName; + } + + /** + * + * @return column number in input text file for syntax error + */ + public int getColumnNumber() { + return columnNumber; + } + + /** + * + * @return line number in input text file for syntax error + */ + public int getLineNumber() { + return lineNumber; + } + + /** + * + * @return expected non terminal not found in input text file for syntax + * error + */ + public Set<String> getExpectedNonTerminal() { + return expectedNonTerminal; + } + + /** + * + * @return expected terminal not found in input text file for syntax error + */ + public Set<TerminalCharRange> getExpectedTerminal() { + return expectedTerminal; + } + + /** + * @return the error status list + */ + public List<IParseNodeInErrorStatus> getErrorStatusList() { + return errorStatusList; + } + + /** + * + * @return line number in input text file for reduce error + */ + public int getReduceErrorLineNumber() { + return reduceErrorLineNumber; + } + + /** + * + * @return column number in input text file for reduce error + */ + public int getReduceErrorColumnNumber() { + return reduceErrorColumnNumber; + } + + @SuppressWarnings("PMD") + public void setParseStacks(final ParseStack[] parseStacks, + final ParseStack[] tokenParseStacks, + final boolean[] tokenRecognition, + final boolean[] stringTokenRecognition) { + this.parseStacks = parseStacks; + this.tokenParseStacks = tokenParseStacks; + this.tokenRecognition = tokenRecognition; + this.stringTokenRecognition = stringTokenRecognition; + } + + /** + * @return the parseStacks + */ + public ParseStack[] getParseStacks() { + return parseStacks;// NOPMD + } + + /** + * @return the tokenParseStacks + */ + public ParseStack[] getTokenParseStacks() { + return tokenParseStacks;// NOPMD + } + + /** + * @return the tokenRecognition + */ + public boolean[] getTokenRecognition() { + return tokenRecognition;// NOPMD + } + + public boolean[] getStringTokenRecognition() { + return stringTokenRecognition;// NOPMD + } + + public Map<Integer, NonTerminal> getNonTerminalByIdentifierMap() { + return nonTerminalByIdentifierMap; + } + + public void setPreserveParseNode(final boolean preserveParseNode) { + this.preserveParseNode = preserveParseNode; + } + + public boolean isPreserveParseNode() { + return preserveParseNode; + } + + @Override + public String toString() { + final StringBuilder stringBuilder = new StringBuilder(); + if (isInError()) { + stringBuilder.append("has parsing error\n"); + } else { + stringBuilder.append("does not have parsing error\n"); + } + if (preserveParseNode) { + stringBuilder.append("parse node preserved\n"); + } else { + stringBuilder.append("parse node not preserved\n"); + } + if (parseError) { + stringBuilder.append("parse failed: file "); + stringBuilder.append(fileName); + stringBuilder.append(", line "); + stringBuilder.append(lineNumber); + stringBuilder.append(" column "); + stringBuilder.append(columnNumber); + stringBuilder.append(", last character read "); + if (lastTerminalValueRead == -1) { + stringBuilder.append("EOF"); + } else if (lastTerminalValueRead < ' ' + || lastTerminalValueRead >= 0x7f) { + stringBuilder.append(String.format("0X%2X", + new Object[] { lastTerminalValueRead })); + } else { + stringBuilder.append('"'); + stringBuilder.append((char) lastTerminalValueRead); + stringBuilder.append('"'); + } + stringBuilder.append("\nexpected terminal:\n"); + for (TerminalCharRange terminalCharRange : expectedTerminal) { + stringBuilder.append('\t'); + stringBuilder.append(terminalCharRange.toString()); + stringBuilder.append('\n'); + } + stringBuilder.append("expected non terminal:\n"); + for (String nonTerminal : expectedNonTerminal) { + stringBuilder.append('\t'); + stringBuilder.append(nonTerminal); + stringBuilder.append('\n'); + } + } else { + stringBuilder.append("parse succeed\n"); + } + if (errorStatusList == null) { + stringBuilder.append("no reduce error\n"); + } else { + stringBuilder.append("reduce error: "); + for (IParseNodeInErrorStatus status : errorStatusList) { + stringBuilder.append(status.toString()); + stringBuilder.append('\n'); + } + } + + if (parseStacks != null) { + stringBuilder.append("syntax parse stacks\n"); + stringBuilder.append(parsingToStringUtil.parseStacksToString( + parseStacks, tokenRecognition, stringTokenRecognition, + false/* + * true/ addParseTree + */)); + } + + if (tokenParseStacks != null) { + stringBuilder.append("lexical parse stacks\n"); + stringBuilder.append(parsingToStringUtil.parseStacksToString( + tokenParseStacks, tokenRecognition, stringTokenRecognition, + false/* + * true/ addParseTree + */)); + } + return stringBuilder.toString(); + } + + + + public int getLastTerminalValueRead() { + return lastTerminalValueRead; + } + + public ParsingToStringUtil getParsingToStringUtil() { + return parsingToStringUtil; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |