/*
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
.
*/
/*
* PostfixToken.java
*
* Created on March 2, 2008, 6:35 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package syntacticanalysis;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import lex.SourceToken;
/**
*
* @author Brandon
*/
public class PostfixToken {
public static final String NewLine = "nl";
private static int nextID;
public static int getNextID() {
int ret = nextID;
nextID++;
return ret;
}
public static void resetID() {
nextID = 0;
}
private String semanticValue;
private String lexicalValue;
private String sourceValue;
private int id;
private int lineNumber;
/** Creates a new instance of PostfixToken */
public PostfixToken(String semanticValue, String lexicalValue, String sourceValue, int lineNumber) {
this.setSemanticValue(semanticValue);
this.setLexicalValue(lexicalValue);
this.setSourceValue(sourceValue);
if (sourceValue.equals("\n")) {
this.setSourceValue(NewLine);
}
this.setId(getNextID());
this.setLineNumber(lineNumber);
}
public PostfixToken(String semanticValue, SourceToken sourceToken, int lineNumber) {
this.setSemanticValue(semanticValue);
this.setLexicalValue(sourceToken.getLexicalValue());
this.setSourceValue(sourceToken.getSourceValue());
if (getSourceValue().equals("\n")) {
this.setSourceValue(NewLine);
}
this.setId(getNextID());
this.setLineNumber(lineNumber);
}
public PostfixToken(String line) {
String[] split = line.split("~");
this.setSemanticValue(split[0]);
this.setLexicalValue(split[1]);
this.setSourceValue(split[2]);
this.setId(Integer.parseInt(split[3]));
this.setLineNumber(Integer.parseInt(split[4]));
}
public String toString() {
return getSemanticValue() + "~" + getLexicalValue() + "~" + getSourceValue() + "~" + getId() + "~" + getLineNumber();
}
public static void writeToFile(ArrayList tokens, String filePath) throws IOException {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
for(PostfixToken token : tokens) {
pw.println(token.toString());
}
pw.close();
}
public static ArrayList readFromFile(String filePath) throws IOException {
ArrayList tokens = new ArrayList();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line = br.readLine();
while(line != null) {
tokens.add(new PostfixToken(line));
line = br.readLine();
}
br.close();
return tokens;
}
public String getSemanticValue() {
return semanticValue;
}
public void setSemanticValue(String semanticValue) {
this.semanticValue = semanticValue;
}
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLineNumber() {
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
}