Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / factor.rl
1 // factor.rl written by Alfredo Beaumont <alfredo.beaumont@gmail.com>
2 // Based on lisp.rl
3
4 /************************* Required for every parser *************************/
5 #ifndef OHCOUNT_FACTOR_PARSER_H
6 #define OHCOUNT_FACTOR_PARSER_H
7
8 #include "../parser_macros.h"
9
10 // the name of the language
11 const char *FACTOR_LANG = LANG_FACTOR;
12
13 // the languages entities
14 const char *factor_entities[] = {
15   "space", "comment", "string", "any"
16 };
17
18 // constants associated with the entities
19 enum {
20   FACTOR_SPACE = 0, FACTOR_COMMENT, FACTOR_STRING, FACTOR_ANY
21 };
22
23 /*****************************************************************************/
24
25 %%{
26   machine factor;
27   write data;
28   include common "common.rl";
29
30   # Line counting machine
31
32   action factor_ccallback {
33     switch(entity) {
34     case FACTOR_SPACE:
35       ls
36       break;
37     case FACTOR_ANY:
38       code
39       break;
40     case INTERNAL_NL:
41       std_internal_newline(FACTOR_LANG)
42       break;
43     case NEWLINE:
44       std_newline(FACTOR_LANG)
45     }
46   }
47
48   factor_comment = '!' @comment nonnewline*;
49
50   factor_string =
51     '"' @code (
52       newline %{ entity = INTERNAL_NL; } %factor_ccallback
53       |
54       ws
55       |
56       [^\r\n\f\t "\\] @code
57       |
58       '\\' nonnewline @code
59     )* '"';
60
61   factor_line := |*
62     spaces        ${ entity = FACTOR_SPACE; } => factor_ccallback;
63     factor_comment;
64     factor_string;
65     newline       ${ entity = NEWLINE;    } => factor_ccallback;
66     ^space        ${ entity = FACTOR_ANY;   } => factor_ccallback;
67   *|;
68
69   # Entity machine
70
71   action factor_ecallback {
72     callback(FACTOR_LANG, factor_entities[entity], cint(ts), cint(te),
73              userdata);
74   }
75
76   factor_entity := 'TODO:';
77 }%%
78
79 /* Parses a string buffer with Factor code.
80  *
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.
89  */
90 void parse_factor(char *buffer, int length, int count,
91                   void (*callback) (const char *lang, const char *entity, int s,
92                                     int e, void *udata),
93                   void *userdata
94   ) {
95   init
96
97   %% write init;
98   cs = (count) ? factor_en_factor_line : factor_en_factor_entity;
99   %% write exec;
100
101   // if no newline at EOF; callback contents of last line
102   if (count) { process_last_line(FACTOR_LANG) }
103 }
104
105 #endif