OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / dcl.rl
1 // dcl.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_DCL_PARSER_H
5 #define OHCOUNT_DCL_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *DCL_LANG = LANG_DCL;
11
12 // the languages entities
13 const char *dcl_entities[] = {
14   "space", "line_start", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   DCL_SPACE = 0, DCL_LINE_START, DCL_COMMENT, DCL_STRING, DCL_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine dcl;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action dcl_ccallback {
32     switch(entity) {
33     case DCL_SPACE:
34     case DCL_LINE_START:
35       ls
36       break;
37     case DCL_ANY:
38       code
39       break;
40     case INTERNAL_NL:
41       std_internal_newline(DCL_LANG)
42       break;
43     case NEWLINE:
44       std_newline(DCL_LANG)
45     }
46   }
47
48   dcl_comment = '!' @comment nonnewline*;
49
50   dcl_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
51
52   dcl_line := |*
53     '$'            ${ entity = DCL_LINE_START; } => dcl_ccallback;
54     spaces         ${ entity = DCL_SPACE;      } => dcl_ccallback;
55     dcl_comment;
56     dcl_string;
57     newline        ${ entity = NEWLINE;        } => dcl_ccallback;
58     ^(space | '$') ${ entity = DCL_ANY;        } => dcl_ccallback;
59   *|;
60
61   # Entity machine
62
63   action dcl_ecallback {
64     callback(DCL_LANG, dcl_entities[entity], cint(ts), cint(te), userdata);
65   }
66
67   dcl_comment_entity = '!' nonnewline*;
68
69   dcl_entity := |*
70     space+             ${ entity = DCL_SPACE;   } => dcl_ecallback;
71     dcl_comment_entity ${ entity = DCL_COMMENT; } => dcl_ecallback;
72     # TODO:
73     ^space;
74   *|;
75 }%%
76
77 /************************* Required for every parser *************************/
78
79 /* Parses a string buffer with DCL code.
80  *
81  * @param *buffer The string to parse.
82  * @param length The length of the string to parse.
83  * @param count Integer flag specifying whether or not to count lines. If yes,
84  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
85  *   machine optimized for returning entity positions.
86  * @param *callback Callback function. If count is set, callback is called for
87  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
88  *   'lblank' respectively. Otherwise callback is called for each entity found.
89  */
90 void parse_dcl(char *buffer, int length, int count,
91                void (*callback) (const char *lang, const char *entity, int s,
92                                  int e, void *udata),
93                void *userdata
94   ) {
95   init
96
97   %% write init;
98   cs = (count) ? dcl_en_dcl_line : dcl_en_dcl_entity;
99   %% write exec;
100
101   // if no newline at EOF; callback contents of last line
102   if (count) { process_last_line(DCL_LANG) }
103 }
104
105 #endif
106
107 /*****************************************************************************/