XML-Easy

 view release on metacpan or  search on metacpan

lib/XML/Easy/Syntax.pm  view on Meta::CPAN

=head1 NAME

XML::Easy::Syntax - excruciatingly correct XML syntax

=head1 SYNOPSIS

    use XML::Easy::Syntax qw($xml10_name_rx);
    if($name =~ /\A$xml10_name_rx\z/o) { ...
    # and many other regular expressions

=head1 DESCRIPTION

This module supplies Perl regular expressions describing the grammar of
XML 1.0.  This is intended to support doing irregular things with XML,
rather than for normal parsing.

These regular expressions encompass the entire XML grammar except for
document type declarations and DTDs.
This document assumes general familiarity with XML.

=cut

package XML::Easy::Syntax;

{ use 5.010001; }
use warnings;
use strict;

our $VERSION = "0.014";

use parent "Exporter";
our @EXPORT_OK = qw(
	$xml10_char_rx $xml10_s_rx $xml10_eq_rx
	$xml10_namestartchar_rx $xml10_namechar_rx
	$xml10_name_rx $xml10_names_rx $xml10_nmtoken_rx $xml10_nmtokens_rx
	$xml10_charref_rx $xml10_entityref_rx $xml10_reference_rx
	$xml10_chardata_rx
	$xml10_cdata_rx $xml10_cdstart_rx $xml10_cdend_rx $xml10_cdsect_rx
	$xml10_attvalue_rx $xml10_attribute_rx
	$xml10_stag_rx $xml10_etag_rx $xml10_emptyelemtag_rx
	$xml10_comment_rx $xml10_pitarget_rx $xml10_pi_rx
	$xml10_content_rx $xml10_element_rx
	$xml10_versionnum_rx $xml10_versioninfo_rx
	$xml10_encname_rx $xml10_encodingdecl_rx
	$xml10_sddecl_rx $xml10_xmldecl_rx $xml10_textdecl_rx
	$xml10_misc_rx $xml10_miscseq_rx
	$xml10_prolog_xdtd_rx $xml10_document_xdtd_rx $xml10_extparsedent_rx
);

sub _charclass_regexp($) {
	my($class) = @_;
	$class =~ tr/ \t\n//d;
	return eval("qr/[$class]/");
}

=head1 REGULAR EXPRESSIONS

Each of these regular expressions corresponds precisely to one of
the productions in the EBNF grammar in the XML 1.0 specification.
Well-formedness constraints that are not expressed in the EBNF are
I<not> checked by the regular expressions; these are noted in the
documentation below.  The regular expressions do not include any anchors,
so to check whether an entire string matches a production you must supply
the anchors yourself.

=head2 Syntax pieces

=over

=item $xml10_char_rx

Any single character that is acceptable to XML 1.0.  This includes most
Unicode characters (up to codepoint 0x10ffff).  The excluded codepoints
are the sentinels 0xfffe and 0xffff, the surrogate blocks, and most of
the C0 control characters (0x00 to 0x1f, except for 0x09 (tab), 0x0a
(linefeed/newline), and 0x0d (carriage return)).

It is a rule of XML that all characters making up an XML document
must be in this permitted set.  The grammar productions can only match
sequences of acceptable characters.  This rule is enforced by the regular
expressions in this module.

Furthermore, it is a rule that the character data in a document cannot
even I<represent> a character outside the permitted set.  This is
expressed as a well-formedness constraint on character references.

=cut

our $xml10_char_rx = _charclass_regexp(q(
	\x{9}
	\x{a}
	\x{d}
	\x{20}-\x{d7ff}
	\x{e000}-\x{fffd}
	\x{10000}-\x{10ffff}
));

=item $xml10_s_rx

lib/XML/Easy/Syntax.pm  view on Meta::CPAN

acceptable character (as discussed at C<$xml10_char_rx>).

=cut

our $xml10_charref_rx = qr/&#(?:[0-9]+|x[0-9a-fA-F]+);/;

=item $xml10_entityref_rx

A general entity reference (beginning with "B<&>" and ending with "B<;>").
There are non-syntactic well-formedness constraints: the referenced entity
must be declared (possibly implicitly), must not be an unparsed entity,
must not contain a recursive reference to itself, and its replacement
text must itself be well-formed.

=cut

our $xml10_entityref_rx = qr/&$xml10_name_rx;/o;

=item $xml10_reference_rx

Either a character reference or an entity reference.  The well-formedness
constraints of both reference types (see above) apply.

=cut

our $xml10_reference_rx = qr/$xml10_entityref_rx|$xml10_charref_rx/o;

=back

=head2 Character data

=over

=item $xml10_chardata_rx

Ordinary literal character data.  This consists of zero or more acceptable
characters, other than the metacharacters "B<< < >>" and "B<&>", and
not including "B<< ]]> >>" as a subsequence.  Such data stands for
itself when it appears between the start and end tags of an element,
where it can be interspersed with references, CDATA sections, comments,
and processing instructions.

In the XML grammar, character data is parsed, and taken literally,
I<after> line endings have been canonicalised (to the newline character).
Pre-canonicalisation character data, with variable line endings, will
still match this production but should not be interpreted literally.

Beware that a string that does not match this production might parse as
two adjacent strings each of which matches.  This can happen because
of the prohibition on "B<< ]]> >>" being embedded in character data,
while the characters of that sequence are acceptable individually.
The XML grammar does not allow two instances of this production to abut.

=cut

our $xml10_chardata_rx = qr/(?:
	\]?(?![<&\]])$xml10_char_rx
	|\]{2,}(?![<&\>\]])$xml10_char_rx
)*\]*/xo;

=item $xml10_cdata_rx

Literal character data in a CDATA section.  This consists of zero or
more acceptable characters, not including "B<< ]]> >>" as a subsequence.
Unlike ordinary literal character data, the characters "B<< < >>" and
"B<&>" are not metacharacters here.  Such data stands for itself when
it appears within a CDATA section.

As with ordinary literal character data (see above), this data is meant
to be taken literally only after line endings have been canonicalised.
Also, as with ordinary literal character data, two instances of this
production should not abut.

=cut

our $xml10_cdata_rx = qr/(?:
	\]?(?!\])$xml10_char_rx
	|\]{2,}(?![\>\]])$xml10_char_rx
)*\]*/xo;

=item $xml10_cdstart_rx

=item $xml10_cdend_rx

The fixed strings "B<< <![CDATA[ >>" and "B<< ]]> >>" which begin and
finish a CDATA section.

=cut

our $xml10_cdstart_rx = qr/<!\[CDATA\[/;
our $xml10_cdend_rx = qr/\]\]>/;

=item $xml10_cdsect_rx

A CDATA section.  This consists of "B<< <![CDATA[ >>", literal character
data with metacharacters disabled, and "B<< ]]> >>".

=cut

# Note: using the $xml10_cdata_rx regexp (from above) here would be much
# less efficient than this use of (?>...).  It would also run into the
# perl bug described in L</BUGS>.

our $xml10_cdsect_rx = qr/(?><!\[CDATA\[$xml10_char_rx*?\]\]>)/o;

=back

=head2 Tags

=over

=item $xml10_attvalue_rx

A quoted attribute value.  This consists of acceptable characters
other than "B<< < >>", "B<&>", and the quote character, interspersed
with references, surrounded by matching "B<">" or "B<'>" quotes.
The well-formedness constraints of references apply, and additionally
the replacement text of any referenced entities must not contain any "B<<
< >>" characters, and it is not permitted to refer to external entities.

=cut

our $xml10_attvalue_rx = qr/"(?:(?![<&"])$xml10_char_rx|$xml10_reference_rx)*"
			   |'(?:(?![<&'])$xml10_char_rx|$xml10_reference_rx)*'
			   /xo;

=item $xml10_attribute_rx

A complete attribute, consisting of name, equals sign, and quoted value.
The well-formedness constraints of attribute values (pertaining to
references) apply.

=cut

our $xml10_attribute_rx = qr/$xml10_name_rx$xml10_eq_rx$xml10_attvalue_rx/o;

=item $xml10_stag_rx

A start-tag, used to begin an element.  This consists of "B<< < >>",
the element type name, whitespace-separated list of attributes, and "B<<
> >>".  The well-formedness constraints of attribute values (pertaining
to references) apply.  There is also a well-formedness constraint that
attribute names must be unique within the tag.

=cut

our $xml10_stag_rx = qr#<$xml10_name_rx
			(?:$xml10_s_rx$xml10_attribute_rx)*
			$xml10_s_rx?>#xo;

=item $xml10_etag_rx

An end-tag, used to finish an element.  This consists of "B<< </ >>",
the element type name, and "B<< > >>".

=cut

our $xml10_etag_rx = qr#</$xml10_name_rx$xml10_s_rx?>#o;

=item $xml10_emptyelemtag_rx



( run in 0.477 second using v1.01-cache-2.11-cpan-d8267643d1d )