Log in

View Full Version : Text Highlighting Algorithms


Astronouth7303
30-04-2004, 20:09
I'm working on something I think I'll call 'SuperPad' or 'MongoPad' (taking name ideas!). One of the features I think I'll include in some version (It's a long list!) is text highlighting. Does anyone know a highlighting algorithm for their favorite language?

I'm thinking a rule based system (Things higher in the list take presedence over items farther down).

ahecht
30-04-2004, 20:12
If you search around for "syntax highlighting", you will find several algorithms for various languages.

If you know Java, you can check out the implementation used in Ostermiller.Syntax from http://ostermiller.org/syntax/. Source code is available form the download link.

Astronouth7303
30-04-2004, 20:16
Thanx! (Google (http://www.google.com/search?q=syntax+highlighting))

(Ok, after going through google I'm a little frustrated: it's all language specific! does anyone know algorithms that are in psuedo code? [I'd say language specific, but the thought of syntax coloring for all languages came to mind...])

Guest
30-04-2004, 21:46
Thanx! (Google (http://www.google.com/search?q=syntax+highlighting))

(Ok, after going through google I'm a little frustrated: it's all language specific! does anyone know algorithms that are in psuedo code? [I'd say language specific, but the thought of syntax coloring for all languages came to mind...])
Pseudocode coming up:

make an array of the rules
make an array "colors" of RGB values with length equal to length of code
for each rule in the array
- for each range of strings in the code that match this rule
- - set the range of values in "colors" to the color matching the rule
- end for
end for
change the color of each character according to the colors array

When you're done could you add the code to the repository?

mtrawls
30-04-2004, 22:13
I'm working on something I think I'll call 'SuperPad' or 'MongoPad' (taking name ideas!). One of the features I think I'll include in some version (It's a long list!) is text highlighting. Does anyone know a highlighting algorithm for their favorite language?


What kind of highlighting are you talking about? You mean a user can select a certain language and it would highlight text specific to that? (Presumably where a user could specify a custom highlighting scheme with custom rules?) Sounds complicated ... be wary of the many exceptions. Some food for thought: ignore keywords in between quotes, bearing in mind escape characters, which in general can be a pain ... ignore keywords between comments, bearing in mind that in C, anyway, you can't nest the /**/-style (C-style) comments. And, of course, more caveats abound! I suggest you take a look at Syntax::Highlight::Perl (http://search.cpan.org/~johnsca/Syntax-Highlight-Perl-1.0/lib/Syntax/Highlight/Perl.pm) -- I can't think of a more complicated case to come up with syntax highlihgting for, where a single symbol (possibly multiple characters) can have more context and different results for each context! I don't know if you are familiar with perl, but if you take a gander at CPAN (http://cpan.org) you'll find a lot of useful modules that could assist in this (particularly to parse documents based on rules, not to mention that perl has some pretty powerful text handling ability ... granted, that can quickly turn into text mangling ability if you aren't careful. ;)) Also, you might want to check out emacs for general edification (if you haven't already).

Greg Ross
30-04-2004, 23:40
I'm working on something I think I'll call 'SuperPad' or 'MongoPad' (taking name ideas!). One of the features I think I'll include in some version (It's a long list!) is text highlighting. Does anyone know a highlighting algorithm for their favorite language?

I'm thinking a rule based system (Things higher in the list take presedence over items farther down).
Jamie,

I think there is already a SuperPad editor.


This site (http://www.deter.com/webpad/) says that "Source code for SuperPad is delivered with Microsoft's Visual C++ (http://msdn.microsoft.com/visualc)

Astronouth7303
01-05-2004, 10:19
Oh. I did have a long list of possible names (It started out as 'Tabbed Notepad', but the feature list got too long for that). However, currently, it's only framework. But when I finish the first version, I'll send it to anyone who wants it!

Astronouth7303
01-05-2004, 11:45
Pseudocode coming up:

make an array of the rules
make an array "colors" of RGB values with length equal to length of code
for each rule in the array
- for each range of strings in the code that match this rule
- - set the range of values in "colors" to the color matching the rule
- end for
end for
change the color of each character according to the colors array

When you're done could you add the code to the repository?
I ment for a particular language. Setting up the structure isn't hard, filling it in is.

And yes. when I make a working version, I will upload it. I'm still on Alpha 0.0.000.