str
str(s: string, m?: string): Parser
Parses the string s.
The number of characters read equals the number of characters in s. If there are not that many characters remaining in the input, the parser will automatically fail.
If s is the empty string, the parser will automatically succeed and consume no input.
Example
const parser = str('Test')
const s = parse(parser, 'Testing')
console.log(status(s)) // "ok"
console.log(success(s)) // "Test"
const f = parse(parser, 'test')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// test
// ^
// Expected 'Test'
Parameters
s: The string to match against the input at its current location.m: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if the first
ncharacters of the input at its current location is the same ass, wherenequals the number of characters ins(including 0; havingsbe the empty string causes automatic success). The read characters are returned as a single string, and that many characters are consumed from the input.
Failure
- Fails if there are not as many characters left in the input as there are in
s. - Fails if the first
ncharacters do not equals.
Throws
- Throws an error if
sis not a string. - Throws an error if
mexists and is not a string.