1 | /*============================================================================= |
---|
2 | Copyright (c) 2002 2004 Joel de Guzman |
---|
3 | Copyright (c) 2004 Eric Niebler |
---|
4 | http://spirit.sourceforge.net/ |
---|
5 | |
---|
6 | Use, modification and distribution is subject to the Boost Software |
---|
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | =============================================================================*/ |
---|
10 | #if !defined(BOOST_SPIRIT_QUICKBOOK_SYNTAX_HIGHLIGHT_HPP) |
---|
11 | #define BOOST_SPIRIT_QUICKBOOK_SYNTAX_HIGHLIGHT_HPP |
---|
12 | |
---|
13 | #include <boost/spirit/core.hpp> |
---|
14 | #include <boost/spirit/utility/confix.hpp> |
---|
15 | #include <boost/spirit/utility/chset.hpp> |
---|
16 | #include <boost/spirit/symbols/symbols.hpp> |
---|
17 | #include <boost/spirit/utility/escape_char.hpp> |
---|
18 | |
---|
19 | namespace quickbook |
---|
20 | { |
---|
21 | using namespace boost::spirit; |
---|
22 | |
---|
23 | // Grammar for C++ highlighting |
---|
24 | template < |
---|
25 | typename Process |
---|
26 | , typename Space |
---|
27 | , typename Macro |
---|
28 | , typename DoMacro |
---|
29 | , typename Unexpected |
---|
30 | , typename Out> |
---|
31 | struct cpp_highlight |
---|
32 | : public grammar<cpp_highlight<Process, Space, Macro, DoMacro, Unexpected, Out> > |
---|
33 | { |
---|
34 | cpp_highlight(Out& out, Macro const& macro, DoMacro do_macro) |
---|
35 | : out(out), macro(macro), do_macro(do_macro) {} |
---|
36 | |
---|
37 | template <typename Scanner> |
---|
38 | struct definition |
---|
39 | { |
---|
40 | definition(cpp_highlight const& self) |
---|
41 | { |
---|
42 | program |
---|
43 | = |
---|
44 | *( macro |
---|
45 | | preprocessor [Process("preprocessor", self.out)] |
---|
46 | | comment [Process("comment", self.out)] |
---|
47 | | keyword [Process("keyword", self.out)] |
---|
48 | | identifier [Process("identifier", self.out)] |
---|
49 | | special [Process("special", self.out)] |
---|
50 | | string_ [Process("string", self.out)] |
---|
51 | | char_ [Process("char", self.out)] |
---|
52 | | number [Process("number", self.out)] |
---|
53 | | space_p [Space(self.out)] |
---|
54 | | anychar_p [Unexpected(self.out)] |
---|
55 | ) |
---|
56 | ; |
---|
57 | |
---|
58 | macro |
---|
59 | = eps_p(*space_p >> self.macro) |
---|
60 | >> (*space_p) [Space(self.out)] |
---|
61 | >> self.macro [self.do_macro] |
---|
62 | ; |
---|
63 | |
---|
64 | preprocessor |
---|
65 | = *space_p >> '#' >> ((alpha_p | '_') >> *(alnum_p | '_')) |
---|
66 | ; |
---|
67 | |
---|
68 | comment |
---|
69 | = +(*space_p >> (comment_p("//") | comment_p("/*", "*/"))) |
---|
70 | ; |
---|
71 | |
---|
72 | keyword |
---|
73 | = *space_p >> keyword_ >> (eps_p - (alnum_p | '_')) |
---|
74 | ; // make sure we recognize whole words only |
---|
75 | |
---|
76 | keyword_ |
---|
77 | = "and_eq", "and", "asm", "auto", "bitand", "bitor", |
---|
78 | "bool", "break", "case", "catch", "char", "class", |
---|
79 | "compl", "const_cast", "const", "continue", "default", |
---|
80 | "delete", "do", "double", "dynamic_cast", "else", |
---|
81 | "enum", "explicit", "export", "extern", "false", |
---|
82 | "float", "for", "friend", "goto", "if", "inline", |
---|
83 | "int", "long", "mutable", "namespace", "new", "not_eq", |
---|
84 | "not", "operator", "or_eq", "or", "private", |
---|
85 | "protected", "public", "register", "reinterpret_cast", |
---|
86 | "return", "short", "signed", "sizeof", "static", |
---|
87 | "static_cast", "struct", "switch", "template", "this", |
---|
88 | "throw", "true", "try", "typedef", "typeid", |
---|
89 | "typename", "union", "unsigned", "using", "virtual", |
---|
90 | "void", "volatile", "wchar_t", "while", "xor_eq", "xor" |
---|
91 | ; |
---|
92 | |
---|
93 | special |
---|
94 | = *space_p >> +chset_p("~!%^&*()+={[}]:;,<.>?/|\\-") |
---|
95 | ; |
---|
96 | |
---|
97 | string_ |
---|
98 | = *space_p >> !as_lower_d['l'] >> confix_p('"', *c_escape_ch_p, '"') |
---|
99 | ; |
---|
100 | |
---|
101 | char_ |
---|
102 | = *space_p >> !as_lower_d['l'] >> confix_p('\'', *c_escape_ch_p, '\'') |
---|
103 | ; |
---|
104 | |
---|
105 | number |
---|
106 | = *space_p >> |
---|
107 | ( |
---|
108 | as_lower_d["0x"] >> hex_p |
---|
109 | | '0' >> oct_p |
---|
110 | | real_p |
---|
111 | ) |
---|
112 | >> *as_lower_d[chset_p("ldfu")] |
---|
113 | ; |
---|
114 | |
---|
115 | identifier |
---|
116 | = *space_p >> ((alpha_p | '_') >> *(alnum_p | '_')) |
---|
117 | ; |
---|
118 | } |
---|
119 | |
---|
120 | rule<Scanner> program, macro, preprocessor, comment, special, |
---|
121 | string_, char_, number, identifier, keyword; |
---|
122 | symbols<> keyword_; |
---|
123 | |
---|
124 | rule<Scanner> const& |
---|
125 | start() const { return program; } |
---|
126 | }; |
---|
127 | |
---|
128 | Out& out; |
---|
129 | Macro const& macro; |
---|
130 | DoMacro do_macro; |
---|
131 | }; |
---|
132 | |
---|
133 | // Grammar for Python highlighting |
---|
134 | // See also: The Python Reference Manual |
---|
135 | // http://docs.python.org/ref/ref.html |
---|
136 | template < |
---|
137 | typename Process |
---|
138 | , typename Space |
---|
139 | , typename Macro |
---|
140 | , typename DoMacro |
---|
141 | , typename Unexpected |
---|
142 | , typename Out> |
---|
143 | struct python_highlight |
---|
144 | : public grammar<python_highlight<Process, Space, Macro, DoMacro, Unexpected, Out> > |
---|
145 | { |
---|
146 | python_highlight(Out& out, Macro const& macro, DoMacro do_macro) |
---|
147 | : out(out), macro(macro), do_macro(do_macro) {} |
---|
148 | |
---|
149 | template <typename Scanner> |
---|
150 | struct definition |
---|
151 | { |
---|
152 | definition(python_highlight const& self) |
---|
153 | { |
---|
154 | program |
---|
155 | = |
---|
156 | *( macro |
---|
157 | | comment [Process("comment", self.out)] |
---|
158 | | keyword [Process("keyword", self.out)] |
---|
159 | | identifier [Process("identifier", self.out)] |
---|
160 | | special [Process("special", self.out)] |
---|
161 | | string_ [Process("string", self.out)] |
---|
162 | | number [Process("number", self.out)] |
---|
163 | | space_p |
---|
164 | | anychar_p [Unexpected(self.out)] |
---|
165 | ) |
---|
166 | ; |
---|
167 | |
---|
168 | macro |
---|
169 | = eps_p(*space_p >> self.macro) |
---|
170 | >> (*space_p) [Space(self.out)] |
---|
171 | >> self.macro [self.do_macro] |
---|
172 | ; |
---|
173 | |
---|
174 | comment |
---|
175 | = +(*space_p >> comment_p("#")) |
---|
176 | ; |
---|
177 | |
---|
178 | keyword |
---|
179 | = *space_p >> keyword_ >> (eps_p - (alnum_p | '_')) |
---|
180 | ; // make sure we recognize whole words only |
---|
181 | |
---|
182 | keyword_ |
---|
183 | = |
---|
184 | "and", "del", "for", "is", "raise", |
---|
185 | "assert", "elif", "from", "lambda", "return", |
---|
186 | "break", "else", "global", "not", "try", |
---|
187 | "class", "except", "if", "or", "while", |
---|
188 | "continue", "exec", "import", "pass", "yield", |
---|
189 | "def", "finally", "in", "print", |
---|
190 | |
---|
191 | // Technically "as" and "None" are not yet keywords (at Python |
---|
192 | // 2.4). They are destined to become keywords, and we treat them |
---|
193 | // as such for syntax highlighting purposes. |
---|
194 | |
---|
195 | "as", "None" |
---|
196 | ; |
---|
197 | |
---|
198 | special |
---|
199 | = *space_p >> +chset_p("~!%^&*()+={[}]:;,<.>/|\\-") |
---|
200 | ; |
---|
201 | |
---|
202 | string_prefix |
---|
203 | = as_lower_d[str_p("u") >> ! str_p("r")] |
---|
204 | ; |
---|
205 | |
---|
206 | string_ |
---|
207 | = *space_p >> ! string_prefix >> (long_string | short_string) |
---|
208 | ; |
---|
209 | |
---|
210 | short_string |
---|
211 | = confix_p('\'', * c_escape_ch_p, '\'') | |
---|
212 | confix_p('"', * c_escape_ch_p, '"') |
---|
213 | ; |
---|
214 | |
---|
215 | long_string |
---|
216 | // Note: the "str_p" on the next two lines work around |
---|
217 | // an INTERNAL COMPILER ERROR when using VC7.1 |
---|
218 | = confix_p(str_p("'''"), * lex_escape_ch_p, "'''") | |
---|
219 | confix_p(str_p("\"\"\""), * lex_escape_ch_p, "\"\"\"") |
---|
220 | ; |
---|
221 | |
---|
222 | number |
---|
223 | = *space_p >> |
---|
224 | ( |
---|
225 | as_lower_d["0x"] >> hex_p |
---|
226 | | '0' >> oct_p |
---|
227 | | real_p |
---|
228 | ) |
---|
229 | >> *as_lower_d[chset_p("lj")] |
---|
230 | ; |
---|
231 | |
---|
232 | identifier |
---|
233 | = *space_p >> ((alpha_p | '_') >> *(alnum_p | '_')) |
---|
234 | ; |
---|
235 | } |
---|
236 | |
---|
237 | rule<Scanner> program, macro, comment, special, |
---|
238 | string_, string_prefix, short_string, long_string, |
---|
239 | number, identifier, keyword; |
---|
240 | symbols<> keyword_; |
---|
241 | |
---|
242 | rule<Scanner> const& |
---|
243 | start() const { return program; } |
---|
244 | }; |
---|
245 | |
---|
246 | Out& out; |
---|
247 | Macro const& macro; |
---|
248 | DoMacro do_macro; |
---|
249 | }; |
---|
250 | } |
---|
251 | |
---|
252 | #endif // BOOST_SPIRIT_QUICKBOOK_SYNTAX_HIGHLIGHT_HPP |
---|