Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / octave.rl
1 // octave.rl based on Mitchell Foral's matlab.rl, modified my Jason Riedy <jason@acm.org>
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_OCTAVE_PARSER_H
5 #define OHCOUNT_OCTAVE_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *OCTAVE_LANG = LANG_OCTAVE;
11
12 // the languages entities
13 const char *octave_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   OCTAVE_SPACE = 0, OCTAVE_COMMENT, OCTAVE_STRING, OCTAVE_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine octave;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action octave_ccallback {
32     switch(entity) {
33     case OCTAVE_SPACE:
34       ls
35       break;
36     case OCTAVE_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(OCTAVE_LANG)
41       break;
42     case NEWLINE:
43       std_newline(OCTAVE_LANG)
44     }
45   }
46
47   # note: GNU Octave accepts % as well as #, but not '...' at the moment.  We
48   # accept it anyways, as that may change.  Also, GNU Octave does not currently
49   # support block comments but likely will someday.
50   octave_line_comment = (('%' | '...') [^{] @{ fhold; } | '#') @comment nonnewline*;
51   octave_block_comment =
52     '%{' @comment (
53       newline %{ entity = INTERNAL_NL; } %octave_ccallback
54       |
55       ws
56       |
57       (nonnewline - ws) @code
58     )* :>> '%}';
59   octave_comment = octave_line_comment | octave_block_comment;
60
61   octave_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
62   octave_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
63   octave_string = octave_sq_str | octave_dq_str;
64
65   octave_line := |*
66     spaces          ${ entity = OCTAVE_SPACE; } => octave_ccallback;
67     octave_comment;
68     octave_string;
69     newline         ${ entity = NEWLINE;      } => octave_ccallback;
70     ^space          ${ entity = OCTAVE_ANY;   } => octave_ccallback;
71   *|;
72
73   # Entity machine
74
75   action octave_ecallback {
76     callback(OCTAVE_LANG, octave_entities[entity], cint(ts), cint(te),
77              userdata);
78   }
79
80   octave_line_comment_entity = (('%' | '...') [^{] @{ fhold; } | '#') nonnewline*;
81   octave_block_comment_entity = '%{' any* :>> '%}';
82   octave_comment_entity =
83     octave_line_comment_entity | octave_block_comment_entity;
84
85   octave_entity := |*
86     space+                ${ entity = OCTAVE_SPACE;   } => octave_ecallback;
87     octave_comment_entity ${ entity = OCTAVE_COMMENT; } => octave_ecallback;
88     # TODO:
89     ^space;
90   *|;
91 }%%
92
93 /************************* Required for every parser *************************/
94
95 /* Parses a string buffer with OCTAVE 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_octave(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) ? octave_en_octave_line : octave_en_octave_entity;
115   %% write exec;
116
117   // if no newline at EOF; callback contents of last line
118   if (count) { process_last_line(OCTAVE_LANG) }
119 }
120
121 #endif
122
123 /*****************************************************************************/