changeset 32:bdd1c8d6b560

Do not read EOF more than once, report unexpected EOF in function data.
author Mikhail Kryshen <mikhail@kryshen.net>
date Fri, 25 Sep 2009 02:37:16 +0400
parents 3e77076621a8
children b637a4491862
files nbproject/ide-targets.xml nbproject/project.xml src/kryshen/tema/FunctionDataParser.java src/kryshen/tema/TemplateParser.java src/kryshen/tema/io/TemplateReader.java
diffstat 5 files changed, 132 insertions(+), 24 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nbproject/ide-targets.xml	Fri Sep 25 02:37:16 2009 +0400
     1.3 @@ -0,0 +1,23 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project basedir=".." name="tema-IDE">
     1.6 +    <import file="../build.xml"/>
     1.7 +    <!-- TODO: edit the following target according to your needs -->
     1.8 +    <!-- (more info: http://www.netbeans.org/kb/articles/freeform-config.html#debugj2se) -->
     1.9 +    <target depends="compile" name="debug-nb">
    1.10 +        <nbjpdastart addressproperty="jpda.address" name="tema" transport="dt_socket">
    1.11 +            <classpath>
    1.12 +                <pathelement location="${build}"/>
    1.13 +                <path refid="project.libs"/>
    1.14 +            </classpath>
    1.15 +        </nbjpdastart>
    1.16 +        <java classname="${main_class}" fork="true">
    1.17 +            <classpath>
    1.18 +                <pathelement location="${build}"/>
    1.19 +                <path refid="project.libs"/>
    1.20 +            </classpath>
    1.21 +            <arg value="-demo"/>
    1.22 +            <jvmarg value="-Xdebug"/>
    1.23 +            <jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}"/>
    1.24 +        </java>
    1.25 +    </target>
    1.26 +</project>
     2.1 --- a/nbproject/project.xml	Sun Aug 30 02:51:15 2009 +0400
     2.2 +++ b/nbproject/project.xml	Fri Sep 25 02:37:16 2009 +0400
     2.3 @@ -35,6 +35,10 @@
     2.4                  <action name="run">
     2.5                      <target>run.demo</target>
     2.6                  </action>
     2.7 +                <action name="debug">
     2.8 +                    <script>nbproject/ide-targets.xml</script>
     2.9 +                    <target>debug-nb</target>
    2.10 +                </action>
    2.11              </ide-actions>
    2.12              <export>
    2.13                  <type>folder</type>
    2.14 @@ -65,6 +69,7 @@
    2.15                          <label>Compile manual</label>
    2.16                          <target>doc.manual</target>
    2.17                      </action>
    2.18 +                    <ide-action name="debug"/>
    2.19                  </context-menu>
    2.20              </view>
    2.21              <subprojects/>
     3.1 --- a/src/kryshen/tema/FunctionDataParser.java	Sun Aug 30 02:51:15 2009 +0400
     3.2 +++ b/src/kryshen/tema/FunctionDataParser.java	Fri Sep 25 02:37:16 2009 +0400
     3.3 @@ -103,7 +103,12 @@
     3.4          } else {
     3.5              r = tp.parse(in, out, fd, null);
     3.6          }
     3.7 -        
     3.8 +
     3.9 +        if (r.terminator == Terminator.EOF) {
    3.10 +            throw new TemplateException
    3.11 +                    ("Unexpected end of instruction data", in);
    3.12 +        }
    3.13 +
    3.14          if (r.terminator != Terminator.SEPARATOR)
    3.15              available = false; // No more function data available.
    3.16          
     4.1 --- a/src/kryshen/tema/TemplateParser.java	Sun Aug 30 02:51:15 2009 +0400
     4.2 +++ b/src/kryshen/tema/TemplateParser.java	Fri Sep 25 02:37:16 2009 +0400
     4.3 @@ -271,9 +271,7 @@
     4.4              int c = in.read();
     4.5              
     4.6              if (c != s.charAt(k)) {
     4.7 -                if (c >= 0) {
     4.8 -                    in.unread(c);
     4.9 -                }
    4.10 +                in.unread(c);
    4.11                  
    4.12                  for (int j = k - 1; j >= 0; j--) {
    4.13                      in.unread(s.charAt(j));
    4.14 @@ -290,10 +288,7 @@
    4.15          readLoop:
    4.16              while (true) {
    4.17                  int c = in.read();
    4.18 -                
    4.19 -                if (c < 0)
    4.20 -                    break;
    4.21 -                
    4.22 +                              
    4.23                  for (int i = 0; i < chars.length; i++) {
    4.24                      if (c == chars[i])
    4.25                          continue readLoop;
    4.26 @@ -307,11 +302,8 @@
    4.27      private void skipEscaped(TemplateReader in) throws IOException {
    4.28          if (matchInput(in, ESCAPE_WHITESPACE)) {
    4.29              int c = in.read();
    4.30 -            
    4.31 -            if (c < 0)
    4.32 -                return;
    4.33 -            
    4.34 -            if (!Character.isWhitespace(c)) {
    4.35 +                       
    4.36 +            if (c < 0 || !Character.isWhitespace(c)) {
    4.37                  in.unread(c);
    4.38                  in.unread(ESCAPE_WHITESPACE);
    4.39                  return;
    4.40 @@ -319,10 +311,9 @@
    4.41              
    4.42              do {
    4.43                  c = in.read();
    4.44 -            } while (Character.isWhitespace(c));
    4.45 +            } while (c > 0 && Character.isWhitespace(c));
    4.46              
    4.47 -            if (c >= 0)
    4.48 -                in.unread(c);
    4.49 +            in.unread(c);
    4.50              
    4.51              return;
    4.52          }
     5.1 --- a/src/kryshen/tema/io/TemplateReader.java	Sun Aug 30 02:51:15 2009 +0400
     5.2 +++ b/src/kryshen/tema/io/TemplateReader.java	Fri Sep 25 02:37:16 2009 +0400
     5.3 @@ -20,10 +20,10 @@
     5.4  
     5.5  package kryshen.tema.io;
     5.6  
     5.7 +import java.io.FilterReader;
     5.8  import java.io.IOException;
     5.9  import java.io.Reader;
    5.10  import java.io.LineNumberReader;
    5.11 -import java.io.PushbackReader;
    5.12  import kryshen.tema.TemplateParser;
    5.13  
    5.14  /**
    5.15 @@ -32,7 +32,7 @@
    5.16   *
    5.17   * @author Mikhail Kryshen
    5.18   */
    5.19 -public class TemplateReader extends PushbackReader {
    5.20 +public class TemplateReader extends FilterReader {
    5.21      static final int UNREAD_BUFFER_SIZE;
    5.22      
    5.23      /* Calculate UNREAD_BUFFER_SIZE value. */
    5.24 @@ -50,13 +50,16 @@
    5.25          max = Math.max(max, TemplateParser.ESCAPE_NEWLINE.length() + 2);
    5.26          max = Math.max(max, TemplateParser.ESCAPE_WHITESPACE.length());
    5.27          
    5.28 -        UNREAD_BUFFER_SIZE = max;
    5.29 +        UNREAD_BUFFER_SIZE = max + 1;
    5.30      }
    5.31 -    
    5.32 +
    5.33      private final String source;
    5.34      private final LineNumberReader lnReader;
    5.35      private final TemplateReader parentReader;
    5.36 -    
    5.37 +
    5.38 +    private final int[] unreadBuffer = new int[UNREAD_BUFFER_SIZE];
    5.39 +    private int unread = 0;
    5.40 +
    5.41      public TemplateReader(Reader in) {
    5.42          this(new LineNumberReader(in));
    5.43      }
    5.44 @@ -70,7 +73,7 @@
    5.45      }
    5.46      
    5.47      public TemplateReader(LineNumberReader in, String source) {
    5.48 -        super(in, UNREAD_BUFFER_SIZE);
    5.49 +        super(in);
    5.50          
    5.51          this.parentReader = null;
    5.52          this.lnReader = in;
    5.53 @@ -78,7 +81,7 @@
    5.54      }
    5.55      
    5.56      public TemplateReader(Reader in, TemplateReader parent) {
    5.57 -        super(in, UNREAD_BUFFER_SIZE);
    5.58 +        super(in);
    5.59          
    5.60          this.parentReader = parent;
    5.61          this.lnReader = null;
    5.62 @@ -98,8 +101,89 @@
    5.63          
    5.64          return parentReader.getLineNumber();
    5.65      }
    5.66 -    
    5.67 +
    5.68 +    @Override
    5.69 +    public int read() throws IOException {
    5.70 +        if (unread == 0) {
    5.71 +            return super.read();
    5.72 +        }
    5.73 +
    5.74 +        return unreadBuffer[--unread];
    5.75 +    }
    5.76 +
    5.77 +    @Override
    5.78 +    public int read(char[] cbuf, int off, int len) throws IOException {
    5.79 +        int n = 0;
    5.80 +
    5.81 +        while (unread > 0) {
    5.82 +            cbuf[off + n++] = (char) unreadBuffer[--unread];
    5.83 +
    5.84 +            if (n == len) {
    5.85 +                return n;
    5.86 +            }
    5.87 +        }
    5.88 +
    5.89 +        return n + super.read(cbuf, off + n, len - n);
    5.90 +    }
    5.91 +
    5.92 +    @Override
    5.93 +    public long skip(long n) throws IOException {
    5.94 +        if (n <= unread) {
    5.95 +            unread -= n;
    5.96 +            return n;
    5.97 +        }
    5.98 +
    5.99 +        n -= unread;
   5.100 +        long ret = super.skip(n) + unread;
   5.101 +        unread = 0;
   5.102 +
   5.103 +        return ret;
   5.104 +    }
   5.105 +
   5.106 +
   5.107      public void unread(String s) throws IOException {
   5.108          unread(s.toCharArray());
   5.109      }
   5.110 +
   5.111 +    public void unread(int c) {
   5.112 +        if (unread + 1 > UNREAD_BUFFER_SIZE) {
   5.113 +            throw new IndexOutOfBoundsException();
   5.114 +        }
   5.115 +
   5.116 +        unreadBuffer[unread++] = c;
   5.117 +    }
   5.118 +
   5.119 +    public void unread(char[] buff) {
   5.120 +        unread(buff, 0, buff.length);
   5.121 +    }
   5.122 +
   5.123 +    public void unread(char[] buff, int off, int len) {
   5.124 +        if (unread + len > UNREAD_BUFFER_SIZE) {
   5.125 +            throw new IndexOutOfBoundsException();
   5.126 +        }
   5.127 +
   5.128 +        for (int i = off + len - 1; i >= off; i--) {
   5.129 +            unreadBuffer[unread++] = buff[i];
   5.130 +        }
   5.131 +    }
   5.132 +
   5.133 +    @Override
   5.134 +    public boolean ready() throws IOException {
   5.135 +        return unread > 0 || super.ready();
   5.136 +    }
   5.137 +
   5.138 +    @Override
   5.139 +    public void mark(int readAheadLimit) throws IOException {
   5.140 +        throw new IOException("Not supported");
   5.141 +    }
   5.142 +
   5.143 +    @Override
   5.144 +    public void reset() throws IOException {
   5.145 +        throw new IOException("Not supported");
   5.146 +    }
   5.147 +
   5.148 +    @Override
   5.149 +    public boolean markSupported() {
   5.150 +        return false;
   5.151 +    }
   5.152  }