OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / pascal.rl
1 // pascal.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_PASCAL_PARSER_H
5 #define OHCOUNT_PASCAL_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *PASCAL_LANG = LANG_PASCAL;
11
12 // the languages entities
13 const char *pascal_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   PASCAL_SPACE = 0, PASCAL_COMMENT, PASCAL_STRING, PASCAL_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine pascal;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action pascal_ccallback {
32     switch(entity) {
33     case PASCAL_SPACE:
34       ls
35       break;
36     case PASCAL_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(PASCAL_LANG)
41       break;
42     case NEWLINE:
43       std_newline(PASCAL_LANG)
44     }
45   }
46
47   pascal_line_comment = '//' @comment nonnewline*;
48   pascal_old_block_comment =
49     '(*' @comment (
50       newline %{ entity = INTERNAL_NL; } %pascal_ccallback
51       |
52       ws
53       |
54       (nonnewline - ws) @code
55     )* :>> '*)';
56   pascal_turbo_block_comment =
57     '{' @comment (
58       newline %{ entity = INTERNAL_NL; } %pascal_ccallback
59       |
60       ws
61       |
62       (nonnewline - ws) @comment
63     )* :>> '}';
64   pascal_comment = pascal_line_comment | pascal_old_block_comment |
65                    pascal_turbo_block_comment;
66
67   pascal_string =
68     '\'' @code (
69       newline %{ entity = INTERNAL_NL; } %pascal_ccallback
70       |
71       ws
72       |
73       [^\r\n\f\t '\\] @code
74       |
75       '\\' nonnewline @code
76     )* '\'';
77
78   pascal_line := |*
79     spaces          ${ entity = PASCAL_SPACE; } => pascal_ccallback;
80     pascal_comment;
81     pascal_string;
82     newline         ${ entity = NEWLINE;      } => pascal_ccallback;
83     ^space          ${ entity = PASCAL_ANY;   } => pascal_ccallback;
84   *|;
85
86   # Entity machine
87
88   action pascal_ecallback {
89     callback(PASCAL_LANG, pascal_entities[entity], cint(ts), cint(te),
90              userdata);
91   }
92
93   pascal_line_comment_entity = '//' nonnewline*;
94   pascal_old_block_comment_entity = '(*' any* :>> '*)';
95   pascal_turbo_block_comment_entity = '{' any* :>> '}';
96   pascal_comment_entity = pascal_line_comment_entity |
97     pascal_old_block_comment_entity | pascal_turbo_block_comment_entity;
98
99   pascal_entity := |*
100     space+                ${ entity = PASCAL_SPACE;   } => pascal_ecallback;
101     pascal_comment_entity ${ entity = PASCAL_COMMENT; } => pascal_ecallback;
102     # TODO:
103     ^space;
104   *|;
105 }%%
106
107 /************************* Required for every parser *************************/
108
109 /* Parses a string buffer with Pascal code.
110  *
111  * @param *buffer The string to parse.
112  * @param length The length of the string to parse.
113  * @param count Integer flag specifying whether or not to count lines. If yes,
114  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
115  *   machine optimized for returning entity positions.
116  * @param *callback Callback function. If count is set, callback is called for
117  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
118  *   'lblank' respectively. Otherwise callback is called for each entity found.
119  */
120 void parse_pascal(char *buffer, int length, int count,
121                   void (*callback) (const char *lang, const char *entity, int s,
122                                     int e, void *udata),
123                   void *userdata
124   ) {
125   init
126
127   %% write init;
128   cs = (count) ? pascal_en_pascal_line : pascal_en_pascal_entity;
129   %% write exec;
130
131   // if no newline at EOF; callback contents of last line
132   if (count) { process_last_line(PASCAL_LANG) }
133 }
134
135 #endif
136
137 /*****************************************************************************/