Fixes recursion bug in disambiguate_in().
[ohcount] / src / parsers / pike.rl
1 // pike.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_PIKE_PARSER_H
5 #define OHCOUNT_PIKE_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *PIKE_LANG = LANG_PIKE;
11
12 // the languages entities
13 const char *pike_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   PIKE_SPACE = 0, PIKE_COMMENT, PIKE_STRING, PIKE_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine pike;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action pike_ccallback {
32     switch(entity) {
33     case PIKE_SPACE:
34       ls
35       break;
36     case PIKE_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(PIKE_LANG)
41       break;
42     case NEWLINE:
43       std_newline(PIKE_LANG)
44     }
45   }
46
47   pike_line_comment = '//' @comment nonnewline*;
48   pike_block_comment =
49     '/*' @comment (
50       newline %{ entity = INTERNAL_NL; } %pike_ccallback
51       |
52       ws
53       |
54       (nonnewline - ws) @comment
55     )* :>> '*/';
56   pike_comment = pike_line_comment | pike_block_comment;
57
58   pike_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
59   pike_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
60   pike_string = pike_sq_str | pike_dq_str;
61
62   pike_line := |*
63     spaces        ${ entity = PIKE_SPACE; } => pike_ccallback;
64     pike_comment;
65     pike_string;
66     newline       ${ entity = NEWLINE;    } => pike_ccallback;
67     ^space        ${ entity = PIKE_ANY;   } => pike_ccallback;
68   *|;
69
70   # Entity machine
71
72   action pike_ecallback {
73     callback(PIKE_LANG, pike_entities[entity], cint(ts), cint(te), userdata);
74   }
75
76   pike_line_comment_entity = '//' nonnewline*;
77   pike_block_comment_entity = '/*' any* :>> '*/';
78   pike_comment_entity = pike_line_comment_entity | pike_block_comment_entity;
79
80   pike_entity := |*
81     space+              ${ entity = PIKE_SPACE;   } => pike_ecallback;
82     pike_comment_entity ${ entity = PIKE_COMMENT; } => pike_ecallback;
83     # TODO:
84     ^space;
85   *|;
86 }%%
87
88 /************************* Required for every parser *************************/
89
90 /* Parses a string buffer with Pike code.
91  *
92  * @param *buffer The string to parse.
93  * @param length The length of the string to parse.
94  * @param count Integer flag specifying whether or not to count lines. If yes,
95  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
96  *   machine optimized for returning entity positions.
97  * @param *callback Callback function. If count is set, callback is called for
98  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
99  *   'lblank' respectively. Otherwise callback is called for each entity found.
100  */
101 void parse_pike(char *buffer, int length, int count,
102                 void (*callback) (const char *lang, const char *entity, int s,
103                                   int e, void *udata),
104                 void *userdata
105   ) {
106   init
107
108   %% write init;
109   cs = (count) ? pike_en_pike_line : pike_en_pike_entity;
110   %% write exec;
111
112   // if no newline at EOF; callback contents of last line
113   if (count) { process_last_line(PIKE_LANG) }
114 }
115
116 #endif
117
118 /*****************************************************************************/