Commit 70ab400c authored by Johannes Roith's avatar Johannes Roith

Initial import of the old Java 1.5 scanner.

parent 71b8de1e
package de.in.tum.www2.java;
import java.io.Reader;
import java.io.IOException;
import java.io.InputStream;
import de.in.tum.www2.cup.IErrorReporter;
import de.in.tum.www2.cup.ErrorManager;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import de.in.tum.www2.java.internal.onefivelexer.Lexer;
public class JavaScanner
{
// TODO: the java scanner should be able to check
// against a declaration class, similar to the cup one,
// that marks the cup variables (after colon) and the derived symbols
// such as blahxleft, blahxright in a special color.
// -> however, this is context-sensitive and should only happen in the
// local correct scope.
//
// Also, in an action block, the RESULT variable should be colored the same.
// likewise, all fields declared in the special global "action code" block should
// be marked (differently). REQUIRES JAVA PARSER!
//
// All other variables must necessarily be local variables and can be colored
// appropriately (in the local block). REQUIRES JAVA PARSER!
//
// The syntax highlighter does not need to care if functions exist and can
// just make them italic, like eclipse.
//
// Finally, there are types. These are not colored in eclipse anyway.
/*
private Lexer lexer;
public JavaScanner(IErrorReporter er, Reader r) {
ComplexSymbolFactory factory = new ComplexSymbolFactory();
ErrorManager errMan = new ErrorManager(er);
this.lexer = new Lexer(errMan, factory, r);
}
public JavaSymbol next_token() throws IOException {
ComplexSymbol symbol = (ComplexSymbol) lexer.next_token();
return new JavaSymbol(symbol);
}
*/
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class BooleanLiteral extends Literal {
Boolean val;
BooleanLiteral(boolean b) { this.val = new Boolean(b); }
Symbol token() { return new Symbol(Sym.BOOLEAN_LITERAL, val); }
public String toString() { return "BooleanLiteral <"+val.toString()+">"; }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class CharacterLiteral extends Literal {
Character val;
CharacterLiteral(char c) { this.val = new Character(c); }
Symbol token() { return new Symbol(Sym.CHARACTER_LITERAL, val); }
public String toString() {
return "CharacterLiteral <"+Token.escape(val.toString())+">";
}
}
package de.in.tum.www2.java.internal.onefivelexer;
abstract class Comment extends InputElement {
private StringBuffer comment = new StringBuffer();
String getComment() { return comment.toString(); }
void appendLine(String more) { // 'more' is '\n' terminated.
int i=0;
// skip leading white space.
for (; i<more.length(); i++)
if (!Character.isSpaceChar(more.charAt(i)))
break;
// skip any leading stars.
for (; i<more.length(); i++)
if (more.charAt(i)!='*')
break;
// the rest of the string belongs to the comment.
if (i<more.length())
comment.append(more.substring(i));
}
}
package de.in.tum.www2.java.internal.onefivelexer;
class DocumentationComment extends Comment {
DocumentationComment() { }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class DoubleLiteral extends NumericLiteral {
DoubleLiteral(double d) { this.val = new Double(d); }
Symbol token() { return new Symbol(Sym.FLOATING_POINT_LITERAL, val); }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class EOF extends Token {
EOF() {}
Symbol token() { return new Symbol(Sym.EOF); }
public String toString() { return "EOF"; }
}
package de.in.tum.www2.java.internal.onefivelexer;
class EndOfLineComment extends Comment {
EndOfLineComment(String comment) { appendLine(comment); }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java.io.Reader;
import java.io.FilterReader;
import java.io.IOException;
public class EscapedUnicodeReader extends FilterReader {
int pushback=-1;
boolean isEvenSlash = true;
public EscapedUnicodeReader(Reader in) {
super(in);
}
public int read() throws IOException {
int r = (pushback==-1)?in.read():pushback; pushback=-1;
if (r!='\\') {
isEvenSlash=true;
return r;
} else { // found a backslash;
if (!isEvenSlash) { // Only even slashes are eligible unicode escapes.
isEvenSlash=true;
return r;
}
// Check for the trailing u.
pushback=in.read();
if (pushback!='u') {
isEvenSlash=false;
return '\\';
}
// OK, we've found backslash-u.
// Reset pushback and snarf up all trailing u's.
pushback=-1;
while((r=in.read())=='u')
;
// Now we should find 4 hex digits.
// If we don't, we can raise bloody hell.
int val=0;
for (int i=0; i<4; i++, r=in.read()) {
int d=Character.digit((char)r, 16);
if (r<0 || d<0)
throw new Error("Invalid unicode escape character.");
val = (val*16) + d;
}
// yeah, we made it.
pushback = r;
isEvenSlash=true;
return val;
}
}
// synthesize array read from single-character read.
public int read(char cbuf[], int off, int len) throws IOException {
for (int i=0; i<len; i++) {
int c = read();
if (c==-1) return (i==0)?-1:i; // end of stream reached.
else cbuf[i+off] = (char) c;
}
return len;
}
public boolean markSupported() { return false; }
public boolean ready() throws IOException {
if (pushback!=-1) return true;
else return in.ready();
}
}
package de.in.tum.www2.java.internal.onefivelexer;
/** FIFO class. This helps implement the lookahead we need for JSR-14.
* Copyright (C) 2002 C. Scott Ananian <cananian@alumni.princeton.edu>
* This program is released under the terms of the GPL; see the file
* COPYING for more details. There is NO WARRANTY on this code.
*/
class FIFO {
java_cup.runtime.Symbol[] backing = new java_cup.runtime.Symbol[10];
int start=0, end=0;
final Getter getter;
FIFO(Getter getter) { this.getter = getter; }
public boolean isEmpty() { return start==end; }
private boolean isFull() {
return start==end+1 || (start==0 && end==backing.length-1);
}
private int size() {
return ((end<start)?end+backing.length:end)-start;
}
public void put(java_cup.runtime.Symbol o) {
if (isFull()) {
java_cup.runtime.Symbol[] nbacking =
new java_cup.runtime.Symbol[backing.length*2];
System.arraycopy(backing, start, nbacking, 0, backing.length-start);
System.arraycopy(backing, 0, nbacking, backing.length-start, start);
start = 0;
end = backing.length-1;
backing = nbacking;
}
ASSERT(!isFull());
backing[end++] = o;
if (end == backing.length)
end = 0;
ASSERT(!isEmpty());
}
public java_cup.runtime.Symbol get() throws java.io.IOException {
if (isEmpty())
put(getter.next());
ASSERT(!isEmpty());
java_cup.runtime.Symbol o = backing[start++];
if (start == backing.length)
start = 0;
ASSERT(!isFull());
return o;
}
public java_cup.runtime.Symbol peek(int i) throws java.io.IOException {
while (i >= size())
put(getter.next());
int index = start+i;
if (index >= backing.length) index -= backing.length;
ASSERT(0<= index && index < backing.length);
return backing[index];
}
abstract static class Getter {
abstract java_cup.runtime.Symbol next()
throws java.io.IOException;
}
private static void ASSERT(boolean b) {
if (!b) throw new RuntimeException();
}
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class FloatLiteral extends NumericLiteral {
FloatLiteral(float f) { this.val = new Float(f); }
Symbol token() { return new Symbol(Sym.FLOATING_POINT_LITERAL, val); }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
public class Identifier extends Token {
String identifier;
public Identifier(String identifier) { this.identifier=identifier; }
public String toString() { return "Identifier <"+identifier+">"; }
/* Ben Walter <bwalter@mit.edu> correctly pointed out that
* the first released version of this grammar/lexer did not
* return the string value of the identifier in the parser token.
* Should be fixed now. ;-) <cananian@alumni.princeton.edu>
*/
Symbol token() { return new Symbol(Sym.IDENTIFIER, identifier); }
}
package de.in.tum.www2.java.internal.onefivelexer;
abstract class InputElement {}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class IntegerLiteral extends NumericLiteral {
IntegerLiteral(int i) { this.val = new Integer(i); }
Symbol token() { return new Symbol(Sym.INTEGER_LITERAL, val); }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java.util.Hashtable;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class Keyword extends Token {
String keyword;
Keyword(String s) { keyword = s; }
Symbol token() {
Integer i = (Integer) key_table.get(keyword);
return new Symbol(i.intValue());
}
public String toString() { return "Keyword <"+keyword+">"; }
static private final Hashtable key_table = new Hashtable();
static {
key_table.put("abstract", new Integer(Sym.ABSTRACT));
key_table.put("assert", new Integer(Sym.ASSERT));
key_table.put("boolean", new Integer(Sym.BOOLEAN));
key_table.put("break", new Integer(Sym.BREAK));
key_table.put("byte", new Integer(Sym.BYTE));
key_table.put("case", new Integer(Sym.CASE));
key_table.put("catch", new Integer(Sym.CATCH));
key_table.put("char", new Integer(Sym.CHAR));
key_table.put("class", new Integer(Sym.CLASS));
key_table.put("const", new Integer(Sym.CONST));
key_table.put("continue", new Integer(Sym.CONTINUE));
key_table.put("default", new Integer(Sym.DEFAULT));
key_table.put("do", new Integer(Sym.DO));
key_table.put("double", new Integer(Sym.DOUBLE));
key_table.put("else", new Integer(Sym.ELSE));
key_table.put("enum", new Integer(Sym.ENUM));
key_table.put("extends", new Integer(Sym.EXTENDS));
key_table.put("final", new Integer(Sym.FINAL));
key_table.put("finally", new Integer(Sym.FINALLY));
key_table.put("float", new Integer(Sym.FLOAT));
key_table.put("for", new Integer(Sym.FOR));
key_table.put("goto", new Integer(Sym.GOTO));
key_table.put("if", new Integer(Sym.IF));
key_table.put("implements", new Integer(Sym.IMPLEMENTS));
key_table.put("import", new Integer(Sym.IMPORT));
key_table.put("instanceof", new Integer(Sym.INSTANCEOF));
key_table.put("int", new Integer(Sym.INT));
key_table.put("interface", new Integer(Sym.INTERFACE));
key_table.put("long", new Integer(Sym.LONG));
key_table.put("native", new Integer(Sym.NATIVE));
key_table.put("new", new Integer(Sym.NEW));
key_table.put("package", new Integer(Sym.PACKAGE));
key_table.put("private", new Integer(Sym.PRIVATE));
key_table.put("protected", new Integer(Sym.PROTECTED));
key_table.put("public", new Integer(Sym.PUBLIC));
key_table.put("return", new Integer(Sym.RETURN));
key_table.put("short", new Integer(Sym.SHORT));
key_table.put("static", new Integer(Sym.STATIC));
key_table.put("strictfp", new Integer(Sym.STRICTFP));
key_table.put("super", new Integer(Sym.SUPER));
key_table.put("switch", new Integer(Sym.SWITCH));
key_table.put("synchronized", new Integer(Sym.SYNCHRONIZED));
key_table.put("this", new Integer(Sym.THIS));
key_table.put("throw", new Integer(Sym.THROW));
key_table.put("throws", new Integer(Sym.THROWS));
key_table.put("transient", new Integer(Sym.TRANSIENT));
key_table.put("try", new Integer(Sym.TRY));
key_table.put("void", new Integer(Sym.VOID));
key_table.put("volatile", new Integer(Sym.VOLATILE));
key_table.put("while", new Integer(Sym.WHILE));
}
}
package de.in.tum.www2.java.internal.onefivelexer;
abstract class Literal extends Token { }
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class LongLiteral extends NumericLiteral {
LongLiteral(long l) { this.val = new Long(l); }
Symbol token() { return new Symbol(Sym.INTEGER_LITERAL, val); }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class NullLiteral extends Literal {
NullLiteral() { }
Symbol token() { return new Symbol(Sym.NULL_LITERAL); }
public String toString() { return "NullLiteral <null>"; }
}
package de.in.tum.www2.java.internal.onefivelexer;
abstract class NumericLiteral extends Literal {
Number val;
public String toString() { return "NumericLiteral <"+val.toString()+">"; }
}
package de.in.tum.www2.java.internal.onefivelexer;
import java.util.Hashtable;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class Operator extends Token {
String which;
Operator(String which) { this.which = which; }
public String toString() { return "Operator <"+which+">"; }
Symbol token() {
Integer i = (Integer) op_table.get(which);
return new Symbol(i.intValue());
}
static private final Hashtable op_table = new Hashtable();
static {
op_table.put("=", new Integer(Sym.EQ));
op_table.put(">", new Integer(Sym.GT));
op_table.put("<", new Integer(Sym.LT));
op_table.put("!", new Integer(Sym.NOT));
op_table.put("~", new Integer(Sym.COMP));
op_table.put("?", new Integer(Sym.QUESTION));
op_table.put(":", new Integer(Sym.COLON));
op_table.put("==", new Integer(Sym.EQEQ));
op_table.put("<=", new Integer(Sym.LTEQ));
op_table.put(">=", new Integer(Sym.GTEQ));
op_table.put("!=", new Integer(Sym.NOTEQ));
op_table.put("&&", new Integer(Sym.ANDAND));
op_table.put("||", new Integer(Sym.OROR));
op_table.put("++", new Integer(Sym.PLUSPLUS));
op_table.put("--", new Integer(Sym.MINUSMINUS));
op_table.put("+", new Integer(Sym.PLUS));
op_table.put("-", new Integer(Sym.MINUS));
op_table.put("*", new Integer(Sym.MULT));
op_table.put("/", new Integer(Sym.DIV));
op_table.put("&", new Integer(Sym.AND));
op_table.put("|", new Integer(Sym.OR));
op_table.put("^", new Integer(Sym.XOR));
op_table.put("%", new Integer(Sym.MOD));
op_table.put("<<", new Integer(Sym.LSHIFT));
op_table.put(">>", new Integer(Sym.RSHIFT));
op_table.put(">>>", new Integer(Sym.URSHIFT));
op_table.put("+=", new Integer(Sym.PLUSEQ));
op_table.put("-=", new Integer(Sym.MINUSEQ));
op_table.put("*=", new Integer(Sym.MULTEQ));
op_table.put("/=", new Integer(Sym.DIVEQ));
op_table.put("&=", new Integer(Sym.ANDEQ));
op_table.put("|=", new Integer(Sym.OREQ));
op_table.put("^=", new Integer(Sym.XOREQ));
op_table.put("%=", new Integer(Sym.MODEQ));
op_table.put("<<=", new Integer(Sym.LSHIFTEQ));
op_table.put(">>=", new Integer(Sym.RSHIFTEQ));
op_table.put(">>>=", new Integer(Sym.URSHIFTEQ));
op_table.put("@", new Integer(Sym.AT));
}
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class Separator extends Token {
char which;
Separator(char which) { this.which = which; }
Symbol token() {
switch(which) {
case '(': return new Symbol(Sym.LPAREN);
case ')': return new Symbol(Sym.RPAREN);
case '{': return new Symbol(Sym.LBRACE);
case '}': return new Symbol(Sym.RBRACE);
case '[': return new Symbol(Sym.LBRACK);
case ']': return new Symbol(Sym.RBRACK);
case ';': return new Symbol(Sym.SEMICOLON);
case ',': return new Symbol(Sym.COMMA);
case '.': return new Symbol(Sym.DOT);
case '\u2026': return new Symbol(Sym.ELLIPSIS);
default:
throw new Error("Invalid separator.");
}
}
public String toString() {
return "Separator <"+which+">";
}
}
package de.in.tum.www2.java.internal.onefivelexer;
import java_cup.runtime.Symbol;
import de.in.tum.www2.java.internal.onefivelexer.Sym;
class StringLiteral extends Literal {
String val;
StringLiteral(String s) { this.val = s; }
Symbol token() { return new Symbol(Sym.STRING_LITERAL, val); }
public String toString() {
return "StringLiteral <"+Token.escape(val)+">";
}
}
package de.in.tum.www2.java.internal.onefivelexer;
public interface Sym {
/* terminals */
public static final int AT = 94;
public static final int SHORT = 4;
public static final int IDENTIFIER = 12;
public static final int ANDEQ = 91;
public static final int GT = 71;
public static final int IMPLEMENTS = 37;
public static final int CONST = 101;
public static final int STRICTFP = 103;
public static final int NOTEQ = 76;
public static final int PLUSEQ = 86;
public static final int ENUM = 106;
public static final int RBRACK = 11;
public static final int CATCH = 56;
public static final int COMMA = 16;
public static final int RBRACE = 18;
public static final int THROW = 54;
public static final int RPAREN = 21;
public static final int LBRACK = 10;
public static final int LT = 70;
public static final int ANDAND = 80;
public static final int OROR = 81;
public static final int DOUBLE = 9;
public static final int LBRACE = 17;
public static final int TRANSIENT = 33;
public static final int LPAREN = 20;
public static final int XOREQ = 92;
public static final int PROTECTED = 26;
public static final int INTEGER_LITERAL = 95;
public static final int NOT = 64;
public static final int FINAL = 30;
public static final int FLOAT = 8;
public static final int GOTO = 102;
public static final int URSHIFTEQ = 90;
public static final int PACKAGE = 23;
public static final int COMP = 63;
public static final int EQ = 19;
public static final int BOOLEAN_LITERAL = 97;
public static final int MOD = 66;
public static final int CLASS = 35;
public static final int SUPER = 41;
public static final int ABSTRACT = 29;
public static final int NATIVE = 31;
public static final int LONG = 6;
public static final int PLUS = 61;
public static final int QUESTION = 82;
public static final int WHILE = 49;
public static final int EXTENDS = 36;
public static final int INTERFACE = 42;
public static final int CHAR = 7;
public static final int BOOLEAN = 2;
public static final int SWITCH = 45;
public static final int DO = 48;
public static final int FOR = 50;
public static final int RSHIFTEQ = 89;
public static final int VOID = 38;
public static final int DIV = 65;
public static final int PUBLIC = 25;
public static final int RETURN = 53;
public static final int MULT = 15;
public static final int ELSE = 44;
public static final int TRY = 55;
public static final int GTEQ = 73;
public static final int BREAK = 51;
public static final int DOT = 13;
public static final int INT = 5;
public static final int NULL_LITERAL = 100;
public static final int THROWS = 39;
public static final int STRING_LITERAL = 99;
public static final int EQEQ = 75;
public static final int EOF = 0;
public static final int SEMICOLON = 14;
public static final int THIS = 40;
public static final int DEFAULT = 47;
public static final int MULTEQ = 83;
public static final int IMPORT = 24;
public static final int MINUS = 62;
public static final int LTEQ = 72;
public static final int OR = 79;
public static final int error = 1;
public static final int URSHIFT = 69;
public static final int SYNCHRONIZED = 32;
public static final int DIVEQ = 84;
public static final int LSHIFTEQ = 88;
public static final int FINALLY = 57;
public static final int CONTINUE = 52;
public static final int INSTANCEOF = 74;
public static final int IF = 43;
public static final int MODEQ = 85;
public static final int MINUSMINUS = 60;
public static final int ASSERT = 104;
public static final int COLON = 22;
public static final int CHARACTER_LITERAL = 98;
public static final int OREQ = 93;
public static final int VOLATILE = 34;
public static final int CASE = 46;
public static final int PLUSPLUS = 59;
public static final int NEW = 58;
public static final int RSHIFT = 68;
public static final int BYTE = 3;
public static final int AND = 77;
public static final int PRIVATE = 27;
public static final int ELLIPSIS = 105;
public static final int STATIC = 28;
public static final int LSHIFT = 67;