|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Lexer.java | 62.5% | 82.7% | 75% | 79.4% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.util.sexp; | |
| 38 | ||
| 39 | import java.util.*; | |
| 40 | import java.io.*; | |
| 41 | ||
| 42 | class Lexer extends StreamTokenizer { | |
| 43 | ||
| 44 | public HashMap<String,Tokens.SExpToken> wordTable = new HashMap<String,Tokens.SExpToken>(); | |
| 45 | ||
| 46 | private Tokens.SExpToken buffer; | |
| 47 | ||
| 48 | 0 | public Lexer(File file) throws FileNotFoundException{ |
| 49 | 0 | this(new BufferedReader(new FileReader(file))); |
| 50 | } | |
| 51 | ||
| 52 | 22 | public Lexer(Reader reader) { |
| 53 | 22 | super(new BufferedReader(reader)); |
| 54 | 22 | initLexer(); |
| 55 | } | |
| 56 | ||
| 57 | 22 | private void initLexer() { |
| 58 | ||
| 59 | // configure StreamTokenizer portion of this | |
| 60 | 22 | resetSyntax(); |
| 61 | 22 | parseNumbers(); |
| 62 | 22 | slashSlashComments(true); |
| 63 | 22 | wordChars('!','\''); |
| 64 | 22 | wordChars('*','~'); |
| 65 | 22 | quoteChar('"'); |
| 66 | 22 | ordinaryChars('(',')'); |
| 67 | 22 | whitespaceChars(0,' '); |
| 68 | 22 | commentChar(';'); |
| 69 | ||
| 70 | 22 | initWordTable(); |
| 71 | 22 | buffer = null; // buffer initially empty |
| 72 | } | |
| 73 | ||
| 74 | /** Skips through the input stream until an EOL is encountered */ | |
| 75 | 0 | public void flush() throws IOException { |
| 76 | 0 | eolIsSignificant(true); |
| 77 | 0 | while (nextToken() != TT_EOL) ; // eat tokens until EOL |
| 78 | 0 | eolIsSignificant(false); |
| 79 | } | |
| 80 | ||
| 81 | /** Performs a nextToken() operation from StreamTokenizer except for throwing an unchecked LexingException instead of | |
| 82 | * a checked IOException */ | |
| 83 | 1198 | private int getToken() { |
| 84 | 1198 | try { |
| 85 | 1198 | int tokenType = nextToken(); |
| 86 | 1198 | return tokenType; |
| 87 | } catch(IOException e) { | |
| 88 | 0 | throw new LexingException("Unable to read the data from the given input"); |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | /** Returns the next Tokens.SExpToken without consuming it */ | |
| 93 | 1118 | public Tokens.SExpToken peek() { |
| 94 | 1118 | if (buffer == null) buffer = readToken(); |
| 95 | 1118 | return buffer; |
| 96 | } | |
| 97 | ||
| 98 | /** Reads the next Tokens.SExpToken from the input stream and consumes it. | |
| 99 | * @return the Tokens.SExpToken object representing this Tokens.SExpToken */ | |
| 100 | 2312 | public Tokens.SExpToken readToken() { |
| 101 | ||
| 102 | 2312 | if (buffer != null) { |
| 103 | 1114 | Tokens.SExpToken token = buffer; |
| 104 | 1114 | buffer = null; // clear buffer |
| 105 | 1114 | return token; |
| 106 | } | |
| 107 | ||
| 108 | 1198 | int tokenType = getToken(); |
| 109 | // Process the Tokens.SExpToken returned by StreamTokenizer | |
| 110 | 1198 | switch (tokenType) { |
| 111 | 115 | case TT_NUMBER: |
| 112 | 115 | return new Tokens.NumberToken(nval); |
| 113 | ||
| 114 | 317 | case TT_WORD: |
| 115 | 317 | String s = sval.toLowerCase(); |
| 116 | 317 | Tokens.SExpToken regToken = wordTable.get(s); |
| 117 | 316 | if (regToken == null) return new Tokens.WordToken(sval); |
| 118 | ||
| 119 | 1 | return regToken; |
| 120 | ||
| 121 | 21 | case TT_EOF: return null; |
| 122 | 313 | case '(': return Tokens.LeftParenToken.ONLY; |
| 123 | 310 | case ')': return Tokens.RightParenToken.ONLY; |
| 124 | 122 | case '"': return new Tokens.QuotedTextToken(sval); |
| 125 | 0 | case '\\': |
| 126 | // int t = getToken(); | |
| 127 | // if (t == '"') { | |
| 128 | // return new Tokens.WordToken("\""); | |
| 129 | // } | |
| 130 | // else if (t == '\\') { | |
| 131 | // return new Tokens.WordToken("\\"); | |
| 132 | // } | |
| 133 | // else if (t == ' ') { | |
| 134 | // return new Tokens.WordToken(" "); | |
| 135 | // } | |
| 136 | // else if (t == 'n') { | |
| 137 | // return new Tokens.WordToken("\n"); | |
| 138 | // } | |
| 139 | // else if (t == 't') { | |
| 140 | // return new Tokens.WordToken("\t"); | |
| 141 | // } | |
| 142 | // else { | |
| 143 | // pushBack(); | |
| 144 | 0 | return Tokens.BackSlashToken.ONLY; |
| 145 | // throw new SExpParseException("Invalid escape sequence: \\" + (char)t); | |
| 146 | ||
| 147 | 0 | default: |
| 148 | 0 | return new Tokens.WordToken("" + (char)tokenType); |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | ||
| 153 | /** Initialize the word table used by the lexer to classify Tokens */ | |
| 154 | 22 | private void initWordTable() { |
| 155 | // initialize wordTable | |
| 156 | 22 | wordTable.put("true", Tokens.BooleanToken.TRUE); |
| 157 | 22 | wordTable.put("false", Tokens.BooleanToken.FALSE); |
| 158 | } | |
| 159 | } |
|
||||||||||