2003/06/16 06:24:31
[org.ibex.core.git] / src / org / xwt / js / Lexer.java
index f7039f4..eb3f69a 100644 (file)
@@ -17,6 +17,8 @@
  * Contributor(s): Roger Lawrence, Mike McCabe
  */
 
+// FIXME: mark lots of these methods 'final' so they get inlined
+
 package org.xwt.js;
 import java.io.*;
 
@@ -25,13 +27,16 @@ class Lexer implements Tokens {
 
     /** for debugging */
     public static void main(String[] s) throws Exception {
-       Lexer l = new Lexer(new InputStreamReader(System.in));
+       Lexer l = new Lexer(new InputStreamReader(System.in), "stdin", 0);
        int tok = 0;
        while((tok = l.getToken()) != -1) System.out.println(codeToString[tok]);
     }
 
     /** the token that was just parsed */
     protected int op;
+   /** the most recently parsed token, <i>regardless of pushbacks</i> */
+    protected int mostRecentlyReadToken;
 
     /** if the token just parsed was a NUMBER, this is the numeric value */
     protected Number number = null;
@@ -39,17 +44,25 @@ class Lexer implements Tokens {
     /** if the token just parsed was a NAME or STRING, this is the string value */
     protected String string = null;
 
-    /** the line number of the current token */
-    protected int line = 0;
+    /** the line number of the most recently <i>lexed</i> token */
+    private int line = 0;
+
+    /** the line number of the most recently <i>parsed</i> token */
+    protected int parserLine = 0;
 
     /** the column number of the current token */
     protected int col = 0;
 
     /** the name of the source code file being lexed */
-    protected String sourceName = "unknown";
+    protected String sourceName;
 
     private SmartReader in;
-    public Lexer(Reader r) throws IOException { in = new SmartReader(r); }
+    public Lexer(Reader r, String sourceName, int line) throws IOException {
+       this.sourceName = sourceName;
+       this.line = line;
+       this.parserLine = line;
+       in = new SmartReader(r);
+    }
 
 
     // Predicates ///////////////////////////////////////////////////////////////////////
@@ -343,7 +356,7 @@ class Lexer implements Tokens {
             lastread = reader.read();
             if (accumulator != null) accumulator.append((char)lastread);
            if (lastread != '\n' && lastread != '\r') col++;
-           if (lastread == '\n') { line++; col = 0; }
+           if (lastread == '\n') { parserLine = ++line; col = 0; }
             return lastread;
         }
 
@@ -396,7 +409,10 @@ class Lexer implements Tokens {
     public int getToken() throws IOException {
        number = null;
        string = null;
-       if (pushBackDepth == 0) return op = _getToken();
+       if (pushBackDepth == 0) {
+           mostRecentlyReadToken = op;
+           return op = _getToken();
+       }
        pushBackDepth--;
        op = pushBackInts[pushBackDepth];
        if (pushBackObjects[pushBackDepth] != null) {