1 // jsp.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_JSP_PARSER_H
5 #define OHCOUNT_JSP_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *JSP_LANG = LANG_HTML;
12 // the languages entities
13 const char *jsp_entities[] = {
14 "space", "comment", "doctype",
15 "tag", "entity", "any"
18 // constants associated with the entities
20 JSP_SPACE = 0, JSP_COMMENT, JSP_DOCTYPE,
21 JSP_TAG, JSP_ENTITY, JSP_ANY
24 /*****************************************************************************/
27 #include "javascript.h"
33 include common "common.rl";
38 # Line counting machine
40 action jsp_ccallback {
49 emb_internal_newline(JSP_LANG)
54 case CHECK_BLANK_ENTRY:
55 check_blank_entry(JSP_LANG)
60 newline %{ entity = INTERNAL_NL; } %jsp_ccallback
64 ^(space | [\-<]) @comment
66 '<' '%' @{ saw(JAVA_LANG); fcall jsp_java_line; }
69 )* :>> '-->' @comment @{ fgoto jsp_line; };
72 newline %{ entity = INTERNAL_NL; } %jsp_ccallback
76 [^\r\n\f\t '\\<] @code
80 '<' '%' @{ saw(JAVA_LANG); fcall jsp_java_line; }
83 )* '\'' @{ fgoto jsp_line; };
85 newline %{ entity = INTERNAL_NL; } %jsp_ccallback
89 [^\r\n\f\t "\\<] @code
93 '<' '%' @{ saw(JAVA_LANG); fcall jsp_java_line; }
96 )* '"' @{ fgoto jsp_line; };
98 ws_or_inl = (ws | newline @{ entity = INTERNAL_NL; } %jsp_ccallback);
100 jsp_css_entry = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>' @code;
101 jsp_css_outry = '</' /style/i ws_or_inl* '>' @check_blank_outry @code;
103 jsp_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 jsp_js_entry = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>' @code;
113 jsp_js_outry = '</' /script/i ws_or_inl* '>' @check_blank_outry @code;
115 jsp_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 jsp_java_entry = '<%' @code;
125 jsp_java_outry = '%>' @check_blank_outry @code;
127 jsp_java_outry @{ p = ts; fret; };
128 # unmodified JAVA patterns
129 spaces ${ entity = JAVA_SPACE; } => java_ccallback;
132 newline ${ entity = NEWLINE; } => java_ccallback;
133 ^space ${ entity = JAVA_ANY; } => java_ccallback;
137 jsp_css_entry @{ entity = CHECK_BLANK_ENTRY; } @jsp_ccallback
138 @{ saw(CSS_LANG); } => { fcall jsp_css_line; };
139 jsp_js_entry @{ entity = CHECK_BLANK_ENTRY; } @jsp_ccallback
140 @{ saw(JS_LANG); } => { fcall jsp_js_line; };
141 jsp_java_entry @{ entity = CHECK_BLANK_ENTRY; } @jsp_ccallback
142 @{ saw(JAVA_LANG); } => { fcall jsp_java_line; };
143 # standard JSP patterns
144 spaces ${ entity = JSP_SPACE; } => jsp_ccallback;
145 '<!--' @comment => { fgoto jsp_comment; };
146 '\'' @code => { fgoto jsp_sq_str; };
147 '"' @code => { fgoto jsp_dq_str; };
148 newline ${ entity = NEWLINE; } => jsp_ccallback;
149 ^space ${ entity = JSP_ANY; } => jsp_ccallback;
154 action jsp_ecallback {
155 callback(JSP_LANG, jsp_entities[entity], cint(ts), cint(te), userdata);
158 jsp_css_entry_entity = '<' /style/i [^>]+ :>> 'text/css' [^>]+ '>';
159 jsp_css_outry_entity = '</' /style/i ws_or_inl* '>';
161 jsp_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 jsp_js_entry_entity = '<' /script/i [^>]+ :>> 'text/javascript' [^>]+ '>';
170 jsp_js_outry_entity = '</' /script/i ws_or_inl* '>';
172 jsp_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 jsp_java_entry_entity = '<%';
181 jsp_java_outry_entity = '%>';
182 jsp_java_entity := |*
183 jsp_java_outry_entity @{ fret; };
184 # unmodified Java patterns
185 space+ ${ entity = JAVA_SPACE; } => java_ecallback;
186 java_comment_entity ${ entity = JAVA_COMMENT; } => java_ecallback;
191 jsp_comment_entity = '<!--' any* :>> '-->';
194 # TODO: jsp_ecallback for jsp_*_{entry,outry}_entity
195 jsp_css_entry_entity => { fcall jsp_css_entity; };
196 jsp_js_entry_entity => { fcall jsp_js_entity; };
197 jsp_java_entry_entity => { fcall jsp_java_entity; };
198 # standard JSP patterns
199 space+ ${ entity = JSP_SPACE; } => jsp_ecallback;
200 jsp_comment_entity ${ entity = JSP_COMMENT; } => jsp_ecallback;
206 /************************* Required for every parser *************************/
208 /* Parses a string buffer with JSP code.
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_jsp(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) ? jsp_en_jsp_line : jsp_en_jsp_entity;
232 // if no newline at EOF; callback contents of last line
233 if (count) { process_last_line(JSP_LANG) }
238 /*****************************************************************************/