1 // factor.rl written by Alfredo Beaumont <alfredo.beaumont@gmail.com>
4 /************************* Required for every parser *************************/
5 #ifndef OHCOUNT_FACTOR_PARSER_H
6 #define OHCOUNT_FACTOR_PARSER_H
8 #include "../parser_macros.h"
10 // the name of the language
11 const char *FACTOR_LANG = LANG_FACTOR;
13 // the languages entities
14 const char *factor_entities[] = {
15 "space", "comment", "string", "any"
18 // constants associated with the entities
20 FACTOR_SPACE = 0, FACTOR_COMMENT, FACTOR_STRING, FACTOR_ANY
23 /*****************************************************************************/
28 include common "common.rl";
30 # Line counting machine
32 action factor_ccallback {
41 std_internal_newline(FACTOR_LANG)
44 std_newline(FACTOR_LANG)
48 factor_comment = '!' @comment nonnewline*;
52 newline %{ entity = INTERNAL_NL; } %factor_ccallback
62 spaces ${ entity = FACTOR_SPACE; } => factor_ccallback;
65 newline ${ entity = NEWLINE; } => factor_ccallback;
66 ^space ${ entity = FACTOR_ANY; } => factor_ccallback;
71 action factor_ecallback {
72 callback(FACTOR_LANG, factor_entities[entity], cint(ts), cint(te),
76 factor_entity := 'TODO:';
79 /* Parses a string buffer with Factor code.
81 * @param *buffer The string to parse.
82 * @param length The length of the string to parse.
83 * @param count Integer flag specifying whether or not to count lines. If yes,
84 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
85 * machine optimized for returning entity positions.
86 * @param *callback Callback function. If count is set, callback is called for
87 * every line of code, comment, or blank with 'lcode', 'lcomment', and
88 * 'lblank' respectively. Otherwise callback is called for each entity found.
90 void parse_factor(char *buffer, int length, int count,
91 void (*callback) (const char *lang, const char *entity, int s,
98 cs = (count) ? factor_en_factor_line : factor_en_factor_entity;
101 // if no newline at EOF; callback contents of last line
102 if (count) { process_last_line(FACTOR_LANG) }