Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions lib/calc/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -267,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
Expand All @@ -281,25 +276,31 @@ 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
.take(i)
.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) ??
Expand Down Expand Up @@ -439,7 +440,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 ||
Expand All @@ -465,7 +466,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;
Expand Down
10 changes: 8 additions & 2 deletions lib/calc/lexer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const List<String> _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.
///
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/calc/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
Expand Down
46 changes: 46 additions & 0 deletions test/calc_engine_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -682,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);
Expand Down
19 changes: 19 additions & 0 deletions test/highlight_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
});
}
Loading