|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r59313 - in branches/quickbook-1.5-spirit2: . detail
From: daniel_james_at_[hidden]
Date: 2010-01-27 17:04:28
Author: danieljames
Date: 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
New Revision: 59313
URL: http://svn.boost.org/trac/boost/changeset/59313
Log:
Update syntax highlighters.
Text files modified:
branches/quickbook-1.5-spirit2/block.cpp | 4
branches/quickbook-1.5-spirit2/detail/actions.cpp | 48 ------
branches/quickbook-1.5-spirit2/detail/actions.hpp | 70 ---------
branches/quickbook-1.5-spirit2/detail/actions_class.cpp | 1
branches/quickbook-1.5-spirit2/detail/actions_class.hpp | 1
branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp | 25 ---
branches/quickbook-1.5-spirit2/doc_info.cpp | 4
branches/quickbook-1.5-spirit2/phrase.cpp | 4
branches/quickbook-1.5-spirit2/syntax_highlight.hpp | 281 +++++++++++++++++++++++++--------------
9 files changed, 188 insertions(+), 250 deletions(-)
Modified: branches/quickbook-1.5-spirit2/block.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/block.cpp (original)
+++ branches/quickbook-1.5-spirit2/block.cpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -231,8 +231,8 @@
; // when preformatted.
hard_space =
- (qi::eps - (qi::alnum | '_')) >> space // must not be preceded by
- ; // alpha-numeric or underscore
+ !(qi::alnum | '_') >> space // must not be preceded by
+ ; // alpha-numeric or underscore
comment =
"[/" >> *(dummy_block | (qi::char_ - ']')) >> ']'
Modified: branches/quickbook-1.5-spirit2/detail/actions.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.cpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -53,54 +53,6 @@
++error_count;
}
- void span::operator()(iterator_range x, unused_type, unused_type) const
- {
- iterator first = x.begin(), last = x.end();
- out << "<phrase role=\"" << name << "\">";
- while (first != last)
- detail::print_char(*first++, out.get());
- out << "</phrase>";
- }
-
- void unexpected_char::operator()(iterator_range x, unused_type, unused_type) const
- {
- iterator first = x.begin(), last = x.end();
- boost::spirit::classic::file_position const pos = first.get_position();
-
- detail::outwarn(pos.file, pos.line)
- << "in column:" << pos.column
- << ", unexpected character: " << std::string(first, last)
- << "\n";
-
- // print out an unexpected character
- out << "<phrase role=\"error\">";
- while (first != last)
- detail::print_char(*first++, out.get());
- out << "</phrase>";
- }
-
- void space::operator()(char ch, unused_type, unused_type) const
- {
- detail::print_space(ch, out.get());
- }
-
- void space::operator()(iterator_range x, unused_type, unused_type) const
- {
- iterator first = x.begin(), last = x.end();
- while (first != last)
- detail::print_space(*first++, out.get());
- }
-
- void raw_char_action::operator()(char ch, unused_type, unused_type) const
- {
- phrase << ch;
- }
-
- void raw_char_action::operator()(iterator_range x, unused_type, unused_type) const
- {
- phrase << *x.begin();
- }
-
void plain_char_action::operator()(char ch, unused_type, unused_type) const
{
detail::print_char(ch, phrase.get());
Modified: branches/quickbook-1.5-spirit2/detail/actions.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions.hpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -75,61 +75,9 @@
int& error_count;
};
- struct span
- {
- // Decorates c++ code fragments
-
- span(char const* name, collector& out)
- : name(name), out(out) {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
-
- char const* name;
- collector& out;
- };
-
- struct unexpected_char
- {
- // Handles unexpected chars in c++ syntax
-
- unexpected_char(collector& out)
- : out(out) {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
-
- collector& out;
- };
-
extern char const* quickbook_get_date;
extern char const* quickbook_get_time;
- struct space
- {
- // Prints a space
-
- space(collector& out)
- : out(out) {}
-
- void operator()(iterator_range, unused_type, unused_type) const;
- void operator()(char ch, unused_type, unused_type) const;
-
- collector& out;
- };
-
- struct raw_char_action
- {
- // Prints a single raw (unprocessed) char.
- // Allows '<', '>'... etc.
-
- raw_char_action(collector& phrase)
- : phrase(phrase) {}
-
- void operator()(char ch, unused_type, unused_type) const;
- void operator()(iterator_range, unused_type, unused_type) const;
-
- collector& phrase;
- };
-
struct plain_char_action
{
// Prints a single plain char.
@@ -144,24 +92,6 @@
collector& phrase;
};
- struct markup_action
- {
- template <typename T = void> struct result { typedef void type; };
-
- // A generic markup action
-
- markup_action(collector& phrase, std::string const& str)
- : phrase(phrase), str(str) {}
-
- void operator()(unused_type, unused_type, unused_type) const
- {
- phrase << str;
- }
-
- collector& phrase;
- std::string str;
- };
-
struct syntax_highlight
{
syntax_highlight(
Modified: branches/quickbook-1.5-spirit2/detail/actions_class.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.cpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -61,7 +61,6 @@
, syntax_p(source_mode, *this)
, plain_char(phrase)
- , raw_char(phrase)
{
// turn off __FILENAME__ macro on debug mode = true
std::string filename_str = debug_mode ?
Modified: branches/quickbook-1.5-spirit2/detail/actions_class.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/actions_class.hpp (original)
+++ branches/quickbook-1.5-spirit2/detail/actions_class.hpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -93,7 +93,6 @@
syntax_highlight syntax_p;
plain_char_action plain_char;
- raw_char_action raw_char;
element_id_warning_action element_id_warning;
};
Modified: branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp (original)
+++ branches/quickbook-1.5-spirit2/detail/syntax_highlight.cpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -3,25 +3,6 @@
namespace quickbook
{
- typedef cpp_highlight<
- span
- , space
- , unexpected_char
- , collector>
- cpp_p_type;
-
- typedef python_highlight<
- span
- , space
- , unexpected_char
- , collector>
- python_p_type;
-
- typedef teletype_highlight<
- plain_char_action
- , collector>
- teletype_p_type;
-
std::string syntax_highlight::operator()(iterator first, iterator last) const
{
escape_actions.phrase.push();
@@ -29,17 +10,17 @@
// print the code with syntax coloring
if (source_mode == "c++")
{
- cpp_p_type cpp_p(escape_actions.phrase, escape_actions);
+ cpp_highlight cpp_p(escape_actions);
parse(first, last, cpp_p);
}
else if (source_mode == "python")
{
- python_p_type python_p(escape_actions.phrase, escape_actions);
+ python_highlight python_p(escape_actions);
parse(first, last, python_p);
}
else if (source_mode == "teletype")
{
- teletype_p_type teletype_p(escape_actions.phrase, escape_actions);
+ teletype_highlight teletype_p(escape_actions);
parse(first, last, teletype_p);
}
else
Modified: branches/quickbook-1.5-spirit2/doc_info.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/doc_info.cpp (original)
+++ branches/quickbook-1.5-spirit2/doc_info.cpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -184,8 +184,8 @@
;
hard_space =
- (qi::eps - (qi::alnum | '_')) >> space // must not be preceded by
- ; // alpha-numeric or underscore
+ !(qi::alnum | '_') >> space // must not be preceded by
+ ; // alpha-numeric or underscore
phrase =
*( common
Modified: branches/quickbook-1.5-spirit2/phrase.cpp
==============================================================================
--- branches/quickbook-1.5-spirit2/phrase.cpp (original)
+++ branches/quickbook-1.5-spirit2/phrase.cpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -163,7 +163,7 @@
// when preformatted.
hard_space =
- (qi::eps - (qi::alnum | '_')) >> space
+ !(qi::alnum | '_') >> space
; // must not be preceded by
// alpha-numeric or underscore
@@ -187,7 +187,7 @@
macro =
( actions.macro // must not be followed by
- >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
+ >> !(qi::alpha | '_') // alpha or underscore
) [actions.process]
;
Modified: branches/quickbook-1.5-spirit2/syntax_highlight.hpp
==============================================================================
--- branches/quickbook-1.5-spirit2/syntax_highlight.hpp (original)
+++ branches/quickbook-1.5-spirit2/syntax_highlight.hpp 2010-01-27 17:04:27 EST (Wed, 27 Jan 2010)
@@ -14,18 +14,54 @@
#include <boost/spirit/include/qi_auxiliary.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/qi_directive.hpp>
+#include <boost/fusion/include/adapt_struct.hpp>
+#include "./parse_types.hpp"
#include "./grammars.hpp"
#include "./phrase.hpp"
namespace quickbook
{
+ struct code_token
+ {
+ std::string text;
+ char const* type;
+ };
+
+ struct space
+ {
+ std::string text;
+ char const* dummy;
+ };
+
+ void process(quickbook::actions& actions, code_token const& x)
+ {
+ std::string type = x.type;
+ if(type == "space") {
+ actions.phrase << x.text;
+ }
+ else {
+ actions.phrase << "<phrase role=\"" << x.type << "\">";
+ detail::print_string(x.text, actions.phrase.get());
+ actions.phrase << "</phrase>";
+ }
+ }
+}
+
+BOOST_FUSION_ADAPT_STRUCT(
+ quickbook::code_token,
+ (std::string, text)
+ (char const*, type)
+)
+
+namespace quickbook
+{
namespace qi = boost::spirit::qi;
using boost::spirit::unused_type;
struct parse_escaped_impl
{
- parse_escaped_impl(quickbook::actions& escape_actions)
- : actions(escape_actions) {}
+ parse_escaped_impl(quickbook::actions& actions)
+ : actions(actions) {}
void operator()(boost::iterator_range<iterator> escaped, unused_type, unused_type) const {
bool unused;
@@ -43,60 +79,66 @@
};
// Grammar for C++ highlighting
- template <
- typename Process
- , typename Space
- , typename Unexpected
- , typename Out>
- struct cpp_highlight
- : public qi::grammar<iterator>
+ struct cpp_highlight : public qi::grammar<iterator>
{
- cpp_highlight(Out& out, quickbook::actions& escape_actions)
- : cpp_highlight::base_type(program), out(out), escape_actions(escape_actions)
- , parse_escaped(escape_actions)
+ cpp_highlight(quickbook::actions& actions)
+ : cpp_highlight::base_type(program), actions(actions)
+ , parse_escaped(actions)
{
program
=
- *( qi::raw[+qi::space] [Space(out)]
+ *( space
| macro
| escape
- | qi::raw[preprocessor] [Process("preprocessor", out)]
- | qi::raw[comment] [Process("comment", out)]
- | qi::raw[keyword] [Process("keyword", out)]
- | qi::raw[identifier] [Process("identifier", out)]
- | qi::raw[special] [Process("special", out)]
- | qi::raw[string_] [Process("string", out)]
- | qi::raw[char_] [Process("char", out)]
- | qi::raw[number] [Process("number", out)]
- | qi::raw[qi::char_] [Unexpected(out)]
- )
+ | preprocessor
+ | comment
+ | keyword
+ | identifier
+ | special
+ | string_
+ | char_
+ | number
+ | unexpected
+ ) [actions.process]
;
macro =
- ( escape_actions.macro // must not be followed by
- >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
- ) [escape_actions.process]
+ ( actions.macro // must not be followed by
+ >> !(qi::alpha | '_') // alpha or underscore
+ )
;
escape =
"``" >> (
(qi::raw[+(qi::char_ - "``")] >> "``")
[parse_escaped]
- | qi::raw[*qi::char_] [escape_actions.error]
+ | qi::raw[*qi::char_] [actions.error]
)
;
+ space
+ = qi::raw[+qi::space]
+ >> qi::attr("space");
+
preprocessor
- = '#' >> *qi::space >> ((qi::alpha | '_') >> *(qi::alnum | '_'))
+ = qi::raw[
+ '#' >> *qi::space
+ >> ((qi::alpha | '_') >> *(qi::alnum | '_'))
+ ]
+ >> qi::attr("preprocessor")
;
comment
- = qi::lit("//") >> *(qi::char_ - qi::eol) >> -qi::eol
- | qi::lit("/*") >> *(qi::char_ - "*/") >> -qi::lit("*/")
+ = qi::raw[
+ qi::lit("//") >> *(qi::char_ - qi::eol) >> -qi::eol
+ | qi::lit("/*") >> *(qi::char_ - "*/") >> -qi::lit("*/")
+ ]
+ >> qi::attr("comment")
;
keyword
- = keyword_ >> (qi::eps - (qi::alnum | '_'))
+ = qi::raw[keyword_ >> !(qi::alnum | '_')]
+ >> qi::attr("keyword")
; // make sure we recognize whole words only
keyword_
@@ -118,39 +160,59 @@
special
= +qi::char_("~!%^&*()+={[}]:;,<.>?/|\\-")
+ >> qi::attr("special")
;
string_char = ('\\' >> qi::char_) | (qi::char_ - '\\');
string_
- = -qi::no_case['l'] >> '"' >> *(string_char - '"') >> -qi::lit('"');
+ = qi::raw[
+ -qi::no_case['l']
+ >> '"' >> *(string_char - '"') >> -qi::lit('"')
+ ]
+ >> qi::attr("string")
;
- char_
- = -qi::no_case['l'] >> '\'' >> *(string_char - '\'') >> -qi::lit('\'');
+ char_ =
+ qi::raw[
+ -qi::no_case['l']
+ >> '\'' >> *(string_char - '\'') >> -qi::lit('\'')
+ ]
+ >> qi::attr("char")
;
number
- = (
- qi::no_case["0x"] >> qi::hex
- | '0' >> qi::oct
- | qi::long_double
- )
+ = qi::raw
+ [
+ (
+ qi::no_case["0x"] >> qi::hex
+ | '0' >> qi::oct
+ | qi::long_double
+ )
>> *qi::no_case[qi::char_("ldfu")]
+ ]
+ >> qi::attr("number")
;
identifier
- = (qi::alpha | '_') >> *(qi::alnum | '_')
+ = qi::raw[(qi::alpha | '_') >> *(qi::alnum | '_')]
+ >> qi::attr("identifier")
+ ;
+
+ // TODO: warn user?
+ unexpected
+ = qi::raw[qi::char_]
+ >> qi::attr("error")
;
}
- qi::rule<iterator>
- program, macro, preprocessor, comment, special, string_,
- char_, number, identifier, keyword, escape,
- string_char;
+ qi::rule<iterator> program, escape, string_char;
+ qi::rule<iterator, quickbook::macro()> macro;
+ qi::rule<iterator, code_token()>
+ space, preprocessor, comment, special, string_,
+ char_, number, identifier, keyword, unexpected;
- Out& out;
- quickbook::actions& escape_actions;
+ quickbook::actions& actions;
qi::symbols<> keyword_;
parse_escaped_impl parse_escaped;
@@ -160,53 +222,55 @@
// Grammar for Python highlighting
// See also: The Python Reference Manual
// http://docs.python.org/ref/ref.html
- template <
- typename Process
- , typename Space
- , typename Unexpected
- , typename Out>
- struct python_highlight
- : public qi::grammar<iterator>
+ struct python_highlight : public qi::grammar<iterator>
{
- python_highlight(Out& out, quickbook::actions& escape_actions)
- : python_highlight::base_type(program), out(out), escape_actions(escape_actions)
- , parse_escaped(escape_actions)
+ python_highlight(quickbook::actions& actions)
+ : python_highlight::base_type(program), actions(actions)
+ , parse_escaped(actions)
{
program
=
- *( qi::raw[+qi::space] [Space(out)]
+ *( space
| macro
- | escape
- | qi::raw[comment] [Process("comment", out)]
- | qi::raw[keyword] [Process("keyword", out)]
- | qi::raw[identifier] [Process("identifier", out)]
- | qi::raw[special] [Process("special", out)]
- | qi::raw[string_] [Process("string", out)]
- | qi::raw[number] [Process("number", out)]
- | qi::raw[qi::char_] [Unexpected(out)]
- )
+ | escape
+ | comment
+ | keyword
+ | identifier
+ | special
+ | string_
+ | number
+ | unexpected
+ ) [actions.process]
;
macro =
- ( escape_actions.macro // must not be followed by
- >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
- ) [escape_actions.process]
+ ( actions.macro // must not be followed by
+ >> !(qi::alpha | '_') // alpha or underscore
+ )
;
escape =
"``" >> (
(qi::raw[+(qi::char_ - "``")] >> "``")
[parse_escaped]
- | qi::raw[*qi::char_] [escape_actions.error]
+ | qi::raw[*qi::char_] [actions.error]
)
;
+ space
+ = qi::raw[+qi::space]
+ >> qi::attr("space");
+
comment
- = qi::lit('#') >> *(qi::char_ - qi::eol) >> -qi::eol;
+ = qi::raw[
+ '#' >> *(qi::char_ - qi::eol) >> -qi::eol
+ ]
+ >> qi::attr("comment")
;
keyword
- = keyword_ >> (qi::eps - (qi::alnum | '_'))
+ = qi::raw[keyword_ >> !(qi::alnum | '_')]
+ >> qi::attr("keyword")
; // make sure we recognize whole words only
keyword_
@@ -227,6 +291,7 @@
special
= +qi::char_("~!%^&*()+={[}]:;,<.>/|\\-")
+ >> qi::attr("special")
;
string_prefix
@@ -234,7 +299,11 @@
;
string_
- = - string_prefix >> (long_string | short_string)
+ = qi::raw[
+ -string_prefix
+ >> (long_string | short_string)
+ ]
+ >> qi::attr("string")
;
string_char = ('\\' >> qi::char_) | (qi::char_ - '\\');
@@ -250,26 +319,38 @@
;
number
- = (
- qi::no_case["0x"] >> qi::hex
- | '0' >> qi::oct
- | qi::long_double
- )
- >> *qi::no_case[qi::char_("lj")]
+ = qi::raw[
+ (
+ qi::no_case["0x"] >> qi::hex
+ | '0' >> qi::oct
+ | qi::long_double
+ )
+ >> *qi::no_case[qi::char_("lj")]
+ ]
+ >> qi::attr("number")
;
identifier
- = (qi::alpha | '_') >> *(qi::alnum | '_')
+ = qi::raw[(qi::alpha | '_') >> *(qi::alnum | '_')]
+ >> qi::attr("identifier")
+ ;
+
+ // TODO: warn user?
+ unexpected
+ = qi::raw[qi::char_]
+ >> qi::attr("error")
;
}
qi::rule<iterator>
- program, macro, comment, special, string_, string_prefix,
- short_string, long_string, number, identifier, keyword,
+ program, string_prefix, short_string, long_string,
escape, string_char;
+ qi::rule<iterator, quickbook::macro()> macro;
+ qi::rule<iterator, code_token()>
+ space, comment, special, string_,
+ number, identifier, keyword, unexpected;
- Out& out;
- quickbook::actions& escape_actions;
+ quickbook::actions& actions;
qi::symbols<> keyword_;
parse_escaped_impl parse_escaped;
@@ -277,43 +358,39 @@
};
// Grammar for plain text (no actual highlighting)
- template <
- typename CharProcess
- , typename Out>
- struct teletype_highlight
- : public qi::grammar<iterator>
+ struct teletype_highlight : public qi::grammar<iterator>
{
- teletype_highlight(Out& out, quickbook::actions& escape_actions)
- : teletype_highlight::base_type(program), out(out), escape_actions(escape_actions)
- , parse_escaped(escape_actions)
+ teletype_highlight(quickbook::actions& actions)
+ : teletype_highlight::base_type(program), actions(actions)
+ , parse_escaped(actions)
{
program
=
- *( macro
+ *( macro [actions.process]
| escape
- | qi::char_ [CharProcess(out)]
+ | qi::char_ [actions.plain_char]
)
;
macro =
- ( escape_actions.macro // must not be followed by
- >> &(qi::eps - (qi::alpha | '_')) // alpha or underscore
- ) [escape_actions.process]
+ ( actions.macro // must not be followed by
+ >> !(qi::alpha | '_') // alpha or underscore
+ )
;
escape =
"``" >> (
(qi::raw[+(qi::char_ - "``")] >> "``")
- [parse_escaped]
- | qi::raw[*qi::char_] [escape_actions.error]
+ [parse_escaped]
+ | qi::raw[*qi::char_] [actions.error]
)
;
}
- qi::rule<iterator> program, macro, escape;
+ qi::rule<iterator> program, escape;
+ qi::rule<iterator, quickbook::macro()> macro;
- Out& out;
- quickbook::actions& escape_actions;
+ quickbook::actions& actions;
parse_escaped_impl parse_escaped;
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