Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 8afe17b

Browse files
a script for sussing out the relationship between language extension files and language identifiers
1 parent 9d50782 commit 8afe17b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tools/lang-handler-aliases.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
LANG_DIR="$1"
4+
shift
5+
6+
HAS_LANG_FILES=1
7+
if echo "$LANG_DIR"/lang-*.js | egrep -q "[*]" >& /dev/null; then
8+
HAS_LANG_FILES=0
9+
fi
10+
11+
if [ -n "$*" ] || [ -z "$LANG_DIR" ] || ! (( $HAS_LANG_FILES )); then
12+
echo "Usage: $0 <LANG_DIR>"
13+
echo
14+
echo "Dumps lines of the form"
15+
echo " lang-foo.js : <extension>"
16+
echo "where the first element is a path name under LANG_DIR"
17+
echo "and the second is the name of a language extension for which"
18+
echo "it registers an extension."
19+
exit -1
20+
fi
21+
22+
if [ -z "$JS_INTERPRETER" ]; then
23+
JS_INTERPRETER="jsc-1"
24+
fi
25+
26+
if ! which "$JS_INTERPRETER" >& /dev/null; then
27+
echo "\$JS_INTERPRETER ( '$JS_INTERPRETER' ) is not on the \$PATH."
28+
echo "It should be an executable that loads each argument as a JS file"
29+
echo "and runs them in a context where the print function dumps a string"
30+
echo "to stdout."
31+
exit -1
32+
fi
33+
34+
for JS in "$LANG_DIR"/lang-*.js; do
35+
# Run the language handler in a context where PR.registerLangHandler
36+
# dumps out the handler names without doing anything else, and
37+
# then use a perl script that prepends each handler with the basename
38+
# of the JS file.
39+
# The JS interpreter is run with STDIN of /dev/null so that it does not
40+
# hand waiting for REPL input.
41+
("$JS_INTERPRETER" \
42+
<(echo '
43+
44+
var PR = {
45+
registerLangHandler: function (_, exts) {
46+
for (var i = 0, n = exts.length; i < n; ++i) {
47+
var handler = String(exts[i]);
48+
if (/^\w+$/.test(handler)) {
49+
print(handler);
50+
}
51+
}
52+
},
53+
createSimpleLexer: function () {},
54+
sourceDecorator: function () {}
55+
};
56+
57+
') \
58+
\
59+
"$JS" \
60+
< /dev/null \
61+
|| echo "Failed to execute $JS" 1>&2 ) \
62+
| perl -e '$BASE=shift; while (<STDIN>) { s/^(?=\w)/$BASE : /; print; }' \
63+
"$(basename "$JS")"
64+
done

0 commit comments

Comments
 (0)