Changeset 4131
- Timestamp:
- Dec 20, 2009, 4:49:17 PM (11 years ago)
- Location:
- devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/AbstractParser.java
r4130 r4131 1 1 package org.refal.rfpdt.parser; 2 3 import java.math.BigInteger; 2 4 3 5 import org.refal.rfpdt.compiler.MsgCode; … … 5 7 import org.refal.rfpdt.compiler.SrcPosition; 6 8 7 publicabstract class AbstractParser {8 final AbstractScanner scanner;9 final MsgHandler msgHandler;9 abstract class AbstractParser { 10 private final AbstractScanner scanner; 11 private long tk; 10 12 11 /** The tag and position of the current token */12 private long tk;13 13 SrcPosition tkPos; 14 14 SrcPosition prevTkPos; 15 char tkChar; 16 String tkString; 17 Enum<?> tkKind; 18 BigInteger tkBigInteger; 19 20 final MsgHandler msgHandler; 15 21 16 22 AbstractParser (AbstractScanner scanner, MsgHandler msgHandler) { … … 24 30 tk = scanner.tk; 25 31 tkPos = scanner.tkPos; 32 tkChar = scanner.tkChar; 33 tkString = scanner.tkString; 34 tkKind = scanner.tkKind; 35 tkBigInteger = scanner.tkBigInteger; 26 36 } 27 37 -
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/AbstractScanner.java
r4130 r4131 26 26 } 27 27 } 28 28 29 protected static final TreeMap<String, KwEntry> keyword_table = new TreeMap<String, KwEntry>(); 30 29 31 protected static void kw (String keyword, long kwToken, Enum<?> kwKind) { 30 32 keyword_table.put(keyword, new KwEntry(kwToken, kwKind)); 31 33 } 32 34 33 protected final PushbackReader reader; 34 protected final RfpResource rfpResource; 35 protected final MsgHandler msgHandler; 36 37 protected AbstractScanner (RfpResource rfpResource, Reader reader, MsgHandler msgHandler) { 38 this.reader = new PushbackReader(reader); 39 this.rfpResource = rfpResource; 40 this.msgHandler = msgHandler; 41 chPosition = -1; 42 } 43 44 /* errors */ 35 private final PushbackReader reader; 36 private final RfpResource rfpResource; 37 private final MsgHandler msgHandler; 45 38 46 39 protected int ch; … … 54 47 protected int beginOfCharacetrs = -1; 55 48 56 /* Current token and values associated with it */ 57 58 public SrcPosition tkPos = null; 59 public long tk; 60 public char tkChar; 61 public String tkString; 62 public Enum<?> tkKind; 63 public BigInteger tkBigInteger; 64 65 private void resetToken () { 49 protected AbstractScanner (RfpResource rfpResource, Reader reader, MsgHandler msgHandler) { 50 this.reader = new PushbackReader(reader); 51 this.rfpResource = rfpResource; 52 this.msgHandler = msgHandler; 53 chPosition = -1; 54 } 55 56 protected void scanError (int line, int lineStart, int charStart, int charEnd, MsgCode msgCode, Object... args) { 57 SrcPosition pos = new SrcPosition(rfpResource, line, lineStart, charStart, charEnd); 58 msgHandler.send(pos, msgCode, args); 59 } 60 61 private void firstCh () throws IOException { 62 chLineStart = 0; 63 chPosition = 0; 64 chLine = 1; 65 ch = reader.read(); 66 return; 67 } 68 69 protected void nextCh () throws IOException { 70 if (ch == -1) 71 throw new EOFException(); 72 int prevCh = ch; 73 ch = reader.read(); 74 chPosition++; 75 if (ch == '\n' && prevCh == '\r') { 76 ch = reader.read(); 77 chPosition++; 78 } 79 if (prevCh == '\r' || prevCh == '\n') { 80 chLineStart = chPosition; 81 chLine++; 82 } 83 return; 84 } 85 86 protected void prevCh (int c) throws IOException { 87 reader.unread(ch); 88 ch = c; 89 chPosition--; 90 } 91 92 private void resetTk () { 66 93 tk = 0; 67 94 tkKind = null; … … 71 98 } 72 99 73 private void getFirstChar () throws IOException { 74 chLineStart = 0; 75 chPosition = 0; 76 chLine = 1; 77 ch = reader.read(); 78 return; 79 } 80 81 protected void scanError (int line, int lineStart, int charStart, int charEnd, MsgCode msgCode, Object... args) { 82 SrcPosition pos = new SrcPosition(rfpResource, line, lineStart, charStart, charEnd); 83 msgHandler.send(pos, msgCode, args); 100 protected void initTkPosition () { 101 tkLine = chLine; 102 tkLineStart = chLineStart; 103 tkCharStart = chPosition; 104 tkCharEnd = tkCharStart; 84 105 } 85 106 … … 136 157 } 137 158 138 protected void nextCh () throws IOException {139 if (ch == -1)140 throw new EOFException();141 int prevCh = ch;142 ch = reader.read();143 chPosition++;144 if (ch == '\n' && prevCh == '\r') {145 ch = reader.read();146 chPosition++;147 }148 if (prevCh == '\r' || prevCh == '\n') {149 chLineStart = chPosition;150 chLine++;151 }152 return;153 }154 155 protected void prevCh (int c) throws IOException {156 reader.unread(ch);157 ch = c;158 chPosition--;159 }160 161 protected void skipSingleLineComment () throws IOException {162 for (;;) {163 if (ch == -1)164 return;165 if (ch == '\n' || ch == '\r') {166 nextCh();167 return;168 } else169 nextCh();170 }171 }172 173 protected void skipMultilineComment (int chLine, int chLineStart, int chStart) throws IOException {174 for (;;) {175 if (ch == -1) {176 scanError(chLine, chLineStart, chStart, chPosition, MsgCode.UnexpectedEndOfComment);177 return;178 } else if (ch == '*') {179 nextCh();180 if (ch == '/') {181 nextCh();182 return;183 } else {184 continue;185 }186 } else {187 nextCh();188 continue;189 }190 }191 }192 193 protected String scanWord () throws IOException {194 return scanQuote('\"', MsgCode.UnexpectedEndOfWord);195 }196 197 protected String scanChars () throws IOException {198 return scanQuote('\'', MsgCode.UnexpectedEndOfCharacters);199 }200 201 159 private String scanQuote (char quote, MsgCode msgCode) throws IOException { 160 202 161 assert ch == quote; 203 162 StringBuilder sb = new StringBuilder(); … … 228 187 } 229 188 230 protected BigInteger scanNumber () throws IOException { 231 StringBuffer sb = new StringBuffer(); 232 int radix = 10; 233 if (ch == '+') { 234 nextCh(); 235 } else if (ch == '-') { 236 sb.append('-'); 237 nextCh(); 238 } else if (ch == '0') { 239 nextCh(); 240 if (ch == 'x') { 241 radix = 16; 242 nextCh(); 189 protected String scanWord () throws IOException { 190 return scanQuote('\"', MsgCode.UnexpectedEndOfWord); 191 } 192 193 protected String scanChars () throws IOException { 194 return scanQuote('\'', MsgCode.UnexpectedEndOfCharacters); 195 } 196 197 protected void skipSingleLineComment () throws IOException { 198 for (;;) { 199 if (ch == -1) 200 return; 201 if (ch == '\n' || ch == '\r') { 202 nextCh(); 203 return; 204 } else 205 nextCh(); 206 } 207 } 208 209 protected void skipMultilineComment (int chLine, int chLineStart, int chStart) throws IOException { 210 for (;;) { 211 if (ch == -1) { 212 scanError(chLine, chLineStart, chStart, chPosition, MsgCode.UnexpectedEndOfComment); 213 return; 214 } else if (ch == '*') { 215 nextCh(); 216 if (ch == '/') { 217 nextCh(); 218 return; 219 } else { 220 continue; 221 } 243 222 } else { 244 prevCh('0'); 223 nextCh(); 224 continue; 245 225 } 246 226 } 247 if (radix == 10) { 248 if (!RfpCharacter.isDigit(ch)) { 249 scanError(tkLine, tkLineStart, tkCharStart, chPosition, MsgCode.InvalidNumber); 250 sb.append('0'); 251 } else { 252 do { 253 sb.append((char) ch); 254 nextCh(); 255 } while (RfpCharacter.isDigit(ch)); 256 } 257 } else { // radix == 16 258 if (!RfpCharacter.isHexDigit(ch)) { 259 scanError(tkLine, tkLineStart, tkCharStart, chPosition, MsgCode.InvalidHexLiteralNumber); 260 sb.append('0'); 261 } else { 262 do { 263 sb.append((char) ch); 264 nextCh(); 265 } while (RfpCharacter.isHexDigit(ch)); 266 } 267 } 268 if (RfpCharacter.isAlphanum(ch)) { 269 scanError(tkLine, tkLineStart, tkCharStart, chPosition, 270 MsgCode.NumberImmediatelyFollowedByIdentifierOrVariable); // TODO: change message 271 } 272 return new BigInteger(sb.toString(), radix); 273 } 274 275 276 277 protected String scanKeyword () throws IOException { 278 StringBuilder sb = new StringBuilder(); 279 while (RfpCharacter.isAlphanum(ch) || ch == '?') { 280 sb.append((char) ch); 281 nextCh(); 282 } 283 return sb.toString(); 284 } 285 286 protected void initTkPosition () { 287 tkLine = chLine; 288 tkLineStart = chLineStart; 289 tkCharStart = chPosition; 290 tkCharEnd = tkCharStart; 291 } 227 } 228 229 abstract protected void nextTk () throws IOException; 230 231 /* Current token and values associated with it */ 232 public SrcPosition tkPos; 233 public long tk; 234 public char tkChar; 235 public String tkString; 236 public Enum<?> tkKind; 237 public BigInteger tkBigInteger; 292 238 293 239 public void getNextToken () { 294 resetT oken();240 resetTk(); 295 241 try { 296 242 if (chPosition == -1) 297 getFirstChar();243 firstCh(); 298 244 nextTk(); 299 245 } catch (IOException e) { … … 304 250 tkPos = new SrcPosition(rfpResource, tkLine, tkLineStart, tkCharStart, tkCharEnd); 305 251 } 306 307 abstract protected void nextTk () throws IOException;308 252 } -
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/Rf5Parser.java
r4130 r4131 309 309 astTerm = new AstParen(pos, astExpr); 310 310 } else if (tkIs(VAR)) { 311 VarType t = AstVar.typeOfChar( scanner.tkChar);311 VarType t = AstVar.typeOfChar(tkChar); 312 312 AstName name = null; 313 if ( scanner.tkString != null && !scanner.tkString.equals(""))314 name = new AstName(SrcPosition.fromToLength(tkPos, - scanner.tkString.length()), scanner.tkString);313 if (tkString != null && !tkString.equals("")) 314 name = new AstName(SrcPosition.fromToLength(tkPos, -tkString.length()), tkString); 315 315 astTerm = new AstVar(tkPos, t, name); 316 316 nextTk(); … … 344 344 AstSymbol astSymbol; 345 345 if (tkIs(CHAR)) 346 astSymbol = new AstCharSymbol(tkPos, scanner.tkChar);346 astSymbol = new AstCharSymbol(tkPos, tkChar); 347 347 else if (tkIs(WORD)) 348 astSymbol = new AstWordSymbol(tkPos, scanner.tkString);348 astSymbol = new AstWordSymbol(tkPos, tkString); 349 349 else if (tkIs(NUMB)) 350 astSymbol = new AstNumberSymbol(tkPos, scanner.tkBigInteger);350 astSymbol = new AstNumberSymbol(tkPos, tkBigInteger); 351 351 else { 352 352 assert false; … … 364 364 private AstName name (long ftk, MsgCode msgCode) { 365 365 if (sync(WORD, ftk)) { 366 AstName astName = new AstName(tkPos, scanner.tkString);366 AstName astName = new AstName(tkPos, tkString); 367 367 nextTk(); 368 368 return astName; -
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/Rf5Scanner.java
r4130 r4131 21 21 import java.io.IOException; 22 22 import java.io.Reader; 23 import java.math.BigInteger; 23 24 24 25 import org.refal.rfpdt.ast.AstAlt.FailMode; … … 158 159 } 159 160 161 private BigInteger scanNumber () throws IOException { 162 StringBuffer sb = new StringBuffer(); 163 while (RfpCharacter.isDigit(ch)) { 164 sb.append((char) ch); 165 nextCh(); 166 } 167 if (RfpCharacter.isAlphanum(ch)) { 168 scanError(tkLine, tkLineStart, tkCharStart, chPosition, 169 MsgCode.NumberImmediatelyFollowedByIdentifierOrVariable); // TODO: change message 170 } 171 return new BigInteger(sb.toString()); 172 } 173 160 174 private void skipComm () throws IOException { 161 175 for (;;) { -
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/RfpCharacter.java
r4130 r4131 10 10 } 11 11 12 public static boolean isDigit (int c) { 13 return ('0' <= c && c <= '9'); 14 } 15 12 16 public static boolean isLetter (int c) { 13 17 return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'); … … 19 23 20 24 public static boolean isAlphanum (int c) { 21 return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || (c == '_') || ('0' <= c && c <= '9'); 22 } 23 24 public static boolean isDigit (int c) { 25 return ('0' <= c && c <= '9'); 25 return isLetter(c) || isDigit(c) || (c == '_'); 26 26 } 27 27 -
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/RfpParser.java
r4130 r4131 304 304 private void obj_decl (long ftk, ArrayList<AstTopNode> list, boolean isPublic) { 305 305 assert tkIs(OBJ_DECL); 306 ObjKind kind = (ObjKind) scanner.tkKind;306 ObjKind kind = (ObjKind) tkKind; 307 307 nextTk(); 308 308 AstName[] nameList = names(ftk); … … 355 355 assert tkIs(FUNC_DECL); 356 356 SrcPosition start = tkPos; 357 RetMode retMode = (RetMode) scanner.tkKind;357 RetMode retMode = (RetMode) tkKind; 358 358 nextTk(); 359 359 AstName astName = name(s_expr | EQL | ftk, MsgCode.MissingFunctionName); … … 667 667 if (tkIs(MATCH_DIR)) { 668 668 start = tkPos; 669 matchDir = (MatchDir) scanner.tkKind;669 matchDir = (MatchDir) tkKind; 670 670 nextTk(); 671 671 } … … 682 682 assert tkIs(LCURL); 683 683 SrcPosition start = tkPos; 684 FailMode t = (FailMode) scanner.tkKind;684 FailMode t = (FailMode) tkKind; 685 685 nextTk(); 686 686 AstSentence[] astPathList = sentences(RCURL | ftk); … … 718 718 assert tkIs(LCURL); 719 719 SrcPosition start = tkPos; 720 FailMode t = (FailMode) scanner.tkKind;720 FailMode t = (FailMode) tkKind; 721 721 nextTk(); 722 722 AstSentence[] astPathList = paths(RCURL | ftk); … … 800 800 astTerm = new AstParen(pos, astExpr); 801 801 } else if (tkIs(VAR)) { 802 VarType t = AstVar.typeOfChar( scanner.tkChar);802 VarType t = AstVar.typeOfChar(tkChar); 803 803 AstName name = null; 804 if ( scanner.tkString != null && !scanner.tkString.equals(""))805 name = new AstName(SrcPosition.fromToLength(tkPos, - scanner.tkString.length()), scanner.tkString);804 if (tkString != null && !tkString.equals("")) 805 name = new AstName(SrcPosition.fromToLength(tkPos, -tkString.length()), tkString); 806 806 astTerm = new AstVar(tkPos, t, name); 807 807 nextTk(); … … 835 835 AstSymbol astSymbol; 836 836 if (tkIs(CHAR)) 837 astSymbol = new AstCharSymbol(tkPos, scanner.tkChar);837 astSymbol = new AstCharSymbol(tkPos, tkChar); 838 838 else if (tkIs(WORD)) 839 astSymbol = new AstWordSymbol(tkPos, scanner.tkString);839 astSymbol = new AstWordSymbol(tkPos, tkString); 840 840 else if (tkIs(NUMB)) 841 astSymbol = new AstNumberSymbol(tkPos, scanner.tkBigInteger);841 astSymbol = new AstNumberSymbol(tkPos, tkBigInteger); 842 842 else { 843 843 assert false; … … 868 868 private AstName name (long ftk, MsgCode msgCode) { 869 869 if (sync(WORD, ftk)) { 870 AstName astName = new AstName(tkPos, scanner.tkString);870 AstName astName = new AstName(tkPos, tkString); 871 871 nextTk(); 872 872 return astName; -
devel-tools/trunk/eclipse/org.refal.rfpdt.core/src/org/refal/rfpdt/parser/RfpScanner.java
r4130 r4131 39 39 import java.io.IOException; 40 40 import java.io.Reader; 41 import java.math.BigInteger; 41 42 42 43 import org.refal.rfpdt.ast.AstAlt.FailMode; … … 77 78 super(rfpResource, reader, msgHandler); 78 79 } 79 80 80 81 81 protected void nextTk () throws IOException { … … 330 330 } 331 331 return sb.toString(); 332 }} 332 } 333 334 private String scanKeyword () throws IOException { 335 StringBuilder sb = new StringBuilder(); 336 while (RfpCharacter.isAlphanum(ch) || ch == '?') { 337 sb.append((char) ch); 338 nextCh(); 339 } 340 return sb.toString(); 341 } 342 343 private BigInteger scanNumber () throws IOException { 344 StringBuffer sb = new StringBuffer(); 345 int radix = 10; 346 if (ch == '+') { 347 nextCh(); 348 } else if (ch == '-') { 349 sb.append('-'); 350 nextCh(); 351 } else if (ch == '0') { 352 nextCh(); 353 if (ch == 'x') { 354 radix = 16; 355 nextCh(); 356 } else { 357 prevCh('0'); 358 } 359 } 360 if (radix == 10) { 361 if (!RfpCharacter.isDigit(ch)) { 362 scanError(tkLine, tkLineStart, tkCharStart, chPosition, MsgCode.InvalidNumber); 363 sb.append('0'); 364 } else { 365 do { 366 sb.append((char) ch); 367 nextCh(); 368 } while (RfpCharacter.isDigit(ch)); 369 } 370 } else { // radix == 16 371 if (!RfpCharacter.isHexDigit(ch)) { 372 scanError(tkLine, tkLineStart, tkCharStart, chPosition, MsgCode.InvalidHexLiteralNumber); 373 sb.append('0'); 374 } else { 375 do { 376 sb.append((char) ch); 377 nextCh(); 378 } while (RfpCharacter.isHexDigit(ch)); 379 } 380 } 381 if (RfpCharacter.isAlphanum(ch)) { 382 scanError(tkLine, tkLineStart, tkCharStart, chPosition, 383 MsgCode.NumberImmediatelyFollowedByIdentifierOrVariable); // TODO: change message 384 } 385 return new BigInteger(sb.toString(), radix); 386 } 387 }
Note: See TracChangeset
for help on using the changeset viewer.