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