raz / test / asks.sh

59 lines · 2.5 KiB · executable · raw · history · last change f3dc7707

1#!/bin/sh
2# What the compiler asks of the library: every qualified name of lib/,
3# Sys.write, Rt.exit and so on, that src/ uses in code (not in a comment,
4# a string or a character) must be a row of the tables under "What the
5# compiler asks of Sys, Rt and Files" in doc/compiler.md, and every row
6# must be used. Prints the differences and exits 1 if there are any.
7# Run from the repository's root.
8
9doc=doc/compiler.md
10tmp=${TMPDIR:-/tmp}/asks.$$
11trap 'rm -f $tmp.*' EXIT
12
13# src/*.raz with comments, strings and characters cut out, so that what
14# is left is code. Comments (* ... *) don't nest and may span lines, and
15# so may a `...` string; "..." and '...' end at the line's end at most.
16code() {
17 awk '
18 BEGIN { state = "code" }
19 {
20 n = length($0); out = ""; i = 1
21 while (i <= n) {
22 c = substr($0, i, 1); d = substr($0, i, 2)
23 if (state == "comment") {
24 if (d == "*)") { state = "code"; i += 2 } else i++
25 } else if (state == "raw") {
26 if (c == "`") state = "code"
27 i++
28 } else if (state == "dq" || state == "sq") {
29 if (c == "\\") i += 2
30 else {
31 if ((state == "dq" && c == "\"") || (state == "sq" && c == "\047")) state = "code"
32 i++
33 }
34 } else if (d == "(*") { state = "comment"; i += 2 }
35 else if (c == "`") { state = "raw"; i++ }
36 else if (c == "\"") { state = "dq"; i++ }
37 else if (c == "\047") { state = "sq"; i++ }
38 else { out = out c; i++ }
39 }
40 if (state == "dq" || state == "sq") state = "code"
41 print out
42 }' src/*.raz
43}
44
45# the modules of lib/, as the names a program imports them by
46libs=$(ls lib | sed 's/\..*//' | sort -u | tr '\n' '|' | sed 's/|$//')
47
48code | grep -oE "\b($libs)\.[A-Za-z_][A-Za-z0-9_]*" | sort -u > $tmp.used
49# the names in the first cell of a row of a table in the section:
50# "| `Sys.close`, `Sys.Dir` | what for |"
51sed -n '/^## What the compiler asks of Sys, Rt and Files/,/^## [^W]/p' $doc |
52 sed -n 's/^| `\([^|]*\)|.*/`\1/p' |
53 grep -oE '`[A-Za-z]+\.[A-Za-z_][A-Za-z0-9_]*`' | tr -d '`' | sort -u > $tmp.listed
54
55status=0
56if [ ! -s $tmp.listed ]; then echo "asks: $doc has no rows for the library names"; status=1; fi
57if comm -23 $tmp.used $tmp.listed | sed "s|^|src/ uses it, $doc doesn't say: |" | grep .; then status=1; fi
58if comm -13 $tmp.used $tmp.listed | sed "s|^|$doc lists it, src/ never uses it: |" | grep .; then status=1; fi
59exit $status