Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75254 - branches/quickbook-dev/tools/quickbook/src
From: dnljms_at_[hidden]
Date: 2011-11-02 03:58:11


Author: danieljames
Date: 2011-11-02 03:58:11 EDT (Wed, 02 Nov 2011)
New Revision: 75254
URL: http://svn.boost.org/trac/boost/changeset/75254

Log:
Quickbook: Only create the syntax highlighter keywords once.

Symbol tables are really expensive to create, so share between all
instances of `cpp_highlight` and `python_highlight`. It would be better
to create all the grammars just once, but that's trickier as they're quite
stateful and they're not nearly as expensive as the symbol tables.
Text files modified:
   branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp | 84 ++++++++++++++++++++++-----------------
   1 files changed, 47 insertions(+), 37 deletions(-)

Modified: branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp
==============================================================================
--- branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp (original)
+++ branches/quickbook-dev/tools/quickbook/src/syntax_highlight.cpp 2011-11-02 03:58:11 EDT (Wed, 02 Nov 2011)
@@ -20,6 +20,51 @@
 {
     namespace cl = boost::spirit::classic;
 
+ struct keywords_holder
+ {
+ cl::symbols<> cpp, python;
+
+ keywords_holder()
+ {
+ cpp
+ = "and_eq", "and", "asm", "auto", "bitand", "bitor",
+ "bool", "break", "case", "catch", "char", "class",
+ "compl", "const_cast", "const", "continue", "default",
+ "delete", "do", "double", "dynamic_cast", "else",
+ "enum", "explicit", "export", "extern", "false",
+ "float", "for", "friend", "goto", "if", "inline",
+ "int", "long", "mutable", "namespace", "new", "not_eq",
+ "not", "operator", "or_eq", "or", "private",
+ "protected", "public", "register", "reinterpret_cast",
+ "return", "short", "signed", "sizeof", "static",
+ "static_cast", "struct", "switch", "template", "this",
+ "throw", "true", "try", "typedef", "typeid",
+ "typename", "union", "unsigned", "using", "virtual",
+ "void", "volatile", "wchar_t", "while", "xor_eq", "xor"
+ ;
+
+ python
+ =
+ "and", "del", "for", "is", "raise",
+ "assert", "elif", "from", "lambda", "return",
+ "break", "else", "global", "not", "try",
+ "class", "except", "if", "or", "while",
+ "continue", "exec", "import", "pass", "yield",
+ "def", "finally", "in", "print",
+
+ // Technically "as" and "None" are not yet keywords (at Python
+ // 2.4). They are destined to become keywords, and we treat them
+ // as such for syntax highlighting purposes.
+
+ "as", "None"
+ ;
+ }
+ };
+
+ namespace {
+ keywords_holder keywords;
+ }
+
     // Grammar for C++ highlighting
     struct cpp_highlight
     : public cl::grammar<cpp_highlight>
@@ -106,26 +151,9 @@
                     ;
 
                 keyword
- = keyword_ >> (cl::eps_p - (cl::alnum_p | '_'))
+ = keywords.cpp >> (cl::eps_p - (cl::alnum_p | '_'))
                     ; // make sure we recognize whole words only
 
- keyword_
- = "and_eq", "and", "asm", "auto", "bitand", "bitor",
- "bool", "break", "case", "catch", "char", "class",
- "compl", "const_cast", "const", "continue", "default",
- "delete", "do", "double", "dynamic_cast", "else",
- "enum", "explicit", "export", "extern", "false",
- "float", "for", "friend", "goto", "if", "inline",
- "int", "long", "mutable", "namespace", "new", "not_eq",
- "not", "operator", "or_eq", "or", "private",
- "protected", "public", "register", "reinterpret_cast",
- "return", "short", "signed", "sizeof", "static",
- "static_cast", "struct", "switch", "template", "this",
- "throw", "true", "try", "typedef", "typeid",
- "typename", "union", "unsigned", "using", "virtual",
- "void", "volatile", "wchar_t", "while", "xor_eq", "xor"
- ;
-
                 special
                     = +cl::chset_p("~!%^&*()+={[}]:;,<.>?/|\\-")
                     ;
@@ -159,7 +187,6 @@
                             char_, number, identifier, keyword, qbk_phrase, escape,
                             string_char;
 
- cl::symbols<> keyword_;
             quickbook_grammar& g;
             std::string save;
 
@@ -248,25 +275,9 @@
                     ;
 
                 keyword
- = keyword_ >> (cl::eps_p - (cl::alnum_p | '_'))
+ = keywords.python >> (cl::eps_p - (cl::alnum_p | '_'))
                     ; // make sure we recognize whole words only
 
- keyword_
- =
- "and", "del", "for", "is", "raise",
- "assert", "elif", "from", "lambda", "return",
- "break", "else", "global", "not", "try",
- "class", "except", "if", "or", "while",
- "continue", "exec", "import", "pass", "yield",
- "def", "finally", "in", "print",
-
- // Technically "as" and "None" are not yet keywords (at Python
- // 2.4). They are destined to become keywords, and we treat them
- // as such for syntax highlighting purposes.
-
- "as", "None"
- ;
-
                 special
                     = +cl::chset_p("~!%^&*()+={[}]:;,<.>/|\\-")
                     ;
@@ -312,7 +323,6 @@
                             short_string, long_string, number, identifier, keyword,
                             qbk_phrase, escape, string_char;
 
- cl::symbols<> keyword_;
             quickbook_grammar& g;
             std::string save;
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk