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