Add support for expressions inside expressions.
Add support for templating where the output of one expression results in
another template part with expressions.
E.g., this did not work before, but compilation will now evaluate both
with expression with `$condition` and parameter substitution with
`$text`:
```
{{ with $condition }}
echo '{{ $text }}'
{{ end }}
```
Add also more sanity checks (validation logic) when compiling
expressions to reveal problems quickly.
35 lines
944 B
TypeScript
35 lines
944 B
TypeScript
export class ExpressionPosition {
|
|
constructor(
|
|
public readonly start: number,
|
|
public readonly end: number,
|
|
) {
|
|
if (start === end) {
|
|
throw new Error(`no length (start = end = ${start})`);
|
|
}
|
|
if (start > end) {
|
|
throw Error(`start (${start}) after end (${end})`);
|
|
}
|
|
if (start < 0) {
|
|
throw Error(`negative start position: ${start}`);
|
|
}
|
|
}
|
|
|
|
public isInInsideOf(potentialParent: ExpressionPosition): boolean {
|
|
if (this.isSame(potentialParent)) {
|
|
return false;
|
|
}
|
|
return potentialParent.start <= this.start
|
|
&& potentialParent.end >= this.end;
|
|
}
|
|
|
|
public isSame(other: ExpressionPosition): boolean {
|
|
return other.start === this.start
|
|
&& other.end === this.end;
|
|
}
|
|
|
|
public isIntersecting(other: ExpressionPosition): boolean {
|
|
return (other.start < this.end && other.end > this.start)
|
|
|| (this.end > other.start && other.start >= this.start);
|
|
}
|
|
}
|