Added Pascal parser.
[ohcount] / ext / ohcount_native / ragel_parsers / ebuild.rl
1 // ebuild.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
2
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_EBUILD_PARSER
5 #define RAGEL_EBUILD_PARSER
6
7 #include "ragel_parser_macros.h"
8
9 // the name of the language
10 const char *EBUILD_LANG = "ebuild";
11
12 // the languages entities
13 const char *ebuild_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   EBUILD_SPACE = 0, EBUILD_COMMENT, EBUILD_STRING, EBUILD_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine ebuild;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action ebuild_ccallback {
32     switch(entity) {
33     case EBUILD_SPACE:
34       ls
35       break;
36     case EBUILD_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(EBUILD_LANG)
41       break;
42     case NEWLINE:
43       std_newline(EBUILD_LANG)
44     }
45   }
46
47   ebuild_comment = '#' @comment nonnewline*;
48
49   ebuild_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
50   ebuild_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
51   ebuild_string = ebuild_sq_str | ebuild_dq_str;
52
53   ebuild_line := |*
54     spaces          ${ entity = EBUILD_SPACE; } => ebuild_ccallback;
55     ebuild_comment;
56     ebuild_string;
57     newline         ${ entity = NEWLINE;      } => ebuild_ccallback;
58     ^space          ${ entity = EBUILD_ANY;   } => ebuild_ccallback;
59   *|;
60
61   # Entity machine
62
63   action ebuild_ecallback {
64     callback(EBUILD_LANG, entity, cint(ts), cint(te));
65   }
66
67   ebuild_entity := 'TODO:';
68 }%%
69
70 /************************* Required for every parser *************************/
71
72 /* Parses a string buffer with ebuild code.
73  *
74  * @param *buffer The string to parse.
75  * @param length The length of the string to parse.
76  * @param count Integer flag specifying whether or not to count lines. If yes,
77  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
78  *   machine optimized for returning entity positions.
79  * @param *callback Callback function. If count is set, callback is called for
80  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
81  *   'lblank' respectively. Otherwise callback is called for each entity found.
82  */
83 void parse_ebuild(char *buffer, int length, int count,
84   void (*callback) (const char *lang, const char *entity, int start, int end)
85   ) {
86   init
87
88   %% write init;
89   cs = (count) ? ebuild_en_ebuild_line : ebuild_en_ebuild_entity;
90   %% write exec;
91
92   // if no newline at EOF; callback contents of last line
93   if (count) { process_last_line(EBUILD_LANG) }
94 }
95
96 #endif
97
98 /*****************************************************************************/