1 // Javascript.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_JAVASCRIPT_PARSER
5 #define RAGEL_JAVASCRIPT_PARSER
7 #include "ragel_parser_macros.h"
9 // the name of the language
10 const char *JS_LANG = "javascript";
12 // the languages entities
13 const char *js_entities[] = {
14 "space", "comment", "string", "number", "keyword",
15 "identifier", "operator", "any"
18 // constants associated with the entities
20 JS_SPACE = 0, JS_COMMENT, JS_STRING, JS_NUMBER, JS_KEYWORD,
21 JS_IDENTIFIER, JS_OPERATOR, JS_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
42 std_internal_newline(JS_LANG)
49 js_line_comment = '//' @comment nonnewline*;
52 newline %{ entity = INTERNAL_NL; } %js_ccallback
56 (nonnewline - ws) @comment
58 js_comment = js_line_comment | js_block_comment;
60 # Does Javascript allow newlines inside strings?
61 # I can't find a definitive answer.
64 escaped_newline %{ entity = INTERNAL_NL; } %js_ccallback
74 escaped_newline %{ entity = INTERNAL_NL; } %js_ccallback
82 js_string = js_sq_str | js_dq_str;
85 spaces ${ entity = JS_SPACE; } => js_ccallback;
88 newline ${ entity = NEWLINE; } => js_ccallback;
89 ^space ${ entity = JS_ANY; } => js_ccallback;
95 callback(JS_LANG, entity, cint(ts), cint(te));
101 /* Parses a string buffer with Javascript code.
103 * @param *buffer The string to parse.
104 * @param length The length of the string to parse.
105 * @param count Integer flag specifying whether or not to count lines. If yes,
106 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
107 * machine optimized for returning entity positions.
108 * @param *callback Callback function. If count is set, callback is called for
109 * every line of code, comment, or blank with 'lcode', 'lcomment', and
110 * 'lblank' respectively. Otherwise callback is called for each entity found.
112 void parse_javascript(char *buffer, int length, int count,
113 void (*callback) (const char *lang, const char *entity, int start, int end)
118 cs = (count) ? javascript_en_js_line : javascript_en_js_entity;
121 // if no newline at EOF; callback contents of last line
122 if (count) { process_last_line(JS_LANG) }