1 // java.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #include "ragel_parser_macros.h"
6 // the name of the language
7 const char *JAVA_LANG = "java";
9 // the languages entities
10 const char *java_entities[] = {
11 "space", "comment", "string", "number",
12 "keyword", "identifier", "operator", "any"
15 // constants associated with the entities
17 JAVA_SPACE = 0, JAVA_COMMENT, JAVA_STRING, JAVA_NUMBER,
18 JAVA_KEYWORD, JAVA_IDENTIFIER, JAVA_OPERATOR, JAVA_ANY
21 /*****************************************************************************/
26 include common "common.rl";
28 # Line counting machine
30 action java_ccallback {
39 std_internal_newline(JAVA_LANG)
42 std_newline(JAVA_LANG)
46 java_line_comment = '//' @comment nonnewline*;
49 newline %{ entity = INTERNAL_NL; } %java_ccallback
53 (nonnewline - ws) @comment
55 java_comment = java_line_comment | java_block_comment;
57 java_sq_str = '\'' @code ([^'\\] | '\\' nonnewline)* '\'';
58 java_dq_str = '"' @code ([^"\\] | '\\' nonnewline)* '"';
59 java_string = java_sq_str | java_dq_str;
62 spaces ${ entity = JAVA_SPACE; } => java_ccallback;
65 newline ${ entity = NEWLINE; } => java_ccallback;
66 ^space ${ entity = JAVA_ANY; } => java_ccallback;
71 action java_ecallback {
72 callback(JAVA_LANG, java_entities[entity], cint(ts), cint(te));
75 java_entity := 'TODO:';
78 /* Parses a string buffer with Java code.
80 * @param *buffer The string to parse.
81 * @param length The length of the string to parse.
82 * @param count Integer flag specifying whether or not to count lines. If yes,
83 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
84 * machine optimized for returning entity positions.
85 * @param *callback Callback function. If count is set, callback is called for
86 * every line of code, comment, or blank with 'lcode', 'lcomment', and
87 * 'lblank' respectively. Otherwise callback is called for each entity found.
89 void parse_java(char *buffer, int length, int count,
90 void (*callback) (const char *lang, const char *entity, int start, int end)
96 buffer_start = buffer;
97 whole_line_comment = 0;
98 line_contains_code = 0;
103 cs = (count) ? java_en_java_line : java_en_java_entity;
106 // if no newline at EOF; callback contents of last line
107 if (count) { process_last_line(JAVA_LANG) }