Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / cmake.rl
1 // cmake.rl written by James Webber, bunkerprivate@googlemail.com, based on
2 // shell.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
3 //
4 // From shell, I remove single quote strings and added an @code to the end
5 // of a double quote string in case the terminating double string is on a line
6 // of its own.
7
8 /************************* Required for every parser *************************/
9 #ifndef RAGEL_CMAKE_PARSER
10 #define RAGEL_CMAKE_PARSER
11
12 #include "../parser_macros.h"
13
14 // the name of the language
15 const char *CMAKE_LANG = "cmake";
16
17 // the languages entities
18 const char *cmake_entities[] = {
19   "space", "comment", "string", "any"
20 };
21
22 // constants associated with the entities
23 enum {
24   CMAKE_SPACE = 0, CMAKE_COMMENT, CMAKE_STRING, CMAKE_ANY
25 };
26
27 /*****************************************************************************/
28
29 %%{
30   machine cmake;
31   write data;
32   include common "common.rl";
33
34   # Line counting machine
35
36   action cmake_ccallback {
37     switch(entity) {
38     case CMAKE_SPACE:
39       ls
40       break;
41     case CMAKE_ANY:
42       code
43       break;
44     case INTERNAL_NL:
45       std_internal_newline(CMAKE_LANG)
46       break;
47     case NEWLINE:
48       std_newline(CMAKE_LANG)
49     }
50   }
51
52   action es {
53     printf("endstring\n");
54   }
55
56   cmake_comment = '#' @comment nonnewline*;
57
58   cmake_dq_str =
59     '"' @enqueue @code (
60       newline %{ entity = INTERNAL_NL; } %cmake_ccallback
61       |
62       ws
63       |
64       [^\r\n\f\t "\\] @code
65       |
66       '\\' nonnewline @code
67     )* '"' @code @commit ;
68
69   cmake_string = cmake_dq_str;
70
71   cmake_line := |*
72     spaces         ${ entity = CMAKE_SPACE; } => cmake_ccallback;
73     cmake_comment;
74     cmake_string;
75     newline        ${ entity = NEWLINE;     } => cmake_ccallback;
76     ^space         ${ entity = CMAKE_ANY;   } => cmake_ccallback;
77   *|;
78
79   # Entity machine
80
81   action cmake_ecallback {
82     callback(CMAKE_LANG, cmake_entities[entity], cint(ts), cint(te), userdata);
83   }
84
85   cmake_comment_entity = '#' nonnewline*;
86
87   cmake_entity := |*
88     space+               ${ entity = CMAKE_SPACE;   } => cmake_ecallback;
89     cmake_comment_entity ${ entity = CMAKE_COMMENT; } => cmake_ecallback;
90     # TODO: see shell.rl - (what is the todo for?!)
91     ^space;
92   *|;
93 }%%
94
95 /************************* Required for every parser *************************/
96
97 /* Parses a string buffer with cmake script code.
98  *
99  * @param *buffer The string to parse.
100  * @param length The length of the string to parse.
101  * @param count Integer flag specifying whether or not to count lines. If yes,
102  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
103  *   machine optimized for returning entity positions.
104  * @param *callback Callback function. If count is set, callback is called for
105  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
106  *   'lblank' respectively. Otherwise callback is called for each entity found.
107  */
108 void parse_cmake(char *buffer, int length, int count,
109                  void (*callback) (const char *lang, const char *entity, int s,
110                                    int e, void *udata),
111                  void *userdata
112   ) {
113   init
114
115   %% write init;
116   cs = (count) ? cmake_en_cmake_line : cmake_en_cmake_entity;
117   %% write exec;
118
119   // if no newline at EOF; callback contents of last line
120   if (count) { process_last_line(CMAKE_LANG) }
121 }
122
123 #endif
124
125 /*****************************************************************************/