From ad3c972a8d032ebfec62dda002735b7c01e6be02 Mon Sep 17 00:00:00 2001 From: mitchell Date: Fri, 30 May 2008 13:59:41 -0400 Subject: [PATCH] Added MATLAB parser. --- ext/ohcount_native/ragel_parser.c | 2 + ext/ohcount_native/ragel_parsers/matlab.rl | 107 +++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 ext/ohcount_native/ragel_parsers/matlab.rl diff --git a/ext/ohcount_native/ragel_parser.c b/ext/ohcount_native/ragel_parser.c index 4160fe7..c7961c6 100644 --- a/ext/ohcount_native/ragel_parser.c +++ b/ext/ohcount_native/ragel_parser.c @@ -31,6 +31,7 @@ #include "fortranfree_parser.h" #include "haskell_parser.h" #include "makefile_parser.h" +#include "matlab_parser.h" // END parser includes ParseResult *pr; @@ -74,6 +75,7 @@ struct language languages[] = { { "fortranfree", parse_fortranfree }, { "haskell", parse_haskell }, { "make", parse_makefile }, + { "matlab", parse_matlab }, // END languages { "", NULL } }; diff --git a/ext/ohcount_native/ragel_parsers/matlab.rl b/ext/ohcount_native/ragel_parsers/matlab.rl new file mode 100644 index 0000000..1bc4c1e --- /dev/null +++ b/ext/ohcount_native/ragel_parsers/matlab.rl @@ -0,0 +1,107 @@ +// matlab.rl written by Mitchell Foral. mitchellcaladbolgnet. + +/************************* Required for every parser *************************/ +#ifndef RAGEL_MATLAB_PARSER +#define RAGEL_MATLAB_PARSER + +#include "ragel_parser_macros.h" + +// the name of the language +const char *MATLAB_LANG = "matlab"; + +// the languages entities +const char *matlab_entities[] = { + "space", "comment", "string", "any", +}; + +// constants associated with the entities +enum { + MATLAB_SPACE = 0, MATLAB_COMMENT, MATLAB_STRING, MATLAB_ANY +}; + +/*****************************************************************************/ + +%%{ + machine matlab; + write data; + include common "common.rl"; + + # Line counting machine + + action matlab_ccallback { + switch(entity) { + case MATLAB_SPACE: + ls + break; + case MATLAB_ANY: + code + break; + case INTERNAL_NL: + std_internal_newline(MATLAB_LANG) + break; + case NEWLINE: + std_newline(MATLAB_LANG) + } + } + + matlab_line_comment = '%' [^{] @{ fhold; } @comment nonnewline*; + matlab_block_comment = + '%{' @comment ( + newline %{ entity = INTERNAL_NL; } %matlab_ccallback + | + ws + | + (nonnewline - ws) @code + )* :>> '%}'; + matlab_comment = matlab_line_comment | matlab_block_comment; + + matlab_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\''; + matlab_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"'; + matlab_string = matlab_sq_str | matlab_dq_str; + + matlab_line := |* + spaces ${ entity = MATLAB_SPACE; } => matlab_ccallback; + matlab_comment; + matlab_string; + newline ${ entity = NEWLINE; } => matlab_ccallback; + ^space ${ entity = MATLAB_ANY; } => matlab_ccallback; + *|; + + # Entity machine + + action matlab_ecallback { + callback(MATLAB_LANG, matlab_entities[entity], cint(ts), cint(te)); + } + + matlab_entity := 'TODO:'; +}%% + +/************************* Required for every parser *************************/ + +/* Parses a string buffer with MATLAB code. + * + * @param *buffer The string to parse. + * @param length The length of the string to parse. + * @param count Integer flag specifying whether or not to count lines. If yes, + * uses the Ragel machine optimized for counting. Otherwise uses the Ragel + * machine optimized for returning entity positions. + * @param *callback Callback function. If count is set, callback is called for + * every line of code, comment, or blank with 'lcode', 'lcomment', and + * 'lblank' respectively. Otherwise callback is called for each entity found. + */ +void parse_matlab(char *buffer, int length, int count, + void (*callback) (const char *lang, const char *entity, int start, int end) + ) { + init + + %% write init; + cs = (count) ? matlab_en_matlab_line : matlab_en_matlab_entity; + %% write exec; + + // if no newline at EOF; callback contents of last line + if (count) { process_last_line(MATLAB_LANG) } +} + +#endif + +/*****************************************************************************/ -- 2.32.0.93.g670b81a890