From db4afe4bf263546c825a6c60a56a48ea6f390698 Mon Sep 17 00:00:00 2001 From: Sanjay Kholiya Date: Tue, 17 Nov 2026 00:11:36 +0530 Subject: [PATCH 1/2] =?UTF-8?q?Read=20=C3=97,=20=C3=B7=20and=20=E2=88=92?= =?UTF-8?q?=20as=20operators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The lexer treated every character past ASCII that isn't a currency symbol as a letter, and it looks for names before operators, so ×, ÷ and the minus sign − lexed as words. `3 × 49 usd` failed to parse and the label fallback kept only `49 usd`, showing 49.00 USD, while `10 ÷ 4` and `10 − 3` showed nothing. They now lex as the operators the parser already handles. En and em dashes stay text, as they always have in practice: prose joins ranges with them, as in `2020–2024` and `pages 10–20`. They come out of the operator list, along with the dash handling in the parser and the label rules that could never be reached. --- lib/calc/engine.dart | 17 +++-------------- lib/calc/lexer.dart | 10 ++++++++-- lib/calc/parser.dart | 4 ++-- test/calc_engine_test.dart | 14 ++++++++++++++ test/highlight_test.dart | 19 +++++++++++++++++++ 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/calc/engine.dart b/lib/calc/engine.dart index 371857f..ded8efb 100644 --- a/lib/calc/engine.dart +++ b/lib/calc/engine.dart @@ -233,18 +233,7 @@ class CalcEngine { // numbers is a time or a ratio. if (token.type == TokenType.operator && i > 0 && - const [ - '+', - '-', - '*', - '/', - '^', - '×', - '÷', - '−', - '–', - '—', - ].contains(token.text)) { + const ['+', '-', '*', '/', '^', '×', '÷', '−'].contains(token.text)) { return true; } if (token.type == TokenType.identifier && @@ -439,7 +428,7 @@ class CalcEngine { (next.type == TokenType.operator && next.text == '-')); } return token.type == TokenType.operator && - const ['-', '−', '–', '—'].contains(token.text) && + const ['-', '−'].contains(token.text) && next != null && (next.type == TokenType.number || next.type == TokenType.currencySymbol || @@ -465,7 +454,7 @@ class CalcEngine { case TokenType.operator: // Hyphens belong naturally in model names (`Model-7`). Every other // operator is evidence that the discarded prefix was arithmetic. - if (!const ['-', '−', '–', '—'].contains(token.text)) return false; + if (!const ['-', '−'].contains(token.text)) return false; case TokenType.currencySymbol: case TokenType.percent: return false; diff --git a/lib/calc/lexer.dart b/lib/calc/lexer.dart index a4aa4ec..6c15bc7 100644 --- a/lib/calc/lexer.dart +++ b/lib/calc/lexer.dart @@ -76,7 +76,9 @@ const List _multiCharOperators = [ '->', ]; -const String _singleCharOperators = '+-*/^<>=!:&|×÷−–—'; +/// En and em dashes are left out: in prose they join ranges, `2020–2024` and +/// `pages 10–20`, so they stay part of the words around them. +const String _singleCharOperators = '+-*/^<>=!:&|×÷−'; /// Turns a single line of note text into tokens. /// @@ -363,13 +365,17 @@ class Lexer { return value >= 0 && value < radix; } + /// Letters past ASCII start names too, `café` and `größe`, but the signs + /// among them, `×`, `÷` and `−`, are operators. static bool _isIdentStart(String ch) { final c = ch.codeUnitAt(0); return (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || ch == '_' || ch == '°' || - c > 0x7f && !currencySymbolCodes.containsKey(ch); + c > 0x7f && + !currencySymbolCodes.containsKey(ch) && + !_singleCharOperators.contains(ch); } static bool _isIdentPart(String ch) => _isIdentStart(ch) || _isDigit(ch); diff --git a/lib/calc/parser.dart b/lib/calc/parser.dart index 185958f..42527f3 100644 --- a/lib/calc/parser.dart +++ b/lib/calc/parser.dart @@ -562,7 +562,7 @@ class Parser { var left = _parseUnitSequence(); while (true) { if (_current.type == TokenType.operator && - const ['+', '-', '−', '–', '—'].contains(_current.text)) { + const ['+', '-', '−'].contains(_current.text)) { final op = _current.text == '+' ? '+' : '-'; _advance(); left = BinaryNode(op, left, _parseUnitSequence()); @@ -686,7 +686,7 @@ class Parser { Node _parseUnary() { if (_current.type == TokenType.operator && - const ['-', '−', '–', '—'].contains(_current.text)) { + const ['-', '−'].contains(_current.text)) { _advance(); return UnaryNode('-', _parseUnary()); } diff --git a/test/calc_engine_test.dart b/test/calc_engine_test.dart index 85b3454..2716c01 100644 --- a/test/calc_engine_test.dart +++ b/test/calc_engine_test.dart @@ -148,6 +148,14 @@ void main() { expect(line('7 mod 3'), '1'); }); + test('reads the ×, ÷ and − signs', () { + expect(line('3 × 49 usd'), '147.00 USD'); + expect(line('10 ÷ 4'), '2.5'); + expect(line('10 − 3'), '7'); + expect(line('3 x 49 usd'), '147.00 USD'); + expect(line('3 * 49 usd'), '147.00 USD'); + }); + test('strips thousands separators', () { expect(line('1,250 + 750'), '2,000'); expect(line('1_000_000 / 4'), '250,000'); @@ -641,6 +649,12 @@ void main() { expect(line('# Trip to Lisbon'), isNull); }); + test('leaves en and em dashes as text', () { + expect(line('Lunch — 12 usd'), '12.00 USD'); + expect(line('2020–2024'), isNull); + expect(line('pages 10–20'), isNull); + }); + test('never throws on partially typed input', () { for (final partial in ['1 +', '(', '20% of', '100 usd to', '=', '3 *']) { expect(() => engine.evaluateDocument(partial), returnsNormally); diff --git a/test/highlight_test.dart b/test/highlight_test.dart index 1b5f02e..ff155c4 100644 --- a/test/highlight_test.dart +++ b/test/highlight_test.dart @@ -60,4 +60,23 @@ void main() { expect(highlighted, contains(('sqrt', HighlightKind.function))); expect(highlighted, contains(('today', HighlightKind.constant))); }); + + test('colours ×, ÷ and − as operators and leaves dashes as text', () { + final highlighter = Highlighter( + CalcEngine(ratesPerUsd: const {'EUR': 0.5}).registry, + ); + List<(String, HighlightKind)> spans(String body) => highlighter + .spans(body) + .map((span) => (body.substring(span.start, span.end), span.kind)) + .toList(); + + expect(spans('3 × 49 usd'), contains(('×', HighlightKind.operator))); + expect(spans('10 ÷ 4'), contains(('÷', HighlightKind.operator))); + expect(spans('10 − 3'), contains(('−', HighlightKind.operator))); + expect(spans('Lunch — 12 usd'), [ + ('12', HighlightKind.number), + ('usd', HighlightKind.currency), + ]); + expect(spans('2020–2024'), [('2020', HighlightKind.number)]); + }); } From fba85c8e00900a797d6a7e80e7f5697b34ffe75c Mon Sep 17 00:00:00 2001 From: Sanjay Kholiya Date: Tue, 17 Nov 2026 09:42:16 +0530 Subject: [PATCH 2/2] Read several words before = as a label A name before = had to be a single word, so `looker connector = 29 usd` gave nothing while `looker connector: 29 usd` gave 29.00 USD. Several words before the first = now label the amount the way a colon does: the line shows it and counts in the total, and names nothing a later line can use. A single name is still an assignment, and colons are still read first, so `Q1: revenue = 1000` still defines revenue. With the sign fix, Riverbank's sample note computes as Paper shows it: 29.00 USD, 147.00 USD and a total of 305.00 USD. --- lib/calc/engine.dart | 26 +++++++++++++++++++------- test/calc_engine_test.dart | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/calc/engine.dart b/lib/calc/engine.dart index ded8efb..f642267 100644 --- a/lib/calc/engine.dart +++ b/lib/calc/engine.dart @@ -256,6 +256,12 @@ class CalcEngine { /// answer. Single-name labels such as `budget: 120` are parsed first as /// assignments and keep defining a reusable variable. /// + /// The first equals sign makes the same promise once more than a name + /// stands before it, so `looker connector = 29 usd` reads as + /// `looker connector: 29 usd` does and names nothing. A single name before + /// it is an assignment, read before this. Colons are tried first, so + /// `Q1: revenue = 1000` still defines `revenue`. + /// /// Work from the rightmost colon so nested descriptions such as /// `Quote 7: panels: 12000rs` still select the final value. A colon joining /// two adjacent numbers is time/ratio punctuation (`12:30`, `1:2`), not a @@ -270,17 +276,23 @@ class CalcEngine { .tokenize() .where((t) => t.isSignificant && t.type != TokenType.eof) .toList(); - for (var i = tokens.length - 2; i > 0; i--) { - final colon = tokens[i]; - if (colon.type != TokenType.operator || colon.text != ':') continue; - + final equals = tokens.indexWhere( + (token) => token.type == TokenType.operator && token.text == '=', + ); + final boundaries = [ + for (var i = tokens.length - 2; i > 0; i--) + if (tokens[i].type == TokenType.operator && tokens[i].text == ':') i, + if (equals > 1 && equals < tokens.length - 1) equals, + ]; + for (final i in boundaries) { + final boundary = tokens[i]; final before = tokens[i - 1]; final after = tokens[i + 1]; final joinsNumbers = before.type == TokenType.number && after.type == TokenType.number && - before.end == colon.start && - colon.end == after.start; + before.end == boundary.start && + boundary.end == after.start; if (joinsNumbers) continue; final hasLabel = tokens @@ -288,7 +300,7 @@ class CalcEngine { .any((token) => _isExplicitLabelToken(token, scope)); if (!hasLabel) continue; - final right = source.substring(colon.end).trim(); + final right = source.substring(boundary.end).trim(); if (right.isEmpty) continue; final evaluated = _evaluateExpression(right, index, evaluator, scope) ?? diff --git a/test/calc_engine_test.dart b/test/calc_engine_test.dart index 2716c01..da09b15 100644 --- a/test/calc_engine_test.dart +++ b/test/calc_engine_test.dart @@ -696,6 +696,38 @@ void main() { expect(line('Estimate: 10000 + 18%'), '11,800'); }); + test('reads words before = as it reads them before a colon', () { + expect(line('looker connector = 29 usd'), '29.00 USD'); + expect(line('outreach seats = 3 × 49 usd'), '147.00 USD'); + expect(line('monthly rent = 1200 usd'), '1,200.00 USD'); + expect(line('my budget = 120'), '120'); + }); + + test('words before = name nothing, and a single name still does', () { + expect(doc('looker connector = 29 usd\nlooker connector * 12'), { + 0: '29.00 USD', + }); + expect(doc('ahrefs = 129 usd\nahrefs * 12'), { + 0: '129.00 USD', + 1: '1,548.00 USD', + }); + // The colon is read first, so the assignment after it still counts. + expect(doc('Q1: revenue = 1000\nrevenue * 4'), {0: '1,000', 1: '4,000'}); + }); + + test("computes Riverbank's sample note as Paper shows it", () { + expect( + doc( + 'Tool costs per month\n' + 'ahrefs = 129 usd\n' + 'looker connector = 29 usd\n' + 'outreach seats = 3 × 49 usd\n' + 'total', + ), + {1: '129.00 USD', 2: '29.00 USD', 3: '147.00 USD', 4: '305.00 USD'}, + ); + }); + test('an explicit label does not turn time-like prose into an amount', () { expect(line('Meeting at 12:30'), isNull); expect(line('Ratio 1:2'), isNull);