module watt.text.source

A class for dealing with a file from the perspective of a compiler.

Code Map

//! A class for dealing with a file from the perspective of a compiler.
module watt.text.source;


//! A container for iterating over UTF-8 source code.
class Source
{
public:
	alias eof = empty;


public:
	//! The location of the current character front.
	loc: Location;


public:
	//! Sets this Source to s and the current location and validate it as a
	//! utf8 source.
	this(s: string, filename: string) { }
	//! Have we reached EOF, if we have current = dchar.default.
	fn empty() dchar { }
	//! Returns the current utf8 char.
	fn front() dchar { }
	//! Returns the following utf8 char after front.
	fn following() dchar { }
	//! Advance the source one character.
	fn popFront() { }
	//! Advance the source n character.
	fn popFrontN(n: size_t) { }
	//! Used to skip whitespace in the source file, as defined by
	//! watt.text.ascii.isWhite.
	fn skipWhitespace() { }
	//! Skips till character after next end of line or eof.
	fn skipEndOfLine() { }
	//! Return the unicode character n chars forwards. lookaheadEOF set to true
	//! if we reached EOF, otherwise false.
	fn lookahead(n: size_t, lookaheadEOF: bool) dchar { }
	//! Return the index of the current character.
	fn save() size_t { }
	//! Slices the source from the given mark to (but not including) the
	//! current character. Use save for indicies.
	fn sliceFrom(mark: size_t) string { }
	//! Slice the source from a to b.
	fn slice(a: size_t, b: size_t) string { }
}

//! A simple container for iterating over UTF-8 source code.
struct SimpleSource
{
public:
	alias eof = empty;


public:
	//! Have we reached EOF, if we have front = dchar.default.
	empty: bool;


public:
	//! Setup this simple source and return the full source.
	fn source(src: string) string { }
	//! Return the full source.
	fn source() string { }
	//! Returns the current utf8 char.
	fn front() dchar { }
	//! Returns the following utf8 char after front.
	fn following() dchar { }
	//! Advance the source one character.
	fn popFront() { }
	//! Advance the source n character.
	fn popFrontN(n: size_t) { }
	//! Return the unicode character n chars forwards. lookaheadEOF set to true
	//! if we reached EOF, otherwise false.
	fn lookahead(n: size_t, lookaheadEmpty: bool) dchar { }
	//! Used to skip whitespace in the source file, as defined by
	//! watt.text.ascii.isWhite.
	fn skipWhitespace() { }
	//! Return the index of the current character.
	fn save() size_t { }
	//! Slices the source from the given mark to (but not including) the
	//! current character. Use save for indicies.
	fn sliceFrom(mark: size_t) string { }
	//! Slices two points.
	fn slice(a: size_t, b: size_t) string { }
	//! Decodes a single utf8 code point at index in the given source.
	fn decodeChar(index: size_t) dchar { }
}

//! Struct representing a location in a source file.
struct Location
{
public:
	//! The file from pointed to this locatiom.
	filename: string;
	//! Line number starting at 1.
	line: size_t;
	//! Column starting at 1.
	column: size_t;
	//! Length in characers.
	length: size_t;


public:
	//! Format into a location string.
	fn toString() string { }
	//! Difference between two locations. end - begin == begin ... end
	fn opSub(begin: Location) Location { }
	//! See difference.
	fn spanTo(end: Location) { }


public:
	//! Difference between two locations. end - begin == begin ... end
	static fn difference(end: Location, begin: Location, _default: Location) Location { }
}
class Source

A container for iterating over UTF-8 source code.

Assumes the given source is valid UTF-8.

loc: Location

The location of the current character front.

alias eof

See also

  • empty.

this(s: string, filename: string)

Sets this Source to s and the current location and validate it as a utf8 source.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • Puts all the other fields into known good states.

fn empty() dchar

Have we reached EOF, if we have current = dchar.default.

fn front() dchar

Returns the current utf8 char.

Side-Effects

  • None.

fn following() dchar

Returns the following utf8 char after front.

Side-Effects

  • None.

fn popFront()

Advance the source one character.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • eof set to true if we have reached the EOF.

  • mSrc.mChar is set to the returned character if not at EOF.

  • mSrc.mNextIndex advanced to the end of the given character.

  • mSrc.mLastIndex points to the index of the current character.

fn popFrontN(n: size_t)

Advance the source n character.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • eof set to true if we have reached the EOF.

  • mSrc.mChar is set to the current character if not at EOF.

  • mSrc.mNextIndex advanced to the end of the given character.

  • mSrc.mLastIndex points to the index of the current character.

fn skipWhitespace()

Used to skip whitespace in the source file, as defined by watt.text.ascii.isWhite.

Side-Effects

fn skipEndOfLine()

Skips till character after next end of line or eof.

Side-Effects

fn lookahead(n: size_t, lookaheadEOF: bool) dchar

Return the unicode character n chars forwards. lookaheadEOF set to true if we reached EOF, otherwise false.

If n is 0, this is the same as calling front.

Return

Unicode char at n or dchar.default at EOF.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • None.

fn save() size_t

Return the index of the current character.

Side-Effects

  • None.

fn sliceFrom(mark: size_t) string

Slices the source from the given mark to (but not including) the current character. Use save for indicies.

Side-Effects

  • None.

fn slice(a: size_t, b: size_t) string

Slice the source from a to b.

struct SimpleSource

A simple container for iterating over UTF-8 source code.

Assumes the given source is valid UTF-8.

alias eof

See also

  • empty.

empty: bool

Have we reached EOF, if we have front = dchar.default.

fn source(src: string) string

Setup this simple source and return the full source.

fn source() string

Return the full source.

fn front() dchar

Returns the current utf8 char.

Side-Effects

  • None.

fn following() dchar

Returns the following utf8 char after front.

Side-Effects

  • None.

fn popFront()

Advance the source one character.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • eof set to true if we have reached the EOF.

  • mChar is set to the current character if not at EOF.

  • mNextIndex advanced to the end of the given character.

  • mLastIndex points to the index of the current character.

fn popFrontN(n: size_t)

Advance the source n character.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • eof set to true if we have reached the EOF.

  • mChar is set to the current character if not at EOF.

  • mNextIndex advanced to the end of the given character.

  • mLastIndex points to the index of the current character.

fn lookahead(n: size_t, lookaheadEmpty: bool) dchar

Return the unicode character n chars forwards. lookaheadEOF set to true if we reached EOF, otherwise false.

If n is 0, this is the same as calling front.

Return

Unicode char at n or dchar.default at empty.

Throws

  • UtfException if the source is not valid utf8.

Side-Effects

  • None.

fn skipWhitespace()

Used to skip whitespace in the source file, as defined by watt.text.ascii.isWhite.

Side-Effects

fn save() size_t

Return the index of the current character.

Side-Effects

  • None.

fn sliceFrom(mark: size_t) string

Slices the source from the given mark to (but not including) the current character. Use save for indicies.

Side-Effects

  • None.

fn slice(a: size_t, b: size_t) string

Slices two points.

fn decodeChar(index: size_t) dchar

Decodes a single utf8 code point at index in the given source.

Return

Unicode char at index or dchar.default if out of bound.

Side-Effects

  • None.

struct Location

Struct representing a location in a source file.

Useful for error messages.

filename: string

The file from pointed to this locatiom.

line: size_t

Line number starting at 1.

column: size_t

Column starting at 1.

length: size_t

Length in characers.

fn toString() string

Format into a location string.

fn opSub(begin: Location) Location

Difference between two locations. end - begin == begin ... end

See also

  • watt.text.source.Location.difference

fn difference(end: Location, begin: Location, _default: Location) Location

Difference between two locations. end - begin == begin ... end

On mismatch of filename or if begin is after end _default is returned.

fn spanTo(end: Location)