Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / visual_basic.rl
1 // visual_basic.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_VISUAL_BASIC_PARSER_H
5 #define OHCOUNT_VISUAL_BASIC_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *VB_LANG = LANG_VISUALBASIC;
11
12 // the languages entities
13 const char *vb_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   VB_SPACE = 0, VB_COMMENT, VB_STRING, VB_ANY,
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine visual_basic;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action vb_ccallback {
32     switch(entity) {
33     case VB_SPACE:
34       ls
35       break;
36     case VB_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(VB_LANG)
41       break;
42     case NEWLINE:
43       std_newline(VB_LANG)
44     }
45   }
46
47   vb_comment = ('\'' | /rem/i) @comment nonnewline*;
48
49   vb_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
50
51   vb_line := |*
52     spaces      ${ entity = VB_SPACE; } => vb_ccallback;
53     vb_comment;
54     vb_string;
55     newline     ${ entity = NEWLINE;  } => vb_ccallback;
56     ^space      ${ entity = VB_ANY;   } => vb_ccallback;
57   *|;
58
59   # Entity machine
60
61   action vb_ecallback {
62     callback(VB_LANG, vb_entities[entity], cint(ts), cint(te), userdata);
63   }
64
65   vb_comment_entity = ('\'' | /rem/i) nonnewline*;
66
67   vb_entity := |*
68     space+            ${ entity = VB_SPACE;   } => vb_ecallback;
69     vb_comment_entity ${ entity = VB_COMMENT; } => vb_ecallback;
70     # TODO:
71     ^space;
72   *|;
73 }%%
74
75 /************************* Required for every parser *************************/
76
77 /* Parses a string buffer with Visual Basic code.
78  *
79  * @param *buffer The string to parse.
80  * @param length The length of the string to parse.
81  * @param count Integer flag specifying whether or not to count lines. If yes,
82  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
83  *   machine optimized for returning entity positions.
84  * @param *callback Callback function. If count is set, callback is called for
85  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
86  *   'lblank' respectively. Otherwise callback is called for each entity found.
87  */
88 void parse_visual_basic(char *buffer, int length, int count,
89                         void (*callback) (const char *lang, const char *entity,
90                                           int s, int e, void *udata),
91                         void *userdata
92   ) {
93   init
94
95   %% write init;
96   cs = (count) ? visual_basic_en_vb_line : visual_basic_en_vb_entity;
97   %% write exec;
98
99   // if no newline at EOF; callback contents of last line
100   if (count) { process_last_line(VB_LANG) }
101 }
102
103 #endif
104
105 /*****************************************************************************/