OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / limbo.rl
1 // limbo.rl
2 // http://www.vitanuova.com/inferno/papers/limbo.html
3
4 /************************* Required for every parser *************************/
5 #ifndef RAGEL_LIMBO_PARSER
6 #define RAGEL_LIMBO_PARSER
7
8 #include "../parser_macros.h"
9
10 // the name of the language
11 const char *LIMBO_LANG = "limbo";
12
13 // the languages entities
14 const char *limbo_entities[] = {
15   "space", "comment", "string", "number",
16   "keyword", "identifier", "operator", "any"
17 };
18
19 // constants associated with the entities
20 enum {
21   LIMBO_SPACE = 0, LIMBO_COMMENT, LIMBO_STRING, LIMBO_NUMBER,
22   LIMBO_KEYWORD, LIMBO_IDENTIFIER, LIMBO_OPERATOR, LIMBO_ANY
23 };
24
25 /*****************************************************************************/
26
27 %%{
28   machine limbo;
29   write data;
30   include common "common.rl";
31
32   # Line counting machine
33
34   action limbo_ccallback {
35     switch(entity) {
36     case LIMBO_SPACE:
37       ls
38       break;
39     case LIMBO_ANY:
40       code
41       break;
42     case INTERNAL_NL:
43       std_internal_newline(LIMBO_LANG)
44       break;
45     case NEWLINE:
46       std_newline(LIMBO_LANG)
47     }
48   }
49
50   limbo_comment = '#' @comment nonnewline*;
51
52   limbo_sq_str =
53     '\'' @code (
54       [^\r\n\f\t '\\] @code
55       |
56       '\\' nonnewline @code
57     )* '\'' @commit;
58   limbo_dq_str =
59     '"' @enqueue @code (
60       [^\r\n\f\t "\\] @code
61       |
62       '\\' nonnewline @code
63     )* '"' @commit;
64   limbo_string = limbo_sq_str | limbo_dq_str;
65
66   limbo_line := |*
67     spaces         ${ entity = LIMBO_SPACE; } => limbo_ccallback;
68     limbo_comment;
69     limbo_string;
70     newline        ${ entity = NEWLINE;     } => limbo_ccallback;
71     ^space         ${ entity = LIMBO_ANY;   } => limbo_ccallback;
72   *|;
73
74   # Entity machine
75
76   action limbo_ecallback {
77     callback(LIMBO_LANG, limbo_entities[entity], cint(ts), cint(te), userdata);
78   }
79
80   limbo_comment_entity = '#' nonnewline*;
81
82   # todo: this probably allows multi-line strings too, which limbo doesn't allow?
83   limbo_string_entity = sq_str_with_escapes | dq_str_with_escapes;
84
85   # todo: support numbers with specified radix?  e.g. 16rFF, 8r666, 2b10101
86   limbo_number_entity = float | integer;
87
88   # todo: support utf-8 identifiers
89   limbo_identifier_entity = (alpha | '_') (alnum | '_')*;
90
91   limbo_keyword_entity =
92     'alt' | 'break' | 'continue' | 'exit' | 'return' | 'spawn' | 'implement' | 'import' | 'load' | 'raise' | 'raises'
93     'include' | 'array' | 'big' | 'byte' | 'chan' | 'con' | 'int' | 'list' | 'real' | 'string' | 'fn' |
94     'adt' | 'pick' | 'module' |
95     'for' | 'while' | 'do' |
96     'if' | 'else' | 'case' | 'or' | 'to' |
97     '=>' |
98     'ref' | 'self' | 'cyclic' | 'type' | 'of' |
99     'tl' | 'hd' | 'len' | 'tagof' |
100     'nil';
101
102   limbo_operator_entity = [+\-/*%!=^&|~:;.,()\[\]{}>];
103
104   limbo_entity := |*
105     space+               ${ entity = LIMBO_SPACE;   } => limbo_ecallback;
106     limbo_comment_entity ${ entity = LIMBO_COMMENT; } => limbo_ecallback;
107     limbo_string_entity  ${ entity = LIMBO_STRING;  } => limbo_ecallback;
108     limbo_number_entity  ${ entity = LIMBO_NUMBER;  } => limbo_ecallback;
109     limbo_keyword_entity ${ entity = LIMBO_KEYWORD; } => limbo_ecallback;
110     limbo_identifier_entity ${ entity = LIMBO_IDENTIFIER; } => limbo_ecallback;
111     limbo_operator_entity ${ entity = LIMBO_OPERATOR; } => limbo_ecallback;
112     ^(space | digit)     ${ entity = C_ANY;         } => limbo_ecallback;
113   *|;
114 }%%
115
116 /************************* Required for every parser *************************/
117
118 /* Parses a string buffer with Limbo code.
119  *
120  * @param *buffer The string to parse.
121  * @param length The length of the string to parse.
122  * @param count Integer flag specifying whether or not to count lines. If yes,
123  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
124  *   machine optimized for returning entity positions.
125  * @param *callback Callback function. If count is set, callback is called for
126  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
127  *   'lblank' respectively. Otherwise callback is called for each entity found.
128  */
129 void parse_limbo(char *buffer, int length, int count,
130                  void (*callback) (const char *lang, const char *entity, int s,
131                                    int e, void *udata),
132                  void *userdata
133   ) {
134   init
135
136   %% write init;
137   cs = (count) ? limbo_en_limbo_line : limbo_en_limbo_entity;
138   %% write exec;
139
140   // if no newline at EOF; callback contents of last line
141   if (count) { process_last_line(LIMBO_LANG) }
142 }
143
144 #endif
145
146 /*****************************************************************************/