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