1 // erlang.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_ERLANG_PARSER_H
5 #define OHCOUNT_ERLANG_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *ERLANG_LANG = LANG_ERLANG;
12 // the languages entities
13 const char *erlang_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 ERLANG_SPACE = 0, ERLANG_COMMENT, ERLANG_STRING, ERLANG_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action erlang_ccallback {
40 std_internal_newline(ERLANG_LANG)
43 std_newline(ERLANG_LANG)
47 erlang_comment = '%%' @comment nonnewline*;
49 erlang_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
50 erlang_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
51 erlang_string = erlang_sq_str | erlang_dq_str;
54 spaces ${ entity = ERLANG_SPACE; } => erlang_ccallback;
57 newline ${ entity = NEWLINE; } => erlang_ccallback;
58 ^space ${ entity = ERLANG_ANY; } => erlang_ccallback;
63 action erlang_ecallback {
64 callback(ERLANG_LANG, erlang_entities[entity], cint(ts), cint(te),
68 erlang_comment_entity = '%' nonnewline*;
71 space+ ${ entity = ERLANG_SPACE; } => erlang_ecallback;
72 erlang_comment_entity ${ entity = ERLANG_COMMENT; } => erlang_ecallback;
78 /************************* Required for every parser *************************/
80 /* Parses a string buffer with Erlang code.
82 * @param *buffer The string to parse.
83 * @param length The length of the string to parse.
84 * @param count Integer flag specifying whether or not to count lines. If yes,
85 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
86 * machine optimized for returning entity positions.
87 * @param *callback Callback function. If count is set, callback is called for
88 * every line of code, comment, or blank with 'lcode', 'lcomment', and
89 * 'lblank' respectively. Otherwise callback is called for each entity found.
91 void parse_erlang(char *buffer, int length, int count,
92 void (*callback) (const char *lang, const char *entity, int s,
99 cs = (count) ? erlang_en_erlang_line : erlang_en_erlang_entity;
102 // if no newline at EOF; callback contents of last line
103 if (count) { process_last_line(ERLANG_LANG) }
108 /*****************************************************************************/