2014-05-04 22:25:34 +00:00
|
|
|
#include "lexer.h"
|
|
|
|
|
2014-05-06 22:57:25 +00:00
|
|
|
/*
|
|
|
|
The following two macros allow the Ragel grammar to use generic function calls
|
|
|
|
without relying on the setup of the C or Java lexer. Using these macros we can
|
|
|
|
also pass along `self` to the callback functions without having to hard-code
|
|
|
|
this in to the Ragel grammar.
|
|
|
|
|
|
|
|
In the C lexer we don't need the `data` variable (since this is pulled in based
|
|
|
|
on `ts` and `te`) so the macro ignores this argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define callback(name, data, encoding, start, stop) \
|
|
|
|
liboga_xml_lexer_callback(self, name, encoding, start, stop);
|
|
|
|
|
|
|
|
#define callback_simple(name) \
|
|
|
|
liboga_xml_lexer_callback_simple(self, name);
|
|
|
|
|
2014-09-25 20:49:11 +00:00
|
|
|
#define advance_line(amount) \
|
2015-03-02 15:20:54 +00:00
|
|
|
rb_funcall(self, id_advance_line, 1, INT2NUM(amount));
|
|
|
|
|
2015-03-03 15:28:05 +00:00
|
|
|
#define literal_html_element_p() \
|
|
|
|
rb_funcall(self, id_literal_html_element_p, 0) == Qtrue
|
2015-03-02 15:20:54 +00:00
|
|
|
|
|
|
|
ID id_advance_line;
|
2015-03-03 15:28:05 +00:00
|
|
|
ID id_literal_html_element_p;
|
2014-09-25 20:49:11 +00:00
|
|
|
|
2014-05-06 22:50:34 +00:00
|
|
|
%%machine c_lexer;
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2014-05-05 07:40:08 +00:00
|
|
|
/**
|
|
|
|
* Calls a method defined in the Ruby side of the lexer. The String value is
|
|
|
|
* created based on the values of `ts` and `te` and uses the encoding specified
|
|
|
|
* in `encoding`.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* rb_encoding *encoding = rb_enc_get(...);
|
2014-05-05 19:19:38 +00:00
|
|
|
* liboga_xml_lexer_callback(self, "on_string", encoding, ts, te);
|
2014-05-05 07:40:08 +00:00
|
|
|
*/
|
2014-05-05 19:19:38 +00:00
|
|
|
void liboga_xml_lexer_callback(
|
2014-05-06 07:41:36 +00:00
|
|
|
VALUE self,
|
2015-03-03 23:09:15 +00:00
|
|
|
ID name,
|
2014-05-06 07:41:36 +00:00
|
|
|
rb_encoding *encoding,
|
|
|
|
const char *ts,
|
|
|
|
const char *te
|
2014-05-04 22:25:34 +00:00
|
|
|
)
|
|
|
|
{
|
2014-11-23 21:12:18 +00:00
|
|
|
VALUE value = rb_enc_str_new(ts, te - ts, encoding);
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2014-11-21 00:37:33 +00:00
|
|
|
rb_funcall(self, name, 1, value);
|
2014-05-04 22:25:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 07:40:08 +00:00
|
|
|
/**
|
|
|
|
* Calls a method defined in the Ruby side of the lexer without passing it any
|
|
|
|
* arguments.
|
|
|
|
*
|
|
|
|
* @example
|
2014-05-05 19:19:38 +00:00
|
|
|
* liboga_xml_lexer_callback_simple(self, "on_cdata_start");
|
2014-05-05 07:40:08 +00:00
|
|
|
*/
|
2014-11-21 00:37:33 +00:00
|
|
|
void liboga_xml_lexer_callback_simple(VALUE self, VALUE name)
|
2014-05-04 22:25:34 +00:00
|
|
|
{
|
2014-11-21 00:37:33 +00:00
|
|
|
rb_funcall(self, name, 0);
|
2014-05-04 22:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
%% write data;
|
|
|
|
|
2014-05-05 07:40:08 +00:00
|
|
|
/**
|
2014-05-25 22:28:38 +00:00
|
|
|
* Lexes the String specifies as the method argument. Token values have the
|
|
|
|
* same encoding as the input value.
|
2014-05-05 07:40:08 +00:00
|
|
|
*
|
2014-05-25 22:28:38 +00:00
|
|
|
* This method keeps track of an internal state using the instance variables
|
|
|
|
* `@act` and `@cs`.
|
2014-05-05 07:40:08 +00:00
|
|
|
*/
|
2014-05-25 22:28:38 +00:00
|
|
|
VALUE oga_xml_lexer_advance(VALUE self, VALUE data_block)
|
2014-05-04 22:25:34 +00:00
|
|
|
{
|
2014-10-26 10:38:06 +00:00
|
|
|
OgaLexerState *state;
|
2015-03-03 23:10:24 +00:00
|
|
|
int lines;
|
2014-10-26 10:38:06 +00:00
|
|
|
|
2014-05-06 07:41:36 +00:00
|
|
|
/* Make sure that all data passed back to Ruby has the proper encoding. */
|
2014-05-20 22:27:23 +00:00
|
|
|
rb_encoding *encoding = rb_enc_get(data_block);
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2014-09-28 20:28:55 +00:00
|
|
|
char *data_str_val = StringValueCStr(data_block);
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2014-08-15 18:36:40 +00:00
|
|
|
const char *p = data_str_val;
|
|
|
|
const char *pe = data_str_val + strlen(data_str_val);
|
|
|
|
const char *eof = pe;
|
|
|
|
const char *ts = 0;
|
|
|
|
const char *te = 0;
|
|
|
|
const char *mark = 0;
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2014-11-29 11:53:55 +00:00
|
|
|
ID id_advance_line = rb_intern("advance_line");
|
|
|
|
ID id_on_attribute = rb_intern("on_attribute");
|
|
|
|
ID id_on_attribute_ns = rb_intern("on_attribute_ns");
|
2015-04-14 20:45:55 +00:00
|
|
|
ID id_on_cdata_start = rb_intern("on_cdata_start");
|
|
|
|
ID id_on_cdata_body = rb_intern("on_cdata_body");
|
|
|
|
ID id_on_cdata_end = rb_intern("on_cdata_end");
|
2015-04-14 21:11:22 +00:00
|
|
|
ID id_on_comment_start = rb_intern("on_comment_start");
|
|
|
|
ID id_on_comment_body = rb_intern("on_comment_body");
|
|
|
|
ID id_on_comment_end = rb_intern("on_comment_end");
|
2014-11-29 11:53:55 +00:00
|
|
|
ID id_on_doctype_end = rb_intern("on_doctype_end");
|
|
|
|
ID id_on_doctype_inline = rb_intern("on_doctype_inline");
|
|
|
|
ID id_on_doctype_name = rb_intern("on_doctype_name");
|
|
|
|
ID id_on_doctype_start = rb_intern("on_doctype_start");
|
|
|
|
ID id_on_doctype_type = rb_intern("on_doctype_type");
|
|
|
|
ID id_on_element_end = rb_intern("on_element_end");
|
|
|
|
ID id_on_element_name = rb_intern("on_element_name");
|
|
|
|
ID id_on_element_ns = rb_intern("on_element_ns");
|
|
|
|
ID id_on_element_open_end = rb_intern("on_element_open_end");
|
|
|
|
ID id_on_element_start = rb_intern("on_element_start");
|
|
|
|
ID id_on_proc_ins_end = rb_intern("on_proc_ins_end");
|
|
|
|
ID id_on_proc_ins_name = rb_intern("on_proc_ins_name");
|
|
|
|
ID id_on_proc_ins_start = rb_intern("on_proc_ins_start");
|
|
|
|
ID id_on_string_body = rb_intern("on_string_body");
|
|
|
|
ID id_on_string_dquote = rb_intern("on_string_dquote");
|
|
|
|
ID id_on_string_squote = rb_intern("on_string_squote");
|
|
|
|
ID id_on_text = rb_intern("on_text");
|
|
|
|
ID id_on_xml_decl_end = rb_intern("on_xml_decl_end");
|
|
|
|
ID id_on_xml_decl_start = rb_intern("on_xml_decl_start");
|
2014-11-21 00:37:33 +00:00
|
|
|
|
2015-03-03 23:10:24 +00:00
|
|
|
Data_Get_Struct(self, OgaLexerState, state);
|
|
|
|
|
|
|
|
lines = state->lines;
|
|
|
|
|
2014-05-06 07:41:36 +00:00
|
|
|
%% write exec;
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2014-10-26 10:38:06 +00:00
|
|
|
state->lines = lines;
|
2014-05-25 22:28:38 +00:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the internal state of the lexer.
|
|
|
|
*/
|
|
|
|
VALUE oga_xml_lexer_reset(VALUE self)
|
|
|
|
{
|
2014-10-26 10:38:06 +00:00
|
|
|
OgaLexerState *state;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, OgaLexerState, state);
|
|
|
|
|
|
|
|
state->act = 0;
|
|
|
|
state->cs = c_lexer_start;
|
|
|
|
state->lines = 0;
|
|
|
|
state->top = 0;
|
2014-05-25 22:28:38 +00:00
|
|
|
|
2014-05-06 07:41:36 +00:00
|
|
|
return Qnil;
|
2014-05-04 22:25:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-26 10:38:06 +00:00
|
|
|
/**
|
|
|
|
* Frees the associated lexer state struct.
|
|
|
|
*/
|
|
|
|
void oga_xml_lexer_free(void *state)
|
|
|
|
{
|
|
|
|
free((OgaLexerState *) state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates and wraps the C lexer state struct. This state is used to keep
|
|
|
|
* track of the current position, line numbers, etc.
|
|
|
|
*/
|
|
|
|
VALUE oga_xml_lexer_allocate(VALUE klass)
|
|
|
|
{
|
|
|
|
OgaLexerState *state = malloc(sizeof(OgaLexerState));
|
|
|
|
|
|
|
|
return Data_Wrap_Struct(klass, NULL, oga_xml_lexer_free, state);
|
|
|
|
}
|
|
|
|
|
2014-05-04 22:25:34 +00:00
|
|
|
%%{
|
2014-05-06 22:50:34 +00:00
|
|
|
include base_lexer "base_lexer.rl";
|
2014-10-26 10:38:06 +00:00
|
|
|
|
|
|
|
variable top state->top;
|
|
|
|
variable stack state->stack;
|
|
|
|
variable act state->act;
|
|
|
|
variable cs state->cs;
|
2014-05-04 22:25:34 +00:00
|
|
|
}%%
|
|
|
|
|
|
|
|
void Init_liboga_xml_lexer()
|
|
|
|
{
|
2014-05-07 08:51:50 +00:00
|
|
|
VALUE mOga = rb_const_get(rb_cObject, rb_intern("Oga"));
|
|
|
|
VALUE mXML = rb_const_get(mOga, rb_intern("XML"));
|
|
|
|
VALUE cLexer = rb_define_class_under(mXML, "Lexer", rb_cObject);
|
2014-05-04 22:25:34 +00:00
|
|
|
|
2015-03-03 15:28:05 +00:00
|
|
|
id_advance_line = rb_intern("advance_line");
|
|
|
|
id_literal_html_element_p = rb_intern("literal_html_element?");
|
2015-03-02 15:20:54 +00:00
|
|
|
|
2014-05-25 22:28:38 +00:00
|
|
|
rb_define_method(cLexer, "advance_native", oga_xml_lexer_advance, 1);
|
|
|
|
rb_define_method(cLexer, "reset_native", oga_xml_lexer_reset, 0);
|
2014-10-26 10:38:06 +00:00
|
|
|
|
|
|
|
rb_define_alloc_func(cLexer, oga_xml_lexer_allocate);
|
2014-05-04 22:25:34 +00:00
|
|
|
}
|