/* SCCS - @(#)YYlexbuffer.java 1.3 - 02/28/99 - 12:39:29 */ /* * Copyright (c) 1996 Dennis Heimbigner. email dennis@cs.colorado.edu */ package pp.src.jbf; import java.lang.*; import java.io.*; public class YYlexbuffer { protected static final int EOF = -1; protected static final int EOFCHAR = '\377'; // Keep a vector of characters around the current char; Byte_Vector b; // Keep a corresponding vector of char in line counts Int_Vector cno; StringBuffer strbuf; // for returning lexemes protected int current; // index into yybuffer of current char protected int thisch; // char at current protected int mark; // index into yybuffer of start of lexeme protected int markline; protected int markchar0; protected int markchar; /* line and char info */ protected int yychar; // 0-base, char in file protected int yyline; // 0-base line in file protected int yylinebase; protected int linecount; // total # lines in buffer protected int maxlines; // max total # lines kept in buffer protected java.io.InputStream yyin; protected java.io.PrintStream yyout; /** added */ public java.util.Vector wholebuffer; StringBuffer currentbuffer; public YYlexbuffer(InputStream fin, PrintStream fout) {this(fin,fout,2);} public YYlexbuffer(InputStream fin, PrintStream fout, int max) { yyin = fin; yyout = fout; maxlines = max; linecount = 0; b = new Byte_Vector(); cno = new Int_Vector(); current = -1; mark = markchar0 = markline = markchar = 0; thisch = '\n'; // pretend yychar = -1; yyline = -1; yylinebase = -1; yydebug = 0; strbuf = new StringBuffer(); /** added **/ wholebuffer = new java.util.Vector(50,50); currentbuffer = new StringBuffer(); wholebuffer.addElement(currentbuffer); } public int nextchar() throws IOException { int lastch = thisch; current++; if(current >= b.size()) { int ch = yyin.read(); //Error al leed de yyin current = b.size(); b.addElement((byte)ch); cno.addElement(yylinebase); /** added **/ if(ch!=EOF) currentbuffer.append((char)ch); if(ch == '\n') { linecount++; /* added */ currentbuffer=new StringBuffer(); wholebuffer.addElement(currentbuffer); } } thisch = b.elementAt(current); yychar++; if(lastch == '\n') { yyline++; yylinebase = yychar; } if(yydebug >= 100) {yyout.println("next/"+sch(thisch) +"/" +yychar0() +":" +yyline() +"+" +yychar() +"%" +yylinebase +"<" +sch(lastch) +">");} return thisch; } public void backup() { int nextch = thisch; current--; thisch = b.elementAt(current); // remember that newline is considered part // of the line it ends not the next line if(thisch == '\n') { yyline--; yylinebase = cno.elementAt(current); } yychar--; if(yydebug >= 100) {yyout.println("push/"+sch(nextch) +"/" +yychar0() +":" +yyline() +"+" +yychar() +"%" +yylinebase +"<" +sch(thisch) +">");} } public int peek() throws IOException { int ch = nextchar(); backup(); return ch; } public int lastchar() { int ch = 0; if(current > 0) { ch = b.elementAt(current-1); } return ch; } public int currentchar() { int ch = 0; if(current >= 0 && current < b.size()) { ch = b.elementAt(current); } return ch; } public void setmaxlines(int n) { maxlines = n; } public void purge() { int rem = linecount - maxlines; int len = b.size(); int i; if(rem <= 0) return; ///**/yyout.println("purge:linecount="+linecount+" ; maxlines="+maxlines+" ; rem="+rem); ///**/yyout.println("purge: contents before=/"+contents()+"/"); for(i=0;i 0;i++) {if(b.elementAt(i) == '\n') rem--;} if(i > 0) { b.removeElementRange(0,i); // purge thru the final newline cno.removeElementRange(0,i); } linecount = maxlines+rem; mark -= i; current -= i; ///**/yyout.println("purge:count="+i); ///**/yyout.println("purge: contents after=/"+contents()+"/"); } // return 0 based line number public int yyline() {return yyline;} // return 0 based absolute char number public int yychar0() {return yychar;} // return 0 based char number within line public int yychar() {return yychar - yylinebase;} // Note, We assume that mark is called JUST BEFORE // the first character to be in the lexeme() // => the lexeme begins at mark + 1 public void mark() { mark = current+1; markchar0 = yychar0(); markchar = yychar(); markline = yyline(); ///**/yyout.print("mark=/"); ///**/for(int k=mark;k0?i:0;} private String sch(int ch) {return (ch != EOF?String.valueOf((char)ch):"EOF");} public int lexemesize() {return (current - mark) + 1;} public String contents() { //99/2/26 aa: too out hibyte because of deprecation return new String(b.contents(),0,b.size()); } public int currentpos() { return current; } public void printWholeBuffer() { java.util.Enumeration E = wholebuffer.elements(); int line_count =1; System.out.println("\n\nPrint whole buffer"); while (E.hasMoreElements()) { System.out.println("L"+line_count+">>"+(StringBuffer)E.nextElement()); line_count++; } } };