Merge git://github.com/korsakov/ohcount
[ohcount] / ext / ohcount_native / ragel_parsers / dylan.rl
1 // dylan.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net
2
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_DYLAN_PARSER
5 #define RAGEL_DYLAN_PARSER
6
7 #include "ragel_parser_macros.h"
8
9 // the name of the language
10 const char *DYLAN_LANG = "dylan";
11
12 // the languages entities
13 const char *dylan_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   DYLAN_SPACE = 0, DYLAN_COMMENT, DYLAN_STRING, DYLAN_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine dylan;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action dylan_ccallback {
32     switch(entity) {
33     case DYLAN_SPACE:
34       ls
35       break;
36     case DYLAN_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(DYLAN_LANG)
41       break;
42     case NEWLINE:
43       std_newline(DYLAN_LANG)
44     }
45   }
46
47   dylan_line_comment = '//' @comment nonnewline*;
48   dylan_block_comment =
49     '/*' @comment (
50       newline %{ entity = INTERNAL_NL; } %dylan_ccallback
51       |
52       ws
53       |
54       (nonnewline - ws) @comment
55     )* :>> '*/';
56   dylan_comment = dylan_line_comment | dylan_block_comment;
57
58   dylan_char = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline) '\'';
59   dylan_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
60   dylan_string = dylan_char | dylan_dq_str;
61
62   dylan_line := |*
63     spaces         ${ entity = DYLAN_SPACE; } => dylan_ccallback;
64     dylan_comment;
65     dylan_string;
66     newline        ${ entity = NEWLINE;     } => dylan_ccallback;
67     ^space         ${ entity = DYLAN_ANY;   } => dylan_ccallback;
68   *|;
69
70   # Entity machine
71
72   action dylan_ecallback {
73     callback(DYLAN_LANG, dylan_entities[entity], cint(ts), cint(te));
74   }
75
76   dylan_line_comment_entity = '//' nonnewline*;
77   dylan_block_comment_entity = '/*' any* :>> '*/';
78   dylan_comment_entity = dylan_line_comment_entity | dylan_block_comment_entity;
79
80   dylan_entity := |*
81     space+               ${ entity = DYLAN_SPACE;   } => dylan_ecallback;
82     dylan_comment_entity ${ entity = DYLAN_COMMENT; } => dylan_ecallback;
83     # TODO:
84     ^space;
85   *|;
86 }%%
87
88 /************************* Required for every parser *************************/
89
90 /* Parses a string buffer with Dylan 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_dylan(char *buffer, int length, int count,
102   void (*callback) (const char *lang, const char *entity, int start, int end)
103   ) {
104   init
105
106   %% write init;
107   cs = (count) ? dylan_en_dylan_line : dylan_en_dylan_entity;
108   %% write exec;
109
110   // if no newline at EOF; callback contents of last line
111   if (count) { process_last_line(DYLAN_LANG) }
112 }
113
114 #endif
115
116 /*****************************************************************************/