json-parser-prolog-lisp/mktracecmd.py

56 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
import re
import sys
from typing import List
def find_functors(prolog_source) -> List[str]:
"""
Find all functors in the given Prolog source code.
Functors look like the following:
functor_name(arg1, arg2, ..., argN).
another_functor(arg1, arg2, ..., argN) :- stuff...
Functors always start at the beginning of the line. All indented blocks are skipped.
We make sure that the functor name is followed by '('.
This function extracts only the names of the functors, not the arguments.
"""
functors = re.findall(r"^(?!\s)[a-z_]+(?=\()", prolog_source, re.MULTILINE)
# Remove duplicates
functors = list(set(functors))
return functors
def main():
"""
1. Read the Prolog source file specified in the first command line argument
2. Find all functors
3. Output a trace command for each functor, like the following: "trace(functor1), trace(functor2), ..., trace(functorN)."
"""
if len(sys.argv) < 2:
print("Usage: mktracecmd.py <prolog source file>")
sys.exit(1)
# Read the Prolog source file
with open(sys.argv[1], "r") as f:
prolog_source = f.read()
# Find all functors
functors = find_functors(prolog_source)
if "concat" in functors:
functors.remove("concat")
# Output a trace command for each functor
print("trace(" + "), trace(".join(functors) + ").")
if __name__ == "__main__":
main()