Skip to content

Instantly share code, notes, and snippets.

@tattlemuss
Last active December 16, 2023 11:28
Show Gist options
  • Save tattlemuss/d4574326276c3d2cd4e988fa395b9ad0 to your computer and use it in GitHub Desktop.
Save tattlemuss/d4574326276c3d2cd4e988fa395b9ad0 to your computer and use it in GitHub Desktop.
Simple tags generator for Motorola 68k source code and vim.
#!/usr/bin/env python3
""" Simple ctags creator for Motorola m68k assembly.
Format specified by https://docs.ctags.io/en/latest/man/tags.5.html
{tagname} {TAB} {tagfile} {TAB} {tagaddress} {term} {field} ..
NOTE We only add tagaddress as line numbers, adding a search string takes more work,
like escaping regexp characters.
Regexps are very loose to support Devpac syntax, so will "over-find" labels.
Support for local lables is very primitive.
"""
import glob, re, os.path
identifier = "[A-Za-z_][A-Za-z0-9_]*" # shared identifier regexp
labels = (
# These are done in preference order
("d", re.compile("^(" + identifier + ")\s+[\=|EQU|equ]")), # label = y
("d", re.compile("^(" + identifier + ")\s+[MACRO|macro]")), # label MACRO
("l", re.compile("^\.(" + identifier + ")\:")), # .label (local label, optional)
("f", re.compile("^(" + identifier + ")")), # any label at start of line (fallback) without colon
)
if __name__ == "__main__":
paths = glob.glob("**/*.[Ss]", recursive=True)
fh = open("tags", "w")
fh.write("""!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_NAME m68ktags.py //
""")
tags = []
def process(path, fh):
lines = open(path, "r").readlines()
def add(tag):
#print(tag)
tags.append(tag)
last_func = None
for linenum, l in enumerate(lines):
for tagtype, label in labels:
m = label.match(l)
if m != None:
name = m.group(1)
if tagtype == 'f':
last_func = name
elif tagtype == 'l':
tagtype = "l\tfunction:%s" % last_func
add((name, path, str(linenum + 1), tagtype))
break
for path in paths:
process(path, fh)
tags.sort()
for t in tags:
fh.write("%s\t%s\t%s;\"\t%s\n" % t)
fh.close()
print("Tags found:", len(tags))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment