2 // derived from code written by Mitchell Foral. mitchell<att>caladbolg<dott>net
5 /************************* Required for every parser *************************/
6 #ifndef OHCOUNT_OBERON_PARSER_H
7 #define OHCOUNT_OBERON_PARSER_H
9 #include "../parser_macros.h"
11 // the name of the language
12 const char *OBERON_LANG = LANG_OBERON;
14 // the languages entities
15 const char *oberon_entities[] = {
16 "space", "comment", "string", "any"
19 // constants associated with the entities
21 OBERON_SPACE = 0, OBERON_COMMENT, OBERON_STRING, OBERON_ANY
24 /*****************************************************************************/
29 include common "common.rl";
31 # Line counting machine
33 action oberon_ccallback {
42 std_internal_newline(OBERON_LANG)
45 std_newline(OBERON_LANG)
52 action oberon_comment_nc_res { nest_count = 0; }
53 action oberon_comment_nc_inc { nest_count++; }
54 action oberon_comment_nc_dec { nest_count--; }
57 '(*' >oberon_comment_nc_res @comment (
58 newline %{ entity = INTERNAL_NL; } %oberon_ccallback
62 '(*' @oberon_comment_nc_inc @comment
64 '*)' @oberon_comment_nc_dec @comment
67 )* :>> ('*)' when { nest_count == 0 }) @comment;
70 # Oberon string literals
72 oberon_singlequoted_string = '\'' @code [^\r\n\f"]* '\'';
73 oberon_doublequoted_string = '"' @code [^\r\n\f"]* '"';
75 oberon_string = oberon_singlequoted_string | oberon_doublequoted_string;
81 spaces ${ entity = OBERON_SPACE; } => oberon_ccallback;
84 newline ${ entity = NEWLINE; } => oberon_ccallback;
85 ^space ${ entity = OBERON_ANY; } => oberon_ccallback;
91 action oberon_ecallback {
92 callback(OBERON_LANG, oberon_entities[entity], cint(ts), cint(te),
96 oberon_comment_entity = '(*' >oberon_comment_nc_res (
97 '(*' @oberon_comment_nc_inc
99 '*)' @oberon_comment_nc_dec
102 )* :>> ('*)' when { nest_count == 0 });
104 oberon_string_entity = sq_str | dq_str;
107 space+ ${ entity = OBERON_SPACE; } => oberon_ecallback;
108 oberon_comment_entity ${ entity = OBERON_COMMENT; } => oberon_ecallback;
109 oberon_string_entity ${ entity = OBERON_STRING; } => oberon_ecallback;
110 # TODO: detecting other entities may be useful to differentiate dialects
115 /************************* Required for every parser *************************/
117 /* Parses a string buffer with Oberon 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_oberon(char *buffer, int length, int count,
129 void (*callback) (const char *lang, const char *entity, int s,
138 cs = (count) ? oberon_en_oberon_line : oberon_en_oberon_entity;
141 // if no newline at EOF; callback contents of last line
142 if (count) { process_last_line(OBERON_LANG) }
147 /*****************************************************************************/