2 // derived from code written by Mitchell Foral. mitchell<att>caladbolg<dott>net
5 /************************* Required for every parser *************************/
6 #ifndef OHCOUNT_MODULA2_PARSER_H
7 #define OHCOUNT_MODULA2_PARSER_H
9 #include "../parser_macros.h"
11 // the name of the language
12 const char *MODULA2_LANG = LANG_MODULA2;
14 // the languages entities
15 const char *modula2_entities[] = {
16 "space", "comment", "string", "any"
19 // constants associated with the entities
21 MODULA2_SPACE = 0, MODULA2_COMMENT, MODULA2_STRING, MODULA2_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
33 action modula2_ccallback {
42 std_internal_newline(MODULA2_LANG)
45 std_newline(MODULA2_LANG)
52 action modula2_comment_nc_res { nest_count = 0; }
53 action modula2_comment_nc_inc { nest_count++; }
54 action modula2_comment_nc_dec { nest_count--; }
57 '(*' >modula2_comment_nc_res @comment (
58 newline %{ entity = INTERNAL_NL; } %modula2_ccallback
62 '(*' @modula2_comment_nc_inc @comment
64 '*)' @modula2_comment_nc_dec @comment
67 )* :>> ('*)' when { nest_count == 0 }) @comment;
70 # Modula-2 string literals
72 modula2_singlequoted_string = '\'' @code [^\r\n\f"]* '\'';
73 modula2_doublequoted_string = '"' @code [^\r\n\f"]* '"';
75 modula2_string = modula2_singlequoted_string | modula2_doublequoted_string;
81 spaces ${ entity = MODULA2_SPACE; } => modula2_ccallback;
84 newline ${ entity = NEWLINE; } => modula2_ccallback;
85 ^space ${ entity = MODULA2_ANY; } => modula2_ccallback;
91 action modula2_ecallback {
92 callback(MODULA2_LANG, modula2_entities[entity], cint(ts), cint(te),
96 modula2_comment_entity = '(*' >modula2_comment_nc_res (
97 '(*' @modula2_comment_nc_inc
99 '*)' @modula2_comment_nc_dec
102 )* :>> ('*)' when { nest_count == 0 });
104 modula2_string_entity = sq_str | dq_str;
107 space+ ${ entity = MODULA2_SPACE; } => modula2_ecallback;
108 modula2_comment_entity ${ entity = MODULA2_COMMENT; } => modula2_ecallback;
109 modula2_string_entity ${ entity = MODULA2_STRING; } => modula2_ecallback;
110 # TODO: detecting other entities may be useful to differentiate dialects
115 /************************* Required for every parser *************************/
117 /* Parses a string buffer with Modula-2 code.
119 * @param *buffer The string to parse.
120 * @param length The length of the string to parse.
121 * @param count Integer flag specifying whether or not to count lines. If yes,
122 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
123 * machine optimized for returning entity positions.
124 * @param *callback Callback function. If count is set, callback is called for
125 * every line of code, comment, or blank with 'lcode', 'lcomment', and
126 * 'lblank' respectively. Otherwise callback is called for each entity found.
128 void parse_modula2(char *buffer, int length, int count,
129 void (*callback) (const char *lang, const char *entity, int s,
138 cs = (count) ? modula2_en_modula2_line : modula2_en_modula2_entity;
141 // if no newline at EOF; callback contents of last line
142 if (count) { process_last_line(MODULA2_LANG) }
147 /*****************************************************************************/