import { CaretParser, CaretPrefixParselet, TokensTag } from "@caret-js/core"; import { CharToken } from "@caret-js/core"; import { NegativeNode } from "../nodes/negative"; import { DecimalNode } from "../nodes/decimal"; // This parselet looks for a sequence of `CharToken`s that form a decimal number, // meaning it has digits, optionally a single decimal point, and more digits. The digits // are optional on either side of the decimal point, but at least one digit must be present. export class UnaryMinusParselet extends CaretPrefixParselet { parse(parser: CaretParser): NegativeNode | DecimalNode { const token = parser.consume(); this.assert( token instanceof CharToken && token.char === "-", "Expected '-' token" ); const child = parser.parseMinBP(this); return NegativeNode.from(child).addTag(new TokensTag([token])); } }