Skip to content

Commit

Permalink
Translate keywords.pl to python.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesstaats committed Dec 15, 2024
1 parent 875384f commit be5d58e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 82 deletions.
4 changes: 2 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ lex.yy.cc: camp.l

lex.yy.d: $(GCLIB) lex.yy.cc camp.tab.h

keywords.h: camp.l keywords.pl asyprocess.cc
$(PERL) ./keywords.pl --camplfile $< --output $@ --process-file asyprocess.cc
keywords.h: camp.l keywords.py asyprocess.cc
$(PYTHON) ./keywords.py --camplfile $< --output $@ --process-file asyprocess.cc

opsymbols.h: camp.l opsymbols.py
$(PYTHON) ./opsymbols.py --campfile $< --output $@
Expand Down
2 changes: 1 addition & 1 deletion asyprocess.cc
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ class iprompt : public icore {
#define ADDCOMMAND(name, func) \
commands[#name]=&iprompt::func

// keywords.pl looks for ADDCOMMAND to identify special commands in the
// keywords.py looks for ADDCOMMAND to identify special commands in the
// auto-completion.
ADDCOMMAND(quit,exit);
ADDCOMMAND(q,q);
Expand Down
4 changes: 2 additions & 2 deletions cmake-scripts/generated-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ endforeach()
set(KEYWORDS_HEADER_OUT ${GENERATED_INCLUDE_DIR}/keywords.h)
add_custom_command(
OUTPUT ${KEYWORDS_HEADER_OUT}
COMMAND ${PERL_INTERPRETER} ${ASY_SCRIPTS_DIR}/keywords.pl
COMMAND ${PY3_INTERPRETER} ${ASY_SCRIPTS_DIR}/keywords.py
--camplfile ${ASY_RESOURCE_DIR}/camp.l
--output ${GENERATED_INCLUDE_DIR}/keywords.h
--process-file ${ASY_SRC_DIR}/asyprocess.cc
MAIN_DEPENDENCY ${ASY_RESOURCE_DIR}/camp.l
DEPENDS ${ASY_SCRIPTS_DIR}/keywords.pl ${ASY_SRC_DIR}/asyprocess.cc
DEPENDS ${ASY_SCRIPTS_DIR}/keywords.py ${ASY_SRC_DIR}/asyprocess.cc
)

list(APPEND ASYMPTOTE_GENERATED_HEADERS ${KEYWORDS_HEADER_OUT})
Expand Down
77 changes: 0 additions & 77 deletions keywords.pl

This file was deleted.

70 changes: 70 additions & 0 deletions keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python
#####
# keywords.py
# Translated from keywords.pl
#
# Extract keywords from camp.l and list them in a keywords file. These
# keywords are used in autocompletion at the interactive prompt.
#####

import argparse
import re
import sys
import textwrap
from typing import List

parser = argparse.ArgumentParser()
parser.add_argument("--camplfile", required=True)
parser.add_argument("--output", required=True)
parser.add_argument("--process-file", required=True)
args = parser.parse_args()

camplfile = args.camplfile
output_keywords_file = args.output
process_file = args.process_file

# Extra keywords to add that aren't automatically extracted, currently none.
extrawords: List[str] = []

with open(output_keywords_file, "w") as keywords:

keywords.write(
textwrap.dedent("""\
/*****
* This file is automatically generated by keywords.py.
* Changes will be overwritten.
*****/
"""
)
)

def add(word):
keywords.write(f"ADD({word});\n")

for word in extrawords:
add(word)

with open(camplfile) as camp:
# Search for the %% separator, after which the definitions start.
for line in camp:
if re.search(r"^%%\s*$", line):
break

# Grab simple keyword definitions from camp.l
for line in camp:
if re.search(r"^%%\s*$", line):
break # A second %% indicates the end of definitions.
match = re.search(r"^([A-Za-z_][A-Za-z0-9_]*)\s*\{", line)
if match:
add(match.group(1))

# Grab the special commands from the interactive prompt.
with open(process_file) as process:
for line in process:
match = re.search(
r"^\s*ADDCOMMAND\(\s*([A-Za-z_][A-Za-z0-9_]*),",
line,
)
if match:
add(match.group(1))

0 comments on commit be5d58e

Please sign in to comment.