Fixes recursion bug in disambiguate_in().
[ohcount] / src / parsers / rebol.rl
1 // rebol.rl written by Andreas Bolka.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_REBOL_PARSER_H
5 #define OHCOUNT_REBOL_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *REBOL_LANG = LANG_REBOL;
11
12 // the languages entities
13 const char *rebol_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   REBOL_SPACE = 0, REBOL_COMMENT, REBOL_STRING, REBOL_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine rebol;
26   write data;
27   include common "common.rl";
28
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 }
32
33   rebol_cb_str_nest = '{'                       %rebol_inc_string
34                     | '}' when rebol_is_nested  %rebol_dec_string;
35
36   # Line counting machine
37
38   action rebol_ccallback {
39     switch(entity) {
40     case REBOL_SPACE:
41       ls
42       break;
43     case REBOL_ANY:
44       code
45       break;
46     case INTERNAL_NL:
47       std_internal_newline(REBOL_LANG)
48       break;
49     case NEWLINE:
50       std_newline(REBOL_LANG)
51     }
52   }
53
54   rebol_line_comment = ';' @comment nonnewline*;
55   rebol_comment = rebol_line_comment;
56
57   rebol_dq_str = '"' @code ([^\r\n\f"] | '^"')* [\r\n\f"];
58   rebol_cb_str_inner = [^{}] | '^{' | '^}'
59                      | newline %{ entity = INTERNAL_NL; } %rebol_ccallback
60                      | rebol_cb_str_nest;
61   rebol_cb_str = '{' @code rebol_cb_str_inner* @code '}' @code;
62   rebol_string = rebol_dq_str | rebol_cb_str;
63
64   rebol_line := |*
65     spaces                  ${ entity = REBOL_SPACE; }      => rebol_ccallback;
66     rebol_comment;
67     rebol_string;
68     newline                 ${ entity = NEWLINE; }          => rebol_ccallback;
69     ^space                  ${ entity = REBOL_ANY; }        => rebol_ccallback;
70   *|;
71
72   # Entity machine
73
74   action rebol_ecallback {
75     callback(REBOL_LANG, rebol_entities[entity], cint(ts), cint(te), userdata);
76   }
77
78   rebol_line_comment_entity = ';' nonnewline*;
79   rebol_comment_entity = rebol_line_comment_entity;
80
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;
84
85   rebol_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;
90   *|;
91
92 }%%
93
94 /************************* Required for every parser *************************/
95
96 /* Parses a string buffer with REBOL code.
97  *
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.
106  */
107 void parse_rebol(char *buffer, int length, int count,
108                  void (*callback) (const char *lang, const char *entity, int s,
109                                    int e, void *udata),
110                  void *userdata
111   ) {
112   // For {..} multi-line strings which require proper balancing of {}'s.
113   int rebol_string_level = 0;
114   init
115
116   %% write init;
117   cs = (count) ? rebol_en_rebol_line : rebol_en_rebol_entity;
118   %% write exec;
119
120   // if no newline at EOF; callback contents of last line
121   if (count) { process_last_line(REBOL_LANG) }
122 }
123
124 #endif
125
126 /*****************************************************************************/
127
128 // vim: set ts=2 syn=c: