V
ich hab da noch ein wenig toten code rumliegen. er schafft zwar nicht alles. ich glaub', er schafft zum beispiel keine /**/-kommentare, aber er ist fast schon lesbar einfach.
#include "pch.h"
#include "highlight.h"
#include "globals.h"
#include "Init.h"
#include <set>
char *keywords[]={
"__asm","enum","__multiple_inheritance","template",
"auto","__except","__single_inheritance","this",
"__based","explicit","__virtual_inheritance",
"thread","bool","extern","mutable","throw","break",
"false","naked","true","case","__fastcall","namespace",
"try","catch","__finally","new","__try","__cdecl","float",
"operator","typedef","char","for","private","typeid",
"class","friend","protected","typename","const",
"goto","public","union","const_cast","if","register",
"unsigned","continue","inline","reinterpret_cast",
"using","__declspec","__inline","return","uuid","default",
"int","short","__uuidof","delete","__int8","signed","virtual",
"dllexport","__int16","sizeof","void","dllimport","__int32",
"static","volatile","do","__int64","static_cast",
"double","__leave","__stdcall","while","dynamic_cast",
"long","struct","xalloc","else","switch"};
set<string> keywordSet;
#define countof(X) (sizeof(X)/sizeof(X[0]))
void createKeywordSet()
{
for(int i=0;i<countof(keywords);++i)
keywordSet.insert(keywords[i]);
};
INIT(createKeywordSet,doNothing);
IMPLEMENT_INIT(createKeywordSet,doNothing);
const char *parseSpace(const char *pos)
{
if(*pos==' ')
++pos;
return pos;
};
const char *parseTab(const char *pos)
{
if(*pos=='\t')
++pos;
return pos;
};
const char *parsePreprocessor(const char *pos)
{
if(*pos=='#')
while(*pos!='\0' && *pos!='\n')
++pos;
return pos;
};
const char *parseComment(const char *pos)
{
if(*pos=='/' && *(pos+1)=='/')
while(*pos!='\0' && *pos!='\n')
++pos;
return pos;
};
const char *parseToken(const char *pos)
{
if(isalpha(*pos) || *pos=='_')
{
++pos;
while(*pos!='\0' && (isalpha(*pos)||*pos=='_'||isdigit(*pos)))
++pos;
};
return pos;
};
const char *parseSymbol(const char *pos)
{
if(strchr("+-*/%!=<>()[]{}~|&?:;,.",*pos)!=0)
++pos;
return pos;
};
const char *parseNumber(const char *pos)
{
if(isdigit(*pos))
{
while(*pos!='\0' && (isxdigit(*pos)||*pos=='.'))
++pos;
}
return pos;
};
const char *parseIndextag(const char *oPos)
{
const char *pos=oPos;
if(*pos=='(')
{
++pos;
if(*pos=='i')
{
++pos;
if(*pos==':')
{
++pos;
if(*pos==' ')
{
while(*pos!='\0')
{
if(*pos==')')
return pos+1;
++pos;
}
return pos;
}
}
}
}
return oPos;
};
const char *parseString(const char *pos)
{
if(*pos=='\"')
{
++pos;
while(*pos!='\0')
{
if(*pos=='\"')
return pos+1;
++pos;
}
}
return pos;
};
const char *parseCharacter(const char *pos)
{
if(*pos=='\'')
{
++pos;
while(*pos!='\0')
{
if(*pos=='\'')
return pos+1;
++pos;
}
}
return pos;
};
const char *parseError(const char *pos)
{
while(*pos!='\0')
++pos;
return pos;
};
string textToHtmlCpp(const string &s)
{
ostrstream out;
const char *buffer=s.c_str();
const char *left=buffer;
const char *right=buffer;
while(*right!='\0')
{
right=parsePreprocessor(right);
if(right!=left)
{
string s(left,right-left);
out<<"<font color=green>"<<textToHtml(s)<<"</font>";
left=right;
continue;
};
right=parseComment(right);
if(right!=left)
{
string s(left,right-left);
out<<"<i><font color=gray>"<<textToHtml(s)<<"</font></i>";
left=right;
continue;
};
right=parseIndextag(right);
if(right!=left)
{
left=right;
continue;
};
right=parseSpace(right);
if(right!=left)
{
string s(left,right-left);
out<<" ";
left=right;
continue;
};
right=parseToken(right);
if(right!=left)
{
string s(left,right-left);
if(keywordSet.find(s)!=keywordSet.end())
{
out<<"<font color=blue>"<<textToHtml(s)<<"</font>";
}
else
{
out<<textToHtml(s);
};
left=right;
continue;
};
right=parseSymbol(right);
if(right!=left)
{
string s(left,right-left);
out<<textToHtml(s);
left=right;
continue;
};
right=parseNumber(right);
if(right!=left)
{
string s(left,right-left);
out<<"<font color=red>"<<textToHtml(s)<<"</font>";
left=right;
continue;
};
right=parseString(right);
if(right!=left)
{
string s(left,right-left);
out<<"<font color=navy>"<<textToHtml(s)<<"</font>";
left=right;
continue;
};
right=parseCharacter(right);
if(right!=left)
{
string s(left,right-left);
out<<"<font color=red>"<<textToHtml(s)<<"</font>";
left=right;
continue;
};
right=parseError(right);
if(right!=left)
{
string s(left,right-left);
out<<"<h1>error in syntax highlighting</h1>"<<textToHtml(s);
left=right;
throw("Fehler beim Syntax-Highlighting: "+s);
};
};
return string(out.str(),out.pcount());
};