Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / jam.rl
1 // jam.rl written by Scott Lawrence. bytbox@gmail.com
2
3 /************************* Required for every parser *************************/
4 #ifndef OHCOUNT_JAM_PARSER_H
5 #define OHCOUNT_JAM_PARSER_H
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *JAM_LANG = LANG_JAM;
11
12 // the languages entities
13 const char *jam_entities[] = {
14   "space", "comment", "string", "any"
15 };
16
17 // constants associated with the entities
18 enum {
19   JAM_SPACE = 0, JAM_COMMENT, JAM_STRING, JAM_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine jam;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action jam_ccallback {
32     switch(entity) {
33     case JAM_SPACE:
34       ls
35       break;
36     case JAM_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(JAM_LANG)
41       break;
42     case NEWLINE:
43       std_newline(JAM_LANG)
44     }
45   }
46
47   jam_comment = '#' @comment nonnewline*;
48
49   jam_sq_str =
50     '\'' @enqueue @code (
51       newline %{ entity = INTERNAL_NL; } %jam_ccallback
52       |
53       ws
54       |
55       [^\r\n\f\t '\\] @code
56       |
57       '\\' nonnewline @code
58     )* '\'' @commit;
59   jam_dq_str =
60     '"' @enqueue @code (
61       newline %{ entity = INTERNAL_NL; } %jam_ccallback
62       |
63       ws
64       |
65       [^\r\n\f\t "\\] @code
66       |
67       '\\' nonnewline @code
68     )* '"' @commit;
69   # TODO: heredocs; see ruby.rl for details
70   jam_string = jam_sq_str | jam_dq_str;
71
72   jam_line := |*
73     spaces         ${ entity = JAM_SPACE; } => jam_ccallback;
74     jam_comment;
75     jam_string;
76     newline        ${ entity = NEWLINE;     } => jam_ccallback;
77     ^space         ${ entity = JAM_ANY;   } => jam_ccallback;
78   *|;
79
80   # Entity machine
81
82   action jam_ecallback {
83     callback(JAM_LANG, jam_entities[entity], cint(ts), cint(te), userdata);
84   }
85
86   jam_comment_entity = '#' nonnewline*;
87
88   jam_entity := |*
89     space+               ${ entity = JAM_SPACE;   } => jam_ecallback;
90     jam_comment_entity ${ entity = JAM_COMMENT; } => jam_ecallback;
91     # TODO:
92     ^space;
93   *|;
94 }%%
95
96 /************************* Required for every parser *************************/
97
98 /* Parses a string buffer with Jam code.
99  *
100  * @param *buffer The string to parse.
101  * @param length The length of the string to parse.
102  * @param count Integer flag specifying whether or not to count lines. If yes,
103  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
104  *   machine optimized for returning entity positions.
105  * @param *callback Callback function. If count is set, callback is called for
106  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
107  *   'lblank' respectively. Otherwise callback is called for each entity found.
108  */
109 void parse_jam(char *buffer, int length, int count,
110                  void (*callback) (const char *lang, const char *entity, int s,
111                                    int e, void *udata),
112                  void *userdata
113   ) {
114   init
115
116   %% write init;
117   cs = (count) ? jam_en_jam_line : jam_en_jam_entity;
118   %% write exec;
119
120   // if no newline at EOF; callback contents of last line
121   if (count) { process_last_line(JAM_LANG) }
122 }
123
124 #endif
125
126 /*****************************************************************************/