1 // cshtml.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_CSHTML_PARSER_H
5 #define OHCOUNT_CSHTML_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *CSHTML_LANG = LANG_HTML;
12 // the languages entities
13 const char *cshtml_entities[] = {
14 "space", "comment", "doctype",
15 "tag", "entity", "any"
18 // constants associated with the entities
20 CSHTML_SPACE = 0, CSHTML_COMMENT, CSHTML_DOCTYPE,
21 CSHTML_TAG, CSHTML_ENTITY, CSHTML_ANY
24 /*****************************************************************************/
27 #include "javascript.h"
28 #include "clearsilver.h"
33 include common "common.rl";
38 # Line counting machine
40 action cshtml_ccallback {
49 emb_internal_newline(CSHTML_LANG)
52 emb_newline(CSHTML_LANG)
54 case CHECK_BLANK_ENTRY:
55 check_blank_entry(CSHTML_LANG)
60 newline %{ entity = INTERNAL_NL; } %cshtml_ccallback
64 ^(space | [\-<]) @comment
66 '<' '?cs' @{ saw(CS_LANG); fcall cshtml_cs_line; }
69 )* :>> '-->' @comment @{ fgoto cshtml_line; };
72 newline %{ entity = INTERNAL_NL; } %cshtml_ccallback
76 [^\r\n\f\t '\\<] @code
80 '<' '?cs' @{ saw(CS_LANG); fcall cshtml_cs_line; }
83 )* '\'' @{ fgoto cshtml_line; };
85 newline %{ entity = INTERNAL_NL; } %cshtml_ccallback
89 [^\r\n\f\t "\\<] @code
93 '<' '?cs' @{ saw(CS_LANG); fcall cshtml_cs_line; }
96 )* '"' @{ fgoto cshtml_line; };
98 ws_or_inl = (ws | newline @{ entity = INTERNAL_NL; } %cshtml_ccallback);
100 cshtml_css_entry = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>' @code;
101 cshtml_css_outry = '</' /style/i ws_or_inl* '>' @check_blank_outry @code;
102 cshtml_css_line := |*
103 cshtml_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 cshtml_js_entry = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>' @code;
113 cshtml_js_outry = '</' /script/i ws_or_inl* '>' @check_blank_outry @code;
115 cshtml_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 cshtml_cs_entry = '<?cs' @code;
125 cshtml_cs_outry = '?>' @check_blank_outry @code;
127 cshtml_cs_outry @{ p = ts; fret; };
128 # unmodified Clearsilver patterns
129 spaces ${ entity = CS_SPACE; } => cs_ccallback;
132 newline ${ entity = NEWLINE; } => cs_ccallback;
133 ^space ${ entity = CS_ANY; } => cs_ccallback;
137 cshtml_css_entry @{ entity = CHECK_BLANK_ENTRY; } @cshtml_ccallback
138 @{ saw(CSS_LANG); } => { fcall cshtml_css_line; };
139 cshtml_js_entry @{ entity = CHECK_BLANK_ENTRY; } @cshtml_ccallback
140 @{ saw(JS_LANG); } => { fcall cshtml_js_line; };
141 cshtml_cs_entry @{ entity = CHECK_BLANK_ENTRY; } @cshtml_ccallback
142 @{ saw(CS_LANG); } => { fcall cshtml_cs_line; };
143 # standard CSHTML patterns
144 spaces ${ entity = CSHTML_SPACE; } => cshtml_ccallback;
145 '<!--' @comment => { fgoto cshtml_comment; };
146 '\'' @code => { fgoto cshtml_sq_str; };
147 '"' @code => { fgoto cshtml_dq_str; };
148 newline ${ entity = NEWLINE; } => cshtml_ccallback;
149 ^space ${ entity = CSHTML_ANY; } => cshtml_ccallback;
154 action cshtml_ecallback {
155 callback(CSHTML_LANG, cshtml_entities[entity], cint(ts), cint(te),
159 cshtml_css_entry_entity = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>';
160 cshtml_css_outry_entity = '</' /style/i ws_or_inl* '>';
161 cshtml_css_entity := |*
162 cshtml_css_outry_entity @{ fret; };
163 # unmodified CSS patterns
164 space+ ${ entity = CSS_SPACE; } => css_ecallback;
165 css_comment_entity ${ entity = CSS_COMMENT; } => css_ecallback;
170 cshtml_js_entry_entity = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>';
171 cshtml_js_outry_entity = '</' /script/i ws_or_inl* '>';
172 cshtml_js_entity := |*
173 cshtml_js_outry_entity @{ fret; };
174 # unmodified Javascript patterns
175 space+ ${ entity = JS_SPACE; } => js_ecallback;
176 js_comment_entity ${ entity = JS_COMMENT; } => js_ecallback;
181 cshtml_cs_entry_entity = '<?cs';
182 cshtml_cs_outry_entity = '?>';
183 cshtml_cs_entity := |*
184 cshtml_cs_outry_entity @{ fret; };
185 # unmodified CS patterns
186 space+ ${ entity = CS_SPACE; } => cs_ecallback;
187 cs_comment_entity ${ entity = CS_COMMENT; } => cs_ecallback;
192 cshtml_comment_entity = '<!--' any* :>> '-->';
195 # TODO: cshtml_ecallback for cshtml_*_{entry,outry}_entity
196 cshtml_css_entry_entity => { fcall cshtml_css_entity; };
197 cshtml_js_entry_entity => { fcall cshtml_js_entity; };
198 cshtml_cs_entry_entity => { fcall cshtml_cs_entity; };
199 # standard CSHTML patterns
200 space+ ${ entity = CSHTML_SPACE; } => cshtml_ecallback;
201 cshtml_comment_entity ${ entity = CSHTML_COMMENT; } => cshtml_ecallback;
207 /************************* Required for every parser *************************/
209 /* Parses a string buffer with Clearsilver code (in HTML).
211 * @param *buffer The string to parse.
212 * @param length The length of the string to parse.
213 * @param count Integer flag specifying whether or not to count lines. If yes,
214 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
215 * machine optimized for returning entity positions.
216 * @param *callback Callback function. If count is set, callback is called for
217 * every line of code, comment, or blank with 'lcode', 'lcomment', and
218 * 'lblank' respectively. Otherwise callback is called for each entity found.
220 void parse_cshtml(char *buffer, int length, int count,
221 void (*callback) (const char *lang, const char *entity, int s,
227 const char *seen = 0;
230 cs = (count) ? cshtml_en_cshtml_line : cshtml_en_cshtml_entity;
233 // if no newline at EOF; callback contents of last line
234 if (count) { process_last_line(CSHTML_LANG) }
239 /*****************************************************************************/