Comments + Test case

This commit is contained in:
2026-03-19 13:40:54 +11:00
parent 633450ccc5
commit 08dbfd3fe0
2 changed files with 35 additions and 4 deletions

6
README.md Normal file
View File

@@ -0,0 +1,6 @@
# Discord Syntax Highlighter
Provides Syntax Highlighting for Ground. Simply run this python script (or add to a bot!)
# Usage
Call the `highlight_program()` function, with the ground program as the argument. This returns text which can be
directly copy-pasted into discord.

33
main.py
View File

@@ -1,7 +1,8 @@
def split_by_space(line):
def split_by_space(line: str):
token: str = ""
tokens: list[str] = []
isString = False
isComment = False
for char in line:
if isString:
if char == "\"":
@@ -9,7 +10,13 @@ def split_by_space(line):
continue
token += char
continue
if isComment:
token += char
continue
match char:
case "#":
isComment = True
token += char
case "\"" | "\'":
token = "?"
isString = True
@@ -23,7 +30,7 @@ def split_by_space(line):
case _:
token += char
if tokens != "":
if token != "":
tokens.append(token)
return tokens
@@ -38,6 +45,8 @@ def get_color(arg: str):
return f"\033[33m{arg[0]}\033[0m{arg[1:]}"
case '@':
return f"\033[33m{arg}\033[0m"
case '#':
return f"\033[30m{arg}\033[0m"
case _:
return f"\033[35m{arg}\033[0m"
@@ -53,5 +62,21 @@ def highlight_text(code: str):
print(get_color(arg), end=" ")
print()
print("```")
highlight_text("@label\n\n set &str \"Hello, world!\"\n println $str\n jump %label")
if __name__ == "__main__":
highlight_text(
"""# A cool list
setlist &favWords "hello" "there" "general"
listappend &favWords "kenobi"
println $favWords
set &count 0
set &passedThrough true
@jmpbck
getlistat &favWords $count &tmp
println $tmp
add $count 1 &count
getlistsize &favWords &tmp2
inequal $count $tmp2 &tmp3 #comment
if $tmp3 %jmpbck""")