![Logo](/_astro/logo.DNC1PCTe_Z1abT04.webp)
Enter a search term above to see results...
Enter a search term above to see results...
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.
new StringScanner(input)
Name | Type | Description |
---|---|---|
input | string | The input string to be scanned |
import { StringScanner } from '@semantic-ui/templating';
const scanner = new StringScanner('<div>{{name}}</div>');
The original input string.
The current position in the input string.
Checks if the rest of the input matches a given regular expression.
scanner.matches(regex)
Name | Type | Description |
---|---|---|
regex | RegExp | The regular expression to test |
boolean
- true
if the rest of the input matches the regex, false
otherwise.
if (scanner.matches(/\{\{/)) { // Handle opening double curly braces}
Returns the rest of the input string from the current position.
scanner.rest()
string
- The remaining unscanned portion of the input.
const remaining = scanner.rest();console.log(remaining);
Checks if the scanner has reached the end of the input.
scanner.isEOF()
boolean
- true
if the scanner is at or past the end of the input, false
otherwise.
while (!scanner.isEOF()) { // Process next token}
Returns the character at the current position without advancing.
scanner.peek()
string
- The character at the current position.
const nextChar = scanner.peek();if (nextChar === '{') { // Handle opening curly brace}
Consumes and returns a portion of the input that matches a given pattern.
scanner.consume(pattern)
Name | Type | Description |
---|---|---|
pattern | string|RegExp | The pattern to match and consume |
string|null
- The matched string if found, otherwise null
.
const tag = scanner.consume(/\{\{[^}]+\}\}/);if (tag) { // Process the found tag}
Consumes and returns the portion of the input up to a given pattern.
scanner.consumeUntil(pattern)
Name | Type | Description |
---|---|---|
pattern | string|RegExp | The pattern to stop consuming |
string
- The consumed string up to the pattern.
const content = scanner.consumeUntil('{{');console.log(content); // Everything before the next '{{'
Returns context information about the current position in the input.
scanner.getContext()
Object
- An object containing context information.
const context = scanner.getContext();if (context.insideTag) { // Handle being inside a tag}
Throws an error with the given message and current position information.
scanner.fatal(msg)
Name | Type | Description |
---|---|---|
msg | string | The error message |
if (unexpectedCharacter) { scanner.fatal('Unexpected character in template');}