1 // cshtml.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_CSHTML_PARSER
5 #define RAGEL_CSHTML_PARSER
7 #include "ragel_parser_macros.h"
9 // the name of the language
10 const char *CSHTML_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 /*****************************************************************************/
26 #include "css_parser.h"
27 #include "javascript_parser.h"
28 #include "clearsilver_parser.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));
158 cshtml_entity := 'TODO:';
161 /************************* Required for every parser *************************/
163 /* Parses a string buffer with Clearsilver code (in HTML).
165 * @param *buffer The string to parse.
166 * @param length The length of the string to parse.
167 * @param count Integer flag specifying whether or not to count lines. If yes,
168 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
169 * machine optimized for returning entity positions.
170 * @param *callback Callback function. If count is set, callback is called for
171 * every line of code, comment, or blank with 'lcode', 'lcomment', and
172 * 'lblank' respectively. Otherwise callback is called for each entity found.
174 void parse_cshtml(char *buffer, int length, int count,
175 void (*callback) (const char *lang, const char *entity, int start, int end)
179 const char *seen = 0;
182 cs = (count) ? cshtml_en_cshtml_line : cshtml_en_cshtml_entity;
185 // if no newline at EOF; callback contents of last line
186 if (count) { process_last_line(CSHTML_LANG) }
191 /*****************************************************************************/