Start lipsum command
This commit is contained in:
parent
144d90e871
commit
c437958406
@ -1,10 +1,15 @@
|
|||||||
import {LexInput} from '../lexer.js'
|
import {LexInput} from '../lexer.js'
|
||||||
import {ExpectedEndOfInputError, InvalidVariableNameError, UnexpectedEndOfInputError} from "../parse.js";
|
import {
|
||||||
|
ExpectedEndOfInputError,
|
||||||
|
InvalidVariableNameError,
|
||||||
|
IsNotKeywordError,
|
||||||
|
UnexpectedEndOfInputError
|
||||||
|
} from "../parse.js";
|
||||||
|
|
||||||
export type StrLVal = { term: 'variable', name: string }
|
export type StrLVal = { term: 'variable', name: string }
|
||||||
|
|
||||||
export type StrTerm =
|
export type StrTerm =
|
||||||
{ term: 'string', value: string }
|
{ term: 'string', value: string, literal?: true }
|
||||||
| StrLVal
|
| StrLVal
|
||||||
|
|
||||||
export class ParseContext {
|
export class ParseContext {
|
||||||
@ -25,7 +30,7 @@ export class ParseContext {
|
|||||||
|
|
||||||
popTerm(): StrTerm {
|
popTerm(): StrTerm {
|
||||||
if ( !this.inputs.length ) {
|
if ( !this.inputs.length ) {
|
||||||
throw new UnexpectedEndOfInputError('Unexpected end of input. Expected term.');
|
throw new UnexpectedEndOfInputError('Unexpected end of input. Expected term.')
|
||||||
}
|
}
|
||||||
|
|
||||||
const input = this.inputs.shift()!
|
const input = this.inputs.shift()!
|
||||||
@ -40,7 +45,19 @@ export class ParseContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, parse it as a string literal:
|
// Otherwise, parse it as a string literal:
|
||||||
return { term: 'string', value: input.value }
|
return { term: 'string', value: input.value, literal: input.literal }
|
||||||
|
}
|
||||||
|
|
||||||
|
popKeywordInSet<T extends string[]>(options: T) {
|
||||||
|
if ( !this.inputs.length ) {
|
||||||
|
throw new UnexpectedEndOfInputError('Unexpected end of input. Expected one of: ' + options.join(', '))
|
||||||
|
}
|
||||||
|
|
||||||
|
const input = this.inputs.shift()!
|
||||||
|
|
||||||
|
if ( input.literal || !options.includes(input.value) ) {
|
||||||
|
throw new IsNotKeywordError('Unexpected term: ' + input.value + ' (expected one of: ' + options.join(', ') + ')')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
popLVal(): StrLVal {
|
popLVal(): StrLVal {
|
||||||
|
|||||||
18
src/vm/commands/lipsum.ts
Normal file
18
src/vm/commands/lipsum.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import {Command, ParseContext, StrTerm} from './command.js'
|
||||||
|
import {LexInput} from '../lexer.js'
|
||||||
|
|
||||||
|
export class Lipsum extends Command<{ length: StrTerm }> {
|
||||||
|
attemptParse(context: ParseContext): { length: StrTerm } {
|
||||||
|
return {
|
||||||
|
length: context.popTerm(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getDisplayName(): string {
|
||||||
|
return 'lipsum'
|
||||||
|
}
|
||||||
|
|
||||||
|
isParseCandidate(token: LexInput): boolean {
|
||||||
|
return this.isKeyword(token, 'lipsum')
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user