1 8 13 package jfun.parsec; 14 15 20 final class ParsecError extends AbstractParsecError implements ParseError{ 21 ParsecError render(){return this;} 23 private final Object sys_unexpected; 24 private final String [] unexpected; 25 private final String [] expecting; 26 private final String [] raw; 27 static int getPrecedenceForExpecting(String s){ 28 return s!=null?2:1; 29 } 30 35 private ParsecError(final boolean nm, final int at, 36 Object sys, String [] unexpected, String [] expecting, String [] raw, 37 Object exception) { 38 super(nm, at, (expecting != null || unexpected != null || raw != null)? 39 2:1, exception); 40 this.sys_unexpected = sys; 41 this.unexpected = unexpected; 42 this.expecting = expecting; 43 this.raw = raw; 44 } 46 AbstractParsecError setExpecting(final String s){ 47 return new ParsecError(false, getIndex(), 48 sys_unexpected, unexpected, new String []{s}, raw, getException()); 49 } 50 static ParsecError raiseRaw(final int at, 51 final String msg){ 52 return new ParsecError(false, at, null, null, null, new String []{msg}, null); 53 } 54 static ParsecError raiseSysUnexpected(final int at, 55 final Object obj){ 56 return new ParsecError(true, at, obj, null, null, null, null); 57 } 58 static ParsecError raiseUnexpected(final int at 59 ,final String s){ 60 return new ParsecError(false, at, null, new String []{s}, null, null, null); 61 } 62 static ParsecError raiseExpecting(final int at, 63 final String s){ 64 return new ParsecError(false, at, null, null, new String []{s}, null, null); 65 } 66 static AbstractParsecError raiseExpecting(final int at, final String s, 67 final AbstractParsecError err){ 68 return new ParsecErrorExpecting(false, at, 69 myPrecedence(err.getPrecedence(), s), err.getException(), 70 err, s); 71 } 72 private static int max(int a, int b){ 73 return a>b?a:b; 74 } 75 private static int myPrecedence(int pred, String s){ 76 return max(pred, ParsecError.getPrecedenceForExpecting(s)); 77 } 78 static ParsecError throwException(final int at, 79 final Object e){ 80 return new ParsecError(false, at, null, null, null, null, e); 81 } 82 Object getSysUnexpected(){return sys_unexpected;} 83 public String getEncountered(){ 84 if(sys_unexpected == null) return null; 85 else return sys_unexpected.toString(); 86 } 87 public String [] getUnexpected(){return unexpected;} 88 public String [] getExpecting(){return expecting;} 89 public String [] getMessages(){return raw;} 90 static ParsecError noError(){ 91 return null; 92 } 93 private static String [] mergeMsgs(final String [] a, final String [] b){ 94 if(a==null)return b; 95 if(b==null)return a; 96 if(a==b)return a; 97 final String [] msgs = new String [a.length+b.length]; 98 104 System.arraycopy(a, 0, msgs, 0, a.length); 105 System.arraycopy(b, 0, msgs, a.length, b.length); 106 return msgs; 107 } 108 static ParsecError mergeError( 109 final int ind, final Object exception, final ParsecError e1, final ParsecError e2){ 110 return new ParsecError(false, ind, 111 MergedParsecError.mergeObj(e1.sys_unexpected, e2.sys_unexpected), 112 mergeMsgs(e1.unexpected, e2.unexpected), 113 mergeMsgs(e1.expecting, e2.expecting), 114 mergeMsgs(e1.raw, e2.raw), 115 exception 116 ); 117 } 118 } 119 | Popular Tags |