1 33 34 package edu.rice.cs.util.sexp; 35 36 import java.util.*; 37 import java.io.*; 38 39 class Lexer extends StreamTokenizer { 40 41 public HashMap<String ,Tokens.SExpToken> wordTable = new HashMap<String ,Tokens.SExpToken>(); 42 43 private Tokens.SExpToken buffer; 44 45 public Lexer(File file) throws FileNotFoundException{ 46 this(new BufferedReader(new FileReader(file))); 47 } 48 49 public Lexer(Reader reader) { 50 super(new BufferedReader(reader)); 51 initLexer(); 52 } 53 54 private void initLexer() { 55 56 resetSyntax(); 58 parseNumbers(); 59 slashSlashComments(true); 60 wordChars('!','\''); 61 wordChars('*','~'); 62 quoteChar('"'); 63 ordinaryChars('(',')'); 64 whitespaceChars(0,' '); 65 commentChar(';'); 66 67 initWordTable(); 68 buffer = null; } 70 71 72 public void flush() throws IOException { 73 eolIsSignificant(true); 74 while (nextToken() != TT_EOL) ; eolIsSignificant(false); 76 } 77 78 80 private int getToken() { 81 try { 82 int tokenType = nextToken(); 83 return tokenType; 84 } catch(IOException e) { 85 throw new LexingException("Unable to read the data from the given input"); 86 } 87 } 88 89 90 public Tokens.SExpToken peek() { 91 if (buffer == null) buffer = readToken(); 92 return buffer; 93 } 94 95 97 public Tokens.SExpToken readToken() { 98 99 if (buffer != null) { 100 Tokens.SExpToken token = buffer; 101 buffer = null; return token; 103 } 104 105 int tokenType = getToken(); 106 switch (tokenType) { 108 case TT_NUMBER: 109 return new Tokens.NumberToken(nval); 110 111 case TT_WORD: 112 String s = sval.toLowerCase(); 113 Tokens.SExpToken regToken = wordTable.get(s); 114 if (regToken == null) return new Tokens.WordToken(sval); 115 116 return regToken; 117 118 case TT_EOF: return null; 119 case '(': return Tokens.LeftParenToken.ONLY; 120 case ')': return Tokens.RightParenToken.ONLY; 121 case '"': return new Tokens.QuotedTextToken(sval); 122 case '\\': 123 return Tokens.BackSlashToken.ONLY; 142 144 default: 145 return new Tokens.WordToken("" + (char)tokenType); 146 } 147 } 148 149 150 151 private void initWordTable() { 152 wordTable.put("true", Tokens.BooleanToken.TRUE); 154 wordTable.put("false", Tokens.BooleanToken.FALSE); 155 } 156 } | Popular Tags |