Go to repository
Abstract

Class AbstractDocumentSourcePosition

An AbstractDocumentSourcePosition points to a particular position, that is well known, and belongs to a particular document. That implies that this position is different than EndOfInput.

There are two known cases for a position in a document. The first case is is when the source position is pointing to any character in the document, defined by the subclass DocumentSourcePosition. The second case is when the position is the last element of the document, that is, the EndOfDocument, which is a special case of position, just after the last character of the document has been reached, which is implemented by the class EndOfDocumentSourcePosition. Additionally the property isEndOfDocument may be used to check which case we are in, which is redefined in each subclass properly.

There are additional operation that the instances of this subclass can answer, such as inquiring for the document name, and the contents of the document.

Among the different operations provided in this class there are a few operations used to determine context of the source input surrounding the current position: documentContextBefore, and documentContextAfter. The implementation of all these is achieved by a Template Method Pattern to provide a common validation and different logics depending on the actual subclass. Protected methods /SourcePositions.AbstractKnownSourcePosition._documentContextBefore | _documentContextBefore , and /SourcePositions.AbstractKnownSourcePosition._documentContextAfter | _documentContextAfter , are used to implement the aforementioned pattern.

Hierarchy (view full)

Constructors

Returns a document source position belonging to some SourceReader. It is intended to be used only by SourceReader.

PRECONDITIONS: (not verified during execution)

  • all numbers are >= 0
  • numbers are consistent with the reader state

Parameters

  • sourceReader: SourceReader

    The SourceReader of the input this position belongs to.

  • line: number

    The line number of this position in the current input. It will be modified only by the constructor. INVARIANT: line >=1, and it is a valid line in that reader.

  • column: number

    The column number of this position in the current input. It will be modified only by the constructor. INVARIANT: column >= 1 and it is a valid column in that reader.

  • regions: string[]

    The regions the position in the current input belongs to. It will be modified only by the constructor. INVARIANT: the regions are valid in the position's reader.

  • _documentIndex: number

    The index with information about the input document in the _sourceReader. INVARIANT: documentIndex >= 0 and it is a valid index in that reader.

  • _charIndex: number

    The index with information about the exact char pointed to by this position in the input document. INVARIANT: charIndex >= 0 and it is a valid index in that reader.

  • _visibleCharIndex: number

    The index with information about the exact char pointed to by this position in the visible input document. INVARIANT: visibleCharIndex >= 0 and it is a valid index in that reader.

Returns AbstractDocumentSourcePosition

Properties

Readonly
_charIndex: number

The index with information about the exact char pointed to by this position in the input document. INVARIANT: charIndex >= 0 and it is a valid index in that reader.

Readonly
_documentIndex: number

The index with information about the input document in the _sourceReader. INVARIANT: documentIndex >= 0 and it is a valid index in that reader.

Readonly
_visibleCharIndex: number

The index with information about the exact char pointed to by this position in the visible input document. INVARIANT: visibleCharIndex >= 0 and it is a valid index in that reader.

Readonly
column: number

The column number of this position in the current input. It will be modified only by the constructor. INVARIANT: column >= 1 and it is a valid column in that reader.

Readonly
isEndOfInput: false = false

Answers if this position corresponds to the end of input of the SourceReader it belongs, or not. The EndOfInput is reached when all documents in the source input has been processed.

PRECONDITIONS:

  • the position is known
Readonly
isUnknown: false = false

Answers if this position correspond to one in some SourceReader, or if it is unknown.

Readonly
line: number

The line number of this position in the current input. It will be modified only by the constructor. INVARIANT: line >=1, and it is a valid line in that reader.

Readonly
regions: string[]

The regions the position in the current input belongs to. It will be modified only by the constructor. INVARIANT: the regions are valid in the position's reader.

Readonly
sourceReader: SourceReader

The SourceReader of the input this position belongs to.

Accessors

get documentName(): string

The name of the input document this position belongs to.

PRECONDITIONS:

  • the position is not unknown
  • the position is not at the end of input

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the position is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the position is at the end of input.

get fullDocumentContents(): string

The contents of the input document this position belongs to (both visible and non visible).

PRECONDITIONS:

  • the position is not unknown
  • the position is not at the end of input

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the receiver is the end of input.

Abstract
get isEndOfDocument(): boolean

Answers if this position corresponds to the end of a document in the SourceReader it belongs, or not. The EndOfDocument is reached when all the characters in a document in the source input has been processed, but before the processing of the next document started.

PRECONDITIONS:

  • the position is not unknown
  • the position is not at the end of input

Returns boolean

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the position is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the position is at the end of input.

get visibleDocumentContents(): string

The contents of the visible input document this position belongs to.

PRECONDITIONS:

  • the position is not unknown
  • the position is not at the end of input

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the receiver is the end of input.

Methods

Protected Abstract

Returns the full context of the source document after the position, up to the beginning of the given number of lines or the end of the document, whichever comes first.

The char at the given position is the first one in the solution.

It implements the specific logic of each subclass for the Template Method Pattern of documentContextBefore. It must be reimplemented by subclasses.

Parameters

  • lines: number

Returns string[]

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the position is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the at the end of input.

Protected Abstract

Returns the full context of the source document before the position, up to the beginning of the given number of lines, or the beginning of the document, whichever comes first.

The char at the given position is NOT included in the solution.

It implements the specific logic of each subclass for the Template Method Pattern of documentContextBefore. It must be reimplemented by subclasses.

Parameters

  • lines: number

Returns string[]

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the position is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the at the end of input.

Protected

The exact portion of the source that is enclosed between from position and this position (not included), both visible and non-visible. If from comes after this, the result is the empty string.

It implements the specific logic of each subclass for the Template Method Pattern of fullContentsFrom. It must be reimplemented by subclasses.

PRECONDITION:

  • both positions correspond to the same reader (not validated, as it is a protected operation).

Parameters

Returns string

Protected

The exact portion of the source that is enclosed between this position and to position (not included), both visible and non-visible. If this comes after to, the result is the empty string.

It implements the specific logic of each subclass for the Template Method Pattern of fullContentsTo. It must be reimplemented by subclasses.

PRECONDITION: both positions correspond to the same reader (not validated, as it is a protected operation).

Parameters

Returns string

The index in the SourceReader's document indicated by the document index where the character which this positions corresponds to is stored. It may be one longer thant the lenght, in case the position is EOD.

Parameters

  • visible: boolean

Returns number

The index in the SourceReader's document list where the document which this position corresponds is stored. It may be one longer that the lenght, in case the position is EOI.

Returns number

Protected

Validates that:

  • argument position is not unknown
  • both positions correspond to the same reader.

Implements a common validation for the Template Method Pattern.

Parameters

Returns void

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver or the argument positions are unknown.

SourceReader/Errors.MismatchedInputsError if the receiver and the argument positions do not belong to the same reader.

Protected

The exact portion of the source that is enclosed between from position and this position (not included) and is visible. If from comes after this, the result is the empty string.

It implements the specific logic of each subclass for the Template Method Pattern of visibleContentsFrom. It must be reimplemented by subclasses.

PRECONDITION: both positions correspond to the same reader (not validated, as it is a protected operation).

Parameters

Returns string

Protected

The exact portion of the source that is enclosed between this position and to position (not included) and is visible. If this comes after to, the result is the empty string.

It implements the specific logic of each subclass for the Template Method Pattern of visibleContentsTo. It must be reimplemented by subclasses.

PRECONDITION: both positions correspond to the same reader (not validated, as it is a protected operation).

Parameters

Returns string

Returns the full context of the source document after the position, up to the beginning of the given number of lines or the end of the document.

The char at the given position is the first one in the solution.

PRECONDITIONS:

  • the position is not unknown
  • the position is not at the end of input

Parameters

  • lines: number

Returns string[]

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the position is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the position is at the end of input.

Returns the full context of the source document before the position, up to the beginning of the given number of lines, or the beginning of the document, whichever comes first.

The char at the given position is NOT included in the solution.

PRECONDITIONS:

  • the position is not unknown
  • the position is not at the end of input

Parameters

  • lines: number

Returns string[]

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the position is unknown.

SourceReader/Errors.InvalidOperationAtEOIError if the position is at the end of input.

The exact portion of the source that is enclosed between the argument position and the receiver position (not included), both visible and non visible. If the receiver does not come after the argument, the result is the empty string.

PRECONDITIONS:

  • both positions are known
  • both positions correspond to the same reader
  • the receiver is not at the end of input

Parameters

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver or the argument positions are unknown.

SourceReader/Errors.MismatchedInputsError if the receiver and the argument positions do not belong to the same reader.

SourceReader/Errors.InvalidOperationAtEOIError if the receiver is the end of input.

The exact portion of the source, both visible and non visible, that is enclosed between the receiver position and the argument position (not included). If the argument does not come after the receiver, the result is the empty string.

PRECONDITIONS:

  • both positions are known
  • both positions correspond to the same reader
  • the receiver is not at the end of input

Parameters

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver or the argument positions are unknown.

SourceReader/Errors.MismatchedInputsError if the receiver and the argument positions do not belong to the same reader.

SourceReader/Errors.InvalidOperationAtEOIError if the receiver is the end of input.

Abstract

Gives a string representation of the position. It is NOT useful for persistence, as it may loose information.

Returns string

The exact portion of the source that is enclosed between the from position and this position (not included) and is visible. If this does not come after from, the result is the empty string.

PRECONDITIONS:

  • both positions are known
  • both positions correspond to the same reader
  • the receiver is not at the end of input

Parameters

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver or the argument positions are unknown.

SourceReader/Errors.MismatchedInputsError if the receiver and the argument positions do not belong to the same reader.

SourceReader/Errors.InvalidOperationAtEOIError if the receiver is the end of input.

The exact portion of the source that is enclosed between the this position and to position (not included) and is visible. If to does not come after this, the result is the empty string.

PRECONDITIONS:

  • both positions are known
  • both positions correspond to the same reader
  • the receiver is not at the end of input

Parameters

Returns string

SourceReader/Errors.InvalidOperationAtUnknownPositionError if the receiver or the argument positions are unknown.

SourceReader/Errors.MismatchedInputsError if the receiver and the argument positions do not belong to the same reader.

SourceReader/Errors.InvalidOperationAtEOIError if the receiver is the end of input.