Syntax highlighting for Forth?

Get help with the installation and running of the Zeus editor. Please do not post bug reports or feature requests here. When in doubt post your question here.
robert37

Syntax highlighting for Forth?

Postby robert37 » Wed Jan 19, 2005 8:12 am

How do I set up syntax highlighting for the Forth language? Does anyone have a wordlist for this, especially for the Forth-83 style?

jussij
Site Admin
Posts: 2229
Joined: Fri Aug 13, 2004 5:10 pm

Postby jussij » Wed Jan 19, 2005 2:07 pm

Zeus does come with a Forth document type defined for the following file extensions:

Code: Select all

f;fs;fs8;fsg;fsw;fsz;fsh;fse;fsy;fsp;fsu;fsf;fsa;fsx

but as I am not a Forth programmer it is hard to say how accurate the definition actually is :(

In an effort to test the validity of the definition I found this web page: http://astro.pas.rochester.edu/Forth/forth-words.html
and saved the text to a dummy forth file and then loaded the file in Zeus.

The first thing I noticed was none of the keywords where highlighted so I went to the Zeus document type and turned off the case sensitivity.

Now some of the keywords where highligted but many where missing, so I added the missing words to the existing forth keywords file. After importing this new keyword list into the document type, from what I could tell all the keywords listed where now highlighted :)

The new keywords file can be found here: http://www.zeusedit.com/fwords.txt

But I must stress this does mean the highlighting is actually correct :(

Since my understand of the Forth language it is very limited it is hard to tell if the highlighting is in fact correct. If this new keyword list does seem to work could you please post back here as I will then update the current installer.

Cheers Jussi

robert37
Posts: 17
Joined: Wed Jan 19, 2005 8:15 am

Postby robert37 » Thu Jan 20, 2005 12:57 am

Thank you for this work that you have done. I will try out the wordlist immediately.

Can I somehow include the extension .fth in your list of Forth extensions? How would I do this?

jussij
Site Admin
Posts: 2229
Joined: Fri Aug 13, 2004 5:10 pm

Postby jussij » Thu Jan 20, 2005 6:37 am

Sure. Just edit the Forth document type using the Options, Document Types menu and add fth; to the list of file extensions.

Cheers Jussi

robert37
Posts: 17
Joined: Wed Jan 19, 2005 8:15 am

Postby robert37 » Thu Feb 10, 2005 6:50 am

I do not understand the numbers string for syntax highlighting. For instance, you have provided a default string of:
(0x*[0-9A-Fa-f]+[Hh]*)|([0-9]+)

What is being done in the above string? How does it work?

I ask, because the above string does not work for syntax coloring of numbers in my Forth language setup. I use only decimal numbers, such as:

1
123
+45
-222
1.23E0
-2.222e-4
-0.174e2
etc. Just the symbols 0-9 + - . E

I would like for any and all of the above number types to be highlighted with the number-color specified in Zeus. How would I create a number-string to accomplish this?

jussij
Site Admin
Posts: 2229
Joined: Fri Aug 13, 2004 5:10 pm

Postby jussij » Thu Feb 10, 2005 12:59 pm

What is being done in the above string? How does it work?

This is a two part regular expression which reads a number is defined as this (0x*[0-9A-Fa-f.]+[Hh]*) or as this ([0-9]+).

If you take the second part ([0-9]+) first it says search for a set (ie []) of characters in the range of 0-9. The + sign means at least one. Thus in English the expression reads find one or more characters in the range 0 thru 9.

Next consider the first part (0x*[0-9A-Fa-f]+[Hh]*) of the string.

The 0x* reads search for the optional characters 0x where the presence of the * makes this part optional. The next part [0-9A-Fa-f]+ reads find one or more of the characters in the set of 0 thru 9 or the letters a thru f or the letters A thru F. The final part again is an optional match for the characters h or H.

Hence this pattern is designed to match these type of hexidecimal numbers:

Code: Select all

0x01af
0x02bfh
0x03CF
0x03CFH


I ask, because the above string does not work for syntax coloring of numbers in my Forth language


In this case something like this would work better:

Code: Select all

[0-9]+[.]*[e0-9]*

I find writing regular expression always involves a bit of trial and error. I start with a small expression and just keep adding to it, for example to come up with this pattern I would build it in these steps:

Code: Select all

[0-9]+
[0-9]+[.]*
[0-9]+[.]*[e0-9]*

At each step I can test the expression using the Find Dialog and turning on the regular expression option.

Cheers Jussi

robert37
Posts: 17
Joined: Wed Jan 19, 2005 8:15 am

Postby robert37 » Mon Feb 21, 2005 2:37 am

Thank you. For the Forth language, I have made the following number string, which seems to work:

[.]*[+]*[-]*[0-9]+[.]*[0-9]*[eE]*[+]*[-]*[0-9]*

Now, I have another question. In Forth, strings are compiled by enclosing them in quotes with a leading space, as in:

" THIS IS A QUOTE"

The first " must be followed by a space, and the last " needs no space.

Putting a " in the Zeus string keyword box, this seems to work fine. All such quoted strings are displayed in the chosen string color.

However, there is another word, used within a Forth word, that prints a string. It is .", as in:

." THIS IS A STRING"

The point-quote must be followed by a space, and the closing quote needs no space.

At present, the point part of the above is colored in text-color, while the quotes and the string are displayed in the string color. Is there a way to specify that ." is also a string function, so that the whole thing is displayed in the string-color? I tried adding ." to the string-keyword box, but then Zeus colored all periods in the string color. Is there a way to include ." and " as string functions, while . is not?

Another question: in Forth, comments begin with parenthesis-space and end with parenthesis, as in:

( THIS IS A COMMENT)

robert37
Posts: 17
Joined: Wed Jan 19, 2005 8:15 am

Postby robert37 » Mon Feb 21, 2005 2:48 am

Somehow, my previous message got sent before I finished it. Here is the complete message:

Thank you. For the Forth language, I have made the following number string, which seems to work:

[.]*[+]*[-]*[0-9]+[.]*[0-9]*[eE]*[+]*[-]*[0-9]*

Now, I have another question. In Forth, strings are compiled by enclosing them in quotes with a leading space, as in:

" THIS IS A QUOTE"

The first " must be followed by a space, and the last " needs no space.

Putting a " in the Zeus string keyword box, this seems to work fine. All such quoted strings are displayed in the chosen string color.

However, there is another word, used within a Forth word, that prints a string. It is .", as in:

." THIS IS A STRING"

The point-quote must be followed by a space, and the closing quote needs no space.

At present, the point part of the above is colored in text-color, while the quotes and the string are displayed in the string color. Is there a way to specify that ." is also a string function, so that the whole thing is displayed in the string-color? I tried adding ." to the string-keyword box, but then Zeus colored all periods in the string color. Is there a way to include ." and " as string functions, while . is not?

Another question: in Forth, comments begin with parenthesis-space and end with parenthesis, as in:

( THIS IS A COMMENT)

In Zeus, I have entered ( for the comment-start and ) for the comment-end. This works fine, and the comment text and the parentheses are collored in the chosen comment-color.

However, a strange thing happens. Sometimes, the code following the comment is also colored in the comment-color, as if the closing ) were not there. But I can stop this by adding or subtracting a space within the comment line.

For instance, in the following code, the second line is erroneously colored in the comment-color:

: POLY2 ( A B C T X: X=A+BT+CT^2)

FDUP 2 FROLL F* 2 FROLL F+ F* F+ ;

But if I add a space within the comment, then the second line is colored correctly, as in:

: POLY2 ( A B C T X: X=A+BT+CT^2)

FDUP 2 FROLL F* 2 FROLL F+ F* F+ ;

Please note, that I have added an extra space between the A and the B within the comment.

Is there a way to fix this, so that I do not have to go through all my previous code, adding and subtracting spaces?

jussij
Site Admin
Posts: 2229
Joined: Fri Aug 13, 2004 5:10 pm

Postby jussij » Mon Feb 21, 2005 3:36 am

The first " must be followed by a space, and the last " needs no space. Putting a " in the Zeus string keyword box, this seems to work fine.

One thing you will have noticed is that the numbers field is the only field that is a regular expression. Thus putting a '' in the quotes field will work but Zeus will highlight all of these as strings:

Code: Select all

"a string"
" a string"
" a string "

Is there a way to specify that ." is also a string function, so that the whole thing is displayed in the string-color?

I found the following way to do this:
  1. Define the strings as per usual using the " character
  2. Select the user "1: defined words" option
  3. In the pattern section enter the following characters: ."*"
  4. Change to the coloring section and set the color for the user defined words to match the color of the strings.
From the little testing that I did, this seemed to work just fine :)

However, a strange thing happens. Sometimes, the code following the comment is also colored in the comment-color, as if the closing ) were not there.

As a test I created a new, empty forth file and pasted your 4 lines of code into the file but for me the comment block coloring seemed to work fine.

So I can only guess is that since the ) is both a comment and a delimiter the Zeus coloring might be getting a bit confused. Does Forth need () defined as delimiters :? If not I would try removing these characters from the list of delimiters. Also, one way to re-synch the coloring would be to File Save the file and then use the File Reload menu. If the coloring is confused, running these to commands should get it back on track.

But if I add a space within the comment, then the second line is colored correctly, as in:

I am guessing the action of adding the character triggered a the coloring re-sysnch similar to the File Save command I described earlier and this results in the comments being pained correctly.

Is there a way to fix this, so that I do not have to go through all my previous code, adding and subtracting spaces?

Do you have some example code that has the comments colored incorrectly as soon as the file is loaded :? If so post a small sample of code here, and I will try to replicate the bug at this end.

Cheers Jussi
Last edited by jussij on Tue Dec 07, 2010 4:53 am, edited 1 time in total.

TonyMc
Posts: 8
Joined: Sat Apr 16, 2005 6:51 pm
Location: Birmingham

Postby TonyMc » Sun Apr 17, 2005 2:42 pm

Hello,

I have put together a syntax highlighting definition for SwiftForth which may be helpful to others. I have treated all words in the ANS Forth CORE wordset as keywords, words in other ANS Forth wordsets as User defined (set 1) and words specific to SwiftForth as User defined (set 2). This works well.

My only difficulty is that I would like to specify block comments in two ways. The ANS Forth standard uses ( blah blah ... ) comments and SwiftForth uses { blah blah .. } as an extension. I can't figure out a pattern to allow the brace delimited block comments. As a regular expression it would be { [^}]*} but patterns seem to be more limited than regexps. Can anyone help?

I removed all the characters from the delimites input field. Only whitespace delimits Forth words.

I can send the various wordsets as txt files ready for import using the keywords dialog if others are interested.

Best,
Tony

Guest

Postby Guest » Mon Apr 18, 2005 3:02 am

Hi Tony,

My only difficulty is that I would like to specify block comments in two ways. The ANS Forth standard uses ( blah blah ... ) comments and SwiftForth uses { blah blah .. } as an extension.

Unfortunately there is no way to do this :( All document types are limited to one and only block comment and two line comments.

I can't figure out a pattern to allow the brace delimited block comments.

The comment entries can not be defined as regular expressions. In fact you will notice the comment entry fields only accepts up to a maximum of three characters. This is done for performance reason.

Lots of the code in Zeus (including the syntax highlighting) was originally written to run on 80386 machines and these 60-100 Mhz machine had no where near the power todays GHz machines So lots of the code in Zeus had to be highly optimized for performance and not flexibility :(

I can send the various wordsets as txt files ready for import using the keywords dialog if others are interested.

If you send the files to jussij@zeusedit.com I will be more than happy to update the Forth document type of the Zeus installer :)

Cheers Jussi

robert37
Posts: 17
Joined: Wed Jan 19, 2005 8:15 am

Postby robert37 » Mon Apr 18, 2005 8:02 am

I too would like to see the word files. Please send to robert1111@starcenter.com

jussij
Site Admin
Posts: 2229
Joined: Fri Aug 13, 2004 5:10 pm

Postby jussij » Fri Feb 24, 2012 4:14 am

All document types are limited to one and only block comment and two line comments.

The later versions of Zeus now have room for two block comments and two line comments so it is now possible to specify block comments in two ways ;)

Cheers Jussi


Return to “General”

Who is online

Users browsing this forum: No registered users and 0 guests