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