1 23 package com.tc.jrexx.regex; 24 25 import java.util.Vector ; 26 import java.text.ParsePosition ; 27 28 class PScanner { 29 public static final int UNLIMITED_MAX_LENGTH = Integer.MAX_VALUE; 30 31 private final Automaton_Pattern.TerminalFormat[] terminalFormats; 32 private final int[] terminalsMaxLength; 33 private final boolean terminalFormatsAreExclusive; 34 35 public PScanner(Automaton_Pattern.TerminalFormat[] terminalFormats) { 36 this(terminalFormats,false); 37 } 38 39 public PScanner(Automaton_Pattern.TerminalFormat[] terminalFormats, boolean terminalFormatsAreExclusive) { 40 this.terminalFormats = terminalFormats; 41 this.terminalFormatsAreExclusive = terminalFormatsAreExclusive; 42 43 final int n = this.terminalFormats.length; 44 if (!this.terminalFormatsAreExclusive) { 45 for (int i= (n-1)>>1; i>=0; --i) { 47 Automaton_Pattern.TerminalFormat temp = this.terminalFormats[i]; 48 this.terminalFormats[i] = this.terminalFormats[n-i]; 49 this.terminalFormats[n-i] = temp; 50 } 51 } 52 this.terminalsMaxLength = new int[n]; 53 for (int i=0; i<n; i++) { 54 this.terminalsMaxLength[i] = this.terminalFormats[i].maxLength(); 55 } 56 } 57 58 59 public Vector scan(String source) { 60 return this.scan(source,0); 61 } 62 63 64 public Vector scan(String source, int startIndex) { 65 if (source==null) { 66 String message = "null source specified"; 67 throw new IllegalArgumentException (message); 68 } 69 70 final char[] input = source.toCharArray(); 71 72 int firstIndexOfTerminalFormats = -1; 73 int lastIndexOfTerminalFormats = -1; 74 75 for (int i=this.terminalFormats.length-1; i>=0; i--) { 76 if (this.terminalFormats[i]!=null) { 77 lastIndexOfTerminalFormats = i; 78 break; 79 } 80 } 81 82 if (lastIndexOfTerminalFormats==-1) { 83 String message = "no terminal formats added"; 84 throw new NullPointerException (message); 85 } 86 87 for (int i=0; i<=lastIndexOfTerminalFormats; i++) { 88 if (this.terminalFormats[i]!=null) { 89 firstIndexOfTerminalFormats = i; 90 break; 91 } 92 } 93 94 final Vector tokenList = new Vector (); 96 final int inputLength = input.length; 97 final ParsePosition pos = new ParsePosition (startIndex); 98 int index = startIndex; 99 while (index<inputLength) { 100 int longestMatch = -1; 101 Object lastToken = null, token; 102 for (int i=lastIndexOfTerminalFormats; i>=firstIndexOfTerminalFormats; i--) { 103 if (this.terminalsMaxLength[i]>=longestMatch) { 104 pos.setIndex(index); 105 106 token = this.terminalFormats[i].parseObject(input,pos); 109 final int matchLength = pos.getIndex()-index; 111 if (token!=null) { 112 if (this.terminalFormatsAreExclusive) { 113 longestMatch = matchLength; 114 lastToken = token; 115 break; 116 } else { 117 if (matchLength>=longestMatch) { 118 longestMatch = matchLength; 119 lastToken = token; 120 } 121 } 122 } 123 } 124 } 125 if (lastToken!=null) tokenList.addElement(lastToken); 127 else { 128 String message = "can not scan input:" 129 +"\n"+new String (input,startIndex,input.length-startIndex) 130 +"\nerrorPosition: "+index 131 +"\n"+new String (input,index,input.length-index); 132 throw new ParseException(message); 133 } 134 index+= longestMatch; 135 } 136 137 return tokenList; 138 } 139 140 public String toString() { 141 StringBuffer answer = new StringBuffer (); 142 answer.append("Scanner("); 143 if (this.terminalFormatsAreExclusive) answer.append("exclusive"); 144 answer.append(")"); 145 for (int i=0; i<this.terminalFormats.length; i++) 146 if (this.terminalFormats[i]!=null) 147 answer.append('\n').append(this.terminalFormats[i]); 148 return answer.toString(); 149 } 150 151 } | Popular Tags |