*
The moose likes Swing / AWT / SWT and the fly likes Syntax Highlighting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Syntax Highlighting" Watch "Syntax Highlighting" New topic
Author

Syntax Highlighting

Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Hi
Im after developing a simple editor using swing and am hoping to add syntax highlighting for languages such as java html etc. I have been looking at scanners such as the Java compiler compiler, bison etc, however the seem very heavy. Could you please help with a scanner that would just provide syntax highlighting or some other methods of implementing it.
Thanks,
Ger
Michael Crutcher
Ranch Hand

Joined: Feb 18, 2002
Posts: 48
You might try Lucene.
You can search for words and phrases. After you found the word or phrase you were looking for you could highlight it.
Michael Crutcher
Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Thanks
Ger
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi,
I don't think lucene is the best choice for syntax highlighting. in a programming language most of the tokens will be hits, so a parser that looks at each token one after another is more effective.
moreover, lucene has to build up an index to be able to return hits - do you want to rebuilt an index after the user hit a key?? IMHO lucene is definitely no solution.
you could take a look at open source editors that have syntax highlighting. for example jedit.
or take a look at the examples of the parser generators. I know that ANTLR has a java parser in its example directories. (www.antlr.org)
in fact, for syntax highlighting you will have to use exactly the same parser as for the compiler. otherwise you will end up with bugs or with a lot of more work than with a generated parser.
Chantal
Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Thanks,
I found a package called JFlex from which you can
generate a JavaLexer.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Ger,
yes, I know JFlex. I think it's nice. But if you want to generate a parser you will need a parser generator, as well. JFlex generates only the Lexer that will be the input for the Parser generator. JFlex and Java CUP work together but in theory you should be able to use any parser generator. I first tried to use JFlex and CUP but I got some problems and switched to ANTLR. That one brings a Lexer, Parser, and Tree Parser generator. They have similar programming rules, so if you know how to write a Lexer in ANTLR you know how to write the Parser as well. There is a detailed manual included in the download.
Chantal
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
to add:
there is an optional ANTLR task for ant, and the jedit community provides a ANTLR edit mode for jEdit.
I managed to integrate JFlex into the ant build file but got problems with CUP. It takes input from the commandline using '<' which is not very practical IMHO if you have more than one parser to generate, or you have a bigger project where the parser is only a little part of respectively.
chantal
Ger Sweeney
Greenhorn

Joined: Jan 29, 2003
Posts: 11
Thanks Chantal,
I've downloaded the package and am reading the manual as we speak.
Thanks again for your help,
Ger
 
GeeCON Prague 2014
 
subject: Syntax Highlighting