Before a formula can be parsed it has to be broken into tokens - the atoms a parser reads. Today you build the tokenizer's core, stripping the leading equals sign and scanning numbers, arithmetic operators, and parentheses, with the full token set defined up front.
Tokenize a formula's numbers, arithmetic operators, and parentheses into a token stream.
Parsing happens in two stages, and the first is tokenizing (also called
lexing): turning the raw formula string into a flat list of meaningful atoms. The
parser never looks at characters - it reads tokens like “the number 12”, “a plus”,
“an open paren”. A key detail is that a number is one token even when it spans
several digits, so 12 scans as a single Number 12, not two separate digits.
Two setup decisions pay off later. First, a formula always begins with =, so the
tokenizer strips that leading sign before scanning - by the time the parser runs,
the = is gone. Second, define the entire token set now, including kinds we
cannot produce yet (cell references, colons, commas, comparison operators). Naming
them all up front means the parser’s later lessons only add scanning rules, never
reshape the token type. End every stream with an explicit EOF token so the parser
always knows when input has run out.
type TokKind intconst ( // the full token set the parser will ever seeTNum TokKind = iotaTPlus; TMinus; TStar; TSlash; TLParen; TRParenTCell; TColon; TComma; TIdentTGt; TLt; TGe; TLe; TEq; TNeTEOF)// scan a run of digits as one Number; single chars for + - * / ( )