Skip to content

Instantly share code, notes, and snippets.

@raiph
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raiph/63508a5f4ade000acc7f to your computer and use it in GitHub Desktop.
Save raiph/63508a5f4ade000acc7f to your computer and use it in GitHub Desktop.
CPAN::MIRRORED_BY::Grammar
# For parsing http://www.cpan.org/MIRRORED.BY
use v6;
#===============================================================
=Setup
#===============================================================
my $MIRRORED_BY-input-file = '/home/raiph/work/CPAN-MIRRORED_BY-Grammar/MIRRORED.BY_for_testing';
my $this-module = "CPAN::MIRRORED_BY::Grammar";
# See =Warnings block toward end of this module to see strings these are initialized to:
my ($updated-warning, $explanation, $explanation-warning, $dst_GENERIC_MATCH-warning);
grammar CPAN::MIRRORED_BY::Grammar { ... };
#===============================================================
=Mainline (run if this module is run as the mainline script)
#===============================================================
sub MAIN ($file = $MIRRORED_BY-input-file) {
say CPAN::MIRRORED_BY::Grammar.parsefile($file);
}
#===============================================================
=Grammar
#===============================================================
grammar CPAN::MIRRORED_BY::Grammar {
token TOP { <.initial-comment-block> <mirror>* }
token initial-comment-block { <updated> <explanation> }
token updated { '# Last updated' \N* \n || { warn $updated-warning } }
token explanation { $explanation || { warn $explanation-warning } }
token ws-or-comments { \h | \v | '#' \N* \n }
token mirror { <.ws-or-comments>* <domain> ':' <.ws-or-comments>*
[ <data-item> <.ws-or-comments>* ]* }
token domain { <-[:]>* }
proto token data-item { * }
# frequency = "daily|twice daily" or like "1d|24h|12h|8h|6h"
# or "instant" if you use "instant mirroring"
rule data-item:frequency { 'frequency' '=' :!s <frequency> }
token frequency { \N* }
# dst_ftp = "ftp://your.host.name:/CPAN/mirror/directory/"
rule data-item:dst_ftp { 'dst_ftp' '=' :!s <dst_ftp> }
token dst_ftp { \N* }
# dst_http = "http://your.host.name:/CPAN/mirror/directory/"
rule data-item:dst_http { 'dst_http' '=' :!s <dst_http> }
token dst_http { \N* }
# dst_rsync = "rsync://your.host.name/CPAN/"
rule data-item:dst_rsync { 'dst_rsync' '=' :!s <dst_rsync> }
token dst_rsync { \N* }
# dst_location = "city, (area,)? country, continent (lat long)"
rule data-item:dst_location { 'dst_location' '=' :!s <dst_location> }
token dst_location { \N* }
# dst_organisation = "full organisation name"
rule data-item:dst_organisation { 'dst_organisation' '=' :!s <dst_organisation> }
token dst_organisation { \N* }
# dst_timezone = "[+-]n(.dd)?"
rule data-item:dst_timezone { 'dst_timezone' '=' :!s <dst_timezone> }
token dst_timezone { \N* }
# dst_bandwidth = "Approximate connection speed in Mb/s"
rule data-item:dst_bandwidth { 'dst_bandwidth' '=' :!s <dst_bandwidth> }
token dst_bandwidth { \N* }
# dst_contact = "email.address.to.contact@for.this.mirror"
rule data-item:dst_contact { 'dst_contact' '=' :!s <dst_contact> }
token dst_contact { \N* }
# dst_src = "host.that.you.mirror.from"
rule data-item:dst_src { 'dst_src' '=' :!s <dst_src> }
token dst_src { \N* }
# dst_loadbal = "Y" or "N" Join the DNS load balancing pool for ftp.cpan.org
rule data-item:dst_loadbal { 'dst_loadbal' '=' :!s <dst_loadbal> }
token dst_loadbal { \N* }
# dst_notes = "Server is load balanced over 2 machines using an Arrowpoint CS-800"
# This data-item syntax was NOT in the "Explanation of the syntax" section of
# the MIRRORED_BY_for_testing file but WAS in the data sections in the file.
rule data-item:dst_notes { 'dst_notes' '=' :!s <dst_notes> }
token dst_notes { \N* }
# A catchall generic data-item in case a new dst_* data-item appears.
# Generates a warning but keeps on trucking.
rule data-item:dst_GENERIC-MATCH {
'dst_' <-[=\n]>* '=' :!s <dst_GENERIC-MATCH> \n
{ warn $dst_GENERIC_MATCH-warning }
}
token dst_GENERIC-MATCH { \N* }
}
#===============================================================
=Warnings
#===============================================================
$updated-warning = qq:to/END/;
Module $this-module failed to parse an expected "# Last updated"
line at the top of the MIRRORED_BY input file $MIRRORED_BY-input-file.
Has the the syntax changed? If so this module may need updating.
For now we'll fudge the update date to be today's date and continue.
END
$explanation = q:to/END/;
#
# Explanation of the syntax:
#
# your.domain.name:
# frequency = "daily|twice daily" or like "1d|24h|12h|8h|6h"
# or "instant" if you use "instant mirroring"
# dst_ftp = "ftp://your.host.name:/CPAN/mirror/directory/"
# dst_http = "http://your.host.name:/CPAN/mirror/directory/"
# dst_rsync = "rsync://your.host.name/CPAN/"
# dst_location = "city, (area,)? country, continent (lat long)"
# dst_organisation = "full organisation name"
# dst_timezone = "[+-]n(.dd)?"
# dst_bandwidth = "Approximate connection speed in Mb/s"
# dst_contact = "email.address.to.contact@for.this.mirror"
# dst_src = "host.that.you.mirror.from"
# dst_loadbal = "Y" or "N" Join the DNS load balancing pool for ftp.cpan.org
#
# Notes:
# - This file is UTF-8 encoded.
# - dst_location :
# - The "(lat long)" is required. It is the latitude and longtitude
# in degrees.minutes_IN_DECIMAL (45 minutes = 0.75) ; North and East
# are positive.
# To find your location, use the yellow button on the CPAN map :
# http://mirrors.cpan.org/map.html
# - The "area" is optional. It is the state (United States), county,
# prefecture, district etc.
# - The continent is : Africa, Asia, Europe, North America,
# Oceania or South America.
# - dst_organisation :
# - The language is usually "native (english)" but for example
# in Canada, well, is native English or French?
# - The contact email address is obfuscated.
END
$explanation-warning = qq:to/END/;
Module $this-module failed to find an EXACT match between
\$explanation and an expected "# Explanation of the syntax" block
of comments at the top of the MIRRORED_BY input file $MIRRORED_BY-input-file.
Has the the syntax changed? If so this module may need updating.
END
$dst_GENERIC_MATCH-warning = qq:to/END/;
This line in the MIRRORED_BY input file $MIRRORED_BY-input-file:
----------
$<data-item>
----------
contains new $<dst_GENERIC-MATCH> syntax that this module may not handle
properly. To quiet this warning, update this $this-module module.
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment