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.synthetic) return false;
43 if (u.name==null) return false;
44 if (u.name.length() == 0) return false;
45 char c = u.name.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);
53 for(Node nn : (Iterable<Node>)n.parents())
54 barf(sb, nn, indent, skip, count-1, n.phase().getLocation());
57 static <Tok> void barf(HashMap<Element,Input.Location> sb, Node n, int indent, boolean skip, Input.Location loc) {
58 if (touched.contains(n)) return;
61 for(int i=0; i< indent; i++) s += " ";
64 boolean alldone = false;
66 boolean force = false;
67 for(Position p : (Iterable<Position>)parent.state()) {
68 if (skip) p = p.next();
72 if (p.isLast()) break;
74 Input.Location l = sb.get(p.element());
75 if (l == null || l.compareTo(loc) < 0)
76 sb.put(p.element(), loc);
81 else if (p.pos-raise > 0)
82 barf(sb, n, indent, false, 1);
84 if (!new Walk.Cache().possiblyEpsilon(p.element()))
89 if (!done) barf(sb, n, indent, true, 1, loc);
94 if (!alldone) barf(sb, n, indent, false, 1, loc);
100 private static HashSet<Node> touched = new HashSet<Node>();
101 static <Tok> void complain(Node n, HashMap<String,HashSet<String>> errors, boolean force, int indent) {
102 if (touched.contains(n)) return;
104 for(Position p : (Iterable<Position>)n.state()) {
105 //if (!p.isLast() && !p.next().isLast()) continue;
106 if (((p.isFirst() || p.isLast()) && !force)/* || p.owner().name==null*/ ||
108 for(Node n2 : n.parents())
109 complain(n2, errors, force /*| p.isFirst()*/, indent);
111 String seqname = p.owner()/*.name*/+"";
112 HashSet<String> hs = errors.get(seqname);
113 if (hs==null) errors.put(seqname, hs = new HashSet<String>());
115 hs.add(" "+p.element()+"");
120 static String el(Object e) {
121 String s = e.toString();
122 if (s.length()==0 || s.charAt(0)!='\"' || s.charAt(s.length()-1)!='\"') return ANSI.yellow(s);
124 s = s.substring(0, s.length()-1);
125 StringBuffer ret = new StringBuffer();
126 for(int i=0; i<s.length(); i++) {
127 if (s.charAt(i)=='\\' && i<s.length()-1) ret.append(s.charAt(++i));
130 return ANSI.purple(ret.toString());
133 static void error(String message,
136 Iterable<Node> nodes,
139 GSS gss) throws ParseFailed{
140 String lookAhead = token==null ? "<EOF>" : token.toString();
141 StringBuffer ret = new StringBuffer();
142 ret.append(ANSI.bold(ANSI.red(message)));
145 ret.append(ANSI.cyan(StringUtil.escapify(token+"", "\\\'\r\n")));
149 ret.append(ANSI.yellow(region+""));
152 ret.append(" text: ");
154 String second = input.showRegion(region);
155 budget -= second.length();
156 Input.Location after = region.getEnd();
157 for(int i=0; i<10; i++) after = after.next() == null ? after : after.next();
158 String third = input.showRegion(region.getEnd().createRegion(after));
159 budget -= third.length();
160 Input.Location before = region.getStart();
161 for(int i=0; i<budget; i++) before = before.prev() == null ? before : before.prev();
162 String first = input.showRegion(before.createRegion(region.getStart()));
163 ret.append(ANSI.cyan(first));
164 ret.append(ANSI.invert(ANSI.red(second)));
165 ret.append(ANSI.cyan(third));
169 for(int i=0; i<first.length(); i++) ret.append(' ');
170 for(int i=0; i<second.length(); i++) ret.append(ANSI.red("^"));
173 HashMap<Element,Input.Location> hm = new HashMap<Element,Input.Location>();
175 barf(hm, no, 0, false, region.getStart());
176 ret.append("\n expected: ");
177 Set<Element> hs = hm.keySet();
178 if (hs.size() == 1) {
179 ret.append(hs.iterator().next());
182 for(Element s : hs) {
183 Input.Location loc2 = hm.get(s);
185 ret.append("" + ANSI.purple(s));
187 ret.append("\n or " + ANSI.purple(s));
189 Input.Region reg = loc2.createRegion(region.getEnd());
190 ret.append(" to match \"" + ANSI.cyan(input.showRegion(reg)) + "\" at " + ANSI.yellow(reg));
195 ret.append("\n The author of SBP apologizes for the these nearly-useless error messages:\n\n");
196 HashMap<String,HashSet<String>> errors = new HashMap<String,HashSet<String>>();
197 for(Node n : nodes) {
198 //System.out.println(n.state);
199 complain(n, errors, false, 0);
201 for(String s : errors.keySet()) {
202 ret.append(" while parsing " + ANSI.yellow(s));
203 HashSet<String> hs = errors.get(s);
205 if (hs.size()==1) ret.append("\n expected " + ANSI.yellow(el(hs.iterator().next())) + "\n\n");
207 ret.append("\n expected ");
208 boolean first = true;
209 for(String s2 : hs) {
210 if (!first) ret.append(" or ");
212 ret.append(ANSI.yellow(el(s2)));
218 throw new ParseFailed(ret.toString(), region, input, gss);