619bf65f88ae2465a6b3e9ce49c71a73c04d2324
[sbp.git] / src / edu / berkeley / sbp / ParseFailed.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
4 import edu.berkeley.sbp.GSS.Phase;
5 import edu.berkeley.sbp.GSS.Phase.Node;
6 import edu.berkeley.sbp.util.*;
7 import java.io.*;
8 import java.util.*;
9
10 /** thrown when the parser arrives at a state from which it is clear that no valid parse can result */
11 public class ParseFailed extends Exception {
12
13     private final Input.Location location;
14     private final String message;
15     public ParseFailed() { this("", null); }
16     public ParseFailed(String message, Input.Location loc) { this.location = loc; this.message = message; }
17     public Input.Location getLocation() { return location; }
18     public String toString() { return message/* + (location==null ? "" : (" at " + location))*/; }
19
20     // FIXME
21     private static HashSet<GSS.Phase.Node> touched = new HashSet<GSS.Phase.Node>();
22     public static <Tok> void complain(GSS.Phase<Tok>.Node n, HashMap<String,HashSet<String>> errors, boolean force) {
23         if (touched.contains(n)) return;
24         touched.add(n);
25         for(Position p : n.state) {
26             if (((p.isFirst() || p.isLast()) && !force)/* || p.owner().name==null*/) {
27                 for(Node n2 : n.parents())
28                     complain(n2, errors, force | p.isFirst());
29             } else {
30                 String seqname = p.owner()/*.name*/+"";
31                 HashSet<String> hs = errors.get(seqname);
32                 if (hs==null) errors.put(seqname, hs = new HashSet<String>());
33                 hs.add(p.element()+"");
34             }
35         }
36     }
37
38     public static String el(Object e) {
39         String s = e.toString();
40         if (s.length()==0 || s.charAt(0)!='\"' || s.charAt(s.length()-1)!='\"') return ANSI.yellow(s);
41         s = s.substring(1);
42         s = s.substring(0, s.length()-1);
43         StringBuffer ret = new StringBuffer();
44         for(int i=0; i<s.length(); i++) {
45             if (s.charAt(i)=='\\' && i<s.length()-1) ret.append(s.charAt(++i));
46             else ret.append(s);
47         }
48         return ANSI.purple(ret.toString());
49     }
50     public static String error(String message, Object token, Iterable<Node> nodes) {
51         String lookAhead = token==null ? "<EOF>" : token.toString();
52         StringBuffer ret = new StringBuffer();
53         ret.append("\n  ");
54         ret.append(message);
55         HashMap<String,HashSet<String>> errors = new HashMap<String,HashSet<String>>();
56         for(Node n : nodes) {
57             //System.out.println(n.state);
58             complain(n, errors, false);
59         }
60         for(String s : errors.keySet()) {
61             ret.append("    while parsing " + ANSI.yellow(s));
62             HashSet<String> hs = errors.get(s);
63             if (hs.size()==1) ret.append(" expected " + ANSI.yellow(el(hs.iterator().next())) + "\n");
64             else {
65                 ret.append(" expected ");
66                 boolean first = true;
67                 for(String s2 : hs) {
68                     if (!first) ret.append(" or ");
69                     first = false;
70                     ret.append(ANSI.yellow(el(s2)));
71                 }
72                 ret.append("\n");
73             }
74         }
75         return ret.toString();
76     }
77
78 }