Spaces:
Sleeping
Sleeping
File size: 1,789 Bytes
11811dc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // @flow
import {NumberType, toString} from '../types';
import {typeOf} from '../values';
import RuntimeError from '../runtime_error';
import type {Expression} from '../expression';
import type ParsingContext from '../parsing_context';
import type EvaluationContext from '../evaluation_context';
import type {Type} from '../types';
class Length implements Expression {
type: Type;
input: Expression;
constructor(input: Expression) {
this.type = NumberType;
this.input = input;
}
static parse(args: $ReadOnlyArray<mixed>, context: ParsingContext) {
if (args.length !== 2)
return context.error(`Expected 1 argument, but found ${args.length - 1} instead.`);
const input = context.parse(args[1], 1);
if (!input) return null;
if (input.type.kind !== 'array' && input.type.kind !== 'string' && input.type.kind !== 'value')
return context.error(`Expected argument of type string or array, but found ${toString(input.type)} instead.`);
return new Length(input);
}
evaluate(ctx: EvaluationContext) {
const input = this.input.evaluate(ctx);
if (typeof input === 'string') {
return input.length;
} else if (Array.isArray(input)) {
return input.length;
} else {
throw new RuntimeError(`Expected value to be of type string or array, but found ${toString(typeOf(input))} instead.`);
}
}
eachChild(fn: (_: Expression) => void) {
fn(this.input);
}
outputDefined() {
return false;
}
serialize() {
const serialized = ["length"];
this.eachChild(child => { serialized.push(child.serialize()); });
return serialized;
}
}
export default Length;
|