interplane.Get Omega
Read in your language

tree-sitter-omega

aien-dev/tree-sitter-omega

Brought by drakestapleton

branch main Branches Tags History

tree-sitter-omega / scripts / coverage.sh

191 lines · 9487 bytes Raw Blame History
1#!/bin/sh
2# Parse coverage of this grammar over real Omega: every *.om file under one or more directories (CI:
3# an aien-dev/omega-mv checkout), with omc as the judge of which files are valid Omega, and
4# optionally the composed units a repository builds by concatenation (library units are compiled only
5# inside such a unit, never alone).
6#
7# Usage:
8# scripts/coverage.sh [--omc OMC] [--units MANIFEST]... [LABEL=]DIR...
9#
10# DIR is searched for *.om files. LABEL (default: the basename of DIR) names the directory in
11# manifests and, when more than one DIR is given, prefixes every path printed (LABEL/PATH).
12#
13# MANIFEST lists composed units, one per line (`#` starts a comment line):
14# unit NAME MEMBER... the members concatenated in order
15# each NAME LABEL:GLOB MEMBER... one unit per directory or file GLOB matches; in NAME and in the
16# members, `%` is that match (LABEL:path) and `%n` its basename
17# (less a trailing .om)
18# A MEMBER is LABEL:PATH (PATH relative to that DIR, may hold a glob, expanded in C order), or `@om2`
19# / `@ir` for that profile line. A member that matches no file is a usage error.
20#
21# stdout, one line per file (tab separated):
22# <path> omc=<accept|refuse|not_run> parse=<clean|error> <first ERROR/MISSING node or ->
23# then one line per unit, the node followed by the member and line it falls in:
24# unit:<NAME> omc=<accept|refuse|not_run> parse=<clean|error> <node in MEMBER:LINE or ->
25# then the summary lines:
26# COVERAGE all <clean>/<total>
27# COVERAGE omc-accepted <clean>/<accepted> (only with --omc)
28# COVERAGE omc-refused-flagged <error>/<refused> (refused files the grammar also marks; this
29# grammar is not a type checker, so most parse clean)
30# COVERAGE units-accepted <clean>/<accepted> refused=<n> refused-files-covered=<n>
31# (only with --units; the last field counts files omc
32# refuses alone that are members of an accepted unit)
33# each followed by LABEL=<commit> for every DIR (`unpinned` outside git).
34# With an omc (AArch64 Linux; seed/out/omc after `sh seed/omega-from-seed.sh`), each file and each
35# unit is fed to it on stdin and its exit status decides accept (0) or refuse (non-zero).
36# Exit status: 0 when every omc-accepted file and unit parses clean (or no omc was given), 1 otherwise,
37# 2 on a usage error. Diagnostics go to stderr.
38set -eu
39export LC_ALL=C
40usage() {
41 [ $# = 0 ] || echo "coverage.sh: $*" >&2
42 echo "usage: coverage.sh [--omc OMC] [--units MANIFEST]... [LABEL=]DIR..." >&2
43 exit 2
44}
45here=$(cd "$(dirname "$0")/.." && pwd)
46ts="$here/node_modules/.bin/tree-sitter"
47[ -x "$ts" ] || { echo "coverage.sh: $ts missing; run npm ci" >&2; exit 2; }
48work=$(mktemp -d)
49trap 'rm -rf "$work"' EXIT
50omc=
51: >"$work/manifests"
52: >"$work/dirs"
53while [ $# -gt 0 ]; do
54 case $1 in
55 --omc) [ $# -ge 2 ] || usage "--omc needs a path"; omc=$2; shift 2 ;;
56 --units) [ $# -ge 2 ] || usage "--units needs a file"; [ -f "$2" ] || usage "no manifest $2"
57 printf '%s\n' "$2" >>"$work/manifests"; shift 2 ;;
58 -*) usage "unknown option $1" ;;
59 *) case $1 in
60 *=*) label=${1%%=*}; dir=${1#*=} ;;
61 *) dir=$1; label=$(basename "$1") ;;
62 esac
63 [ -d "$dir" ] || usage "$dir is not a directory"
64 case $label in ''|*[!A-Za-z0-9._-]*) usage "bad label '$label'" ;; esac
65 ! awk -F'\t' -v l="$label" '$1 == l { f = 1 } END { exit !f }' "$work/dirs" || usage "label $label given twice"
66 printf '%s\t%s\n' "$label" "$(cd "$dir" && pwd)" >>"$work/dirs"
67 shift ;;
68 esac
69done
70[ -s "$work/dirs" ] || usage
71[ -z "$omc" ] || [ -x "$omc" ] || usage "omc '$omc' is not executable"
72ndirs=$(wc -l <"$work/dirs")
73dirof() { awk -F'\t' -v l="$1" '$1 == l { print $2; f = 1 } END { exit !f }' "$work/dirs"; }
74show() { if [ "$ndirs" = 1 ]; then printf '%s' "$2"; else printf '%s/%s' "$1" "$2"; fi; }
75
76# files: label, path relative to its DIR, path printed, absolute path
77: >"$work/files"
78while IFS="$(printf '\t')" read -r label dir; do
79 (cd "$dir" && find . -name '*.om' -type f | sed 's|^\./||' | sort) | while IFS= read -r f; do
80 printf '%s\t%s\t%s\t%s\n' "$label" "$f" "$(show "$label" "$f")" "$dir/$f"
81 done >>"$work/files"
82done <"$work/dirs"
83[ -s "$work/files" ] || { echo "coverage.sh: no .om files under the given directories" >&2; exit 2; }
84
85# units: compose each into $work/u/<n>.om; <n>.map holds `<first line> <member printed>` per member
86mkdir "$work/u"
87: >"$work/units"
88nu=0
89member() { # LABEL:PATH or @om2/@ir -> appends to the unit being composed
90 case $1 in
91 @om2|@ir) printf '%s\n' "${1#@}" >>"$cur"; printf '%s\t%s\n' "$line" "$1" >>"$map"; line=$((line + 1)); return ;;
92 *:*) ;;
93 *) echo "coverage.sh: $mf: member '$1' is not LABEL:PATH, @om2 or @ir" >&2; exit 2 ;;
94 esac
95 ml=${1%%:*}; mp=${1#*:}
96 md=$(dirof "$ml") || { echo "coverage.sh: $mf: no DIR labelled '$ml'" >&2; exit 2; }
97 hits=$(cd "$md" && set +f && for g in $mp; do [ -f "$g" ] && printf '%s\n' "$g"; done; true)
98 [ -n "$hits" ] || { echo "coverage.sh: $mf: member $1 matches no file" >&2; exit 2; }
99 for g in $hits; do
100 cat "$md/$g" >>"$cur"
101 printf '%s\t%s\n' "$line" "$(show "$ml" "$g")" >>"$map"
102 line=$(( $(wc -l <"$cur") + 1 ))
103 done
104}
105compose() { # NAME MEMBER...
106 nu=$((nu + 1)); cur="$work/u/$nu.om"; map="$work/u/$nu.map"; : >"$cur"; : >"$map"; line=1
107 printf '%s\t%s\n' "$nu" "$1" >>"$work/units"; shift
108 for m in "$@"; do member "$m"; done
109}
110set -f
111while IFS= read -r mf; do
112 while IFS= read -r l || [ -n "$l" ]; do
113 set -- $l
114 [ $# -gt 0 ] || continue
115 case $1 in
116 \#*) ;;
117 unit) [ $# -ge 3 ] || { echo "coverage.sh: $mf: '$l' needs NAME and members" >&2; exit 2; }
118 shift; compose "$@" ;;
119 each) [ $# -ge 4 ] || { echo "coverage.sh: $mf: '$l' needs NAME, LABEL:GLOB and members" >&2; exit 2; }
120 tpl=$2; gl=${3%%:*}; gp=${3#*:}; shift 3
121 gd=$(dirof "$gl") || { echo "coverage.sh: $mf: no DIR labelled '$gl'" >&2; exit 2; }
122 ds=$(cd "$gd" && set +f && for g in $gp; do [ -e "$g" ] && printf '%s\n' "${g%/}"; done; true)
123 [ -n "$ds" ] || { echo "coverage.sh: $mf: $gl:$gp matches nothing" >&2; exit 2; }
124 for d in $ds; do
125 n=$(basename "$d" .om)
126 # Manifest words hold no blanks, so the expanded words split back on blanks (globbing is off).
127 compose $(for a in "$tpl" "$@"; do printf '%s\n' "$a" | sed "s|%n|$n|g; s|%|$gl:$d|g"; done)
128 done ;;
129 *) echo "coverage.sh: $mf: unknown line '$l'" >&2; exit 2 ;;
130 esac
131 done <"$mf"
132done <"$work/manifests"
133set +f
134
135# --quiet prints only the files whose tree holds an ERROR or MISSING node, with the first one.
136{ cut -f4 "$work/files"; awk -F'\t' -v u="$work/u" '{ print u "/" $1 ".om" }' "$work/units"; } >"$work/abs"
137"$ts" parse -p "$here" --quiet --paths "$work/abs" 2>"$work/parse.err" >"$work/parse.out" || true
138firstnode() { awk -v p="$1" -F'\t' '{ sub(/ +$/, "", $1) } $1 == p { n = $NF; sub(/^[^(]*/, "", n); print n; exit }' "$work/parse.out"; }
139judge() { if [ -z "$omc" ]; then echo not_run; elif timeout "$2" "$omc" <"$1" >/dev/null 2>&1; then echo accept; else echo refuse; fi; }
140
141total=0; clean=0; acc=0; acc_clean=0; ref=0; ref_flag=0
142: >"$work/refused"
143while IFS="$(printf '\t')" read -r label rel p abs; do
144 total=$((total + 1))
145 node=$(firstnode "$abs")
146 if [ -n "$node" ]; then parse=error; else parse=clean; node=-; clean=$((clean + 1)); fi
147 verdict=$(judge "$abs" 20)
148 case $verdict in
149 accept) acc=$((acc + 1)); [ "$parse" = error ] || acc_clean=$((acc_clean + 1)) ;;
150 refuse) ref=$((ref + 1)); [ "$parse" = clean ] || ref_flag=$((ref_flag + 1)); printf '%s\n' "$p" >>"$work/refused" ;;
151 esac
152 printf '%s\tomc=%s\tparse=%s\t%s\n' "$p" "$verdict" "$parse" "$node"
153done <"$work/files"
154
155uacc=0; uacc_clean=0; uref=0
156: >"$work/covered"
157while IFS="$(printf '\t')" read -r n name; do
158 f="$work/u/$n.om"
159 node=$(firstnode "$f")
160 if [ -n "$node" ]; then
161 parse=error
162 row=$(printf '%s\n' "$node" | sed -n 's/^([^[]*\[\([0-9]*\),.*/\1/p')
163 at=$(awk -F'\t' -v r="$((row + 1))" '$1 <= r { m = $2; s = $1 } END { print m ":" r - s + 1 }' "$work/u/$n.map")
164 node="$node in $at"
165 else parse=clean; node=-; fi
166 verdict=$(judge "$f" 120)
167 case $verdict in
168 accept) uacc=$((uacc + 1)); [ "$parse" = error ] || uacc_clean=$((uacc_clean + 1)); cut -f2 "$work/u/$n.map" >>"$work/covered" ;;
169 refuse) uref=$((uref + 1)) ;;
170 esac
171 printf 'unit:%s\tomc=%s\tparse=%s\t%s\n' "$name" "$verdict" "$parse" "$node"
172done <"$work/units"
173
174pins=$(while IFS="$(printf '\t')" read -r label dir; do
175 printf ' %s=%s' "$label" "$(git -C "$dir" rev-parse HEAD 2>/dev/null || echo unpinned)"; done <"$work/dirs")
176echo "COVERAGE all $clean/$total$pins"
177if [ -n "$omc" ]; then
178 echo "COVERAGE omc-accepted $acc_clean/$acc$pins"
179 echo "COVERAGE omc-refused-flagged $ref_flag/$ref$pins"
180else
181 echo "COVERAGE omc-accepted NOT_RUN (no omc given)"
182fi
183if [ -s "$work/units" ]; then
184 if [ -n "$omc" ]; then
185 covered=$(sort -u "$work/covered" | grep -Fxf "$work/refused" | wc -l || true)
186 echo "COVERAGE units-accepted $uacc_clean/$uacc refused=$uref refused-files-covered=$covered$pins"
187 else
188 echo "COVERAGE units-accepted NOT_RUN (no omc given)"
189 fi
190fi
191[ "$acc_clean" = "$acc" ] && [ "$uacc_clean" = "$uacc" ] || exit 1