OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / blitzmax.rl
1 // blitzmax.rl written by Bruce A Henderson (http://brucey.net)
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_BLITZMAX_PARSER_H
5 #define OHCOUNT_BLITZMAX_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *BLITZMAX_LANG = LANG_BLITZMAX;
11
12 // the languages entities
13 const char *blitzmax_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   BLITZMAX_SPACE = 0, BLITZMAX_COMMENT, BLITZMAX_STRING, BLITZMAX_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine blitzmax;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action blitzmax_ccallback {
32     switch(entity) {
33     case BLITZMAX_SPACE:
34       ls
35       break;
36     case BLITZMAX_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(BLITZMAX_LANG)
41       break;
42     case NEWLINE:
43       std_newline(BLITZMAX_LANG)
44     }
45   }
46
47   blitzmax_line_comment = '\'' @comment nonnewline*;
48   blitzmax_rem_block_comment =
49     /rem/i @comment (
50       newline %{ entity = INTERNAL_NL; } %blitzmax_ccallback
51       |
52       ws
53       |
54       (nonnewline - ws) @comment
55     )* :>> (/end rem/i | /endrem/i);
56
57   blitzmax_comment = blitzmax_line_comment | blitzmax_rem_block_comment;
58
59   blitzmax_string = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
60
61   blitzmax_line := |*
62     spaces          ${ entity = BLITZMAX_SPACE; } => blitzmax_ccallback;
63     blitzmax_comment;
64     blitzmax_string;
65     newline         ${ entity = NEWLINE;      } => blitzmax_ccallback;
66     ^space          ${ entity = BLITZMAX_ANY;   } => blitzmax_ccallback;
67   *|;
68
69   # Entity machine
70
71   action blitzmax_ecallback {
72     callback(BLITZMAX_LANG, blitzmax_entities[entity], cint(ts), cint(te),
73              userdata);
74   }
75
76   blitzmax_line_comment_entity = '\'' nonnewline*;
77   blitzmax_rem_block_comment_entity =
78     /rem/i (
79       newline %{ entity = INTERNAL_NL; } %blitzmax_ecallback
80       |
81       ws
82       |
83       (nonnewline - ws)
84     )* :>> (/end rem/i | /endrem/i);
85
86   blitzmax_comment_entity = blitzmax_line_comment_entity | blitzmax_line_comment_entity;
87
88   blitzmax_entity := |*
89     space+                ${ entity = BLITZMAX_SPACE;   } => blitzmax_ecallback;
90     blitzmax_comment_entity ${ entity = BLITZMAX_COMMENT; } => blitzmax_ecallback;
91     # TODO:
92     ^space;
93   *|;
94 }%%
95
96 /************************* Required for every parser *************************/
97
98 /* Parses a string buffer with BlitzMax code.
99  *
100  * @param *buffer The string to parse.
101  * @param length The length of the string to parse.
102  * @param count Integer flag specifying whether or not to count lines. If yes,
103  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
104  *   machine optimized for returning entity positions.
105  * @param *callback Callback function. If count is set, callback is called for
106  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
107  *   'lblank' respectively. Otherwise callback is called for each entity found.
108  */
109 void parse_blitzmax(char *buffer, int length, int count,
110                     void (*callback) (const char *lang, const char *entity,
111                                       int s, int e, void *udata),
112                     void *userdata
113   ) {
114   init
115
116   %% write init;
117   cs = (count) ? blitzmax_en_blitzmax_line : blitzmax_en_blitzmax_entity;
118   %% write exec;
119
120   // if no newline at EOF; callback contents of last line
121   if (count) { process_last_line(BLITZMAX_LANG) }
122 }
123
124 #endif
125
126 /*****************************************************************************/