1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.Sequence.Position;
6 import edu.berkeley.sbp.GSS.Phase;
7 import edu.berkeley.sbp.Node;
8 import edu.berkeley.sbp.util.*;
12 /** thrown when the parser arrives at a state from which it is clear that no valid parse can result */
13 public class ParseFailed extends Exception {
15 private final Input.Location location;
16 private final Input.Region region;
17 private final Input input;
18 private final String message;
19 private final GSS gss;
20 ParseFailed() { this("", null, null, null); }
21 ParseFailed(String message, Input.Region region, Input input, GSS gss) {
23 this.location = region.getStart();
24 this.message = message;
28 public Input.Location getLocation() { return location; }
29 private Input.Region getRegion() { return region; }
30 public String toString() {
31 StringBuilder ret = new StringBuilder();
34 return ret.toString();
37 private static boolean important(Position p) {
38 if (p.isLast()) return false;
39 if (p.element() == null) return false;
40 if (!(p.element() instanceof Union)) return false;
41 Union u = (Union)p.element();
42 if (u.isSynthetic()) return false;
43 if (u.getName()==null) return false;
44 if (u.getName().length() == 0) return false;
45 char c = u.getName().charAt(0);
46 return (c >= 'A' && c <= 'Z');
49 static <Tok> void barf(HashMap<Element,Input.Location> sb, Node n, int indent, boolean skip, int count, Input.Location loc) {
51 barf(sb, n, indent, skip, loc);
55 for(Node nn : (Iterable<Node>)n.parents())
56 barf(sb, nn, indent, skip, count-1, n.phase().getLocation());
60 static <Tok> void barf(HashMap<Element,Input.Location> sb, Node n, int indent, boolean skip, Input.Location loc) {
61 if (touched.contains(n)) return;
64 for(int i=0; i< indent; i++) s += " ";
67 boolean alldone = false;
69 boolean force = false;
70 for(Position p : (Iterable<Position>)parent.state()) {
71 if (skip) p = p.next();
75 if (p.isLast()) break;
77 Input.Location l = sb.get(p.element());
78 if (l == null || l.compareTo(loc) < 0)
79 sb.put(p.element(), loc);
84 else if (p.pos-raise > 0)
85 barf(sb, n, indent, false, 1);
86 if (!new Grammar(null, null).possiblyEpsilon(p.element()))
92 if (!done) barf(sb, n, indent, true, 1, loc);
97 if (!alldone) barf(sb, n, indent, false, 1, loc);
103 private static HashSet<Node> touched = new HashSet<Node>();
104 static <Tok> void complain(Node n, HashMap<String,HashSet<String>> errors, boolean force, int indent) {
105 if (touched.contains(n)) return;
107 for(Position p : (Iterable<Position>)n.state()) {
108 //if (!p.isLast() && !p.next().isLast()) continue;
109 if (((p.isFirst() || p.isLast()) && !force)/* || p.owner().name==null*/ ||
113 for(Node n2 : n.parents())
114 complain(n2, errors, force
119 String seqname = p.owner()/*.name*/+"";
120 HashSet<String> hs = errors.get(seqname);
121 if (hs==null) errors.put(seqname, hs = new HashSet<String>());
123 hs.add(" "+p.element()+"");
128 static String el(Object e) {
129 String s = e.toString();
130 if (s.length()==0 || s.charAt(0)!='\"' || s.charAt(s.length()-1)!='\"') return ANSI.yellow(s);
132 s = s.substring(0, s.length()-1);
133 StringBuffer ret = new StringBuffer();
134 for(int i=0; i<s.length(); i++) {
135 if (s.charAt(i)=='\\' && i<s.length()-1) ret.append(s.charAt(++i));
138 return ANSI.purple(ret.toString());
141 static void error(String message, GSS.Phase phase, Object token, Input.Region region) throws ParseFailed {
146 phase.getGSS().getInput(),
149 private static void error(String message,
151 Iterable<Node> nodes,
154 GSS gss) throws ParseFailed{
155 String lookAhead = token==null ? "<EOF>" : token.toString();
156 StringBuffer ret = new StringBuffer();
157 ret.append(ANSI.bold(ANSI.red(message)));
158 String toks = token+"";
160 ret.append(ANSI.yellow(region+""));
163 ret.append(" text: ");
165 String second = input.showRegion(region, 60);
166 budget -= second.length();
167 Input.Location after = region.getEnd();
168 for(int i=0; i<10; i++) after = after.next() == null ? after : after.next();
169 String third = input.showRegion(region.getEnd().createRegion(after), 60);
170 budget -= third.length();
171 Input.Location before = region.getStart();
172 for(int i=0; i<budget; i++) before = before.prev() == null ? before : before.prev();
173 String first = input.showRegion(before.createRegion(region.getStart()), 60);
174 ret.append(ANSI.cyan(first));
175 ret.append(ANSI.invert(ANSI.red(second)));
176 ret.append(ANSI.cyan(third));
180 for(int i=0; i<first.length(); i++) ret.append(' ');
181 for(int i=0; i<second.length(); i++) ret.append(ANSI.red("^"));
184 HashMap<Element,Input.Location> hm = new HashMap<Element,Input.Location>();
186 barf(hm, no, 0, false, region.getStart());
187 ret.append("\n expected: ");
188 Set<Element> hs = hm.keySet();
189 if (hs.size() == 1) {
190 ret.append(hs.iterator().next());
193 for(Element s : hs) {
194 Input.Location loc2 = hm.get(s);
196 ret.append("" + ANSI.purple(s));
198 ret.append("\n or " + ANSI.purple(s));
200 Input.Region reg = loc2.createRegion(region.getEnd());
201 ret.append(" to match \"" + ANSI.cyan(input.showRegion(reg, 60)) + "\" at " + ANSI.yellow(reg));
206 ret.append("\n The author of SBP apologizes for the these nearly-useless error messages:\n\n");
207 HashMap<String,HashSet<String>> errors = new HashMap<String,HashSet<String>>();
208 for(Node n : nodes) {
209 //System.out.println(n.state);
210 complain(n, errors, false, 0);
212 for(String s : errors.keySet()) {
213 ret.append(" while parsing " + ANSI.yellow(s));
214 HashSet<String> hs = errors.get(s);
216 if (hs.size()==1) ret.append("\n expected " + ANSI.yellow(el(hs.iterator().next())) + "\n\n");
218 ret.append("\n expected ");
219 boolean first = true;
220 for(String s2 : hs) {
221 if (!first) ret.append(" or ");
223 ret.append(ANSI.yellow(el(s2)));
229 throw new ParseFailed(ret.toString(), region, input, gss);