1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
//! The expression and condition grammars (PORT.6, slice 6d), ported from
//! VLA_SentenceEngine.bas arm for arm: `ParseExpr`, `ParseSum`, `ParseProd`,
//! `ParsePrim` and `ParsePrimCore` (with LX.14's `TryFunctionPhrase` and
//! `CompleteFunctionPhrase`, the `<word> of <value>` function words, G8's
//! ordinals, B6's `using` calls, B7's `item n of`, the cell readings and
//! V3's keyed read), `ReadTextRef`, `ParseExprReq`; `ParseCond`,
//! `ParseCondSimple`, `TryRangeContains`, `ParseCondReq`; and the name
//! checks a matched sentence meets, `CheckName`, `RefuseCellShaped` and
//! `RefuseCellShapedRead` (LX.13).
//!
//! Precedence, loosest to tightest: `joined with` / `followed by` (&), then
//! `plus` / `minus`, then `times` / `divided by` / `multiplied by`, then a
//! primary with its `%` postfix. Each level backtracks when the right
//! operand fails, so a word like `times` in "Repeat 10 times:" is left for
//! the sentence.
use super::matcher::{
alt_desc, is_cell_part, is_num_tok, is_str_tok, is_word_tok, render_tok, vla_string_lit, Parser,
};
use super::rules::{slot_tok, surface_match};
use super::tokenize::{line_suf, PARA_TOK};
use super::words::{is_engine_call_name, is_reserved_name, ordinal_word, slot_desc};
use crate::form::Form;
use crate::intrinsics::fold;
use crate::messages::{raise, Refusal};
use crate::printer::write_datum;
/// VBA `FirstCellShapedWord` (LX.13): the first bare word in VLA text that
/// is shaped like a cell, or ""; quoted text is skipped.
pub fn first_cell_shaped_word(vla: &str) -> String {
let chars: Vec<char> = vla.chars().collect();
let n = chars.len();
let mut i = 0;
while i < n {
let ch = chars[i];
if ch == '"' {
i += 1;
while i < n {
let ch = chars[i];
if ch == '\\' {
i += 2;
} else if ch == '"' {
break;
} else {
i += 1;
}
}
i += 1;
} else if matches!(ch, '(' | ')' | ' ' | '\t' | '\r' | '\n') {
i += 1;
} else {
let mut word = String::new();
while i < n {
let ch = chars[i];
if matches!(ch, '(' | ')' | ' ' | '\t' | '\r' | '\n' | '"') {
break;
}
word.push(ch);
i += 1;
}
let folded = fold(&word);
if is_cell_part(&folded) {
return folded;
}
}
}
String::new()
}
/// VBA `CellWordShown` (LX.13): a cell-shaped word as a cell is written,
/// its letters in capitals; ASCII only, `Fold`'s mirror.
pub fn cell_word_shown(word: &str) -> String {
word.chars()
.map(|c| {
if c.is_ascii_lowercase() {
c.to_ascii_uppercase()
} else {
c
}
})
.collect()
}
/// VBA `InParamList`: the folded word among the (folded) parameter names.
fn in_param_list(names: &[String], word: &str) -> bool {
let w = fold(word);
names.iter().any(|n| n == &w)
}
/// VBA `JoinParamNames`.
fn join_param_names(names: &[String]) -> String {
names.join(", ")
}
impl<'g> Parser<'g> {
/// VBA `ParseExpr`: `joined with` / `followed by` over sums.
pub(super) fn parse_expr(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let Some(mut a) = self.parse_sum(pos)? else {
return Ok(None);
};
loop {
let sp = *pos;
if self.match_words(pos, "joined with") || self.match_words(pos, "followed by") {
match self.parse_sum(pos)? {
Some(b) => a = format!("(& {a} {b})"),
None => {
*pos = sp;
break;
}
}
} else {
break;
}
}
Ok(Some(a))
}
/// VBA `ParseSum`: `plus` / `minus` over products.
fn parse_sum(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let Some(mut a) = self.parse_prod(pos)? else {
return Ok(None);
};
loop {
let op = match self.tok(*pos) {
"plus" => "+",
"minus" => "-",
_ => break,
};
let sp = *pos;
*pos += 1;
match self.parse_prod(pos)? {
Some(b) => a = format!("({op} {a} {b})"),
None => {
*pos = sp;
break;
}
}
}
Ok(Some(a))
}
/// VBA `ParseProd`: `times` / `divided by` / `multiplied by` over
/// primaries.
fn parse_prod(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let Some(mut a) = self.parse_prim(pos)? else {
return Ok(None);
};
loop {
let sp = *pos;
let op = if self.tok(*pos) == "times" {
*pos += 1;
"*"
} else if self.match_words(pos, "divided by") {
"/"
} else if self.match_words(pos, "multiplied by") {
"*"
} else {
break;
};
match self.parse_prim(pos)? {
Some(b) => a = format!("({op} {a} {b})"),
None => {
*pos = sp;
break;
}
}
}
Ok(Some(a))
}
/// VBA `ParsePrim`: a primary and its `%` / `percent` postfixes (B6.3,
/// B7.2), each dividing by 100 again, as in Excel.
pub(super) fn parse_prim(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let Some(mut r) = self.parse_prim_core(pos)? else {
return Ok(None);
};
while self.tok(*pos) == "%" || self.tok(*pos) == "percent" {
*pos += 1;
r = format!("(/ {r} 100)");
}
Ok(Some(r))
}
/// V3: a lookup declared by "Create a lookup called ..." in the current
/// translation.
pub(super) fn is_dict_name(&self, w: &str) -> bool {
self.dict_names.iter().any(|d| d == w)
}
/// VBA `ParsePrimCore`.
fn parse_prim_core(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let t = self.tok(*pos).to_string();
// LX.14: a function phrase, longest first, ahead of any one-word
// reading. Once its words match it completes, or it is refused there.
if let Some(n2) = self.try_function_phrase(pos)? {
return Ok(Some(n2));
}
let fn_of = self.fn_of_target(&t);
if is_num_tok(&t) {
*pos += 1;
Ok(Some(t))
} else if is_str_tok(&t) {
*pos += 1;
Ok(Some(vla_string_lit(&t[1..])))
} else if let (Some(target), true) = (fn_of, self.tok(*pos + 1) == "of") {
// "<word> of <value>": binds tightly and composes - "length of
// cell B2", "month of today". Read before a value word (LX.14).
let sp = *pos;
*pos += 2;
match self.parse_prim(pos)? {
Some(n2) => Ok(Some(format!("({target} {n2})"))),
None => {
*pos = sp + 1; // no operand: the word is a name
Ok(Some(t))
}
}
} else if let Some(target) = self.fn_nullary_target(&t) {
*pos += 1;
Ok(Some(format!("({target})")))
} else if !ordinal_word(&t).is_empty() {
// G8: ordinal words, read only here and only once "<word> of"
// has failed, so "first of X" never reaches this branch.
*pos += 1;
Ok(Some(ordinal_word(&t)))
} else if (self.is_using_fn(&t) && self.tok(*pos + 1) == "using")
|| (t == "get" && self.is_using_fn(self.tok(*pos + 1)) && self.tok(*pos + 2) == "using")
{
self.parse_using_call(pos, &t)
} else if t == "problem" && self.in_recovery {
// B7: inside an "If that fails:" paragraph, "the problem" is
// what went wrong.
*pos += 1;
Ok(Some("vla-problem".to_string()))
} else if t == "item" {
// B7: "item <n> of <list>" fetches an element; falls back to the
// plain name when the shape does not follow.
let sp = *pos;
*pos += 1;
if let Some(n2) = self.parse_expr(pos)? {
if self.tok(*pos) == "of" {
*pos += 1;
if let Some(cref) = self.parse_prim(pos)? {
return Ok(Some(format!("(vlaitem {cref} {n2})")));
}
}
}
*pos = sp + 1;
Ok(Some(t))
} else if t == "cell" || (t == "value" && self.tok(*pos + 1) == "in") {
// Cell references are values: "cell B2", "cell in column C row
// k", and B5.2's "value in column ..." / "value in cell B2".
let sp = *pos;
*pos += 1;
if self.tok(*pos) == "in" {
*pos += 1;
if t == "value" && self.tok(*pos) == "cell" {
*pos += 1;
if let Some(cref) = self.read_text_ref(pos) {
return Ok(Some(format!("(range {cref})")));
}
} else if self.tok(*pos) == "column" {
*pos += 1;
let cref = if self.tok(*pos) == "number" {
// B5: "cell in column number <expr> row <expr>".
*pos += 1;
self.parse_expr(pos)?
} else {
self.read_text_ref(pos)
};
if let Some(cref) = cref {
if self.tok(*pos) == "row" {
*pos += 1;
if let Some(n2) = self.parse_expr(pos)? {
return Ok(Some(format!("(cells {n2} {cref})")));
}
}
}
}
} else if t == "cell" {
if let Some(cref) = self.read_text_ref(pos) {
return Ok(Some(format!("(range {cref})")));
}
}
*pos = sp + 1; // no reference followed: the word is a name
Ok(Some(t))
} else if !self.word_at(*pos).is_empty() {
if t.contains(':') {
Ok(None) // bare ranges belong in reference slots
} else if self.is_dict_name(&t) && self.tok(*pos + 1) == "for" {
// V3: the keyed read - "<lookup> for <key>".
let sp = *pos;
*pos += 2;
match self.parse_prim(pos)? {
Some(n2) => Ok(Some(format!("(vladictget {t} {n2})"))),
None => {
*pos = sp + 1; // no key followed: the word is a name
Ok(Some(t))
}
}
} else {
*pos += 1;
Ok(Some(t))
}
} else {
Ok(None)
}
}
/// B6: a using-style value action call - named arguments, in any order,
/// omitted ones taking their defaults via VBA named arguments; "get" is
/// optional sugar. After "and", a known parameter name followed by "of"
/// continues the argument list; anything else hands the "and" back to
/// the condition. Unknown and missing-required parameters refuse here,
/// at Check, with the parameter list named.
fn parse_using_call(&mut self, pos: &mut usize, word: &str) -> Result<Option<String>, Refusal> {
let mut t = word.to_string();
if t == "get" {
*pos += 1;
t = self.tok(*pos).to_string();
}
let fn_pos = *pos;
self.claim(&format!("a value call: '{t} using ...'"));
*pos += 2; // the name and "using"
let p_names: Vec<String> = self
.fn_using_names
.get(&fold(&t))
.cloned()
.unwrap_or_default();
let p_req: Vec<bool> = self
.fn_using_req
.get(&fold(&t))
.cloned()
.unwrap_or_default();
let mut kw_args = String::new();
let mut seen: Vec<String> = Vec::new();
loop {
let a_name = self.expect_word(pos, "a parameter name after 'using'")?;
if !in_param_list(&p_names, &a_name) {
let loc = self.line_tag(*pos - 1);
return Err(raise(
"english-action-unknown-param",
&[
("action", &t),
("param", &a_name),
("list", &join_param_names(&p_names)),
("loc", &loc),
],
));
}
if seen.contains(&fold(&a_name)) {
let loc = self.line_tag(*pos - 1);
return Err(raise(
"english-param-given-twice",
&[("param", &a_name), ("action", &t), ("loc", &loc)],
));
}
seen.push(fold(&a_name));
self.expect_word_is(pos, "of")?;
let e = self.parse_expr_req(pos)?;
kw_args.push_str(&format!(" :{a_name} {e}"));
// Parameter lookahead: does this "and" continue the call?
if self.tok(*pos) == "and"
&& in_param_list(&p_names, self.tok(*pos + 1))
&& self.tok(*pos + 2) == "of"
{
*pos += 1;
} else {
break;
}
}
// Every required parameter must have been supplied.
for (ri, pn) in p_names.iter().enumerate() {
if p_req.get(ri).copied().unwrap_or(false) && !seen.contains(pn) {
let loc = self.line_tag(fn_pos);
return Err(raise(
"english-call-missing-required-param",
&[
("action", &t),
("param", pn),
("list", &join_param_names(&p_names)),
("loc", &loc),
],
));
}
}
Ok(Some(format!("({t}{kw_args})")))
}
/// VBA `ReadTextRef`: one reference token (quoted, bare word, or
/// number) as a VBA string literal.
fn read_text_ref(&self, pos: &mut usize) -> Option<String> {
let t = self.tok(*pos);
if is_str_tok(t) {
let r = vla_string_lit(&t[1..]);
*pos += 1;
Some(r)
} else if is_num_tok(t) || is_word_tok(t) {
let r = vla_string_lit(t);
*pos += 1;
Some(r)
} else {
None
}
}
/// VBA `ParseExprReq`.
pub(super) fn parse_expr_req(&mut self, pos: &mut usize) -> Result<String, Refusal> {
match self.parse_expr(pos)? {
Some(e) => Ok(e),
None => Err(raise(
"english-expected-value",
&[("context", &self.sentence_context(*pos))],
)),
}
}
/// VBA `TryFunctionPhrase` (LX.14): the phrases beginning with this
/// word, longest first; the first whose words all match completes.
fn try_function_phrase(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let word = self.tok(*pos).to_string();
let Some(bucket) = self.g.phrase_bucket(&word) else {
return Ok(None);
};
let bucket: Vec<usize> = bucket.to_vec();
for idx in bucket {
let words = self.g.phrases()[idx - 1].words.clone();
let mut p = *pos + 1;
let mut all_words = true;
for w in words.iter().skip(1) {
self.skip_articles(&mut p);
if self.tok(p) != w {
all_words = false;
break;
}
p += 1;
}
if all_words {
let out = self.complete_function_phrase(idx, *pos, &mut p)?;
*pos = p;
return Ok(Some(out));
}
}
Ok(None)
}
/// VBA `CompleteFunctionPhrase`: from the hole on - the value or the
/// reference, then the closing clause - substituted into the template.
fn complete_function_phrase(
&mut self,
idx: usize,
start_pos: usize,
p: &mut usize,
) -> Result<String, Refusal> {
let ph = &self.g.phrases()[idx - 1];
let cat = ph.hole_cat.clone();
let hole_name = ph.hole_name.clone();
let clause = ph.clause.clone();
let template: Form = ph.forms[0].clone();
let mut bn: Vec<String> = Vec::new();
let mut bv: Vec<String> = Vec::new();
let v = if cat == "value" {
match self.parse_prim(p)? {
Some(v) => v,
None => return Err(self.refuse_phrase(idx, start_pos, *p, &slot_desc("value"))),
}
} else {
let mut v = String::new();
if !self.match_ref_token(&cat, p, &mut v) {
return Err(self.refuse_phrase(idx, start_pos, *p, &slot_desc(&cat)));
}
v
};
bn.push(hole_name);
bv.push(v);
for it in &clause {
self.skip_articles(p);
if let Some(slot) = slot_tok(it)? {
let mut hit = false;
let mut stem = String::new();
for alt in slot.cat.split('|') {
let (ok, s) = surface_match(&fold(alt), self.tok(*p));
if ok {
hit = true;
stem = s;
break;
}
}
if !hit {
return Err(self.refuse_phrase(idx, start_pos, *p, &alt_desc(&slot.cat)));
}
bn.push(slot.name.clone());
bv.push(stem);
} else if self.tok(*p) != it {
return Err(self.refuse_phrase(idx, start_pos, *p, &format!("'{it}'")));
}
*p += 1;
}
Ok(write_datum(&super::matcher::form_substitute(
&template, &bn, &bv,
)?))
}
/// VBA `RefusePhrase` (LX.14): a phrase that stopped partway, refused in
/// the teaching frame from where its sentence began.
fn refuse_phrase(&mut self, idx: usize, start_pos: usize, p: usize, expected: &str) -> Refusal {
let found_t = self.tok(p).to_string();
let found = if found_t.is_empty() || found_t == "." || found_t == PARA_TOK {
"the end of the sentence".to_string()
} else {
format!("'{}'", render_tok(&found_t))
};
let mut st = start_pos;
if self.sentence_start >= 1 && self.sentence_start <= start_pos {
st = self.sentence_start;
}
let understood = self.render_tokens(st, p.saturating_sub(1));
let shape = self.g.phrases()[idx - 1].shape.clone();
let loc = self.line_tag(p);
raise(
"english-phrase-incomplete",
&[
("understood", &understood),
("expected", expected),
("found", &found),
("phrase", &shape),
("loc", &loc),
],
)
}
// ---- conditions ---------------------------------------------------------
/// VBA `ParseCond`: simple conditions joined by `and` / `or`.
pub(super) fn parse_cond(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let Some(mut c) = self.parse_cond_simple(pos)? else {
return Ok(None);
};
loop {
let w = self.tok(*pos).to_string();
if w != "and" && w != "or" {
break;
}
let sp = *pos;
*pos += 1;
match self.parse_cond_simple(pos)? {
Some(c2) => c = format!("({w} {c} {c2})"),
None => {
*pos = sp;
break;
}
}
}
Ok(Some(c))
}
/// VBA `ParseCondSimple`: a value, a comparator phrase (longest first),
/// and its value; the idiomatic conditions read through the P-layer's
/// predicates (DF1).
fn parse_cond_simple(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
if let Some(range_cond) = self.try_range_contains(pos)? {
return Ok(Some(range_cond));
}
let Some(l) = self.parse_expr(pos)? else {
return Ok(None);
};
// Nullary comparators first: no right-hand value follows.
if self.match_words(pos, "is not empty") {
return Ok(Some(format!("(not (blank? {l}))")));
}
if self.match_words(pos, "is empty") {
return Ok(Some(format!("(blank? {l})")));
}
// Longest comparator phrases first, in the reference's order: each
// row is the phrase, its operator, its idiom and whether it is the
// divisibility test.
const COMPARATORS: &[(&str, &str, &str, bool)] = &[
("is greater than or equal to", ">=", "", false),
("is less than or equal to", "<=", "", false),
("is at least", ">=", "", false),
("is at most", "<=", "", false),
("is greater than", ">", "", false),
("is more than", ">", "", false),
("is less than", "<", "", false),
("does not contain", "", "ncontains", false),
("contains", "", "contains", false),
("starts with", "", "starts", false),
("ends with", "", "ends", false),
("is divisible by", "", "", true),
("is not equal to", "<>", "", false),
("does not equal", "<>", "", false),
("is not", "<>", "", false),
("is equal to", "=", "", false),
("equals", "=", "", false),
("is", "=", "", false),
];
let Some(&(_, op, kind, divisible)) = COMPARATORS
.iter()
.find(|(phrase, ..)| self.match_words(pos, phrase))
else {
return Ok(None);
};
let Some(r) = self.parse_expr(pos)? else {
return Ok(None);
};
Ok(Some(if divisible {
format!("(zero? (mod {l} {r}))")
} else {
match kind {
"contains" => format!("(positive? (instr 1 {l} {r} vbtextcompare))"),
"ncontains" => format!("(zero? (instr 1 {l} {r} vbtextcompare))"),
"starts" => format!("(= (instr 1 {l} {r} vbtextcompare) 1)"),
"ends" => format!("(= (lcase (right {l} (len {r}))) (lcase {r}))"),
_ => format!("({op} {l} {r})"),
}
}))
}
/// VBA `TryRangeContains` (G-TEXT slice 3): "range A1:D50 contains x",
/// "column C does not contain x", through `VlaFindText`.
fn try_range_contains(&mut self, pos: &mut usize) -> Result<Option<String>, Refusal> {
let kind = self.tok(*pos).to_string();
if kind != "range" && kind != "column" {
return Ok(None);
}
let mut p = *pos + 1;
let mut reference = String::new();
if !self.match_ref_token(&kind, &mut p, &mut reference) {
return Ok(None);
}
let negated = if self.match_words(&mut p, "does not contain") {
true
} else if self.match_words(&mut p, "contains") {
false
} else {
return Ok(None);
};
let Some(r) = self.parse_expr(&mut p)? else {
return Ok(None);
};
let place = if kind == "range" {
format!("(range {reference})")
} else {
format!("(columns {reference})")
};
let cond = if negated {
format!("(zero? (vlafindtext {place} {r} \"row\"))")
} else {
format!("(positive? (vlafindtext {place} {r} \"row\"))")
};
*pos = p;
Ok(Some(cond))
}
/// VBA `ParseCondReq`.
pub(super) fn parse_cond_req(&mut self, pos: &mut usize) -> Result<String, Refusal> {
match self.parse_cond(pos)? {
Some(c) => Ok(c),
None => Err(raise(
"english-expected-condition",
&[("context", &self.sentence_context(*pos))],
)),
}
}
// ---- names ------------------------------------------------------------------
/// VBA `CheckName`: a reserved word, a cell-shaped word (LX.13), a value
/// word, or a name the generated code calls (U.30) cannot be a name.
pub(super) fn check_name(&mut self, n: &str) -> Result<(), Refusal> {
if is_reserved_name(n) {
return Err(raise("english-reserved-word-name", &[("name", n)]));
}
let folded = fold(n);
if is_cell_part(&folded) {
return Err(self.refuse_cell_shaped(&folded, self.cur_line));
}
if self.fn_nullary_target(n).is_some() {
return Err(raise("english-value-word-name", &[("name", n)]));
}
// U.30: last, so date and time keep the reserved-word refusal and
// the value-word one.
if is_engine_call_name(n) {
let loc = if self.cur_line > 0 {
self.err_line = self.cur_line;
line_suf(self.cur_line)
} else {
String::new()
};
return Err(raise(
"english-engine-call-name",
&[("name", &folded), ("loc", &loc)],
));
}
Ok(())
}
/// VBA `RefuseCellShaped` (LX.13): the one refusal for a word shaped
/// like a cell where a name is made or a value is read.
fn refuse_cell_shaped(&mut self, word: &str, ln: u32) -> Refusal {
let loc = if ln > 0 {
self.err_line = ln;
line_suf(ln)
} else {
String::new()
};
raise(
"english-cell-shaped-name",
&[
("name", word),
("cell", &cell_word_shown(word)),
("loc", &loc),
],
)
}
/// VBA `RefuseCellShapedRead` (LX.13): a bare cell-shaped word in a
/// finished translation is a cell written without the word "cell".
pub(super) fn refuse_cell_shaped_read(&mut self, vla: &str, ln: u32) -> Result<(), Refusal> {
let word = first_cell_shaped_word(vla);
if word.is_empty() {
Ok(())
} else {
Err(self.refuse_cell_shaped(&word, ln))
}
}
/// VBA `MarkAssigned`: the name is checked and remembered, with the
/// line it was first assigned on (V1).
pub(super) fn mark_assigned(&mut self, name: &str) -> Result<(), Refusal> {
self.check_name(name)?;
super::matcher::add_keyed(&mut self.assigned, name);
if self.in_program && self.cur_line > 0 {
self.assigned_lines
.entry(fold(name))
.or_insert(self.cur_line);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_first_cell_shaped_word_skips_quoted_text() {
assert_eq!(first_cell_shaped_word("(set! (range \"b2\") 5)"), "");
assert_eq!(first_cell_shaped_word("(set! total (+ a2 1))"), "a2");
assert_eq!(first_cell_shaped_word("(set! x \"a\\\"b2\")"), "");
assert_eq!(first_cell_shaped_word("(x data!a1 :key abcd1)"), "");
assert_eq!(cell_word_shown("fy24"), "FY24");
}
}