Options
All
  • Public
  • Public/Protected
  • All
Menu

Represents a caret, which reads sequentially through a string.

Hierarchy

  • Caret

Index

Constructors

constructor

  • new Caret(contents: string): Caret

Properties

EOF

EOF: boolean

Stores whether the end of the file has been reached or not.

column

column: number

The column in the string the caret is currently at.

contents

contents: string

The string that is currently being read.

line

line: number

The line on the string the caret is currently at.

pos

pos: number

The position on the string the caret is currently at.

Methods

advance

  • advance(n: number): void

getChar

  • getChar(): string

increment

  • increment(): void
  • Moves the caret to the next column or the next line. Automatically keeps track of pos, line, column and EOF. Assumes Windows-style line endings.

    todo

    Take care of Unix and Macintosh line endings.

    Returns void

Private isNumericChar

  • isNumericChar(): boolean

readNumber

  • readNumber(): number
  • Reads the next number from the caret position, until a non-numeric char appears. Supports numbers in the scientific notation format (e.g. 1E+10). Caret automatically [[skips to next content|skipToContent]] afterwards.

    throws

    Will throw an error if the caret is currently at the EOF.

    throws

    Will throw an error if the read number is invalid.

    Returns number

    The read number, or NaN if the caret isn't immmediately before a number.

readUntil

  • readUntil(char: string): string
  • Advances the caret until a certain character is reached. Places the caret at the left of such char.

    Parameters

    • char: string

      The searched-for character.

    Returns string

    The read substring.

readWord

  • readWord(): string
  • Reads the next word from the caret position until a whitespace, newline or hash appears. Caret automatically [[skips to next content|skipToContent]] afterwards.

    throws

    Will throw an error if the caret is currently at the EOF.

    throws

    Will throw an error if the read number is invalid.

    Returns string

    The read word.

skipToChar

  • skipToChar(char: string): void
  • Increments the caret until a certain character is reached. Caret ends up before the character.

    Example

    var C = new Caret("The quick brown fox jumps over the lazy dog.");
    C.skipToChar('q');
    //The |quick brown fox jumps over the lazy dog.
    
    //4
    console.log(C.pos);

    Parameters

    • char: string

      A character to search for.

    Returns void

skipToContent

  • skipToContent(): void
  • Increments the caret but skips whitespaces, new lines, and comments. Leaves the caret at the beginning of the next "content".

    Returns void

skipToString

  • skipToString(str: string): void
  • Increments the caret until a certain string is read. The caret ends up after the string.

    Example

    var C = new Caret("The quick brown fox jumps over the lazy dog.");
    C.skipToString("fox");
    //The quick brown fox| jumps over the lazy dog.
    
    //19
    console.log(C.pos);

    Parameters

    • str: string

      A string to search for.

    Returns void

skipToStringList

  • skipToStringList(strs: string[]): number
  • Increments the caret until one in a list of strings is read. The caret ends up after the string.

    Example

    var C = new Caret("The quick brown fox jumps over the lazy dog.");
    C.skipToStringList(["quick", "lazy"]);
    //The quick| brown fox jumps over the lazy dog.
    
    //9
    console.log(C.pos);

    Parameters

    • strs: string[]

      The list of strings to search for.

    Returns number

    The index of the first found string.

throwError

  • throwError(code: string, dev?: boolean): never
  • Throws an error corresponding to the error code. Automatically inserts the line and column numbers into the error message.

    throws

    The corresponding error.

    Parameters

    • code: string

      The error code.

    • Default value dev: boolean = false

      Whether the error is the user's or the developer's fault.

    Returns never

Generated using TypeDoc, the 1/31/2021 at 6:18:55 AM