Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / makefile.rl
1 // makefile.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_MAKEFILE_PARSER_H
5 #define OHCOUNT_MAKEFILE_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *MAKE_LANG = LANG_MAKE;
11
12 // the languages entities
13 const char *make_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   MAKE_SPACE = 0, MAKE_COMMENT, MAKE_STRING, MAKE_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine makefile;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action make_ccallback {
32     switch(entity) {
33     case MAKE_SPACE:
34       ls
35       break;
36     case MAKE_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(MAKE_LANG)
41       break;
42     case NEWLINE:
43       std_newline(MAKE_LANG)
44     }
45   }
46
47   make_comment = '#' @comment nonnewline*;
48
49   make_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
50   make_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
51   make_string = make_sq_str | make_dq_str;
52
53   make_line := |*
54     spaces        ${ entity = MAKE_SPACE; } => make_ccallback;
55     make_comment;
56     make_string;
57     newline       ${ entity = NEWLINE;    } => make_ccallback;
58     ^space        ${ entity = MAKE_ANY;   } => make_ccallback;
59   *|;
60
61   # Entity machine
62
63   action make_ecallback {
64     callback(MAKE_LANG, make_entities[entity], cint(ts), cint(te), userdata);
65   }
66
67   make_comment_entity = '#' nonnewline*;
68
69   make_entity := |*
70     space+              ${ entity = MAKE_SPACE;   } => make_ecallback;
71     make_comment_entity ${ entity = MAKE_COMMENT; } => make_ecallback;
72     # TODO:
73     ^space;
74   *|;
75 }%%
76
77 /************************* Required for every parser *************************/
78
79 /* Parses a string buffer with Makefile 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_makefile(char *buffer, int length, int count,
91                     void (*callback) (const char *lang, const char *entity,
92                                       int s, int e, void *udata),
93                     void *userdata
94   ) {
95   init
96
97   %% write init;
98   cs = (count) ? makefile_en_make_line : makefile_en_make_entity;
99   %% write exec;
100
101   // if no newline at EOF; callback contents of last line
102   if (count) { process_last_line(MAKE_LANG) }
103 }
104
105 #endif
106
107 /*****************************************************************************/