1 // matlab.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_MATLAB_PARSER
5 #define RAGEL_MATLAB_PARSER
7 #include "ragel_parser_macros.h"
9 // the name of the language
10 const char *MATLAB_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_line_comment = '%' [^{] @{ fhold; } @comment nonnewline*;
48 matlab_block_comment =
50 newline %{ entity = INTERNAL_NL; } %matlab_ccallback
54 (nonnewline - ws) @code
56 matlab_comment = matlab_line_comment | matlab_block_comment;
58 matlab_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
59 matlab_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
60 matlab_string = matlab_sq_str | matlab_dq_str;
63 spaces ${ entity = MATLAB_SPACE; } => matlab_ccallback;
66 newline ${ entity = NEWLINE; } => matlab_ccallback;
67 ^space ${ entity = MATLAB_ANY; } => matlab_ccallback;
72 action matlab_ecallback {
73 callback(MATLAB_LANG, matlab_entities[entity], cint(ts), cint(te));
76 matlab_entity := 'TODO:';
79 /************************* Required for every parser *************************/
81 /* Parses a string buffer with MATLAB code.
83 * @param *buffer The string to parse.
84 * @param length The length of the string to parse.
85 * @param count Integer flag specifying whether or not to count lines. If yes,
86 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
87 * machine optimized for returning entity positions.
88 * @param *callback Callback function. If count is set, callback is called for
89 * every line of code, comment, or blank with 'lcode', 'lcomment', and
90 * 'lblank' respectively. Otherwise callback is called for each entity found.
92 void parse_matlab(char *buffer, int length, int count,
93 void (*callback) (const char *lang, const char *entity, int start, int end)
98 cs = (count) ? matlab_en_matlab_line : matlab_en_matlab_entity;
101 // if no newline at EOF; callback contents of last line
102 if (count) { process_last_line(MATLAB_LANG) }
107 /*****************************************************************************/