OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / src / parsers / common.rl
1 %%{
2 machine common;
3
4 # common definitions
5
6 # whitespace, non-printables
7 ws = [\t ];
8 spaces = [\t ]+;
9 newline = ('\r\n' | '\n' | '\f' | '\r' when { p+1 < pe && *(p+1) != '\n' });
10 escaped_newline = '\\' newline;
11 nonnewline = any - [\r\n\f];
12 nonprintable_char = cntrl - [\r\n\f];
13
14 # numbers
15 dec_num = digit+;
16 hex_num = 0 [xX] [a-fA-F0-9]+;
17 oct_num = 0 [0-7]+;
18 integer = [+\-]? (hex_num | oct_num | dec_num);
19 float = [+\-]? ((digit* '.' digit+) | (digit+ '.' digit*) | digit+)
20         [eE] [+\-]? digit+;
21
22 # common entities
23 sq_str = '\'' [^']* '\'';
24 dq_str = '"' [^"]* '"';
25 sq_str_with_escapes = '\'' ([^'\\] | '\\' any)* '\'';
26 dq_str_with_escapes = '"' ([^"\\] | '\\' any)* '"';
27
28 # common actions
29
30 action enqueue {
31   inqueue = 1;
32   if (callback_list_head) free_queue(); // free the current queue
33   callback_list_head = NULL;
34   callback_list_tail = NULL;
35   // set backup variables
36   last_line_start = line_start;
37   last_line_contains_code = line_contains_code;
38   last_whole_line_comment = whole_line_comment;
39 }
40
41 action commit {
42   if (inqueue) {
43     Callback *item;
44     for (item = callback_list_head; item != NULL; item = item->next)
45       callback(item->lang, item->entity, item->s, item->e, item->udata);
46     free_queue();
47     inqueue = 0;
48   }
49 }
50
51 action ls { if (!line_start) line_start = ts; }
52
53 action code {
54   if (!line_contains_code && !line_start) line_start = ts;
55   line_contains_code = 1;
56 }
57
58 action comment {
59   if (!line_contains_code) {
60     whole_line_comment = 1;
61     if (!line_start) line_start = ts;
62   }
63 }
64
65 action check_blank_outry {
66   if (!line_contains_code && !whole_line_comment) seen = 0;
67 }
68
69 # common conditionals
70
71 action no_code { !line_contains_code }
72 action no_comment { !whole_line_comment }
73 action no_code_or_comment { !line_contains_code && !whole_line_comment }
74
75 action starts_line {
76   p == buffer || *(p-1) == '\r' || *(p-1) == '\n' || *(p-1) == '\f'
77 }
78 action starts_line2 {
79   p == buffer || *(p-2) == '\r' || *(p-2) == '\n' || *(p-2) == '\f'
80 }
81
82 }%%