break Node out of GSS
[sbp.git] / src / edu / berkeley / sbp / ParseFailed.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
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.*;
9 import java.io.*;
10 import java.util.*;
11
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 {
14
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) {
22         this.region = region;
23         this.location = region.getStart();
24         this.message = message;
25         this.input = input;
26         this.gss = gss;
27     }
28     public Input.Location getLocation() { return location; }
29     private Input.Region getRegion() { return region; }
30     public String toString() {
31         StringBuilder ret = new StringBuilder();
32         ret.append(message);
33         ret.append('\n');
34         return ret.toString();
35     }
36
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');
47     }
48
49     static <Tok> void barf(HashMap<Element,Input.Location> sb, Node n, int indent, boolean skip, int count, Input.Location loc) {
50         if (count <= 0) {
51             barf(sb, n, indent, skip, loc);
52         } else {
53             for(Node nn : (Iterable<Node>)n.parents())
54                 barf(sb, nn, indent, skip, count-1, n.phase().getLocation());
55         }
56     }
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;
59         touched.add(n);
60         String s = "";
61         for(int i=0; i< indent; i++) s += " ";
62         Node parent = n;
63         boolean done = false;
64         boolean alldone = false;
65         boolean go = false;
66         boolean force = false;
67         for(Position p : (Iterable<Position>)parent.state()) {
68             if (skip) p = p.next();
69             int raise = 0;
70             done = false;
71             while(p != null) {
72                 if (p.isLast()) break;
73                 if (important(p)) {
74                     Input.Location l = sb.get(p.element());
75                     if (l == null || l.compareTo(loc) < 0)
76                         sb.put(p.element(), loc);
77                     done = true;
78                     alldone = true;
79                 }
80                 /*
81                else if (p.pos-raise > 0)
82                     barf(sb, n, indent, false, 1);
83                 */
84                 if (!new Walk.Cache().possiblyEpsilon(p.element()))
85                     break;
86                 p = p.next();
87                 raise++;
88                 if (p.isLast()) {
89                     if (!done) barf(sb, n, indent, true, 1, loc);
90                     break;
91                 }
92             }
93         }
94         if (!alldone) barf(sb, n, indent, false, 1, loc);
95     }
96
97
98
99     // FIXME
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;
103         touched.add(n);
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*/ ||
107                 !important(p)) {
108                 for(Node n2 : n.parents())
109                     complain(n2, errors, force /*| p.isFirst()*/, indent);
110             } else {
111                 String seqname = p.owner()/*.name*/+"";
112                 HashSet<String> hs = errors.get(seqname);
113                 if (hs==null) errors.put(seqname, hs = new HashSet<String>());
114                 String s = "";
115                 hs.add(" "+p.element()+"");
116             }
117         }
118     }
119
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);
123         s = s.substring(1);
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));
128             else ret.append(s);
129         }
130         return ANSI.purple(ret.toString());
131     }
132
133     static void error(String message,
134                       Input.Location loc,
135                       Object token,
136                       Iterable<Node> nodes,
137                       Input.Region region,
138                       Input input,
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)));
143         if (token != null) {
144             ret.append(" \'");
145             ret.append(ANSI.cyan(StringUtil.escapify(token+"", "\\\'\r\n")));
146             ret.append("\'");
147         }
148         ret.append(" at ");
149         ret.append(ANSI.yellow(region+""));
150         if (input != null) {
151             ret.append('\n');
152             ret.append("     text: ");
153             int budget = 60;
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));
166             /*
167             ret.append('\n');
168             ret.append("           ");
169             for(int i=0; i<first.length(); i++) ret.append(' ');
170             for(int i=0; i<second.length(); i++) ret.append(ANSI.red("^"));
171             */
172         }
173         HashMap<Element,Input.Location> hm = new HashMap<Element,Input.Location>();
174         for(Node no : nodes)
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());
180         } else {
181             int i=0;
182             for(Element s : hs) {
183                 Input.Location loc2 = hm.get(s);
184                 if (i==0) {
185                     ret.append("" + ANSI.purple(s));
186                 } else {
187                     ret.append("\n        or " + ANSI.purple(s));
188                 }
189                 Input.Region reg = loc2.createRegion(region.getEnd());
190                 ret.append(" to match \"" + ANSI.cyan(input.showRegion(reg)) + "\" at " + ANSI.yellow(reg));
191                 i++;
192             }
193         }
194         /*
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);
200         }
201         for(String s : errors.keySet()) {
202             ret.append("    while parsing " + ANSI.yellow(s));
203             HashSet<String> hs = errors.get(s);
204
205             if (hs.size()==1) ret.append("\n      expected " + ANSI.yellow(el(hs.iterator().next())) + "\n\n");
206             else {
207                 ret.append("\n      expected ");
208                 boolean first = true;
209                 for(String s2 : hs) {
210                     if (!first) ret.append(" or ");
211                     first = false;
212                     ret.append(ANSI.yellow(el(s2)));
213                 }
214                 ret.append("\n\n");
215             }
216         }
217         */
218         throw new ParseFailed(ret.toString(), region, input, gss);
219     }
220
221 }