Own syntax file definition: keywords won't work

General questions about using TextPad

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
maverickrocks
Posts: 1
Joined: Thu Jul 20, 2023 1:28 am

Own syntax file definition: keywords won't work

Post by maverickrocks »

Hi
I am trying to create my own syntax file definition but I am not able to highlight the text based on my keywords.
I am not sure what I am doing wrong here. Here is my template. I also looked into the textpad help reference

Code: Select all

C=1

[Syntax]
Namespace1 = 4
IgnoreCase = No
KeyWordLength =
BracketChars = {[()]}
OperatorChars = -+*/<>!~%^&|=
PreprocStart =
SyntaxStart =
SyntaxEnd =
HexPrefix = 0x
CommentStart = /*
CommentEnd = */
CommentStartAlt =
CommentEndAlt =
SingleComment = //
SingleCommentCol =
SingleCommentAlt =
SingleCommentColAlt =
SingleCommentEsc =
StringsSpanLines = Yes
StringStart = "
StringEnd = "
StringAlt = `
StringEsc = \
CharStart = '
CharEnd = '
CharEsc = \

[Keywords 1]
<string name="description">

[Keywords 2]
<file name="source file">

[Keywords 3]
<file name="database">

[Keywords 4]
<file name="loading file">

[Keywords 5]
<string name="correction">
Attachments
textpad.JPG
textpad.JPG (57.96 KiB) Viewed 156 times
Last edited by AmigoJack on Fri Mar 29, 2024 10:32 pm, edited 1 time in total.
Reason: [code] BBCode formatting; more specific title
User avatar
AmigoJack
Posts: 482
Joined: Sun Oct 30, 2016 4:28 pm
Location: グリーン ヒル ゾーン
Contact:

Re: Own syntax file definition: keywords won't work

Post by AmigoJack »

Let me copy text which is actually readible, copyable and can be zoomed into (in contrast to your screenshot that is too far zoomed out) - I marked two items red that might be interesting:
Syntax Definition Files
Syntax definition files contain the information TextPad uses to highlight keywords in different colors. A standard set is provided for common programming languages, and contributed files are available from the TextPad web site.

If you want to implement syntax highlighting for a document class that has not been catered for, you must create a file of keywords that you want to be colored, as described below, and save it as "myfile.SYN" in the Syntax folder. Note that double byte character sets cannot be used in syntax definition files.

The first line of the file must be one of the following:
  • TEXT=1 means that only URLs will be highlighted. All sections of the syntax definition file are ignored, so do not use this type.
  • C=1 means that full syntax highlighting is available for languages which broadly conform to C syntax. All keywords are in a single namespace, so duplicates are not allowed.
  • HTML=1 means that full syntax highlighting is available for languages which broadly conform to HTML syntax. Keywords are in two namespaces, with tags in one and their attributes in the other. The Namespace1 parameter controls how many of the six [Keyword] sections are in the first namespace. Having two namespaces allows keywords such as TITLE, which can be both a tag and an attribute, to be colored according to context.
  • PERL=1 is a variant of C=1, to take account of some peculiarities of Perl syntax.
  • LaTeX=1 is a variant of C=1, to take account of some peculiarities of LaTeX syntax.
We will introduce other grammars as and when necessary.

The rest looks like an .INI file, with the following sections:
  • [Syntax]
  • [Keywords 1]
  • [Keywords 2]
  • [Keywords 3]
  • [Keywords 4]
  • [Keywords 5]
  • [Keywords 6]
  • [Preprocessor keywords]
The [Syntax] section must be first, followed by [Keywords 1]. The other sections are optional. The [Preprocessor keywords] section can only be used when a single character (such as "#") is used to start a statement that is handled by a pre-processor.

Keywords are entered one per line. The split between the keyword sections is arbitrary. You only need to use Keywords 2-6, if you want some keywords in different colors. They are all in the same namespace, unless "HTML=1" is used, when "Namespace1 = n" specifies how many of the keyword sections are in the first namespace, with the remainder in another. This means that the same name can appear in both namespaces when "HTML=1". The pre-processor keywords are always in their own namespace.

You can select the newly created .SYN file using the Preferences command on the Configure menu. It should be displayed in the drop-down list box for Syntax settings, for the new document class you created.

The [Syntax] section must contain the following lines:
  • Namespace1 = a number in the range 1 to 6, which specifies how many of the keyword sections are in the first namespace. There is only one namespace when "C=1" or "LaTeX=1", so this value must be 6. There are two namespaces when "HTML=1", so this value can be between 1 and 5, and defaults to 1. Change it to 2, say, if you want to be able to display HTML tags in two colors, and split the tag keywords between the [Keywords 1] and [Keywords 2] sections, depending on which color you want them in.
  • IgnoreCase = "Yes" if keywords are not case sensitive, otherwise "No".
  • KeyWordLength = the number of characters in each keyword. If this entry is omitted, or set to 0, keywords can be of variable length. It is only required for fixed length keywords that run together, without any delimiters.
  • BracketChars = characters that can be used for brackets.
  • OperatorChars = characters that can be used for operators.
  • PreprocStart = the character used to start a pre-processor statement.
  • HexPrefix = the characters that prefix hexadecimal numbers. If this entry is omitted, it defaults to "0x".
  • SyntaxStart = a string of characters that switch on keyword recognition (e.g. "<"), or leave blank.
  • SyntaxEnd = a string of characters that switch off keyword recognition (e.g. ">"), or leave blank.
  • CommentStart = a string of characters that start a multiple line comment, or leave blank.
  • CommentEnd = a string of characters that end a multiple line comment, or leave blank.
  • CommentStartAlt = an alternative string of characters that start a multiple line comment.
  • CommentEndAlt = an alternative string of characters that end a multiple line comment.
  • SingleComment = a string of characters that start a single line comment (e.g. "//" or "REM"), or leave blank.
  • SingleCommentCol = If a single line comment must start in a specific column, assign the number here. If it must start in the first non-blank column, assign "Leading" here.
  • SingleCommentAlt = an alternative string of characters that start a single line comment (e.g. "//" or "REM"), or leave blank.
  • SingleCommentColAlt = If the alternative single line comment must start in a specific column, assign the number here. If it must start in the first non-blank column, assign "Leading" here.
  • SingleCommentEsc = The character that is used to escape either of the SingleComment strings.
  • StringsSpanLines = "Yes" if strings can continue over line boundaries, otherwise "No". This can be used in conjunction with StringEsc, if the string can only be continued when the new line is escaped with that character.
  • StringStart = The character that indicates the start of a string (e.g. double quote).
  • StringEnd = The character that indicates the end of a string (e.g. double quote).
  • StringAlt = An alternative character that can be used to delimit strings, if the string contains the StringEnd character.
  • StringEsc = The character that is used to escape the StringEnd character, if it is part of a string (e.g. "\").
  • CharStart = The character that is used to start a character literal (e.g. single quote).
  • CharEnd = The character that is used to end a character literal (e.g. single quote).
  • CharEsc = The character that is used to escape the CharEnd character, if it is part of a character literal (e.g. "\").
Since your syntax file is mostly a copy of cpp.syn but your keywords are then suprisingly HTML-/XML-like I wonder why you still insist on C=1. Moreover the term keyword is in doubt meant literally: a word cannot have spaces, otherwise one must call it "keyphrase".

So how does your actual file look like that you want syntax highlighting on? I bet it's not C-like, and I bet you don't want those full lines to be highlighted. Copying from html.syn should be more fitting your needs, with the actual keyword definitions then being only in 2 sections - so your syntax file should look like this:

Code: Select all

; 2024-03-29 for https://forums.textpad.com/viewtopic.php?t=13906

HTML=1

[Syntax]
IgnoreCase = Yes
HexPrefix = #
SyntaxStart = <
SyntaxEnd = >
CommentStart = !--
CommentEnd = -->
StringsSpanLines = Yes
StringStart = "
StringEnd = "
StringAlt = '
CharStart = &
CharEnd = ;

[Keywords 1]
string
file

[Keywords 2]
name
Last edited by AmigoJack on Sat Mar 30, 2024 4:23 pm, edited 1 time in total.
Reason: syntax comment
Post Reply