• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Syntax Highlighting

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
Ger
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
I found a package called JFlex from which you can
generate a JavaLexer.
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chantal,
I've downloaded the package and am reading the manual as we speak.
Thanks again for your help,
Ger
 
Your mother was a hamster and your father was a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic