1 package ro.infoiasi.donald.compiler.lexer; 2 3 import ro.infoiasi.donald.compiler.lexer.exceptions.*; 4 import java.util.*; 5 6 public class ExpList extends ArrayList { 7 private Alphabet alpha = new Alphabet(); 8 9 public ExpList() { 10 super(); 11 } 12 13 public ExpList(String s) throws ExpParseError { 14 super(); 15 16 Stack stackPar = new Stack(); 17 18 for (int i = 0; i<s.length(); i++) { 19 char ch = s.charAt(i); 20 switch (ch) { 21 case '(': addParenthesisLeft(i, stackPar); break; 22 case ')': addParenthesisRight(i, stackPar); break; 23 case '\\': i = escapeNext(i, s); break; 24 default: 25 { 26 Operator op = Operator.get(ch); 27 if (op != null) { 28 addOperatorToken(i, op); 29 } else { 30 addSymbolToken(i, ch); 31 } 32 } 33 break; 34 } 35 } 36 if (stackPar.size() != 0) { 37 throw new ExpParseError("Parenthesis is not closing", 38 ((ParenthesisLeft)stackPar.peek()).strIndex()); 39 } 40 } 41 42 public ExpToken lastToken() { 43 return (ExpToken)get(size()-1); 44 } 45 46 public void addToken(ExpToken et) { 47 Class ce = et.getClass(); 48 if (ce == SymbolToken.class || 49 ce == ParenthesisLeft.class || 50 (ce == OperatorToken.class && 51 ((OperatorToken)et).operator().isUnaryRight())) { 52 57 if (size()>0) { 58 Class cl = lastToken().getClass(); 59 if (cl == SymbolToken.class || 60 cl == ParenthesisRight.class || 61 (cl == OperatorToken.class && 62 ((OperatorToken)lastToken()).operator().isUnaryLeft())) { 63 addToken(new OperatorToken(et.strIndex(), size(), Operator.CONCAT)); 64 et.incTokenNo(); 65 } 66 } 67 } 68 add(et); 69 } 70 71 public void addParenthesisLeft(int index, Stack stackPar) { 72 ParenthesisLeft parLeft = new ParenthesisLeft(index, size()); 73 stackPar.push(parLeft); 74 addToken(parLeft); 75 } 76 77 public void addParenthesisRight(int index, Stack stackPar) throws ExpParseError { 78 if (stackPar.size()==0) { 79 throw new ExpParseError("Parenthesis closing too soon",index); 80 } 81 ParenthesisLeft parLeft = (ParenthesisLeft)stackPar.pop(); 82 ParenthesisRight parRight = new ParenthesisRight(index, size()); 83 parLeft.setPair(parRight); 84 parRight.setPair(parLeft); 85 addToken(parRight); 86 } 87 88 public void addOperatorToken(int index, Operator op) { 89 addToken(new OperatorToken(index, size(), op)); 90 } 91 92 public void addSymbolToken(int index, char ch) { 93 alpha.addSymbol(ch); 94 SymbolToken symbolToken = new SymbolToken(index, size(), ch); 95 addToken(symbolToken); 96 } 97 98 public int escapeNext(int index, String s) throws ExpParseError { 99 index++; 100 if (index<s.length()) { 101 char ch = s.charAt(index); 102 switch (ch) { 103 case 'n': addSymbolToken(index, '\n'); break; 104 default: 106 { Operator op = Operator.get(ch); 108 if (op != null) { 109 addSymbolToken(index, ch); 110 } else { 111 throw new ExpParseError("Illegal escape character",index); 112 } 113 } 114 } 115 return index; 116 } else { 117 throw new ExpParseError("Nothing left to escape",index); 118 } 119 } 120 121 public Alphabet getAlphabet() { 122 return alpha; 123 } 124 125 public String toString() { 126 StringBuffer sb = new StringBuffer (); 127 Iterator it = listIterator(0); 128 while (it.hasNext()) { 129 sb.append((it.next()).toString()); 130 } 131 return new String (sb); 132 } 133 } 134 | Popular Tags |