How to fix bugs in custom language highlighting?

  • Hello, I needed to review some Forth code in NPP7.2 so I tried to define a custom language using menu Language -> Define your language.

    Forth has very simple syntax so I needed only a few highlights:

    • binary numbers (words starting with %)
    • decimal numbers (words starting with #)
    • hex numbers (words starting with $)

    Problem 1: I can define only two number formats, but I need three. How can I define the third number format?
    Problem 2: Hex numbers only work when all digits are decimal. I followed the UDL2.0 suggestion to add the additional digits a-f to Extras, but it does not work… as soon as the hex number contains any hex digit higher than 9, the string is not highlighted as a number.
    Problem 3: For binary numbers it is not possible to limit the valid digits to 0 and 1 only, it highlights any combination with the other digits, which is obviously undesired.

    Comments: Comment delimiter in Forth is actually a Forth word “(” which means it always must be followed by a space (and also must not be preceded by a non-space character). Problem 4: When I input "( " as comment delimiter, the space is ignored and words like (bit) are displayed as comments although they are not.
    Problem 5: The comment delimiter is valid only if preceded by a space or beginning of line (because otherwise it is a part of a word and therefore not a comment delimiter). This does not work either, words like bit(8) should be highlighted as regular text but actually bit is highlighted as regular text and (8) as a comment, which is, of course, wrong and confusing.

    How can I fix this? Is there a possibility to use regular expressions to detect these patterns, instead of the existing highlighting?

  • Using Prefix mode on the Keywords Lists tab might help you to some degree. In one of the groups check Prefix mode and then enter $ in the text box. Finally, click on the Styler button to select the highlight style you want for hex numbers. This will work fine as long as you have some whitespace at the end of your hex numbers. Another limitation is it will highlight "$NotHex " in exactly the same way.

    If that works well enough, you can do something similar for “%” and “#”.

Log in to reply