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