Merge pull request #41 from blackducksw/ubuntu_14
[ohcount] / src / parsers / scilab.rl
1 // scilab.rl written by Sylvestre Ledru based on matlab Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_SCILAB_PARSER
5 #define RAGEL_SCILAB_PARSER
6
7 #include "../parser_macros.h"
8
9 // the name of the language
10 const char *SCILAB_LANG = "scilab";
11
12 // the languages entities
13 const char *scilab_entities[] = {
14   "space", "comment", "string", "any",
15 };
16
17 // constants associated with the entities
18 enum {
19   SCILAB_SPACE = 0, SCILAB_COMMENT, SCILAB_STRING, SCILAB_ANY
20 };
21
22 /*****************************************************************************/
23
24 %%{
25   machine scilab;
26   write data;
27   include common "common.rl";
28
29   # Line counting machine
30
31   action scilab_ccallback {
32     switch(entity) {
33     case SCILAB_SPACE:
34       ls
35       break;
36     case SCILAB_ANY:
37       code
38       break;
39     case INTERNAL_NL:
40       std_internal_newline(SCILAB_LANG)
41       break;
42     case NEWLINE:
43       std_newline(SCILAB_LANG)
44     }
45   }
46
47   scilab_comment = ('//') @comment nonnewline*;
48
49   # The detector is not smart enough to detect GNU Octave's double
50   # quotes around strings, so accept it here.
51   scilab_sq_str = '\'' @code ([^\r\n\f'\\] | '\\' nonnewline)* '\'';
52   scilab_dq_str = '"' @code ([^\r\n\f"\\] | '\\' nonnewline)* '"';
53   scilab_string = scilab_sq_str | scilab_dq_str;
54
55   scilab_line := |*
56     spaces          ${ entity = SCILAB_SPACE; } => scilab_ccallback;
57     scilab_comment;
58     scilab_string;
59     newline         ${ entity = NEWLINE;      } => scilab_ccallback;
60     ^space          ${ entity = SCILAB_ANY;   } => scilab_ccallback;
61   *|;
62
63   # Entity machine
64
65   action scilab_ecallback {
66     callback(SCILAB_LANG, scilab_entities[entity], cint(ts), cint(te),
67              userdata);
68   }
69
70   scilab_comment_entity = ('//') @comment nonnewline*;
71
72   scilab_entity := |*
73     space+                ${ entity = SCILAB_SPACE;   } => scilab_ecallback;
74     scilab_comment_entity ${ entity = SCILAB_COMMENT; } => scilab_ecallback;
75     # TODO:
76     ^space;
77   *|;
78 }%%
79
80 /************************* Required for every parser *************************/
81
82 /* Parses a string buffer with SCILAB code.
83  *
84  * @param *buffer The string to parse.
85  * @param length The length of the string to parse.
86  * @param count Integer flag specifying whether or not to count lines. If yes,
87  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
88  *   machine optimized for returning entity positions.
89  * @param *callback Callback function. If count is set, callback is called for
90  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
91  *   'lblank' respectively. Otherwise callback is called for each entity found.
92  */
93 void parse_scilab(char *buffer, int length, int count,
94                   void (*callback) (const char *lang, const char *entity, int s,
95                                     int e, void *udata),
96                   void *userdata
97   ) {
98   init
99
100   %% write init;
101   cs = (count) ? scilab_en_scilab_line : scilab_en_scilab_entity;
102   %% write exec;
103
104   // if no newline at EOF; callback contents of last line
105   if (count) { process_last_line(SCILAB_LANG) }
106 }
107
108 #endif
109
110 /*****************************************************************************/