Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make RLMeta compilable in one command and replace Bash scripts with P…
…ython.
- Loading branch information
1 parent
6127d4f
commit 935bb77
Showing
6 changed files
with
223 additions
and
132 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
def compile_grammar(grammar): | ||
return CodeGenerator().run( | ||
"ast", | ||
[Parser().run("grammar", grammar)] | ||
) | ||
|
||
if __name__ == "__main__": | ||
import sys | ||
import pprint | ||
def read(path): | ||
if path == "-": | ||
return sys.stdin.read() | ||
with open(path) as f: | ||
return f.read() | ||
args = sys.argv[1:] or ["--compile", "-"] | ||
while args: | ||
command = args.pop(0) | ||
if command == "--support": | ||
sys.stdout.write(SUPPORT) | ||
elif command == "--copy": | ||
sys.stdout.write(read(args.pop(0))) | ||
elif command == "--embed": | ||
sys.stdout.write("{} = {}\n".format( | ||
args.pop(0), | ||
repr(read(args.pop(0))) | ||
)) | ||
elif command == "--compile": | ||
try: | ||
sys.stdout.write(compile_grammar(read(args.pop(0)))) | ||
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) | ||
)) | ||
else: | ||
sys.exit("ERROR: Unknown command '{}'".format(command)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import subprocess | ||
import sys | ||
|
||
def make_next_version(): | ||
intermediate_compilers = meta_compile_rlmeta() | ||
final_compiler = intermediate_compilers.pop(-1) | ||
test(final_compiler) | ||
mv(final_compiler, "rlmeta.py") | ||
for compiler in intermediate_compilers: | ||
rm(compiler) | ||
log("OK!") | ||
|
||
def test(rlmeta): | ||
log("Test: Has its own support library") | ||
assert run_rlmeta(rlmeta, ["--support"]) == read("support.py") | ||
log("Test: Disallow semantic action in the middle") | ||
run_rlmeta(rlmeta, [], "Grammar { x = . -> [] . }", expect_failure=True) | ||
|
||
def meta_compile_rlmeta(): | ||
compiler = "rlmeta.py" | ||
content = read(compiler) | ||
intermediate_compilers = [] | ||
for i in range(4): | ||
next_compiler = "rlmeta{}.py".format(i+1) | ||
log("Compiling {} -> {}".format(compiler, next_compiler)) | ||
next_content = compile_rlmeta(compiler) | ||
write(next_compiler, next_content) | ||
intermediate_compilers.append(next_compiler) | ||
if next_content == content: | ||
return intermediate_compilers | ||
compiler = next_compiler | ||
content = next_content | ||
fail("Unable to produce metacompiler.") | ||
|
||
def compile_rlmeta(rlmeta): | ||
return run_rlmeta(rlmeta, [ | ||
"--embed", "SUPPORT", "support.py", | ||
"--support", | ||
"--compile", "parser.rlmeta", | ||
"--compile", "codegenerator.rlmeta", | ||
"--copy", "main.py", | ||
]) | ||
|
||
def run_rlmeta(rlmeta, args, stdin="", expect_failure=False): | ||
process = subprocess.Popen( | ||
["python", rlmeta]+args, | ||
stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE | ||
) | ||
stdout, _ = process.communicate(stdin) | ||
if expect_failure: | ||
if process.returncode == 0: | ||
fail("Expected failure") | ||
else: | ||
if process.returncode != 0: | ||
fail("Expected success") | ||
return stdout | ||
|
||
def mv(src, dest): | ||
log("Move {} -> {}".format(src, dest)) | ||
os.remove(dest) | ||
os.rename(src, dest) | ||
|
||
def rm(path): | ||
log("Delete {}".format(path)) | ||
os.remove(path) | ||
|
||
def read(path): | ||
with open(path) as f: | ||
return f.read() | ||
|
||
def write(path, content): | ||
with open(path, "w") as f: | ||
return f.write(content) | ||
|
||
def log(message): | ||
sys.stdout.write("\033[0;33m{}\033[0m\n".format(message)) | ||
|
||
def fail(message): | ||
sys.exit("\033[0;31mERROR: {}\033[0m".format(message)) | ||
|
||
if __name__ == "__main__": | ||
make_next_version() |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.