1 // matlab.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_MATLAB_PARSER_H
5 #define OHCOUNT_MATLAB_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *MATLAB_LANG = LANG_MATLAB;
12 // the languages entities
13 const char *matlab_entities[] = {
14 "space", "comment", "string", "any",
17 // constants associated with the entities
19 MATLAB_SPACE = 0, MATLAB_COMMENT, MATLAB_STRING, MATLAB_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 # Line counting machine
31 action matlab_ccallback {
40 std_internal_newline(MATLAB_LANG)
43 std_newline(MATLAB_LANG)
47 # Matlab(TM) comments also may begin with the line-continuing sequence ...
48 matlab_line_comment = (('%' | '...') [^{] @{ fhold; }) @comment nonnewline*;
49 matlab_block_comment =
51 newline %{ entity = INTERNAL_NL; } %matlab_ccallback
55 (nonnewline - ws) @code
57 matlab_comment = matlab_line_comment | matlab_block_comment;
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;
66 spaces ${ entity = MATLAB_SPACE; } => matlab_ccallback;
69 newline ${ entity = NEWLINE; } => matlab_ccallback;
70 ^space ${ entity = MATLAB_ANY; } => matlab_ccallback;
75 action matlab_ecallback {
76 callback(MATLAB_LANG, matlab_entities[entity], cint(ts), cint(te),
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;
86 space+ ${ entity = MATLAB_SPACE; } => matlab_ecallback;
87 matlab_comment_entity ${ entity = MATLAB_COMMENT; } => matlab_ecallback;
93 /************************* Required for every parser *************************/
95 /* Parses a string buffer with MATLAB code.
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.
106 void parse_matlab(char *buffer, int length, int count,
107 void (*callback) (const char *lang, const char *entity, int s,
114 cs = (count) ? matlab_en_matlab_line : matlab_en_matlab_entity;
117 // if no newline at EOF; callback contents of last line
118 if (count) { process_last_line(MATLAB_LANG) }
123 /*****************************************************************************/