//-----------------------------------------------------------------------------
#define CSTR
#include <array>
#include <string>
#include <regex>
//-----------------------------------------------------------------------------
enum class CPP_SYNTAX : int
{
etc,
block_comment,
line_comment,
preprocessor,
string_literal,
character_literal,
real_literal,
integer_literal,
bool_literal,
types,
flow_controls,
keywords,
operators,
end,
};
const std::array< CPP_SYNTAX, 12 > ordered_syntaxes =
{
CPP_SYNTAX::string_literal,
CPP_SYNTAX::block_comment,
CPP_SYNTAX::line_comment,
CPP_SYNTAX::preprocessor,
CPP_SYNTAX::character_literal,
CPP_SYNTAX::real_literal,
CPP_SYNTAX::integer_literal,
CPP_SYNTAX::bool_literal,
CPP_SYNTAX::types,
CPP_SYNTAX::flow_controls,
CPP_SYNTAX::keywords,
CPP_SYNTAX::operators,
};
//-----------------------------------------------------------------------------
const std::array< std::regex, 13 > syntax_rules
{
std::regex( "" ),
std::regex( "\\/\\*(?:[^\\*][^\\/]|[\\s])*\\*\\/" ),
std::regex( "\\/\\/.*" ),
std::regex( "^[\\t ]*#\\s*[a-zA-Z]*" ),
std::regex( "(?:\\bL)?\"[^\"]*\"" ),
std::regex( "(?:\\bL)?'[^']*'" ),
std::regex( "[\\+\\-]?(?:(?:\\d+\\.\\d*|\\.\\d+)"
"(?:[eE][\\+\\-]?\\d+)?)[fFlL]?(?!\\.)" ),
std::regex( "[\\+\\-]?\\b(?:0[0-7]*|[1-9]\\d*|"
"0x[\\dA-F]+)[uUlL]{0,3}\\b(?!\\.)" ),
std::regex( "\\b(?:true|false)\\b" ),
std::regex( "\\b(?:bool|char|double|float|int|long|short|"
"signed|unsigned|void|wchar_t)\\b" ),
std::regex( "\\b(?:break|case|catch|continue|default|do|else|"
"for|goto|if|return|switch|throw|try|while)\\b" ),
std::regex( "\\b(?:asm|auto|class|const_cast|const|delete|"
"dynamic_cast|enum|explicit|export|extern|friend|"
"inline|main|mutable|namespace|new|operator|private|"
"protected|public|register|reinterpret_cast|sizeof|"
"static_cast|static|struct|template|this|typedef|"
"typeid|typename|union|using|virtual|volatile)\\b" ),
std::regex( "[\\{\\}\\[\\]\\(\\)\\.\\?\\*\\+\\-\\^\\|\\\\\\/&<>%:;~!=,]+" ),
};
//-----------------------------------------------------------------------------
const std::string coloring_begins[] =
{
"<E>", "<M>", "<L>", "<D>",
"<G>", "<C>", "<R>", "<Z>", "<N>",
"<T>", "<F>", "<K>", "<O>",
};
const std::string coloring_ends[] =
{
"</E>", "</M>", "</L>", "</D>",
"</G>", "</C>", "</R>", "</Z>", "</N>",
"</T>", "</F>", "</K>", "</O>",
};
auto html_heading( std::string& html )
{
return html = "<style type=\"text/css\">\n"
"E { color: #555; font-family: 'Courier New'; font-size: 10pt; }\n"
"M { color: #AAA; font-family: 'Courier New'; font-size: 10pt; }\n"
"L { color: #AAA; font-family: 'Courier New'; font-size: 10pt; }\n"
"D { color: #238; font-family: 'Courier New'; font-size: 10pt; }\n"
"G { color: #F66; font-family: 'Courier New'; font-size: 10pt; }\n"
"C { color: #D55; font-family: 'Courier New'; font-size: 10pt; }\n"
"R { color: #08F; font-family: 'Courier New'; font-size: 10pt; }\n"
"Z { color: #66F; font-family: 'Courier New'; font-size: 10pt; }\n"
"N { color: #45F; font-family: 'Courier New'; font-size: 10pt; }\n"
"T { color: #45F; font-family: 'Courier New'; font-size: 10pt; }\n"
"F { color: #45F; font-family: 'Courier New'; font-size: 10pt; }\n"
"K { color: #45F; font-family: 'Courier New'; font-size: 10pt; }\n"
"O { color: #8AF; font-family: 'Courier New'; font-size: 10pt; }\n"
"</style>\n<pre>\n";
}
auto html_footing( std::string& html )
{
return html += "\n</pre>\n";
}
//-----------------------------------------------------------------------------
struct RANGE
{
size_t position, length;
CSTR RANGE( const size_t position, const size_t length )
: position( position )
, length( length )
{
}
};
struct COLORING
{
RANGE range;
CPP_SYNTAX syntax;
CSTR COLORING( const RANGE& range, const CPP_SYNTAX syntax )
: range( range )
, syntax( syntax )
{
}
bool operator<( const COLORING& rhs ) const
{
return range.position < rhs.range.position;
}
};
///----------------------------------------------------------------------------
auto regex_find_ranges( const std::string& str, const std::regex& rule )
{
std::vector< RANGE > ranges;
using sregex_it = std::sregex_iterator;
const sregex_it end;
for( sregex_it next( str.begin(), str.end(), rule ); next != end; ++next )
ranges.emplace_back( next->position(), next->length() );
return ranges;
}
///----------------------------------------------------------------------------
void translate_code( std::string& dst, const std::string& src,
size_t& it, const size_t it_end )
{
for( ; it < it_end; ++it )
if( src[ it ] == '&' ) dst += "&";
else if( src[ it ] == '<' ) dst += "<";
else dst += src[ it ];
}
///----------------------------------------------------------------------------
#include <algorithm>
auto build_html( const std::string& code )
{
std::string result;
html_heading( result );
std::vector< COLORING > color_marks;
std::string temp_code =
std::regex_replace( code, std::regex( "\\\\[\\n\\s\\S]" ), "__" );
for( const auto& syntax : ordered_syntaxes )
{
const int index = (int)syntax;
const auto ranges = regex_find_ranges( temp_code, syntax_rules[ index ] );
for( const auto& range : ranges )
{
color_marks.emplace_back( range, syntax );
const auto range_end = range.position + range.length;
for( auto i = range.position; i < range_end; ++i )
temp_code[ i ] = '_';
}
}
sort( color_marks.begin(), color_marks.end() );
size_t position = 0;
for( const auto& mark : color_marks )
{
if( position < mark.range.position )
{
result += coloring_begins[ (int)CPP_SYNTAX::etc ];
translate_code( result, code, position,
mark.range.position );
result += coloring_ends[ (int)CPP_SYNTAX::etc ];
}
if( position == mark.range.position )
{
result += coloring_begins[ (int)mark.syntax ];
translate_code( result, code, position,
mark.range.position + mark.range.length );
result += coloring_ends[ (int)mark.syntax ];
}
}
return html_footing( result );
}
//-----------------------------------------------------------------------------
#include <iostream>
using namespace std;
const string source_code =
"#include <stdio.h>\n"
"\n"
"int main()\n"
"{\n"
" printf( \"Hello world\\n\" );\n"
" return 0;\n"
"}\n";
int main()
{
cout << build_html( source_code );
return 0;
}
//-----------------------------------------------------------------------------
첫댓글 짧은 깨달음. stl 의 regex 는 장애자들이 만든것임에 틀림없다.
땅곰 "그냥 <M>foo</M> 처럼 적어놓고 M { color: red; } 하는 식도 있죠 " <-- 요걸 써봐야할듯. 일단 구현해놓고.
땅곰이 고마워~
Naming rules
You need to have at least one '-' inside the name of a custom element. Any tag names without '-' will result in an error.
Defining a custom element is simple. Just call document.registerElement() with its tag name as the first argument.var XComponent = document.registerElement('x-component');Now you can use <x-component> wherever you want in the document.<x-component></x-component>
http://w3c.github.io/webcomponents/spec/custom/
옹 그냥도 되나 보네 http://www.w3schools.com/html/html_css.asp
@땅곰 테스트 해보니 잘 되어서 코드 수정~
HTML 16 색 component 표현 가르쳐준 루나님 감사~
line comment 와 preprocessor directives 부분의 regex 결과가 vc++ 과 gcc 가 다르군유~ vc++ 은 perl 과 같은 결과가 나오는데 gcc 는 뭐지 =_= 기본 옵션이 다른가...
일단 완료~ 귀찮음.
주석보다 따옴표 처리를 먼저오게 했습니다. 몇 가지 정규식을 수정했습니다. 정규식끼리 호환성이;;;
푸하하 댓글 글씨체도 다 바뀐다. * 은 못쓰겠넹.