I wrote a basic syntax highlighter in Snap!. It is still in beta, but I developed it to the point that I wanted to show the forum it! Feel free to tell me your feedback, and suggest token categories or languages!
(editor is recommended)
Compare to forum’s syntax highlighting:
from time import *
for char in "snappy":
sleep(1)
print(char, end=". ", flush=True)
Usage notes:
You can paste multiline code into the ask input, but consecutive spaces get collapsed into one. This tampers with the indentation. If this gets in the way, paste the code into the (code) variable instead.
There are settings, but many settings do not do anything yet.
Reminds me of the weird/joke code editor i made a while ago.
Could you explain how it works? I’d be interested to know whats happening behind the scenes
If currently in a string, detect if the character (emphasis; multi-character delimiters are not supported yet) is a delimiter to close the string, then exit the string. If not in a string, detect if the character is a delimiter to open the string, then enter the string.
Set the color for the character to default. This will be the color if none of the rules below are satisfied.
Detect if the character is one of the digits allowed in a number literal, the negation sign, or the decimal point, then set the color to the color for numbers. I was lazy to make sure the syntax for numbers is correct, so even if the negation sign or the decimal point does not belong to a number, it is still highlighted as a number.
Detect if the identifier (a string of abcdefghijklmnopqrstuvwxyz_, will be able to be changed in the future) matches one of the words in the list of keywords, then set the color to the color for keywords.
If currently in a string, set the color to the color for strings.
Print out the character.
String delimiter escaping, built-ins (like print() or Array), operators and comments are currently not recognized.
Make sure to open the (\</\> language data::other) block to see how the syntax for languages is stored!
By the way, my inspiration was from Notepad++'s user-defined language editor, which I am glad Notepad++ has. Dunno why defining syntax for languages should be hard and user-unfriendly.
I wanted to make my own version of Notepad++'s UDL feature too, and that is the birth of this project.
I did not feel like making a tokenizer to partition the code into tokens. Instead, each character is colored based on the type of character and the surrounding text.