1 // java.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_JAVA_PARSER_H
5 #define OHCOUNT_JAVA_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *JAVA_LANG = LANG_JAVA;
12 // the languages entities
13 const char *java_entities[] = {
14 "space", "comment", "string", "number",
15 "keyword", "identifier", "operator", "any"
18 // constants associated with the entities
20 JAVA_SPACE = 0, JAVA_COMMENT, JAVA_STRING, JAVA_NUMBER,
21 JAVA_KEYWORD, JAVA_IDENTIFIER, JAVA_OPERATOR, JAVA_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
33 action java_ccallback {
42 std_internal_newline(JAVA_LANG)
45 std_newline(JAVA_LANG)
49 java_line_comment = '//' @comment nonnewline*;
52 newline %{ entity = INTERNAL_NL; } %java_ccallback
56 (nonnewline - ws) @comment
58 java_comment = java_line_comment | java_block_comment;
60 java_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
61 java_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
62 java_string = java_sq_str | java_dq_str;
65 spaces ${ entity = JAVA_SPACE; } => java_ccallback;
68 newline ${ entity = NEWLINE; } => java_ccallback;
69 ^space ${ entity = JAVA_ANY; } => java_ccallback;
74 action java_ecallback {
75 callback(JAVA_LANG, java_entities[entity], cint(ts), cint(te), userdata);
78 java_line_comment_entity = '//' nonnewline*;
79 java_block_comment_entity = '/*' any* :>> '*/';
80 java_comment_entity = java_line_comment_entity | java_block_comment_entity;
83 space+ ${ entity = JAVA_SPACE; } => java_ecallback;
84 java_comment_entity ${ entity = JAVA_COMMENT; } => java_ecallback;
90 /************************* Required for every parser *************************/
92 /* Parses a string buffer with Java code.
94 * @param *buffer The string to parse.
95 * @param length The length of the string to parse.
96 * @param count Integer flag specifying whether or not to count lines. If yes,
97 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
98 * machine optimized for returning entity positions.
99 * @param *callback Callback function. If count is set, callback is called for
100 * every line of code, comment, or blank with 'lcode', 'lcomment', and
101 * 'lblank' respectively. Otherwise callback is called for each entity found.
103 void parse_java(char *buffer, int length, int count,
104 void (*callback) (const char *lang, const char *entity, int s,
111 cs = (count) ? java_en_java_line : java_en_java_entity;
114 // if no newline at EOF; callback contents of last line
115 if (count) { process_last_line(JAVA_LANG) }
120 /*****************************************************************************/