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