1 // rhtml.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_RHTML_PARSER_H
5 #define OHCOUNT_RHTML_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *RHTML_LANG = LANG_HTML;
12 // the languages entities
13 const char *rhtml_entities[] = {
14 "space", "comment", "doctype",
15 "tag", "entity", "any"
18 // constants associated with the entities
20 RHTML_SPACE = 0, RHTML_COMMENT, RHTML_DOCTYPE,
21 RHTML_TAG, RHTML_ENTITY, RHTML_ANY
24 /*****************************************************************************/
27 #include "javascript.h"
33 include common "common.rl";
38 # Line counting machine
40 action rhtml_ccallback {
49 emb_internal_newline(RHTML_LANG)
52 emb_newline(RHTML_LANG)
54 case CHECK_BLANK_ENTRY:
55 check_blank_entry(RHTML_LANG)
60 newline %{ entity = INTERNAL_NL; } %rhtml_ccallback
64 ^(space | [\-<]) @comment
66 '<' '%' @{ saw(RUBY_LANG); fcall rhtml_ruby_line; }
69 )* :>> '-->' @comment @{ fgoto rhtml_line; };
72 newline %{ entity = INTERNAL_NL; } %rhtml_ccallback
76 [^\r\n\f\t '\\<] @code
80 '<' '%' @{ saw(RUBY_LANG); fcall rhtml_ruby_line; }
83 )* '\'' @{ fgoto rhtml_line; };
85 newline %{ entity = INTERNAL_NL; } %rhtml_ccallback
89 [^\r\n\f\t "\\<] @code
93 '<' '%' @{ saw(RUBY_LANG); fcall rhtml_ruby_line; }
96 )* '"' @{ fgoto rhtml_line; };
98 ws_or_inl = (ws | newline @{ entity = INTERNAL_NL; } %rhtml_ccallback);
100 rhtml_css_entry = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>' @code;
101 rhtml_css_outry = '</' /style/i ws_or_inl* '>' @check_blank_outry @code;
103 rhtml_css_outry @{ p = ts; fret; };
104 # unmodified CSS patterns
105 spaces ${ entity = CSS_SPACE; } => css_ccallback;
108 newline ${ entity = NEWLINE; } => css_ccallback;
109 ^space ${ entity = CSS_ANY; } => css_ccallback;
112 rhtml_js_entry = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>' @code;
113 rhtml_js_outry = '</' /script/i ws_or_inl* '>' @check_blank_outry @code;
115 rhtml_js_outry @{ p = ts; fret; };
116 # unmodified Javascript patterns
117 spaces ${ entity = JS_SPACE; } => js_ccallback;
120 newline ${ entity = NEWLINE; } => js_ccallback;
121 ^space ${ entity = JS_ANY; } => js_ccallback;
124 rhtml_ruby_entry = '<%' @code;
125 rhtml_ruby_outry = '%>' @check_blank_outry @code;
126 rhtml_ruby_line := |*
127 rhtml_ruby_outry @{ p = ts; fret; };
128 # unmodified Ruby patterns
129 spaces ${ entity = RUBY_SPACE; } => ruby_ccallback;
132 newline ${ entity = NEWLINE; } => ruby_ccallback;
133 ^space ${ entity = RUBY_ANY; } => ruby_ccallback;
137 rhtml_css_entry @{ entity = CHECK_BLANK_ENTRY; } @rhtml_ccallback
138 @{ saw(CSS_LANG); } => { fcall rhtml_css_line; };
139 rhtml_js_entry @{ entity = CHECK_BLANK_ENTRY; } @rhtml_ccallback
140 @{ saw(JS_LANG); } => { fcall rhtml_js_line; };
141 rhtml_ruby_entry @{ entity = CHECK_BLANK_ENTRY; } @rhtml_ccallback
142 @{ saw(RUBY_LANG); } => { fcall rhtml_ruby_line; };
143 # standard RHTML patterns
144 spaces ${ entity = RHTML_SPACE; } => rhtml_ccallback;
145 '<!--' @comment => { fgoto rhtml_comment; };
146 '\'' @code => { fgoto rhtml_sq_str; };
147 '"' @code => { fgoto rhtml_dq_str; };
148 newline ${ entity = NEWLINE; } => rhtml_ccallback;
149 ^space ${ entity = RHTML_ANY; } => rhtml_ccallback;
154 action rhtml_ecallback {
155 callback(RHTML_LANG, rhtml_entities[entity], cint(ts), cint(te), userdata);
158 rhtml_css_entry_entity = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>';
159 rhtml_css_outry_entity = '</' /style/i ws_or_inl* '>';
160 rhtml_css_entity := |*
161 rhtml_css_outry_entity @{ fret; };
162 # unmodified CSS patterns
163 space+ ${ entity = CSS_SPACE; } => css_ecallback;
164 css_comment_entity ${ entity = CSS_COMMENT; } => css_ecallback;
169 rhtml_js_entry_entity = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>';
170 rhtml_js_outry_entity = '</' /script/i ws_or_inl* '>';
171 rhtml_js_entity := |*
172 rhtml_js_outry_entity @{ fret; };
173 # unmodified Javascript patterns
174 space+ ${ entity = JS_SPACE; } => js_ecallback;
175 js_comment_entity ${ entity = JS_COMMENT; } => js_ecallback;
180 rhtml_ruby_entry_entity = '<%';
181 rhtml_ruby_outry_entity = '%>';
182 rhtml_ruby_entity := |*
183 rhtml_ruby_outry_entity @{ fret; };
184 # unmodified Ruby patterns
185 space+ ${ entity = RUBY_SPACE; } => ruby_ecallback;
186 ruby_comment_entity ${ entity = RUBY_COMMENT; } => ruby_ecallback;
191 rhtml_comment_entity = '<!--' any* :>> '-->';
194 # TODO: rhtml_ecallback for rhtml_*_{entry,outry}_entity
195 rhtml_css_entry_entity => { fcall rhtml_css_entity; };
196 rhtml_js_entry_entity => { fcall rhtml_js_entity; };
197 rhtml_ruby_entry_entity => { fcall rhtml_ruby_entity; };
198 # standard RHTML patterns
199 space+ ${ entity = RHTML_SPACE; } => rhtml_ecallback;
200 rhtml_comment_entity ${ entity = RHTML_COMMENT; } => rhtml_ecallback;
206 /************************* Required for every parser *************************/
208 /* Parses a string buffer with RHTML markup.
210 * @param *buffer The string to parse.
211 * @param length The length of the string to parse.
212 * @param count Integer flag specifying whether or not to count lines. If yes,
213 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
214 * machine optimized for returning entity positions.
215 * @param *callback Callback function. If count is set, callback is called for
216 * every line of code, comment, or blank with 'lcode', 'lcomment', and
217 * 'lblank' respectively. Otherwise callback is called for each entity found.
219 void parse_rhtml(char *buffer, int length, int count,
220 void (*callback) (const char *lang, const char *entity, int s,
226 const char *seen = 0;
229 cs = (count) ? rhtml_en_rhtml_line : rhtml_en_rhtml_entity;
232 // if no newline at EOF; callback contents of last line
233 if (count) { process_last_line(RHTML_LANG) }
238 /*****************************************************************************/