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