A world-class math input for the web
1import { CaretParser, CaretPrefixParselet, TokensTag } from "@caret-js/core";
2import { CharToken } from "@caret-js/core";
3import { NegativeNode } from "../nodes/negative";
4import { DecimalNode } from "../nodes/decimal";
5
6// This parselet looks for a sequence of `CharToken`s that form a decimal number,
7// meaning it has digits, optionally a single decimal point, and more digits. The digits
8// are optional on either side of the decimal point, but at least one digit must be present.
9
10export class UnaryMinusParselet extends CaretPrefixParselet {
11 parse(parser: CaretParser): NegativeNode | DecimalNode {
12 const token = parser.consume();
13 this.assert(
14 token instanceof CharToken && token.char === "-",
15 "Expected '-' token"
16 );
17
18 const child = parser.parseMinBP(this);
19 return NegativeNode.from(child).addTag(new TokensTag([token]));
20 }
21}