-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.sh
executable file
·54 lines (44 loc) · 1.33 KB
/
compile.sh
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
#!/bin/bash
set -e
rlmeta_compiler="$1"
to_python_string() {
python -c 'import sys; sys.stdout.write(repr(sys.stdin.read()))'
}
support_py_string=$(to_python_string < support.py)
support_py=$(python "$rlmeta_compiler" --support)
parser_py=$(python "$rlmeta_compiler" < parser.rlmeta)
codegenerator_py=$(python "$rlmeta_compiler" < codegenerator.rlmeta)
cat <<EOF
import sys
import pprint
SUPPORT = $support_py_string
$support_py
$parser_py
$codegenerator_py
if __name__ == "__main__":
if "--support" in sys.argv:
sys.stdout.write(SUPPORT)
else:
try:
sys.stdout.write(
CodeGenerator().run(
"ast",
[Parser().run("grammar", sys.stdin.read())]
)
)
except MatchError as e:
stream = e.stream
for pos in e.pos[:-1]:
stream = stream[pos]
pos = e.pos[-1]
MARKER = "\\033[0;31m<ERROR POSITION>\\033[0m"
if isinstance(stream, basestring):
stream_string = stream[:pos] + MARKER + stream[pos:]
else:
stream_string = pprint.pformat(stream)
sys.exit("ERROR: {}\\nPOSITION: {}\\nSTREAM:\\n{}".format(
e.message,
pos,
indent(stream_string)
))
EOF