voltaic-driver 0.2.0

Incremental compilation driver for the Voltaic compiler.
Documentation
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
use std::collections::BTreeMap;

use std::fmt::Display;

use std::fmt::Formatter;

use std::fmt::Result;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Class {
  Auto,
  Review,
  Manual,
}
#[allow(unused_imports)]
use Class::*;

impl Display for Class {
  fn fmt(&self, f: &mut Formatter<'_>) -> Result {
    match self {
      Class::Auto => write!(f, "auto"),
      Class::Review => write!(f, "review"),
      Class::Manual => write!(f, "manual"),
    }
  }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Position {
  Anywhere,
  AtEnd,
  AtStart,
}
#[allow(unused_imports)]
use Position::*;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Marker {
  pub needle: String,
  pub construct: String,
  pub kind: Class,
  pub position: Position,
}

pub fn markers() -> Vec<Marker> {
  let mut found = Vec::new();
  at_end(&mut found, "do", "a `do` block", Class::Manual);
  add(
    &mut found,
    "Option<Option<",
    "a nested optional",
    Class::Manual,
  );
  add(
    &mut found,
    "FieldNames",
    "`FieldNames` in a derive template",
    Class::Manual,
  );
  add(
    &mut found,
    "Fields",
    "`Fields` in a derive template",
    Class::Manual,
  );
  add(
    &mut found,
    "__vlt_",
    "a generated identifier named by hand",
    Class::Manual,
  );
  add(
    &mut found,
    ".and_then",
    "`.and_then` over a std shape",
    Class::Review,
  );
  add(&mut found, "where", "a `where` group", Class::Review);
  add(&mut found, "|>", "the `|>` pipeline", Class::Auto);
  add(&mut found, ">>>", "forward composition", Class::Auto);
  add(&mut found, "<<<", "reverse composition", Class::Auto);
  add(&mut found, "otherwise", "`otherwise`", Class::Auto);
  add(&mut found, "newtype ", "`newtype`", Class::Auto);
  add(&mut found, "errors ", "`errors`", Class::Auto);
  add(&mut found, "deriving ", "`deriving`", Class::Auto);
  add(&mut found, "instance ", "an `instance` header", Class::Auto);
  add(&mut found, "import ", "`import`", Class::Auto);
  add(&mut found, "export ", "`export`", Class::Auto);
  add(&mut found, "module ", "`module`", Class::Auto);
  add(&mut found, "async ", "`async`", Class::Auto);
  add(&mut found, ".await", "`await`", Class::Auto);
  add(&mut found, "catch ", "`catch`", Class::Auto);
  add(&mut found, "&mut ", "an explicit `&mut`", Class::Auto);
  at_start(&mut found, "if", "an `if` statement", Class::Review);
  at_start(&mut found, "while", "a `while` loop", Class::Review);
  at_start(&mut found, "for", "a `for` loop", Class::Review);
  add(&mut found, "case", "a `case` expression", Class::Review);
  at_start(&mut found, "guard", "a `guard`", Class::Auto);
  found
}

fn add(into: &mut Vec<Marker>, needle: &str, construct: &str, kind: Class) {
  at(into, needle, construct, kind, Position::Anywhere);
}

fn at_end(into: &mut Vec<Marker>, needle: &str, construct: &str, kind: Class) {
  at(into, needle, construct, kind, Position::AtEnd);
}

fn at_start(into: &mut Vec<Marker>, needle: &str, construct: &str, kind: Class) {
  at(into, needle, construct, kind, Position::AtStart);
}

fn at(into: &mut Vec<Marker>, needle: &str, construct: &str, kind: Class, position: Position) {
  into.push(Marker {
    needle: needle.to_string(),
    construct: construct.to_string(),
    kind,
    position,
  });
  ();
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Finding {
  pub line: usize,
  pub construct: String,
  pub kind: Class,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Verdict {
  pub kind: Class,
  pub findings: Vec<Finding>,
}

impl Verdict {
  pub fn of_class(&self, wanted: Class) -> Vec<&Finding> {
    self.findings.iter().filter(|f| f.kind == wanted).collect()
  }
}

pub fn classify(source: &str) -> Verdict {
  let table = markers();
  let mut findings = Vec::new();
  let mut worst = Class::Auto;
  for pair in source.lines().enumerate() {
    let text = pair.1;
    if is_comment(text) {
      continue;
    }
    for marker in table.iter() {
      if hits(text, marker) {
        if marker.kind > worst {
          worst = marker.kind;
        }
        findings.push(Finding {
          line: pair.0 + 1,
          construct: marker.construct.clone(),
          kind: marker.kind,
        });
      }
    }
  }
  Verdict {
    kind: worst,
    findings,
  }
}

pub fn hits(text: &str, marker: &Marker) -> bool {
  let needle = marker.needle.as_str();
  match marker.position {
    Position::AtEnd => text.trim_end().ends_with(needle) && mentions(text, needle),
    Position::AtStart => opens_a_statement(text, needle),
    Position::Anywhere => mentions(text, needle),
  }
}

pub fn opens_a_statement(text: &str, raw: &str) -> bool {
  let needle = raw.trim();
  let body = text.trim_start();
  if !body.starts_with(needle) {
    return false;
  }
  let start = needle.len();
  let end = body.len();
  start >= end || !is_word_byte(&body[start..start + 1])
}

pub fn mentions(text: &str, raw: &str) -> bool {
  if !is_word(raw) {
    return text.contains(raw);
  }
  let needle = raw.trim();
  for hit in text.match_indices(needle) {
    let start = hit.0;
    let end = start + needle.len();
    let before_ok = start == 0 || !is_word_byte(&text[start - 1..start]);
    let after_ok = end >= text.len() || !is_word_byte(&text[end..end + 1]);
    if before_ok && after_ok {
      return true;
    }
  }
  false
}

fn is_word(needle: &str) -> bool {
  needle
    .trim()
    .chars()
    .all(|c| c == '_' || char::is_alphanumeric(c) || c == ' ')
}

fn is_word_byte(held: &str) -> bool {
  held.chars().any(|c| c == '_' || char::is_alphanumeric(c))
}

fn is_comment(text: &str) -> bool {
  text.trim_start().starts_with("--")
}

#[derive(Debug, Clone, Default)]
pub struct Tally {
  pub files: usize,
  pub by_class: BTreeMap<Class, usize>,
}

impl Tally {
  pub fn new() -> Self {
    Tally {
      files: 0,
      by_class: BTreeMap::new(),
    }
  }

  pub fn record(&mut self, kind: Class) {
    self.files += 1;
    let counter = self.by_class.entry(kind).or_insert(0);
    *counter += 1;
    ();
  }

  pub fn count(&self, kind: Class) -> usize {
    self.by_class.get(&kind).copied().unwrap_or(0)
  }

  pub fn blocking(&self) -> usize {
    self.count(Class::Manual)
  }
}

#[cfg(test)]
mod __vlt_tests {
  use super::*;

  #[test]
  fn a_tail_needle_must_end_the_line_because_do_introduces_a_block() {
    assert_eq!(classify("held = do\n").kind, Class::Manual);
    assert_eq!(
      classify("test \"two names do not conflict\"\n").kind,
      Class::Auto,
      "the English word is not the construct"
    );
    assert_eq!(
      classify("    \"do\" -> Some Do\n").kind,
      Class::Auto,
      "a keyword table names `do` without using it"
    );
  }

  #[test]
  fn a_tail_needle_still_needs_its_word_boundary() {
    assert_eq!(
      classify("shown = tornado\n").kind,
      Class::Auto,
      "`tornado` ends in `do` and is not a block"
    );
  }

  #[test]
  fn a_word_needle_needs_a_word_boundary_on_both_sides() {
    assert!(mentions("data Fields = Fields", "Fields"));
    assert!(
      !mentions("ConstructorFields::Record", "Fields"),
      "a longer identifier is not the construct"
    );
    assert!(
      !mentions("elsewhere = 1", "where"),
      "`elsewhere` is not a `where` group"
    );
  }

  #[test]
  fn a_punctuation_needle_cannot_have_a_word_boundary_so_it_matches_as_a_substring() {
    assert!(mentions("shown = value |> .to_string", "|>"));
    assert!(mentions("held.await", ".await"));
  }

  #[test]
  fn a_needle_at_the_very_start_or_end_of_a_line_still_matches() {
    assert!(mentions("Fields", "Fields"));
    assert!(mentions("newtype Meters = Meters f64", "newtype "));
  }

  #[test]
  fn the_classifier_does_not_match_its_own_marker_table() {
    let found = classify("  at_end (&mut found) \"do\" \"a block\" Class::Manual\n");
    assert_eq!(
      found.kind,
      Class::Auto,
      "a source file that describes source is the hardest case for text matching, and the tail anchor is what answers it"
    );
  }

  #[test]
  fn a_file_with_nothing_notable_is_auto() {
    let found = classify("twice : u64 -> u64\ntwice n = n + n\n");
    assert_eq!(found.kind, Class::Auto);
  }

  #[test]
  fn a_pipeline_is_auto_because_the_rewrite_is_mechanical() {
    let found = classify("shown = value |> .to_string\n");
    assert_eq!(found.kind, Class::Auto);
    assert_eq!(found.of_class(Class::Auto).len(), 1);
  }

  #[test]
  fn a_nested_optional_is_manual_and_doc_86_5_6_is_why() {
    let found = classify("held : Option<Option<u64>>\n");
    assert_eq!(found.kind, Class::Manual);
  }

  #[test]
  fn the_worst_class_in_a_file_is_the_file_s_class() {
    let found = classify("shown = value |> .to_string\nheld : Option<Option<u64>>\n");
    assert_eq!(
      found.kind,
      Class::Manual,
      "one manual construct makes the file manual"
    );
    assert_eq!(
      found.of_class(Class::Auto).len(),
      1,
      "the auto finding is still reported"
    );
  }

  #[test]
  fn review_sits_between_auto_and_manual() {
    assert!(Class::Auto < Class::Review);
    assert!(Class::Review < Class::Manual);
  }

  #[test]
  fn a_construct_inside_a_comment_is_not_a_finding() {
    let found = classify(
      "-- this names |> and Option<Option<u64>> and neither counts\ntwice : u64 -> u64\ntwice n = n + n\n",
    );
    assert_eq!(found.kind, Class::Auto, "a comment is prose, not code");
    assert!(found.findings.is_empty());
  }

  #[test]
  fn a_finding_carries_its_line_counting_from_one() {
    let found = classify("twice : u64 -> u64\ntwice n = n |> .count\n");
    assert_eq!(found.findings[0].line, 2);
  }

  #[test]
  fn a_tally_counts_files_per_class_and_names_the_blocking_number() {
    let mut tally = Tally::new();
    tally.record(Class::Auto);
    tally.record(Class::Auto);
    tally.record(Class::Manual);
    assert_eq!(tally.files, 3);
    assert_eq!(tally.count(Class::Auto), 2);
    assert_eq!(tally.count(Class::Review), 0);
    assert_eq!(
      tally.blocking(),
      1,
      "doc 89 milestone 17 asks this to reach zero"
    );
  }

  #[test]
  fn every_marker_names_a_construct_so_a_report_can_be_read() {
    for marker in markers().iter() {
      assert!(!marker.needle.is_empty());
      assert!(!marker.construct.is_empty());
    }
  }

  #[test]
  fn the_marker_table_covers_all_three_classes() {
    let mut seen = Vec::new();
    for marker in markers().iter() {
      if !seen.contains(&marker.kind) {
        seen.push(marker.kind);
      }
    }
    assert_eq!(
      seen.len(),
      3,
      "a table missing a class would report that class as absent"
    );
  }
}