From e18aa1f9492c3537c0bb2546068b3c439f852a91 Mon Sep 17 00:00:00 2001 From: buraq Date: Fri, 19 Mar 2004 10:03:17 +0000 Subject: added a char[] sourcefile representation and ch... added a char[] sourcefile representation and changed related files --- sources/ch/epfl/lamp/util/AbstractSourceFile.java | 22 +++ sources/ch/epfl/lamp/util/Position.java | 10 +- sources/ch/epfl/lamp/util/SourceFile.java | 2 +- sources/ch/epfl/lamp/util/SourceFile2.java | 192 ++++++++++++++++++++++ 4 files changed, 220 insertions(+), 6 deletions(-) create mode 100644 sources/ch/epfl/lamp/util/AbstractSourceFile.java create mode 100644 sources/ch/epfl/lamp/util/SourceFile2.java (limited to 'sources/ch/epfl/lamp') diff --git a/sources/ch/epfl/lamp/util/AbstractSourceFile.java b/sources/ch/epfl/lamp/util/AbstractSourceFile.java new file mode 100644 index 0000000000..c14b0bc94e --- /dev/null +++ b/sources/ch/epfl/lamp/util/AbstractSourceFile.java @@ -0,0 +1,22 @@ +package ch.epfl.lamp.util; + +interface AbstractSourceFile { + /** Returns the name of this source file. */ + String name() ; + + /** Returns the short name (name without path) of this source file. */ + String getShortName() ; + + /** + * Returns an instance of Position representing the given line and + * column of this source file. + */ + Position getPosition( int line, int column ) ; + + /** Returns the content of the given line. */ + String getLine(int line) ; + + /** Returns the name of this source file. */ + String toString() ; + +} diff --git a/sources/ch/epfl/lamp/util/Position.java b/sources/ch/epfl/lamp/util/Position.java index 514dcf28eb..faae25f00b 100644 --- a/sources/ch/epfl/lamp/util/Position.java +++ b/sources/ch/epfl/lamp/util/Position.java @@ -87,7 +87,7 @@ public class Position { // Private Fields /** The position's file */ - private final SourceFile file; + private final AbstractSourceFile file; /** The position's line number */ private final int line; @@ -104,17 +104,17 @@ public class Position { } /** Initializes a new instance. */ - public Position(SourceFile file) { + public Position(AbstractSourceFile file) { this(file, 0, 0); } /** Initializes a new instance. */ - public Position(SourceFile file, int position) { + public Position(AbstractSourceFile file, int position) { this(file, line(position), column(position)); } /** Initializes a new instance. */ - public Position(SourceFile file, int line, int column) { + public Position(AbstractSourceFile file, int line, int column) { this.file = file; this.line = line; this.column = column; @@ -127,7 +127,7 @@ public class Position { // Public Methods /** Returns the file of this position. */ - public SourceFile file() { + public AbstractSourceFile file() { return file; } diff --git a/sources/ch/epfl/lamp/util/SourceFile.java b/sources/ch/epfl/lamp/util/SourceFile.java index adfa90be9e..6c85ad4651 100644 --- a/sources/ch/epfl/lamp/util/SourceFile.java +++ b/sources/ch/epfl/lamp/util/SourceFile.java @@ -18,7 +18,7 @@ import java.util.ArrayList; /** This class represents a single source file. */ -public class SourceFile { +public class SourceFile implements AbstractSourceFile { //######################################################################## // Public Constants diff --git a/sources/ch/epfl/lamp/util/SourceFile2.java b/sources/ch/epfl/lamp/util/SourceFile2.java new file mode 100644 index 0000000000..1f056dfef4 --- /dev/null +++ b/sources/ch/epfl/lamp/util/SourceFile2.java @@ -0,0 +1,192 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +** ** +** $Id$ +\* */ + +package ch.epfl.lamp.util; + +import java.io.File; +import java.io.InputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; + +import java.nio.*; +import java.nio.channels.*; +import java.nio.charset.*; + +/** This class represents a single source file. + */ +public class SourceFile2 implements AbstractSourceFile { + + //######################################################################## + // Public Constants + + /** Constants used for source parsing */ + public static final char LF = 0x0A; + public static final char FF = 0x0C; + public static final char CR = 0x0D; + public static final char SU = 0x1A; + + //######################################################################## + // Private Fields + + /** The name of this source file */ + private final String name; + + /** The content of source this file */ + private final char[] chars; + + /** The encoding of this source file or null if unspecified */ + private String encoding = "ISO-8859-1"; + + /** The position of the last line returned by getLine */ + private int lineNumber = 0; + private int lineStart = 0; + private int lineLength = 0; + private int nextIndex = 0; + + //######################################################################## + // Public Constructors + + /** Initializes a new instance. */ + public SourceFile2(String name, String encoding) + throws IOException { + this(new File(name), encoding); + } + + /** Initializes a new instance. */ + public SourceFile2(File name, String encoding) + throws IOException { + this(name.toString(), read(name, encoding)); + } + + /** Initializes a new instance. */ + public SourceFile2(String name, InputStream input, String encoding) + throws IOException { + this(name, read(name, input, encoding)); + } + + /** Initializes a new instance. */ + public SourceFile2(String name, char[] inp) { + this.name = name; + this.chars = normalize( inp ); + } + + //######################################################################## + // Public Methods + + /** Returns the name of this source file. */ + public String name() { + return name; + } + + /** Returns the content of this source file. */ + public char[] chars() { + return chars; + } + + /** Returns the short name (name without path) of this source file. */ + public String getShortName() { + int start = name.lastIndexOf( File.separatorChar ); + return start < 0 ? name : name.substring( start + 1 ); + } + + /** + * Returns an instance of Position representing the given line and + * column of this source file. + */ + public Position getPosition( int line, int column ) { + return new Position( this, line, column ); + } + + /** Returns the content of the given line. */ + public String getLine(int line) { + int index = lineNumber <= line ? nextIndex : (lineNumber = 0); + for (; index < chars.length && lineNumber < line; lineNumber++) { + lineStart = index; + for (; index < chars.length; index++) { + if (chars[ index ] == CR) break; + if (chars[ index ] == LF) break; + if (chars[ index ] == FF) break; + } + lineLength = index - lineStart; + if (index < chars.length) + index++; + if (index < chars.length) + if (chars[index - 1] == CR && chars[index] == LF) index++; + } + nextIndex = index; + return new String(chars, lineStart, lineLength); + } + + /** Returns the name of this source file. */ + public String toString() { + return name; + } + + //######################################################################## + // Private Methods + + /** Reads the file and returns its content as a byte array. + */ + private static char[] read(File file, String encoding) throws IOException { + FileInputStream input = new FileInputStream( file ); + try { + return read( input, encoding ); + } finally { + input.close(); + } + } + + private static char[] read(ByteBuffer bb, String encoding) + throws IOException { + try { + CharsetDecoder chd = Charset.forName( encoding ).newDecoder(); + CharBuffer cb = chd.decode( bb ); + if( cb.hasArray() ) + return cb.array(); + else { + char[] cs = new char[ cb.length() ]; + for(int i = 0; cb.hasRemaining(); i++ ) { + cs[i] = cb.get(); + } + return cs; + } + } catch (CharacterCodingException e) { + throw new IOException("error in decoding:"+e.getMessage()); + } + } + + private static char[] read(FileInputStream input, String encoding) + throws IOException { + return read( input + .getChannel() + .map( FileChannel.MapMode.READ_ONLY, 0, input.available()), + encoding ); + } + + /** Reads the InputStream and returns its content as a byte array. + */ + private static char[] read(String name, InputStream input, String encoding) throws IOException { + ByteBuffer bb = ByteBuffer.allocate( input.available() ); + Channels.newChannel( input ).read( bb ); + return read( bb, encoding ); + } + + /** Ensures that the last byte of the array is SU. */ + private static char[] normalize( char[] input ) { + if ( input.length > 0 && input[ input.length - 1 ] == SU ) + return input; + char[] cs = new char[ input.length + 1 ]; + System.arraycopy( input, 0, cs, 0, input.length ); + cs[ input.length ] = SU; + return cs; + } + + //######################################################################## +} -- cgit v1.2.3