libcsdbg  1.28
C++ exception (and generic) stack trace debug library
string * csdbg::parser::highlight ( const i8 syntax = NULL,
bool  icase = false 
) const
virtual

Highlight (escape) the current buffer using a custom syntax.

Parameters
[in]syntaxa POSIX extended regular expression
[in]icasetrue to ignore case while parsing
Returns
the escaped text (heap allocated)
Exceptions
std::bad_alloc
csdbg::exception

Definition at line 593 of file parser.cpp.

References csdbg::string::append(), csdbg::style::apply(), csdbg::string::at(), csdbg::chain< T >::at(), csdbg::string::cmp(), get_style(), likely, lookup(), m_fallback, csdbg::string::match(), parse(), csdbg::chain< T >::size(), csdbg::string::string(), and unlikely.

Referenced by csdbg::operator<<().

594 {
595  const i8 *num = "^0x[0-9a-f]+$|^[0-9]+$";
596 
597  string *retval = new string;
598  chain<string> *tokens = NULL;
599 
600  /* If an exception occurs, release resources and rethrow it */
601  try {
602  /* Parse the buffer */
603  tokens = parse(syntax, icase);
604 
605  /* Highlight and append each token in the result */
606  for (u32 i = 0, sz = tokens->size(); likely(i < sz); i++) {
607  string *token = tokens->at(i);
608 
609  /* Select the style for the current token */
610  style *cur = NULL;
611  if ( likely(i % 2 == 1) )
612  cur = get_style("delimiter");
613 
614  else if ( unlikely(token->match(num, true)) )
615  cur = get_style("number");
616 
617  else if ( unlikely(lookup(*token, "keywords")) )
618  cur = get_style("keyword");
619 
620  else if ( unlikely(lookup(*token, "types")) )
621  cur = get_style("type");
622 
623  /* Ignore case for extension (regexp) lookups */
624  else if ( unlikely(lookup(*token, "extensions", true)) )
625  cur = get_style("file");
626 
627  /* Select the style based on the next delimiter */
628  else if ( likely(i < sz - 1) ) {
629  string *delim = tokens->at(i + 1);
630  i8 ch = delim->at(0);
631 
632  if ( unlikely(delim->cmp("::") == 0) )
633  cur = get_style("scope");
634 
635  else if ( unlikely(ch == '(' || ch == '<' || ch == '\r') )
636  cur = get_style("function");
637  }
638 
639  /* If the token was not identified (plain text) */
640  if ( unlikely(cur == NULL) )
641  cur = m_fallback;
642 
643  /* Apply the style to the token and append it to the result buffer */
644  cur->apply(*token);
645  retval->append(*token);
646  }
647 
648  delete tokens;
649  return retval;
650  }
651 
652  catch (...) {
653  delete tokens;
654  delete retval;
655  throw;
656  }
657 }
char i8
8-bit signed integer
Definition: config.hpp:72
virtual chain< string > * parse(const i8 *=NULL, bool=false) const
Parse the current buffer using a custom syntax.
Definition: parser.cpp:572
virtual style & apply(string &) const
Apply the style to some text.
Definition: style.cpp:304
string(u32=0)
Object constructor.
Definition: string.cpp:127
#define likely(expr)
Offer a hint (positive) to the pipeline branch predictor.
Definition: config.hpp:344
virtual style * get_style(const i8 *) const
Get a style, indexed by name.
Definition: parser.cpp:514
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
virtual bool lookup(const string &, const i8 *, bool=false) const
Lookup an expression in one of the parser dictionaries.
Definition: parser.cpp:673
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
static style * m_fallback
Shared fallback style.
Definition: parser.hpp:37

+ Here is the call graph for this function:

+ Here is the caller graph for this function: