Skip to content

Instantly share code, notes, and snippets.

@think49
Created February 15, 2011 13:52
Show Gist options
  • Save think49/827547 to your computer and use it in GitHub Desktop.
Save think49/827547 to your computer and use it in GitHub Desktop.
abnf.js : RFC5234 規定の ABNF 文字列を ECMAScript5 の RegExp 準拠の文字列に変換する
/**
* abnf.js
*
* [RFC 5234] Augmented BNF for Syntax Specifications: ABNF
* http://www.ietf.org/rfc/rfc5234.txt
* http://www.cam.hi-ho.ne.jp/mendoxi/rfc/rfc5234j.html
*
* @version 0.1
* @author think49
*/
var ABNF;
ABNF = (function () {
function ABNFCoreRules () {
var ALPHA, BIT, CHAR, CR, LF, CRLF, CTL, DIGIT, DQUOTE, HEXDIG, HTAB, OCTET, SP, VCHAR, WSP, LWSP;
if (!(this instanceof ABNFCoreRules)) {
return new ABNFCoreRules;
}
/**
* [RFC 5234] B.1. Core Rules
*/
ALPHA = '[\u0041-\u005A\u0061-\u007A]'; // ; A-Z / a-z
BIT = '[01]';
CHAR = '[\u0001-\u007F]'; // ; any 7-bit US-ASCII character,
// ; excluding NUL
CR = '\u000D'; // ; carriage return
LF = '\u000A'; // ; linefeed
CRLF = '[' + CR + LF + ']'; // ; Internet standard newline
CTL = '[\u0000-\u001F\u007F]'; // ; controls
DIGIT = '[\u0030-\u0039]'; // ; 0-9
DQUOTE = '\u0022'; // ; " (Double Quote)
HEXDIG = '(?:' + DIGIT + '|[A-Z])';
HTAB = '\u0009'; // ; horizontal tab
OCTET = '[\u0000-\u00FF]'; // ; 8 bits of data
SP = '\u0020';
VCHAR = '[\u0021-\u007E]'; // ; visible (printing) characters
WSP = '[' + SP + HTAB + ']'; // ; white space
LWSP = '(?:' + WSP + '|' + CRLF + WSP + ')*'; // ; Use of this linear-white-space rule
// ; permits lines containing only white
// ; space that are no longer legal in
// ; mail headers and have caused
// ; interoperability problems in other
// ; contexts.
// ; Do not use when defining mail
// ; headers and use with caution in
// ; other contexts.
this.ALPHA = ALPHA;
this.BIT = BIT;
this.CHAR = CHAR;
this.CR = CR;
this.LF = LF;
this.CRLF = CRLF;
this.CTL = CTL;
this.DIGIT = DIGIT;
this.DQUOTE = DQUOTE;
this.HEXDIG = HEXDIG;
this.HTAB = HTAB;
this.OCTET = OCTET;
this.SP = SP;
this.VCHAR = VCHAR;
this.WSP = WSP;
this.LWSP = LWSP;
return this;
}
function ABNFSyntax () {
var coreRules, ALPHA, BIT, CRLF, DIGIT, DQUOTE, HEXDIG, VCHAR, WSP;
if (!(this instanceof ABNFSyntax)) {
return new ABNFSyntax;
}
coreRules = new ABNFCoreRules;
ALPHA = coreRules.ALPHA;
BIT = coreRules.BIT;
CRLF = coreRules.CRLF;
DIGIT = coreRules.DIGIT;
DQUOTE = coreRules.DQUOTE;
HEXDIG = coreRules.HEXDIG;
VCHAR = coreRules.VCHAR;
WSP = coreRules.WSP;
/**
* [RFC 5234] 4. ABNF Definition of ABNF
*/
rulename = ALPHA + '(?:' + ALPHA + '|' + DIGIT + '|-)*';
comment = ';(?:' + WSP + '|' + VCHAR + ')*' + CRLF;
c_nl = '(?:' + comment + '|' + CRLF + ')'; // ; comment or newline
c_wsp = '(?:' + WSP + '|' + c_nl + WSP + ')';
defined_as = '(?:' + c_wsp + ')*(?:' + ' = | = /)(?:' + c_wsp + ')*'; // ; basic rules definition and
// ; incremental alternatives
repeat = '(?:' + DIGIT + '+|' + DIGIT + '*\\*' + DIGIT + '*)';
element = '(?:' + rulename + '|' + group + '|' + option + '|' + char_val + '|' + num_val + '|' + prose_val + ')';
repetition = '(?:' + repeat + ')?' + element;
concatenation = repetition + '(?:(?:' + c_wsp + ')+' + repetition + ')*';
alternation = concatenation + '(?:(?:' + c_wsp + ')*|(?:' + c_wsp + ')*' + concatenation + ')*';
group = '\\((?:' + c_wsp + ')*' + alternation + '(?:' + c_wsp + ')*\\)';
option = '\\[(?:' + c_wsp + ')*' + alternation + '(?:' + c_wsp + ')*\\]';
elements = alternation + '(?:' + c_wsp + ')*';
rule = rulename + defined_as + elements + c_nl; // ; continues if next line starts
// ; with white space
rulelist = '(?:' + rule + '|' + '(?:' + c_wsp + ')*' + c_nl + ')+';
char_val = DQUOTE + '[\u0020\u0021\u0023-\u007E]*' + DQUOTE; // ; quoted string of SP and VCHAR
// ; without DQUOTE
num_val = '%(?:' + bin_val + '|' +dec_val + '|' +hex_val + ')';
bin_val = 'b' + BIT + '+(?:(?:\\.' + BIT + '+)+|-' + BIT + '+)?'; // ; series of concatenated bit values
// ; or single ONEOF range
dec_val = 'd' + DIGIT + '+(?:(?:\\.' + DIGIT + '+)+|-' + DIGIT + '+)?';
hex_val = 'x' + HEXDIG + '+(?:(?:\\.' + HEXDIG + '+)+|-' + HEXDIG + '+)?';
prose_val = '<[\u0020-\u003D\u003F-\u007E]*>'; // ; bracketed string of SP and VCHAR
// ; without angles
// ; prose description, to be used as
// ; last resort
return this;
}
function ABNF () {
if (!(this instanceof ABNF)) {
return new ABNF;
}
return this;
}
(function () {
this.coreRules = new ABNFCoreRules;
this.syntax = new ABNFSyntax;
this.toRegExpStringLiteral = function (abnfString) {
var result;
return result;
};
}).call(ABNF.prototype);
return ABNF;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment