Jump to content
  • Advertisement
Sign in to follow this  
ajm113

I need help creating a syntacthighlighter for my program! (C#)

This topic is 4089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I have been searching google for hours on creating a syntax highlight, but the ones I always find are ether out dated or the code does not work. Can someone direct me to a good tutorial or perhaps post one on creating a simple syntax system using the newer version of C#?

Share this post


Link to post
Share on other sites
Advertisement
I highly recommend looking at existing libraries that do this ... much like HTML rendering, it can be done many ways, and hundreds have been tried ... but it is big and complex enough that usually 1 of the 3-4 options out there that have proven themselves useful are well worth the effort to learn instead of writing from scratch.

Be careful to check the license terms BEFORE reading the source code.

I don't know the license but I like the Scintilla library (which is the core of the SciTE program that ships with ruby for windows).

There are others ... look for freeware or open source editors and find the library they use.

Share this post


Link to post
Share on other sites
I was thinking on doing that, but I did not want to run into trouble like you said or have no idea what I am looking at. I am actully supprised for the fact that no one that has written a C# syntax tutorial has not even put a effert into updating them... Sad :(

Share this post


Link to post
Share on other sites
Right, so are you trying to highlight C# syntax or are you trying to write the parser in C# or do you already have it parsed and trying to highlight based on the resultant tree?

I just did a and b and might be able to answer a few questions if you're aiming to learn rather than get stuff done (in which case, use an already existing library) and have enough background knowledge to learn it effectively.

Share this post


Link to post
Share on other sites
I am trying to get C# to write a parser code for a language that is not C++ or C#. I have only been playing with C# for a couple of months so I don't just want to jump into library's and expect myself to find something in their that mite help me.

Share this post


Link to post
Share on other sites
So, what problems are you encountering? I imagine there's a fair number of resources on making recursive descent parsers. Not in C# mind you, but that's just a matter of syntax not of concepts.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
So, what problems are you encountering? I imagine there's a fair number of resources on making recursive descent parsers. Not in C# mind you, but that's just a matter of syntax not of concepts.


For the problem right now is that I hade intil I got rid of the code was that all my text came out in one color when I tried to get the syntax going on here on this tutorial that help alital more then the old ones did:
http://www.c-sharpcorner.com/UploadFile/duncanharris/SyntaxHighlightInRichTextBoxP112012005050840AM/SyntaxHighlightInRichTextBoxP1.aspx

What I can do is try to recreate the old code and give you what I did on the old one that work correctly. Just give me some time on that.

I pretty much saved all the tutorial I could find that looked and seemed that they wold worked for me on my search engine for syntax tutorials under C#: http://www.bombinaid.com/search.php?orgin=Software&search=syntax

Share this post


Link to post
Share on other sites
Ok here is the code:

//
// newRtb
//
this.newRtb.AcceptsTab = true;
this.newRtb.Borderstyle = System.Windows.Forms.Borderstyle.None;
this.newRtb.DetectUrls = false;
this.newRtb.Dock = System.Windows.Forms.Dockstyle.Fill;
this.newRtb.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.Fontstyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.newRtb.HideSelection = false;
this.newRtb.Location = new System.Drawing.Point(3, 3);
this.newRtb.Name = "newRtb";
this.newRtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.newRtb.Size = new System.Drawing.Size(651, 510);
this.newRtb.TabIndex = 0;
this.newRtb.Text = "";

Regex r = new Regex("\\n");
String [] lines = r.Split("");

foreach (string l in lines)
{
ParseLine(l);
}





/// <summary>
/// Create Syntax Color
/// </summary>
void ParseLine(string line)
{
Regex r = new Regex("([ \\t{}():;])");
String[] tokens = r.Split(line);
foreach (string token in tokens)
{
// Set the tokens default color and font.
rtb.SelectionColor = Color.Black;
rtb.SelectionFont = new Font("Courier New", 10, Fontstyle.Regular);
// Check whether the token is a keyword.
String[] keywords = { "public", "void", "using", "static", "class" };
for (int i = 0; i < keywords.Length; i++)
{
if (keywords == token)
{
// Apply alternative color and font to highlight keyword.
newRtb.SelectionColor = Color.Blue;
newRtb.SelectionFont = new Font("Courier New", 10, Fontstyle.Bold);
break;
}
}
newRtb.SelectedText = token;
}
newRtb.SelectedText = "\n";
}

I forgot how the code was set up that made the bug, but anyways here are the parts of the code that are woking together.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!