1 // rebol.rl written by Andreas Bolka.
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_REBOL_PARSER_H
5 #define OHCOUNT_REBOL_PARSER_H
7 #include "../parser_macros.h"
9 // the name of the language
10 const char *REBOL_LANG = LANG_REBOL;
12 // the languages entities
13 const char *rebol_entities[] = {
14 "space", "comment", "string", "any"
17 // constants associated with the entities
19 REBOL_SPACE = 0, REBOL_COMMENT, REBOL_STRING, REBOL_ANY
22 /*****************************************************************************/
27 include common "common.rl";
29 action rebol_inc_string { ++rebol_string_level; }
30 action rebol_dec_string { --rebol_string_level; }
31 action rebol_is_nested { rebol_string_level > 0 }
33 rebol_cb_str_nest = '{' %rebol_inc_string
34 | '}' when rebol_is_nested %rebol_dec_string;
36 # Line counting machine
38 action rebol_ccallback {
47 std_internal_newline(REBOL_LANG)
50 std_newline(REBOL_LANG)
54 rebol_line_comment = ';' @comment nonnewline*;
55 rebol_comment = rebol_line_comment;
57 rebol_dq_str = '"' @code ([^\r\n\f"] | '^"')* [\r\n\f"];
58 rebol_cb_str_inner = [^{}] | '^{' | '^}'
59 | newline %{ entity = INTERNAL_NL; } %rebol_ccallback
61 rebol_cb_str = '{' @code rebol_cb_str_inner* @code '}' @code;
62 rebol_string = rebol_dq_str | rebol_cb_str;
65 spaces ${ entity = REBOL_SPACE; } => rebol_ccallback;
68 newline ${ entity = NEWLINE; } => rebol_ccallback;
69 ^space ${ entity = REBOL_ANY; } => rebol_ccallback;
74 action rebol_ecallback {
75 callback(REBOL_LANG, rebol_entities[entity], cint(ts), cint(te), userdata);
78 rebol_line_comment_entity = ';' nonnewline*;
79 rebol_comment_entity = rebol_line_comment_entity;
81 rebol_dq_str_entity = '"' ([^\r\n\f"] | '^"')* [\r\n\f"];
82 rebol_cb_str_entity = '{' ([^{}] | '^{' | '^}' | rebol_cb_str_nest)* '}';
83 rebol_string_entiy = rebol_dq_str_entity | rebol_cb_str_entity;
86 space+ ${ entity = REBOL_SPACE; } => rebol_ecallback;
87 rebol_comment_entity ${ entity = REBOL_COMMENT; } => rebol_ecallback;
88 rebol_string_entiy ${ entity = REBOL_STRING; } => rebol_ecallback;
89 ^space ${ entity = REBOL_ANY; } => rebol_ecallback;
94 /************************* Required for every parser *************************/
96 /* Parses a string buffer with REBOL code.
98 * @param *buffer The string to parse.
99 * @param length The length of the string to parse.
100 * @param count Integer flag specifying whether or not to count lines. If yes,
101 * uses the Ragel machine optimized for counting. Otherwise uses the Ragel
102 * machine optimized for returning entity positions.
103 * @param *callback Callback function. If count is set, callback is called for
104 * every line of code, comment, or blank with 'lcode', 'lcomment', and
105 * 'lblank' respectively. Otherwise callback is called for each entity found.
107 void parse_rebol(char *buffer, int length, int count,
108 void (*callback) (const char *lang, const char *entity, int s,
112 // For {..} multi-line strings which require proper balancing of {}'s.
113 int rebol_string_level = 0;
117 cs = (count) ? rebol_en_rebol_line : rebol_en_rebol_entity;
120 // if no newline at EOF; callback contents of last line
121 if (count) { process_last_line(REBOL_LANG) }
126 /*****************************************************************************/
128 // vim: set ts=2 syn=c: