Skip to content

Commit

Permalink
Implement prototype feature described in issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
padolsey committed Apr 1, 2013
1 parent d762da1 commit 32f6bf9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
22 changes: 16 additions & 6 deletions src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,12 @@ LSeries

return singleA;
}
case 'Directive': {
return ['IncGroup', [singleA, singleB]];
}
case 'Prototype':
case 'Directive':
case 'Attribute': {
return ['IncGroup', [singleA, singleB]];
}
}
console.warn(':::', singleA, singleB)
return 'ERROR';
}
/ '(' _ head:MSeries body:(_ '/' _ MSeries)* _ ')' selector:selectorRepeatableComponent* _ tail:ExcGroupRHS? {
Expand Down Expand Up @@ -265,6 +263,7 @@ ChildrenDeclaration
*/
Single
= Attribute
/ PrototypeDefinition
/ Element
/ Text
/ Directive
Expand All @@ -277,13 +276,24 @@ Element
return ['Element', [s,[]]];
}

/**
* PrototypeDefinition
*/
PrototypeDefinition
= name:PrototypeName [ \t]* '=' [ \t]* s:SingleSelector {
return ['Prototype', [name, s]];
}

PrototypeName
= a:[a-zA-Z_$] b:[a-zA-Z0-9$_-]* { return a+b.join(''); }

/**
* Selector
*/
Selector "Selector"
= singleSelector
= SingleSelector

singleSelector
SingleSelector
= s:(selectorTag selectorRepeatableComponent*) {
s[1].unshift(s[0]);
return s[1];
Expand Down
40 changes: 30 additions & 10 deletions src/siml.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var siml = typeof module != 'undefined' && module.exports ? module.exports : win
'use strict';

var push = [].push;
var splice = [].splice;

var DEFAULT_TAG = 'div';
var DEFAULT_INDENTATION = ' ';
Expand Down Expand Up @@ -139,6 +140,7 @@ var siml = typeof module != 'undefined' && module.exports ? module.exports : win
this.attrs = [];
this.classes = [];
this.pseudos = [];
this.prototypes = objCreate(parentElement && parentElement.prototypes || null);

this.isSingular = false;
this.multiplier = 1;
Expand All @@ -163,16 +165,27 @@ var siml = typeof module != 'undefined' && module.exports ? module.exports : win

make: function() {

var selector = this.selector;
var selector = this.selector.slice();
var selectorPortionType;
var selectorPortion;
var protoSelector;

for (var i = 0, l = selector.length; i < l; ++i) {
selectorPortionType = selector[i][0];
selectorPortion = selector[i][1];
switch (selectorPortionType) {
case 'Tag':
this.tag = selectorPortion; break;
this.tag = selectorPortion;
// Prevent recursion for checking if we've already discovered our
// proto-selector:
if (!protoSelector && (protoSelector = this.prototypes[this.tag])) {
protoSelector = protoSelector.slice();
l += protoSelector.length;
protoSelector.unshift(0);
protoSelector.unshift(i + 1);
splice.apply(selector, protoSelector);
}
break;
case 'Id':
this.id = selectorPortion; break;
case 'Attr':
Expand Down Expand Up @@ -396,15 +409,22 @@ var siml = typeof module != 'undefined' && module.exports ? module.exports : win

} else {
for (i = 0; i < cl; ++i) {
var child = children[i][1];
var childType = children[i][0];
if (childType === 'Element') {
this.processElement(children[i][1]);
} else if (childType === 'IncGroup') {
this.processIncGroup(children[i][1]);
} else if (childType === 'ExcGroup') {
throw new Error('SIML: Found ExcGroup in unexpected location');
} else {
this.processProperty(childType, children[i][1]);
switch (childType) {
case 'Element':
this.processElement(child);
break;
case 'Prototype':
this.prototypes[child[0]] = child[1];
break;
case 'IncGroup':
this.processIncGroup(child);
break;
case 'ExcGroup':
throw new Error('SIML: Found ExcGroup in unexpected location');
default:
this.processProperty(childType, child);
}
}
}
Expand Down

0 comments on commit 32f6bf9

Please sign in to comment.