StringScanner API reference for the StringScanner class in Semantic UI Templating search Guide

StringScanner

The StringScanner class is a utility used internally by the TemplateCompiler to scan and parse template strings. It provides methods for moving through a string, matching patterns, and extracting relevant information during the template compilation process.

This library should not generally be used unless you plan on writing a custom string templating language.

Constructor

Syntax

new StringScanner(input)

Parameters

NameTypeDescription
inputstringThe input string to be scanned

Usage

import { StringScanner } from '@semantic-ui/templating';
const scanner = new StringScanner('<div>{{name}}</div>');

Properties

input

The original input string.

pos

The current position in the input string.

Methods

matches

Checks if the rest of the input matches a given regular expression.

Syntax

scanner.matches(regex)

Parameters

NameTypeDescription
regexRegExpThe regular expression to test

Returns

boolean - true if the rest of the input matches the regex, false otherwise.

Usage

if (scanner.matches(/\{\{/)) {
// Handle opening double curly braces
}

rest

Returns the rest of the input string from the current position.

Syntax

scanner.rest()

Returns

string - The remaining unscanned portion of the input.

Usage

const remaining = scanner.rest();
console.log(remaining);

isEOF

Checks if the scanner has reached the end of the input.

Syntax

scanner.isEOF()

Returns

boolean - true if the scanner is at or past the end of the input, false otherwise.

Usage

while (!scanner.isEOF()) {
// Process next token
}

peek

Returns the character at the current position without advancing.

Syntax

scanner.peek()

Returns

string - The character at the current position.

Usage

const nextChar = scanner.peek();
if (nextChar === '{') {
// Handle opening curly brace
}

consume

Consumes and returns a portion of the input that matches a given pattern.

Syntax

scanner.consume(pattern)

Parameters

NameTypeDescription
patternstring|RegExpThe pattern to match and consume

Returns

string|null - The matched string if found, otherwise null.

Usage

const tag = scanner.consume(/\{\{[^}]+\}\}/);
if (tag) {
// Process the found tag
}

consumeUntil

Consumes and returns the portion of the input up to a given pattern.

Syntax

scanner.consumeUntil(pattern)

Parameters

NameTypeDescription
patternstring|RegExpThe pattern to stop consuming

Returns

string - The consumed string up to the pattern.

Usage

const content = scanner.consumeUntil('{{');
console.log(content); // Everything before the next '{{'

getContext

Returns context information about the current position in the input.

Syntax

scanner.getContext()

Returns

Object - An object containing context information.

Usage

const context = scanner.getContext();
if (context.insideTag) {
// Handle being inside a tag
}

fatal

Throws an error with the given message and current position information.

Syntax

scanner.fatal(msg)

Parameters

NameTypeDescription
msgstringThe error message

Usage

if (unexpectedCharacter) {
scanner.fatal('Unexpected character in template');
}