| 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 | |
| 9 | doc=doc/compiler.md |
| 10 | tmp=${TMPDIR:-/tmp}/asks.$$ |
| 11 | trap '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. |
| 16 | code() { |
| 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 |
| 46 | libs=$(ls lib | sed 's/\..*//' | sort -u | tr '\n' '|' | sed 's/|$//') |
| 47 | |
| 48 | code | 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 |" |
| 51 | sed -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 | |
| 55 | status=0 |
| 56 | if [ ! -s $tmp.listed ]; then echo "asks: $doc has no rows for the library names"; status=1; fi |
| 57 | if comm -23 $tmp.used $tmp.listed | sed "s|^|src/ uses it, $doc doesn't say: |" | grep .; then status=1; fi |
| 58 | if comm -13 $tmp.used $tmp.listed | sed "s|^|$doc lists it, src/ never uses it: |" | grep .; then status=1; fi |
| 59 | exit $status |