/* PseudoCode Interpreted Language (PCIL): Part of the algoviz@vt collection of algorithm visualizations. Copyright (C) 2008 Brandon Malone, Frank Hadlock This file is part of the PseudoCode Interpreted Language. PseudoCode Interpreted Language is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PseudoCode Interpreted Language is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PseudoCode Interpreted Language. If not, see . */ /* * SourceToken.java * * Created on February 25, 2008, 11:03 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package lex; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; /** * * @author Brandon */ public class SourceToken { public static final String NewLine = "#nl#"; public static ArrayList readFromFile(String sourceTokenFilePath) throws IOException { ArrayList sourceTokens = new ArrayList(); BufferedReader br = new BufferedReader(new FileReader(sourceTokenFilePath)); String line = br.readLine(); while(line != null) { SourceToken t = new SourceToken(line); sourceTokens.add(t); line = br.readLine(); } br.close(); return sourceTokens; } private String lexicalValue; private String sourceValue; /** Creates a new instance of SourceToken */ public SourceToken(String lexicalValue, String sourceValue) { this.setLexicalValue(lexicalValue); this.setSourceValue(sourceValue); } // read a string that was written to a file using toString public SourceToken(String s) { // assume s looks like "" // strip off the first and last characters s = s.substring(1, s.length() - 1); // split on the comma String[] split = s.split(","); // grab the lexical value. strip out any extraneous white space setLexicalValue(split[0].trim()); // and the source value. strip out any extraneous white space // check if the source value is the newLine String sourceValue = split[1].trim(); if (sourceValue.equals(NewLine)) { sourceValue = "\n"; } setSourceValue(sourceValue); // if there were 4 fields in the split, assume the character is a comma if (split.length == 3) { setLexicalValue(","); setSourceValue(","); } } public String getLexicalValue() { return lexicalValue; } public void setLexicalValue(String lexicalValue) { this.lexicalValue = lexicalValue; } public String getSourceValue() { return sourceValue; } public void setSourceValue(String sourceValue) { this.sourceValue = sourceValue; } public String toString() { return "<" + lexicalValue + ", " + sourceValue + ">"; } }