Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert several perl scripts to python #511

Merged
merged 17 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ $(SYMBOL_FILES:=.d): %.d: %.cc gen_preprocessed_depfile.py
--out-i-file=$*.raw.i \
--in-src-file=$<

$(SYMBOL_FILES:=.symbols.h): %.symbols.h: %.raw.i findsym.pl
$(PERL) findsym.pl $@ $<
$(SYMBOL_FILES:=.symbols.h): %.symbols.h: %.raw.i findsym.py
$(PYTHON) findsym.py $@ $<

$(SYMBOL_FILES:=.o): %.o: %.symbols.h

allsymbols.h: $(SYMBOL_FILES:=.raw.i) findsym.pl
$(PERL) ./findsym.pl $@ $(filter-out findsym.pl,$^)
allsymbols.h: $(SYMBOL_FILES:=.raw.i) findsym.py
$(PYTHON) ./findsym.py $@ $(filter-out findsym.py,$^)

symbol.o: $(SYMBOLSH)

Expand All @@ -254,11 +254,11 @@ 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.pl
$(PERL) ./opsymbols.pl --campfile $< --output $@
opsymbols.h: camp.l opsymbols.py
$(PYTHON) ./opsymbols.py --campfile $< --output $@

envcompleter.d: keywords.h

Expand All @@ -268,7 +268,7 @@ asy-keywords.el: asy base/v3dtypes.asy base/v3dheadertypes.asy
ls $(addsuffix /*.asy,$(KEYWORDS)) | grep -v plain\* | \
grep -v three_\* | grep -v map | xargs $(ASY) -l >> asy.list
revision=`cat version.txt`
$(PERL) ./asy-list.pl --asy-list-file asy.list --revision $(revision) --output-file $@
$(PYTHON) ./asy-list.py --asy-list-file asy.list --revision $(revision) --output-file $@

install-notexhash: asy-keywords.el install-asy install-man

Expand Down
125 changes: 0 additions & 125 deletions asy-list.pl

This file was deleted.

145 changes: 145 additions & 0 deletions asy-list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python3
#####
# asy-list.py
#
# Build asy-keywords.el from a list of Asymptote global functions and
# variables. This script reads definitions from 'camp.l' and writes Emacs Lisp
# code to 'asy-keywords.el'.
#
#####

import argparse
import re
import textwrap

parser = argparse.ArgumentParser()
parser.add_argument(
"--asy-list-file", type=str, required=True, help="Path to the asy list file"
)
parser.add_argument("--revision", type=str, required=True, help="Revision identifier")
parser.add_argument(
"--output-file", type=str, required=True, help="Path to output file"
)
args = parser.parse_args()

# Open the file 'asy-keywords.el' for writing.
with open(args.output_file, "w", encoding="ascii") as keywords:

# Write header information to 'asy-keywords.el'.
# This includes comments and the definition of 'asy-keywords-version' using a
# command-line argument.
keywords.write(
textwrap.dedent(
f"""\
;;
;; This file is automatically generated by asy-list.py.
;; Changes will be overwritten.
;;
(defvar asy-keywords-version "{args.revision}")

"""
)
)

# Define a function 'add' that adds a keyword to the output file.
def add(keyword):
keywords.write(keyword + " ")

# Write the beginning of the Emacs Lisp definition for 'asy-keyword-name'.
keywords.write("(defvar asy-keyword-name '(\n")

# Open the file 'camp.l' for reading.
with open("camp.l", "r", encoding="ascii") as camp:

# Read lines from 'camp.l' until reaching a line that contains only '%%'.
for line in camp:
if re.search(r"^%%\s*$", line):
break

# Continue reading lines from 'camp.l' after the '%%' line.
for line in camp:
if re.search(r"^%%\s*$", line):
# Exit the loop when a second '%%' line is found, indicating the end of
# the section.
break
# Match lines that start with a word (the keyword) followed by optional
# whitespace and a '{'.
match = re.search(r"^(\w+)\s*\{", line)
if match:
# Write the keyword followed by a space.
keywords.write(match.group(1) + " ")

# Open an input file specified in the command-line arguments.
with open(args.asy_list_file, "r", encoding="ascii") as asylist:

# Lists to store types, functions, and variables found in the file.
types = [] # List to hold data types.
functions = [] # List to hold function names.
variables = [] # List to hold variable names.

# Read each line from the file handle asylist.
for line in asylist:
# Match lines that define functions.
# The pattern looks for:
# - An optional word (\w*) representing the return type.
# - Any number of non-space characters ([^ ]*), which may include
# modifiers.
# - A space character.
# - Capture the function name (\w*).
# - An opening parenthesis '(', indicating the start of the parameter
# list.
matchFun = re.search(r"^(\w*)[^ ]* (\w*)\(", line)
if matchFun:
types.append(matchFun.group(1))
functions.append(matchFun.group(2))
# Match lines that declare variables.
# The pattern looks for:
# - Any non-space characters before a space ([^ ]*), representing the
# type.
# - A space character.
# - Capture the variable name (\w*).
# - A semicolon ';', indicating the end of the declaration.
matchVarDec = re.search(r"^([^ ]*) (\w*);", line)
if matchVarDec:
variables.append(matchVarDec.group(2))

# Remove duplicates and sort the lists.
types = sorted(set(types))
functions = sorted(set(functions))
variables = sorted(set(variables))

# Write the closing parentheses for the 'asy-keyword-name' definition in the
# output file.
keywords.write("))\n\n")

# Write the beginning of the 'asy-type-name' definition to the output file.
keywords.write("(defvar asy-type-name '(\n")

# Write each type from types to the output file.
for t in types:
keywords.write(t + " ") # Write the type followed by a space.

# Write the closing parentheses for the 'asy-type-name' definition.
keywords.write("))\n\n")

# Write the beginning of the 'asy-function-name' definition to the output
# file.
keywords.write("(defvar asy-function-name '(\n")

# Write each function name from functions to the output file.
for f in functions:
keywords.write(f + " ") # Write the function name followed by a space.

# Write the closing parentheses for the 'asy-function-name' definition.
keywords.write("))\n\n")

# Write the beginning of the 'asy-variable-name' definition to the output
# file.
keywords.write("(defvar asy-variable-name '(\n")

# Write each variable name from variables to the output file.
for v in variables:
keywords.write(v + " ") # Write the variable name followed by a space.

# Write the closing parentheses for the 'asy-variable-name' definition.
keywords.write("))\n")
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
2 changes: 1 addition & 1 deletion camp.l
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void savesymbol(symbol name)
* etc. Following the Don't Repeat Yourself principle, the mapping from
* strings to names is defined only here in camp.l (because we can't produce
* lex rules from a C style macro).
* The script opsymbols.pl reads this file scanning for rules using DEFSYMBOL
* The script opsymbols.py reads this file scanning for rules using DEFSYMBOL
* and creates opsymbols.h which defines the names for use in C++ code.
*/
#define DEFSYMBOL(name) \
Expand Down
4 changes: 2 additions & 2 deletions cmake-scripts/asy-misc-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ add_custom_command(
# asy-keywords.el
add_custom_command(
OUTPUT ${ASY_MISC_FILES_OUT_DIR}/asy-keywords.el
COMMAND ${PERL_INTERPRETER} ${ASY_SCRIPTS_DIR}/asy-list.pl
COMMAND ${PY3_INTERPRETER} ${ASY_SCRIPTS_DIR}/asy-list.py
--asy-list-file ${ASY_MISC_FILES_OUT_DIR}/asy.list
--revision ${ASY_VERSION}
--output-file ${ASY_MISC_FILES_OUT_DIR}/asy-keywords.el
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${ASY_MISC_FILES_OUT_DIR}/asy.list ${CMAKE_CURRENT_SOURCE_DIR}/camp.l ${ASY_SCRIPTS_DIR}/asy-list.pl
DEPENDS ${ASY_MISC_FILES_OUT_DIR}/asy.list ${CMAKE_CURRENT_SOURCE_DIR}/camp.l ${ASY_SCRIPTS_DIR}/asy-list.py
)

set(ASY_OUTPUT_DIST_MISC_FILES
Expand Down
Loading
Loading