Ok, I have written the bare-bones of this program that parses through C code and highlights key words and common things. Like an IDE an it puts HTML encoding around the words that need to be highlighted. I got everything working except checking for comments.
i got the // to work but not the: /* and */
any ideas on how I could get that to work, here is the function that does the parsing:
Code:
void parse(CString filename, const apvector<synkey> &set,ostream& out)
{
ifstream in(filename.std_str());
CString buf;
char c;
char back;
int k=0;
bool end=false;
while(in.get(c))
{
if(buf=="//")
{
out<<"<font color=\""<<color("green")<<"\">";
out<<buf<<c;
getline(in,buf);
out<<buf;
out<<"</font>"<<endl;
buf="";
}
else if(c==' '||c=='\t'||c=='\n'||c=='('||c=='['||c=='{'||c==';'||c==')'||c=='}'||c==']'||c=='<'||c=='>')
{
if(buf!="")
{
if((k=iskey(buf,set))!=-1)
out<<"<font color=\""<<color(set[k].color)<<"\">"<<buf<<"</font>";
else
out<<buf;
buf="";
}
if(c=='<')
out<<"<";
else if(c=='>')
out<<">";
else
out<<c;
}
else
buf+=c;
}
}
if you run it with the rest of my prog. it works fine but when I implement something like:
Code:
else if(buf=="/*")
{
out<<"<font color=\""<<color("green")<<"\">";
out<<buf<<c;
buf="";
while(!end && !(in.eof()))
{
back=c;
in.get(c);
if(back=='*'&&c=='/')
{
out<<"*/</font>";
end=true;
}
else
out<<c;
}
buf="";
}
right after the first if clause, it doesn't do it right. I am doing something wrong I can tell, but I can't figure it out. I have run through the text file character by character trying to figure out why it does it...
here was an output from a small section of highlighted code that I was checking for comments on:
Code:
<font color="#66CC00">//loads of comments</font>
<font color="#66CC00">//yes comments!</font>
<font color="#66CC00">/* comments **/</font>
<font color="#66CC00">/*more comments*/
<font color="#0000FF">struct</font> synkey
{
CString key;
CString color;
};
CString color(CString clr <font color="#66CC00">/*what should I do about all these comments?*/)
when the original piece was:
Code:
//loads of comments
//yes comments!
/* comments */
/*more comments*/
struct synkey
{
CString key;
CString color;
};
CString color(CString clr /*what should I do about all these comments?*/)
any ideas or improvements on my method of parsing would be greatly appreciated, I plan to redo the whole thing if I can't figure this out. Usually if I redo it all and start over it just works itself out.
As you can see I take apart the input by character and do single character checking and use a str (CString) to hold words in a buffer and then check them from a syntax file
Also, know of any ways to make double character checking easier?
I was thinking about using in-code tags to signal if Quotes are being used or a comment line is active but I would have to rewrite almost all of parse()
Right now, I just want to see if I can get the bare-minimum working before I tear apart the program.
Thanks
-LC