Merge branch 'master' of git://github.com/robinluckey/ohcount
[ohcount] / ext / ohcount_native / ragel_parsers / xmlschema.rl
1 // xmlschema.rl written by Mitchell Foral. mitchell<att>caladbolg<dott>net.
2
3 /************************* Required for every parser *************************/
4 #ifndef RAGEL_XMLSCHEMA_PARSER
5 #define RAGEL_XMLSCHEMA_PARSER
6
7 #include "ragel_parser_macros.h"
8
9 // the name of the language
10 const char *XMLSCHEMA_LANG = "xmlschema";
11
12 // the languages entities
13 const char *xmlschema_entities[] = {
14   "space", "comment", "doctype",
15   "tag", "entity", "any"
16 };
17
18 // constants associated with the entities
19 enum {
20   XMLSCHEMA_SPACE = 0, XMLSCHEMA_COMMENT, XMLSCHEMA_DOCTYPE,
21   XMLSCHEMA_TAG, XMLSCHEMA_ENTITY, XMLSCHEMA_ANY
22 };
23
24 /*****************************************************************************/
25
26 %%{
27   machine xmlschema;
28   write data;
29   include common "common.rl";
30
31   # Line counting machine
32
33   action xmlschema_ccallback {
34     switch(entity) {
35     case XMLSCHEMA_SPACE:
36       ls
37       break;
38     case XMLSCHEMA_ANY:
39       code
40       break;
41     case INTERNAL_NL:
42       std_internal_newline(XMLSCHEMA_LANG)
43       break;
44     case NEWLINE:
45       std_newline(XMLSCHEMA_LANG)
46       break;
47     case CHECK_BLANK_ENTRY:
48       check_blank_entry(XMLSCHEMA_LANG)
49     }
50   }
51
52   xmlschema_comment =
53     '<!--' @comment (
54       newline %{ entity = INTERNAL_NL; } %xmlschema_ccallback
55       |
56       ws
57       |
58       (nonnewline - ws) @comment
59     )* :>> '-->';
60
61   xmlschema_sq_str = '\'' ([^\r\n\f'\\] | '\\' nonnewline)* '\'' @code;
62   xmlschema_dq_str = '"' ([^\r\n\f"\\] | '\\' nonnewline)* '"' @code;
63   xmlschema_cdata_str =
64     '<![CDATA[' @code (
65       newline %{ entity = INTERNAL_NL; } %xmlschema_ccallback
66       |
67       ws
68       |
69       (nonnewline - ws) @code
70     )* :>> ']]>';
71   xmlschema_string = xmlschema_sq_str | xmlschema_dq_str | xmlschema_cdata_str;
72
73   xmlschema_line := |*
74     spaces             ${ entity = XMLSCHEMA_SPACE; } => xmlschema_ccallback;
75     xmlschema_comment;
76     xmlschema_string;
77     newline            ${ entity = NEWLINE;         } => xmlschema_ccallback;
78     ^space             ${ entity = XMLSCHEMA_ANY;   } => xmlschema_ccallback;
79   *|;
80
81   # Entity machine
82
83   action xmlschema_ecallback {
84     callback(XMLSCHEMA_LANG, xmlschema_entities[entity], cint(ts), cint(te));
85   }
86
87   xmlschema_comment_entity = '<!--' any* :>> '-->';
88
89   xmlschema_entity := |*
90     space+                   ${ entity = XMLSCHEMA_SPACE;   } => xmlschema_ecallback;
91     xmlschema_comment_entity ${ entity = XMLSCHEMA_COMMENT; } => xmlschema_ecallback;
92     # TODO:
93     ^space;
94   *|;
95
96 }%%
97
98 /************************* Required for every parser *************************/
99
100 /* Parses a string buffer with XML Schema markup.
101  *
102  * @param *buffer The string to parse.
103  * @param length The length of the string to parse.
104  * @param count Integer flag specifying whether or not to count lines. If yes,
105  *   uses the Ragel machine optimized for counting. Otherwise uses the Ragel
106  *   machine optimized for returning entity positions.
107  * @param *callback Callback function. If count is set, callback is called for
108  *   every line of code, comment, or blank with 'lcode', 'lcomment', and
109  *   'lblank' respectively. Otherwise callback is called for each entity found.
110  */
111 void parse_xmlschema(char *buffer, int length, int count,
112   void (*callback) (const char *lang, const char *entity, int start, int end)
113   ) {
114   init
115
116   %% write init;
117   cs = (count) ? xmlschema_en_xmlschema_line : xmlschema_en_xmlschema_entity;
118   %% write exec;
119
120   // if no newline at EOF; callback contents of last line
121   if (count) { process_last_line(XMLSCHEMA_LANG) }
122 }
123
124 #endif
125
126 /*****************************************************************************/