ZX BASIC syntax highlighter

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
patters
Manic Miner
Posts: 471
Joined: Thu Apr 11, 2019 1:06 am

ZX BASIC syntax highlighter

Post by patters »

I went down the considerable rabbit hole of making a syntax highlighter for ZX BASIC for the Linux/Mac/Windows code editor Sublime Text. It'll highlight regular .bas files too since it's a superset of Sinclair BASIC. It's nice to program for the Speccy on a modern editor at last :D

https://github.com/patters-syno/zx-basic-syntax

Image
chrisfoulds
Drutt
Posts: 5
Joined: Sat Jan 23, 2021 5:34 pm

Re: ZX BASIC syntax highlighter

Post by chrisfoulds »

Awesome, will use that!
User avatar
Jbizzel
Dynamite Dan
Posts: 1537
Joined: Mon May 04, 2020 4:34 pm
Location: Hull
Contact:

Re: ZX BASIC syntax highlighter

Post by Jbizzel »

Brilliant.

I can't get it to work though. It's not highlighting anything. I followed the instructions. I've never used sublime before, am.i doing something stupid? Please help
User avatar
patters
Manic Miner
Posts: 471
Joined: Thu Apr 11, 2019 1:06 am

Re: ZX BASIC syntax highlighter

Post by patters »

I've just gone through the motions and it works for me, even on a completely fresh downloaded copy of Sublime.
Did you definitely put the syntax file inside the User folder, and not one level up?
And can you see ZX BASIC as a choice in the View > Syntax menu?
equinox
Dynamite Dan
Posts: 1052
Joined: Mon Oct 08, 2018 1:57 am
Location: SE England

Re: ZX BASIC syntax highlighter

Post by equinox »

Technically I suppose you need slightly different rules for different models, e.g. on a +3 you can do COPY "..." TO SPECTRUM FORMAT, and on a 48k you can use the buggy MOVE a$,b$ (which I slipped into my CSSCGC entry) but that doesn't work on a +2a or +3. And of course the grey +2 has that weird exclamation-mark RAM-disc syntax, ERASE ! "myfile"
User avatar
patters
Manic Miner
Posts: 471
Joined: Thu Apr 11, 2019 1:06 am

Re: ZX BASIC syntax highlighter

Post by patters »

As per the blurb on my GitHub the aim isn't to highlight all BASIC keywords though. Doing that just highlights most of the entire screen full of code in pink or purple which really isn't helpful. The compiler is already good at catching misspellings etc., so the real value this adds is code legibility and clarity. Since I copied the contexts from the built-in language support it offers some really neat stuff like if you hover the mouse over a function name you can jump to where it's defined, or to each mention of it in the code.
GitHub description wrote:ZX BASIC is an updated BASIC dialect which compiles to optimised Z80 machine code for the ZX Spectrum computer. The Sublime Text editor, like the ZX BASIC compiler, runs on Linux, Mac OS, and Windows.

This syntax highlighter does not exhaustively match all BASIC keywords, rather its focus is code clarity. Mariana colour scheme (pictured) is recommended over the default Monokai, since some highlights are invisible in Monokai.

Highlighting Scope:
  • Comment lines, comment blocks, inline assembly comments
  • Strings including all escaped characters
  • To avoid over-colouring, keywords are limited to conditionals and those controlling execution flow
  • Sub and Function definitions and calls, although their calls are indistinguishable from array references (and strings without sigils) since the parser does not track objects
  • Variable types, numbers, separators, operator symbols, and compiler keywords
User avatar
patters
Manic Miner
Posts: 471
Joined: Thu Apr 11, 2019 1:06 am

Re: ZX BASIC syntax highlighter

Post by patters »

After exchanging PMs with [mention]Jbizzel[/mention] he confirmed he's got the syntax highlighter working. I have improved the instructions on the GitHub repo to make it easier to download the raw syntax file.
Last edited by patters on Sun Feb 14, 2021 10:21 pm, edited 1 time in total.
User avatar
Jbizzel
Dynamite Dan
Posts: 1537
Joined: Mon May 04, 2020 4:34 pm
Location: Hull
Contact:

Re: ZX BASIC syntax highlighter

Post by Jbizzel »

patters wrote: Sun Feb 14, 2021 9:45 pm After exchanging PMs with @JBizzel he confirmed he's got the syntax highlighter working. I have improved the instructions on the GitHub repo to make it easier to download the raw syntax file.
many thanks for your help - This works like a treat. Brilliant work. CGC2021 won't know what hit it lol
User avatar
Twig
Drutt
Posts: 14
Joined: Sat Jun 24, 2023 7:40 pm

Re: ZX BASIC syntax highlighter

Post by Twig »

Very nifty.

I don't suppose there's a way of supporting other text editors, like Geany?
C.Born
Manic Miner
Posts: 234
Joined: Sat Dec 09, 2017 4:09 pm

Re: ZX BASIC syntax highlighter

Post by C.Born »

Hi
I installed subliemeditor and the 2 sets of highlite

testing on a pure basic file it show the line number in BLUE but only up to 999
from 1000 and above they become orange like the INT values
is it ment to be that all line numbers are blue then you have a small typo/bug some where
i use linux mint21.
btw the INT value after a GO TO is orange aswell, is there a change that an INT after a GO TO or GO SUB is set in blue aswell?
cheers

EDIT:
if have another colour request, I use Fuse with +D emulation. the +D has a problem with printing CHR$ above 127, it wil send the IMAGE VALUE instead as a hex/dec value in the file that FUSE makes with "emulate printer" , next line holds severall UDG's as you can 'see'
1500 LET q$="=>?@K\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00 \C0@K\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00K\00 \00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00K\00\00`auQa=": GO SUB aa: PRINT #n;AT n,VAL "8";q$: GO SUB ui: PRINT #n;AT e,VAL "5";"Radio RefleXed Crystal": GO SUB aa
the characters CHANGE here inside the forum display, maybe its my linux
in subliem it look like
<0x1b>K<0x08>
and those hex values are printed in darkgrey, which is hard to read using a "darktheme". probably i can find that my self

about those characters above 127, i looked for a PC zx spectrum FONT but few have the correct blockgrafic-images 128 to 143.
if those where include perhaps a different solution is possible, but is supose thats a "subliem" request.
or even for the OS as its about system fonts. an realy adaptable UDG part is probably fantasy
Post Reply