Scanner

A Scanner object is a simple string parser that allows for easy scanning for substrings and characters in a character set, and well as numeric values in several representations.

To set a scanner object to ignore a set of characters as it scans the string, use the skipChars property. Characters in the skip set are skipped over before the target is scanned. The default set of characters to skip is the whitespace and newline character set.

The Scanner class is not subclassable.

let string = "Foobar abc 12.0";

let scanner = new Scanner(string);

scanner.scanString("Foo"); // => "Foo"
scanner.scanString("Foo"); // => null
scanner.scanString("bar"); // => "bar"

scanner.scanChars(Charset.alphanumeric); // => "abc";

scanner.scanFloat(); // => 12.0
scanner.scanFloat(); // => null

scanner.atEnd; // => true

Class Methods

constructor(string)

Creates a new Scanner object with a provided string to scan.

Properties

string

The string the scanner is parsing.

This property is readonly.

location

The current character position of the scanner in its string as an integer.

This property is settable.

atEnd

Whether the scanner’s location is at the end of its string.

This property is readonly.

skipChars

The set of characters that should be skipped during parsing, as a Charset. Characters in the skip set are skipped over before the target is scanned. The default set of characters to skip is the whitespace and newline character set.

This property is settable.

caseSensitive

Whether the scanner parses using case-sensitive rules. By default this is false.

This property is settable.

Methods

scanString(string)

Scans the provided string, returning the string if found and null if it was not found.

scanUpToString(string)

Scans the string until a given string is encountered, accumulating characters in a string that is returned. If no characters are scanned (such as if the string to be scanned up to is already at the scan location or the scanner is empty), this method returns null.

scanChars(charset)

Scans for characters in the provided Charset, accumulating them into a string that is returned. If no characters are scanned, this method returns null.

scanUpToChars(charset)

Scans the string until a character in the provided Charset is encountered, accumulating characters in a string that is returned. If no characters are scanned (such as if a characters to be scanned up to is already at the scan location or the scanner is empty), this method returns null.

scanInt()

Scans for an integer value in decimal representation, returning it if found, and returning null if no value is found.

scanFloat()

Scans for a floating-point value in decimal representation, returning it if found, and returning null if no value is found.

scanHexInt()

Scans for an integer value in hexadecimal representation, returning it if found, and returning null if no value is found.

scanHexFloat()

Scans for a floating-point value in hexadecimal representation, returning it if found, and returning null if no value is found.