CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-antlr--antlr-runtime

Java runtime library for ANTLR v3 - a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions.

Pending
Overview
Eval results
Files

character-streams.mddocs/

Character Input Streams

Character stream implementations for reading input from various sources including strings, files, readers, and input streams. These classes form the foundation of the ANTLR lexical analysis pipeline.

Capabilities

CharStream Interface

Base interface for all character input streams providing lookahead and position tracking.

/**
 * A source of characters for an ANTLR lexer
 */
public interface CharStream extends IntStream {
    public static final int EOF = -1;
    
    /**
     * For infinite streams, you don't need this; primarily I'm providing
     * a useful interface for action code. Just make sure actions don't
     * use this on streams that don't support it.
     */
    public String substring(int start, int stop);
    
    /**
     * Get the ith character of lookahead. This is the same usually as
     * LA(i). This will be used for labels in the generated
     * lexer code. I'd prefer to return a char here type-wise, but it's
     * probably better to be 32-bit clean and be consistent with LA.
     */
    public int LT(int i);
    
    /** ANTLR tracks the line information automatically */
    int getLine();
    
    /** Because this stream can rewind, we need to be able to reset the line */
    void setLine(int line);
    
    void setCharPositionInLine(int pos);
    
    /** The index of the character relative to the beginning of the line 0..n-1 */
    int getCharPositionInLine();
}

String Input Stream

Read characters directly from a String in memory.

/**
 * Character stream reading from a String
 */
public class ANTLRStringStream implements CharStream {
    protected String data;
    protected int n;
    protected int p;
    protected int line;
    protected int charPositionInLine;
    protected int markDepth;
    protected List<CharStreamState> markers;
    protected int lastMarker;
    protected String name;
    
    public ANTLRStringStream();
    public ANTLRStringStream(String input);
    public ANTLRStringStream(String input, String sourceName);
    public ANTLRStringStream(char[] data, int numberOfActualCharsInArray);
    public ANTLRStringStream(char[] data, int numberOfActualCharsInArray, String sourceName);
    
    public void reset();
    public String substring(int start, int stop);
    public int LT(int i);
    public int getLine();
    public void setLine(int line);
    public void setCharPositionInLine(int pos);
    public int getCharPositionInLine();
    public void consume();
    public int LA(int i);
    public int mark();
    public void rewind(int m);
    public void rewind();
    public void release(int marker);
    public void seek(int index);
    public int size();
    public int index();
    public String getSourceName();
}

Usage Examples:

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;

// Create from string
CharStream input = new ANTLRStringStream("hello world");

// Create with source name 
CharStream input2 = new ANTLRStringStream("x = 42;", "example.txt");

// Create from character array
char[] chars = {'a', 'b', 'c'};
CharStream input3 = new ANTLRStringStream(chars, 3);

// Basic usage
System.out.println("First char: " + (char)input.LA(1));
System.out.println("Line: " + input.getLine());
System.out.println("Position: " + input.getCharPositionInLine());

File Input Stream

Read characters from a file on disk with automatic encoding handling.

/**
 * Character stream reading from a file
 */
public class ANTLRFileStream extends ANTLRStringStream {
    protected String fileName;
    
    public ANTLRFileStream(String fileName) throws IOException;
    public ANTLRFileStream(String fileName, String encoding) throws IOException;
    
    public void load(String fileName, String encoding) throws IOException;
    public String getSourceName();
}

Usage Examples:

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import java.io.IOException;

// Read file with default encoding
try {
    CharStream input = new ANTLRFileStream("input.txt");
    System.out.println("File size: " + input.size());
} catch (IOException e) {
    System.err.println("Cannot read file: " + e.getMessage());
}

// Read file with specific encoding
try {
    CharStream input = new ANTLRFileStream("input.txt", "UTF-8");
    System.out.println("Source: " + input.getSourceName());
} catch (IOException e) {
    System.err.println("Cannot read file: " + e.getMessage());
}

InputStream Character Stream

Read characters from any Java InputStream with encoding support.

/**
 * Character stream reading from InputStream
 */
public class ANTLRInputStream extends ANTLRStringStream {
    public ANTLRInputStream();
    public ANTLRInputStream(InputStream input) throws IOException;
    public ANTLRInputStream(InputStream input, String encoding) throws IOException;
    public ANTLRInputStream(InputStream input, int size) throws IOException;
    public ANTLRInputStream(InputStream input, int size, String encoding) throws IOException;
    public ANTLRInputStream(InputStream input, int size, int readBufferSize) throws IOException;
    public ANTLRInputStream(InputStream input, int size, int readBufferSize, String encoding) throws IOException;
    
    public void load(InputStream input, int size, String encoding) throws IOException;
}

Usage Examples:

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CharStream;
import java.io.*;

// Read from standard input
try {
    CharStream input = new ANTLRInputStream(System.in);
    // Process input...
} catch (IOException e) {
    System.err.println("Cannot read from stdin: " + e.getMessage());
}

// Read from file input stream with encoding
try {
    FileInputStream fis = new FileInputStream("data.xml");
    CharStream input = new ANTLRInputStream(fis, "UTF-8");
    // Process input...
    fis.close();
} catch (IOException e) {
    System.err.println("Cannot read file: " + e.getMessage());
}

// Read with buffer size control
try {
    FileInputStream fis = new FileInputStream("large-file.txt");
    CharStream input = new ANTLRInputStream(fis, 8192, 1024, "UTF-8");
    // Process input...
    fis.close();
} catch (IOException e) {
    System.err.println("Cannot read file: " + e.getMessage());
}

Reader Character Stream

Read characters from any Java Reader with automatic buffer management.

/**
 * Character stream reading from Reader
 */
public class ANTLRReaderStream extends ANTLRStringStream {
    public ANTLRReaderStream();
    public ANTLRReaderStream(Reader r) throws IOException;
    public ANTLRReaderStream(Reader r, int size) throws IOException;
    public ANTLRReaderStream(Reader r, int size, int readChunkSize) throws IOException;
    
    public void load(Reader r, int size, int readChunkSize) throws IOException;
}

Usage Examples:

import org.antlr.runtime.ANTLRReaderStream;
import org.antlr.runtime.CharStream;
import java.io.*;

// Read from StringReader
StringReader sr = new StringReader("sample input text");
try {
    CharStream input = new ANTLRReaderStream(sr);
    // Process input...
} catch (IOException e) {
    System.err.println("Cannot read: " + e.getMessage());
}

// Read from FileReader with buffer control
try {
    FileReader fr = new FileReader("input.txt");
    CharStream input = new ANTLRReaderStream(fr, 4096, 512);
    // Process input...
    fr.close();
} catch (IOException e) {
    System.err.println("Cannot read file: " + e.getMessage());
}

Types

Character Stream State

public class CharStreamState {
    int p;
    int line;
    int charPositionInLine;
}

Common Patterns

Error Handling

try {
    CharStream input = new ANTLRFileStream("input.txt");
    // Use the stream...
} catch (IOException e) {
    System.err.println("Failed to read input: " + e.getMessage());
    // Handle error appropriately
}

Stream Positioning

CharStream input = new ANTLRStringStream("hello world");

// Mark current position
int marker = input.mark();

// Consume some characters
input.consume(); // 'h'
input.consume(); // 'e'

// Look ahead
int nextChar = input.LA(1); // 'l'

// Rewind to marked position
input.rewind(marker);

// Or rewind to last mark
input.rewind();

Substring Extraction

CharStream input = new ANTLRStringStream("hello world");

// Extract substring (inclusive start, exclusive stop)
String hello = input.substring(0, 5); // "hello"
String world = input.substring(6, 11); // "world"

Install with Tessl CLI

npx tessl i tessl/maven-org-antlr--antlr-runtime

docs

character-streams.md

debug-support.md

error-handling.md

index.md

lexical-analysis.md

parsing.md

token-streams.md

tree-construction.md

tile.json