2014-05-06 22:50:34 +00:00
|
|
|
%%machine base_lexer;
|
|
|
|
|
|
|
|
%%{
|
2014-05-07 22:21:23 +00:00
|
|
|
##
|
|
|
|
# Base grammar for the XML lexer.
|
|
|
|
#
|
|
|
|
# This grammar is shared between the C and Java extensions. As a result of
|
|
|
|
# this you should **not** include language specific code in Ragel
|
|
|
|
# actions/callbacks.
|
|
|
|
#
|
|
|
|
# To call back in to Ruby you can use one of the following two functions:
|
|
|
|
#
|
|
|
|
# * callback
|
|
|
|
# * callback_simple
|
|
|
|
#
|
|
|
|
# The first function takes 5 arguments:
|
|
|
|
#
|
|
|
|
# * The name of the Ruby method to call.
|
|
|
|
# * The input data.
|
|
|
|
# * The encoding of the input data.
|
|
|
|
# * The start of the current buffer.
|
|
|
|
# * The end of the current buffer.
|
|
|
|
#
|
|
|
|
# The function callback_simple only takes one argument: the name of the
|
|
|
|
# method to call. This function should be used for callbacks that don't
|
|
|
|
# require any values.
|
|
|
|
#
|
|
|
|
# When you call a method in Ruby make sure that said method is defined as
|
|
|
|
# an instance method in the `Oga::XML::Lexer` class.
|
2014-05-20 22:21:21 +00:00
|
|
|
#
|
|
|
|
# ## Machine Transitions
|
|
|
|
#
|
|
|
|
# To transition from one machine to another always use `fnext` instead of
|
|
|
|
# `fcall` and `fret`. This removes the need for the code to keep track of a
|
|
|
|
# stack.
|
|
|
|
#
|
2014-05-07 22:21:23 +00:00
|
|
|
|
2014-05-06 22:50:34 +00:00
|
|
|
newline = '\n' | '\r\n';
|
|
|
|
whitespace = [ \t];
|
2014-09-02 22:51:13 +00:00
|
|
|
ident_char = [a-zA-Z0-9\-_];
|
|
|
|
identifier = ident_char+;
|
2014-05-06 22:50:34 +00:00
|
|
|
|
2014-05-20 22:21:21 +00:00
|
|
|
# Comments
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/html-markup/syntax.html#comments
|
|
|
|
#
|
|
|
|
# Unlike the W3 specification these rules *do* allow character sequences
|
|
|
|
# such as `--` and `->`. Putting extra checks in for these sequences would
|
|
|
|
# actually make the rules/actions more complex.
|
|
|
|
#
|
2014-08-15 18:36:40 +00:00
|
|
|
|
|
|
|
comment_start = '<!--';
|
|
|
|
comment_end = '-->';
|
2014-08-16 14:03:55 +00:00
|
|
|
comment = comment_start (any* -- comment_end) comment_end;
|
2014-08-15 18:36:40 +00:00
|
|
|
|
|
|
|
action start_comment {
|
2014-08-16 14:03:55 +00:00
|
|
|
callback("on_comment", data, encoding, ts + 4, te - 3);
|
2014-08-15 18:36:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-20 22:21:21 +00:00
|
|
|
# CDATA
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/html-markup/syntax.html#cdata-sections
|
|
|
|
#
|
|
|
|
# In HTML CDATA tags have no meaning/are not supported. Oga does
|
|
|
|
# support them but treats their contents as plain text.
|
|
|
|
#
|
2014-08-15 18:47:58 +00:00
|
|
|
|
|
|
|
cdata_start = '<![CDATA[';
|
|
|
|
cdata_end = ']]>';
|
2014-08-16 14:03:55 +00:00
|
|
|
cdata = cdata_start (any* -- cdata_end) cdata_end;
|
2014-08-15 18:47:58 +00:00
|
|
|
|
|
|
|
action start_cdata {
|
2014-08-16 14:03:55 +00:00
|
|
|
callback("on_cdata", data, encoding, ts + 9, te - 3);
|
2014-08-15 18:47:58 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 20:04:45 +00:00
|
|
|
# Processing Instructions
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/xpath/#section-Processing-Instruction-Nodes
|
|
|
|
# http://en.wikipedia.org/wiki/Processing_Instruction
|
|
|
|
#
|
|
|
|
# These are tags meant to be used by parsers/libraries for custom behaviour.
|
|
|
|
# One example are the tags used by PHP: <?php and ?>. Note that the XML
|
|
|
|
# declaration tags (<?xml ?>) are not considered to be a processing
|
|
|
|
# instruction.
|
|
|
|
#
|
|
|
|
|
|
|
|
proc_ins_start = '<?' identifier;
|
|
|
|
proc_ins_end = '?>';
|
|
|
|
|
|
|
|
action start_proc_ins {
|
|
|
|
callback_simple("on_proc_ins_start");
|
|
|
|
callback("on_proc_ins_name", data, encoding, ts + 2, te);
|
|
|
|
|
|
|
|
mark = te;
|
|
|
|
|
|
|
|
fnext proc_ins_body;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_ins_body := |*
|
|
|
|
proc_ins_end => {
|
|
|
|
callback("on_text", data, encoding, mark, ts);
|
|
|
|
callback_simple("on_proc_ins_end");
|
|
|
|
|
|
|
|
fnext main;
|
|
|
|
};
|
|
|
|
|
|
|
|
any;
|
|
|
|
*|;
|
|
|
|
|
2014-05-06 22:50:34 +00:00
|
|
|
# Strings
|
|
|
|
#
|
|
|
|
# Strings in HTML can either be single or double quoted. If a string
|
|
|
|
# starts with one of these quotes it must be closed with the same type
|
|
|
|
# of quote.
|
2014-05-20 22:21:21 +00:00
|
|
|
#
|
2014-05-06 22:50:34 +00:00
|
|
|
dquote = '"';
|
|
|
|
squote = "'";
|
|
|
|
|
2014-05-19 18:26:07 +00:00
|
|
|
string_dquote = (dquote ^dquote+ dquote);
|
|
|
|
string_squote = (squote ^squote+ squote);
|
2014-05-06 22:50:34 +00:00
|
|
|
|
2014-05-19 18:26:07 +00:00
|
|
|
string = string_dquote | string_squote;
|
2014-05-06 22:50:34 +00:00
|
|
|
|
2014-05-19 18:26:07 +00:00
|
|
|
action emit_string {
|
|
|
|
callback("on_string", data, encoding, ts + 1, te - 1);
|
|
|
|
}
|
2014-05-06 22:50:34 +00:00
|
|
|
|
|
|
|
# DOCTYPES
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/html-markup/syntax.html#doctype-syntax
|
|
|
|
#
|
|
|
|
# These rules support the 3 flavours of doctypes:
|
|
|
|
#
|
|
|
|
# 1. Normal doctypes, as introduced in the HTML5 specification.
|
|
|
|
# 2. Deprecated doctypes, the more verbose ones used prior to HTML5.
|
|
|
|
# 3. Legacy doctypes
|
|
|
|
#
|
|
|
|
doctype_start = '<!DOCTYPE'i whitespace+;
|
|
|
|
|
|
|
|
action start_doctype {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback_simple("on_doctype_start");
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext doctype;
|
2014-05-06 22:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Machine for processing doctypes. Doctype values such as the public
|
|
|
|
# and system IDs are treated as T_STRING tokens.
|
|
|
|
doctype := |*
|
|
|
|
'PUBLIC' | 'SYSTEM' => {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback("on_doctype_type", data, encoding, ts, te);
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
|
2014-05-09 22:28:11 +00:00
|
|
|
# Consumes everything between the [ and ]. Due to the use of :> the ]
|
|
|
|
# is not consumed by any+.
|
|
|
|
'[' any+ :> ']' => {
|
|
|
|
callback("on_doctype_inline", data, encoding, ts + 1, te - 1);
|
|
|
|
};
|
|
|
|
|
2014-05-06 22:50:34 +00:00
|
|
|
# Lex the public/system IDs as regular strings.
|
2014-05-19 18:26:07 +00:00
|
|
|
string => emit_string;
|
2014-05-06 22:50:34 +00:00
|
|
|
|
|
|
|
# Whitespace inside doctypes is ignored since there's no point in
|
|
|
|
# including it.
|
|
|
|
whitespace;
|
|
|
|
|
|
|
|
identifier => {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback("on_doctype_name", data, encoding, ts, te);
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
'>' => {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback_simple("on_doctype_end");
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext main;
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
*|;
|
|
|
|
|
|
|
|
# XML declaration tags
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/REC-xml/#sec-prolog-dtd
|
|
|
|
#
|
|
|
|
xml_decl_start = '<?xml';
|
|
|
|
xml_decl_end = '?>';
|
|
|
|
|
|
|
|
action start_xml_decl {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback_simple("on_xml_decl_start");
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext xml_decl;
|
2014-05-06 22:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Machine that processes the contents of an XML declaration tag.
|
|
|
|
xml_decl := |*
|
|
|
|
xml_decl_end => {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback_simple("on_xml_decl_end");
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext main;
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Attributes and their values (e.g. version="1.0").
|
2014-07-20 05:29:37 +00:00
|
|
|
identifier => {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback("on_attribute", data, encoding, ts, te);
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
|
2014-05-19 18:26:07 +00:00
|
|
|
string => emit_string;
|
2014-05-06 22:50:34 +00:00
|
|
|
|
|
|
|
any;
|
|
|
|
*|;
|
|
|
|
|
|
|
|
# Elements
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
|
|
|
|
#
|
2014-05-20 22:21:21 +00:00
|
|
|
# Lexing of elements is broken up into different machines that handle the
|
|
|
|
# name/namespace, contents of the open tag and the body of an element. The
|
|
|
|
# body of an element is lexed using the `main` machine.
|
|
|
|
#
|
2014-09-02 20:50:21 +00:00
|
|
|
|
2014-09-02 22:51:13 +00:00
|
|
|
element_start = '<' ident_char;
|
|
|
|
element_end = '</' identifier (':' identifier)* '>';
|
2014-09-02 20:50:21 +00:00
|
|
|
|
2014-05-06 22:50:34 +00:00
|
|
|
action start_element {
|
2014-05-20 22:11:39 +00:00
|
|
|
callback_simple("on_element_start");
|
2014-09-02 22:51:13 +00:00
|
|
|
fhold;
|
2014-05-20 22:11:39 +00:00
|
|
|
fnext element_name;
|
2014-05-06 22:50:34 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 22:51:13 +00:00
|
|
|
action close_element {
|
|
|
|
callback_simple("on_element_end");
|
|
|
|
}
|
|
|
|
|
2014-05-15 08:22:05 +00:00
|
|
|
# Machine used for lexing the name/namespace of an element.
|
|
|
|
element_name := |*
|
|
|
|
identifier ':' => {
|
|
|
|
callback("on_element_ns", data, encoding, ts, te - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
identifier => {
|
|
|
|
callback("on_element_name", data, encoding, ts, te);
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext element_head;
|
2014-05-15 08:22:05 +00:00
|
|
|
};
|
|
|
|
*|;
|
2014-05-06 22:50:34 +00:00
|
|
|
|
2014-05-20 22:21:21 +00:00
|
|
|
# Machine used for processing the contents of an element's starting tag.
|
|
|
|
# This includes the name, namespace and attributes.
|
2014-05-06 22:50:34 +00:00
|
|
|
element_head := |*
|
|
|
|
whitespace | '=';
|
|
|
|
|
|
|
|
newline => {
|
2014-05-29 12:21:48 +00:00
|
|
|
callback_simple("advance_line");
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
|
2014-07-20 05:29:37 +00:00
|
|
|
# Attribute names and namespaces.
|
|
|
|
identifier ':' => {
|
|
|
|
callback("on_attribute_ns", data, encoding, ts, te - 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
identifier => {
|
2014-05-06 22:57:25 +00:00
|
|
|
callback("on_attribute", data, encoding, ts, te);
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Attribute values.
|
2014-05-19 18:26:07 +00:00
|
|
|
string => emit_string;
|
2014-05-06 22:50:34 +00:00
|
|
|
|
2014-05-20 22:04:53 +00:00
|
|
|
# We're done with the open tag of the element.
|
|
|
|
'>' => {
|
|
|
|
callback_simple("on_element_open_end");
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext main;
|
2014-05-20 22:04:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Self closing tags.
|
|
|
|
'/>' => {
|
|
|
|
callback_simple("on_element_end");
|
2014-05-20 22:08:48 +00:00
|
|
|
fnext main;
|
2014-05-06 22:50:34 +00:00
|
|
|
};
|
|
|
|
*|;
|
|
|
|
|
2014-09-02 22:51:13 +00:00
|
|
|
# Text
|
|
|
|
#
|
|
|
|
# http://www.w3.org/TR/xml/#syntax
|
|
|
|
# http://www.w3.org/TR/html-markup/syntax.html#text-syntax
|
|
|
|
#
|
|
|
|
# Text content is everything leading up to certain special tags such as "</"
|
|
|
|
# and "<?".
|
|
|
|
|
|
|
|
action start_text {
|
|
|
|
fhold;
|
|
|
|
fnext text;
|
|
|
|
}
|
|
|
|
|
|
|
|
# These characters terminate a T_TEXT sequence and instruct Ragel to jump
|
|
|
|
# back to the main machine.
|
|
|
|
#
|
|
|
|
# Note that this only works if each sequence is exactly 2 characters
|
|
|
|
# long. Because of this "<!" is used instead of "<!--".
|
|
|
|
|
|
|
|
terminate_text = '</' | '<!' | '<?' | element_start;
|
|
|
|
allowed_text = any* -- terminate_text;
|
|
|
|
|
|
|
|
text := |*
|
|
|
|
# Text followed by a special tag, such as "foo<!--"
|
|
|
|
allowed_text @{ mark = p; } terminate_text => {
|
|
|
|
callback("on_text", data, encoding, ts, mark);
|
|
|
|
|
|
|
|
p = mark - 1;
|
|
|
|
mark = 0;
|
|
|
|
|
|
|
|
fnext main;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Just regular text.
|
|
|
|
allowed_text => {
|
|
|
|
callback("on_text", data, encoding, ts, te);
|
|
|
|
fnext main;
|
|
|
|
};
|
|
|
|
*|;
|
|
|
|
|
2014-05-20 22:21:21 +00:00
|
|
|
# The main machine aka the entry point of Ragel.
|
2014-05-06 22:50:34 +00:00
|
|
|
main := |*
|
|
|
|
doctype_start => start_doctype;
|
|
|
|
xml_decl_start => start_xml_decl;
|
2014-08-16 14:03:55 +00:00
|
|
|
comment => start_comment;
|
|
|
|
cdata => start_cdata;
|
2014-08-15 20:04:45 +00:00
|
|
|
proc_ins_start => start_proc_ins;
|
2014-09-02 22:51:13 +00:00
|
|
|
element_start => start_element;
|
|
|
|
element_end => close_element;
|
|
|
|
any => start_text;
|
2014-05-06 22:50:34 +00:00
|
|
|
*|;
|
|
|
|
}%%
|