|
| 1 | +\page HIGHLIGHT HIGHLIGHT$ Function |
| 2 | + |
| 3 | +```basic |
| 4 | +HIGHLIGHT$(string-expression) |
| 5 | +``` |
| 6 | + |
| 7 | +Given a string containing BASIC source, returns a copy with ANSI SGR escape sequences applied for syntax highlighting. Intended for program listings, editors, and tools that render to an ANSI-capable terminal, producing a consistent look-and-feel. |
| 8 | + |
| 9 | +**Notes** |
| 10 | + |
| 11 | +* **Strings** — text between double quotes (`"..."`) is highlighted in **light yellow** from the opening quote to the closing quote. If a closing quote is missing, highlighting continues to the end of the input and is then reset. |
| 12 | +* **Comments** |
| 13 | + |
| 14 | + * A single quote (') begins a comment that is highlighted in **dark green** until the end of the input. |
| 15 | + * The keyword `REM` (when not inside a string) causes the **entire line** to be treated as a comment and highlighted in **dark green**. |
| 16 | +* **Keywords / tokens** — recognised BASIC keywords are highlighted in **light blue** when they are not part of a longer identifier. Boundary detection prevents colouring inside names like `FORTH`. Exceptions: `PROC`, `FN`, and `=` may be adjacent to letters/digits/underscores and will still be highlighted. |
| 17 | +* **Built-in functions** |
| 18 | + |
| 19 | + * Integer-returning built-ins are **light green**. |
| 20 | + * String-returning built-ins are **light magenta**. |
| 21 | + * Real/float-returning built-ins are **light cyan**. |
| 22 | +* **Numeric literals** — digits (and a leading unary `+`/`-`) are highlighted in **orange**. |
| 23 | +* **Operators & punctuation** — `() + - * / = < > , ;` are highlighted in **dark red**. |
| 24 | +* **Default text** — rendered in **white**. Colours are reset back to white after each coloured span, and a final reset is appended at the end of the result to prevent “colour leakage” in the caller’s terminal. |
| 25 | + |
| 26 | +**Errors** |
| 27 | + |
| 28 | +* None |
| 29 | + |
| 30 | +**Examples** |
| 31 | + |
| 32 | +```basic |
| 33 | +' Strings and a trailing comment |
| 34 | +A$ = "PRINT " + CHR$(34) + "hello world" + CHR$(34) + " ' This is an example" |
| 35 | +PRINT HIGHLIGHT$(A$) |
| 36 | +
|
| 37 | +' Keywords, numbers, and operators |
| 38 | +B$ = "FOR I = 1 TO 10 + 8" |
| 39 | +PRINT HIGHLIGHT$(B$) |
| 40 | +
|
| 41 | +' REM comment colours the whole line as a comment |
| 42 | +D$ = "REM this whole line is a comment" |
| 43 | +PRINT HIGHLIGHT$(D$) |
| 44 | +``` |
| 45 | + |
| 46 | +**See also** |
| 47 | + |
| 48 | +* \ref PRINT - display output on the screen |
| 49 | +* \ref edit - Text editor |
0 commit comments