2 // derived from code written by Mitchell Foral. mitchell<att>caladbolg<dott>net
5 /************************* Required for every parser *************************/
6 #ifndef OHCOUNT_MODULA3_PARSER_H
7 #define OHCOUNT_MODULA3_PARSER_H
9 #include "../parser_macros.h"
11 // the name of the language
12 const char *MODULA3_LANG = LANG_MODULA3;
14 // the languages entities
15 const char *modula3_entities[] = {
16 "space", "comment", "string", "any"
19 // constants associated with the entities
21 MODULA3_SPACE = 0, MODULA3_COMMENT, MODULA3_STRING, MODULA3_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
33 action modula3_ccallback {
42 std_internal_newline(MODULA3_LANG)
45 std_newline(MODULA3_LANG)
52 action modula3_comment_nc_res { nest_count = 0; }
53 action modula3_comment_nc_inc { nest_count++; }
54 action modula3_comment_nc_dec { nest_count--; }
57 '(*' >modula3_comment_nc_res @comment (
58 newline %{ entity = INTERNAL_NL; } %modula3_ccallback
62 '(*' @modula3_comment_nc_inc @comment
64 '*)' @modula3_comment_nc_dec @comment
67 )* :>> ('*)' when { nest_count == 0 }) @comment;
70 # Modula-3 string literals
72 modula3_singlequoted_string =
74 escaped_newline %{ entity = INTERNAL_NL; } %modula3_ccallback
82 modula3_doublequoted_string =
84 escaped_newline %{ entity = INTERNAL_NL; } %modula3_ccallback
92 modula3_string = modula3_singlequoted_string | modula3_doublequoted_string;
98 spaces ${ entity = MODULA3_SPACE; } => modula3_ccallback;
101 newline ${ entity = NEWLINE; } => modula3_ccallback;
102 ^space ${ entity = MODULA3_ANY; } => modula3_ccallback;
108 action modula3_ecallback {
109 callback(MODULA3_LANG, modula3_entities[entity], cint(ts), cint(te),
113 modula3_comment_entity = '(*' >modula3_comment_nc_res (
114 '(*' @modula3_comment_nc_inc
116 '*)' @modula3_comment_nc_dec
119 )* :>> ('*)' when { nest_count == 0 });
121 modula3_string_entity = sq_str_with_escapes |
125 space+ ${ entity = MODULA3_SPACE; } => modula3_ecallback;
126 modula3_comment_entity ${ entity = MODULA3_COMMENT; } => modula3_ecallback;
127 modula3_string_entity ${ entity = MODULA3_STRING; } => modula3_ecallback;
128 # TODO: detecting other entities may be useful to differentiate dialects
133 /************************* Required for every parser *************************/
135 /* Parses a string buffer with Modula-3 code.
137 * @param *buffer The string to parse.
138 * @param length The length of the string to parse.
139 * @param count Integer flag specifying whether or not to count lines. If yes,
140 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
141 * machine optimized for returning entity positions.
142 * @param *callback Callback function. If count is set, callback is called for
143 * every line of code, comment, or blank with 'lcode', 'lcomment', and
144 * 'lblank' respectively. Otherwise callback is called for each entity found.
146 void parse_modula3(char *buffer, int length, int count,
147 void (*callback) (const char *lang, const char *entity, int s,
156 cs = (count) ? modula3_en_modula3_line : modula3_en_modula3_entity;
159 // if no newline at EOF; callback contents of last line
160 if (count) { process_last_line(MODULA3_LANG) }
165 /*****************************************************************************/