Skip to content

Commit

Permalink
Parser update
Browse files Browse the repository at this point in the history
  • Loading branch information
tapmodo committed Nov 1, 2015
1 parent 79d8843 commit bc38b0e
Show file tree
Hide file tree
Showing 2 changed files with 426 additions and 382 deletions.
108 changes: 56 additions & 52 deletions lib/ldif.pegjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
// A peg.js grammar for parsing LDIF based on RFC2849
// @author Kelly Hallman <[email protected]>
// @copyright 2015 Tapmodo Interactive LLC
// @license MIT

//-------------------------------------------------------
// JAVASCRIPT INITIALIZATION ----------------------------
// This scope will be available to the parser

{
var fs = require('fs');
var type = require('./types');

function base64(value){
return new Buffer(value,'base64').toString();
}

var File = function(value){
this.type = 'file';
this.url = value;
var _pluck = function(list,attr){
return list.map(function(cv){
return cv[attr];
});
};

var _pluck = function(list,attr){
return list.map(function(cv){
return cv[attr];
});
};

}

//-------------------------------------------------------
// INITIAL PARSING RULE ---------------------------------
// Since it's the first rule, it will start the parsing

start
= changes:ldif_schema { return changes; }

Expand All @@ -40,6 +44,20 @@ version_spec "version"
= 'version:' FILL ver:version_number SEP
{ return parseInt(ver); }

//-------------------------------------------------------
// LDIF CONTENT SPEC ------------------------------------
// Schema for content records

ldif_content
= whitespace* ver:(version_spec)? entries:ldif_entry+ {
return { version: ver, type: 'content', entries: entries };
}

ldif_entry "entry"
= dn:dn_spec SEP attribs:attrs_spec whitespace* {
return { dn: dn, attributes: attribs };
}

//-------------------------------------------------------
// LDIF CHANGES -----------------------------------------
// Defines the general schema for changetype records
Expand Down Expand Up @@ -107,34 +125,20 @@ change_modify

mod_spec
= type:("add"/"delete"/"replace") ":"
FILL attr:attributeDescription SEP
FILL attr:AttributeDescription SEP
values:attr_line* "-" SEP
{
values = _pluck(values,'value');
return { type: type, attribute: attr, values: values };
}

//-------------------------------------------------------
// LDIF CONTENT SPEC ------------------------------------
// Schema for content records

ldif_content
= whitespace* ver:(version_spec)? entries:ldif_entry+ {
return { version: ver, type: 'content', entries: entries };
}

ldif_entry "entry"
= dn:dn_spec SEP attribs:attrs_spec whitespace* {
return { dn: dn, attributes: attribs };
}

//-------------------------------------------------------
// DN Formats -------------------------------------------
// Distinguished names; these start off each record

dn_spec "DN"
= comment_line* "dn:" FILL dn:distinguishedName { return dn; }
/ comment_line* "dn::" FILL dn:base64_distinguishedName { return base64(dn); }
/ comment_line* "dn::" FILL dn:base64_distinguishedName { return type.Base64(dn); }

distinguishedName "distinguishedName"
= SAFE_STRING
Expand All @@ -153,20 +157,20 @@ attr_line
= comment_line* attr:attrval_spec { return attr; }

attrval_spec "attribute value line"
= attr:attributeDescription "::" FILL val:base64_value_spec SEP? {
return { attribute: attr, value: base64(val) };
= attr:AttributeDescription "::" FILL val:base64_value_spec SEP? {
return { attribute: attr, value: type.Base64(val) };
}
/ attr:attributeDescription ":<" FILL val:value_spec SEP? {
return { attribute: attr, value: new File(val) };
/ attr:AttributeDescription ":<" FILL val:value_spec SEP? {
return { attribute: attr, value: type.File(val) };
}
/ attr:attributeDescription ":" FILL val:value_spec SEP? {
/ attr:AttributeDescription ":" FILL val:value_spec SEP? {
return { attribute: attr, value: val };
}
/ attr:attributeDescription (":<"/":") FILL SEP {
/ attr:AttributeDescription (":<"/":") FILL SEP {
return { attribute: attr, value: null };
}

attributeDescription "attribute description"
AttributeDescription "attribute description"
= attr:AttributeType opts:(";" options)? {
if (!opts) return attr;
opts.shift();
Expand All @@ -176,7 +180,7 @@ attributeDescription "attribute description"
return { name: attr, options: opts };
}

AttributeType
AttributeType "attribute Type"
= oid:LDAP_OID { return new OID(oid); }
/ $(ALPHA AttrTypeChars*)

Expand All @@ -190,11 +194,11 @@ LDAP_OID "OID"
// ATTRIBUTE OPTIONS ------------------------------------
// These are somewhat rare, but in the spec

options
options "attribute options"
= $ (option ";" options)
/ $ option

option
option "attribute option"
= (AttrTypeChars+)

//-------------------------------------------------------
Expand All @@ -209,49 +213,49 @@ value_recurse "continuation"
= left:$SAFE_CHAR+ SEP SPACE right:value_recurse { return left + right; }
/ $ SAFE_CHAR+

base64_value_spec
base64_value_spec "base64-encoded value"
= left:BASE64_STRING SEP SPACE right:base64_value_spec { return left + right; }
/ $ BASE64_STRING

//-------------------------------------------------------
// AGGREGATES AND HELPERS -------------------------------
// Base types, character classes, and aggregates

whitespace "whitespace"
whitespace "WHITESPACE"
= comment_line
/ [\s]* SEP

comment_line "comment"
comment_line "COMMENT"
= "#" (!SEP .)* SEP

FILL "space fill"
FILL "FILL"
= (SPACE)*

SPACE "single space"
SPACE "SPACE"
= [\x20]

DIGIT "digit"
DIGIT "DIGIT"
= $ [0-9]

ALPHA "alpha"
ALPHA "ALPHA"
= $ [a-zA-Z]

BASE64_STRING
BASE64_STRING "BASE64 STRING"
= $ BASE64_CHAR*

BASE64_CHAR
BASE64_CHAR "BASE64 CHAR"
= $ [\x2B\x2F\x30-\x39\x3D\x41-\x5A\x61-\x7A]

SAFE_STRING "safestring"
SAFE_STRING "SAFE STRING"
= $(SAFE_INIT_CHAR SAFE_CHAR*)

SAFE_INIT_CHAR
SAFE_INIT_CHAR "SAFE INITIALIZER"
= $ [\x01-\x09\x0B-\x0C\x0E-\x1F\x21-\x39\x3B\x3D-\x7F]

SAFE_CHAR "SAFE CHAR"
= $ [\x01-\x09\x0B-\x0C\x0E-\x7F]

SEP "newline"
SEP "NEWLINE"
= "\r\n"
/ "\n"

Loading

0 comments on commit bc38b0e

Please sign in to comment.