Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / matlab.rl
1 // matlab.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_MATLAB_PARSER_H
5 #define OHCOUNT_MATLAB_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *MATLAB_LANG = LANG_MATLAB;
11
12 // the languages entities
13 const char *matlab_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   MATLAB_SPACE = 0, MATLAB_COMMENT, MATLAB_STRING, MATLAB_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine matlab;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action matlab_ccallback {
32     switch(entity) {
33     case MATLAB_SPACE:
34       ls
35       break;
36     case MATLAB_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(MATLAB_LANG)
41       break;
42     case NEWLINE:
43       std_newline(MATLAB_LANG)
44     }
45   }
46
47   # Matlab(TM) comments also may begin with the line-continuing sequence ...
48   matlab_line_comment = (('%' | '...') [^{] @{ fhold; }) @comment nonnewline*;
49   matlab_block_comment =
50     '%{' @comment (
51       newline %{ entity = INTERNAL_NL; } %matlab_ccallback
52       |
53       ws
54       |
55       (nonnewline - ws) @code
56     )* :>> '%}';
57   matlab_comment = matlab_line_comment | matlab_block_comment;
58
59   # The detector is not smart enough to detect GNU Octave's double
60   # quotes around strings, so accept it here.
61   matlab_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
62   matlab_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
63   matlab_string = matlab_sq_str | matlab_dq_str;
64
65   matlab_line := |*
66     spaces          ${ entity = MATLAB_SPACE; } => matlab_ccallback;
67     matlab_comment;
68     matlab_string;
69     newline         ${ entity = NEWLINE;      } => matlab_ccallback;
70     ^space          ${ entity = MATLAB_ANY;   } => matlab_ccallback;
71   *|;
72
73   # Entity machine
74
75   action matlab_ecallback {
76     callback(MATLAB_LANG, matlab_entities[entity], cint(ts), cint(te),
77              userdata);
78   }
79
80   matlab_line_comment_entity = (('%' | '...') [^{] @{ fhold; }) nonnewline*;
81   matlab_block_comment_entity = '%{' any* :>> '%}';
82   matlab_comment_entity =
83     matlab_line_comment_entity | matlab_block_comment_entity;
84
85   matlab_entity := |*
86     space+                ${ entity = MATLAB_SPACE;   } => matlab_ecallback;
87     matlab_comment_entity ${ entity = MATLAB_COMMENT; } => matlab_ecallback;
88     # TODO:
89     ^space;
90   *|;
91 }%%
92
93 /************************* Required for every parser *************************/
94
95 /* Parses a string buffer with MATLAB code.
96  *
97  * @param *buffer The string to parse.
98  * @param length The length of the string to parse.
99  * @param count Integer flag specifying whether or not to count lines. If yes,
100  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
101  *   machine optimized for returning entity positions.
102  * @param *callback Callback function. If count is set, callback is called for
103  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
104  *   'lblank' respectively. Otherwise callback is called for each entity found.
105  */
106 void parse_matlab(char *buffer, int length, int count,
107                   void (*callback) (const char *lang, const char *entity, int s,
108                                     int e, void *udata),
109                   void *userdata
110   ) {
111   init
112
113   %% write init;
114   cs = (count) ? matlab_en_matlab_line : matlab_en_matlab_entity;
115   %% write exec;
116
117   // if no newline at EOF; callback contents of last line
118   if (count) { process_last_line(MATLAB_LANG) }
119 }
120
121 #endif
122
123 /*****************************************************************************/