Skip to content

Commit

Permalink
Implemented CssSelector grammar in Scss grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
bmavity committed Sep 28, 2010
1 parent 75a0f0c commit 73cfa00
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
2 changes: 1 addition & 1 deletion examples/grammarInvoker.js
Expand Up @@ -38,6 +38,6 @@ var parse = function(scssFile, callback) {


module.exports.parse = parse;
parse('a:nth-child(2 3)', function(err) {
parse(':not(h1, h2, h3)', function(err) {
sys.puts(sys.inspect(err, true, null));
});
52 changes: 40 additions & 12 deletions src/cssSelector.ometa
Expand Up @@ -65,8 +65,8 @@ ometa CssSelector <: Parser {
CDC = '-' '-' '>',


selectors_group = selector:pre comma_separated_selector*:post -> { self.add(pre + post.join('')); self },
comma_separated_selector = COMMA:com S*:spacing selector:sel -> { com + spacing.join('') + sel },
selectors_group = selector:pre comma_separated_selector* -> { self.addSelector(pre); self },
comma_separated_selector = COMMA:com S*:spacing selector:sel -> { self.addCommaSeparatedSelector(com + spacing.join(''), sel) },
selector = simple_selector_sequence:sim (combined_sequence)*:additional -> { sim + additional.join('') }
// Css Hack
| combined_sequence*:comb -> { comb.join('') },
Expand All @@ -77,8 +77,7 @@ ometa CssSelector <: Parser {
| TILDE:t S+ -> { t + ' ' }
| TILDE:t -> { t }
| S+:spacing -> { spacing.join('') },
combined_sequence = combinator:comb simple_selector_sequence:sel -> { comb + sel }
| combinator+:comb simple_selector_sequence:sel -> { comb.join('') + sel },
combined_sequence = combinator+:comb simple_selector_sequence:sel -> { comb.join('') + sel },
non_namespaced_selector = (HASH | class | attrib | negation | pseudo):sel -> { sel },
simple_selector_sequence = namespace_prefix:pre '*' non_namespaced_selector*:post -> { pre + '*' + post.join('') }
| namespace_prefix:pre element_name:ele non_namespaced_selector*:post -> { pre + ele + post.join('') }
Expand All @@ -105,7 +104,7 @@ ometa CssSelector <: Parser {
| ':' (functional_pseudo | IDENT):i -> { ':' + i },
functional_pseudo = FUNCTION:f S* full_expression:e ')' -> { f + e + ')' }
// Css Hack for :-moz-any(...)
| FUNCTION:f S* selectors_group:sel ')' -> { f + sel + ')' },
| FUNCTION:f S* selectors_group:sel ')' -> { f + sel.toString() + ')' },

expression_content = (PLUS | '-' | PERCENTAGE | DIMENSION | NUMBER | STRING | IDENT):e -> { e },
expression = expression_content:ec S+ expression:e -> { ec + ' ' + e }
Expand All @@ -116,18 +115,47 @@ ometa CssSelector <: Parser {

//negation_arg = (type_selector | universal | HASH | class | attrib | pseudo):na -> { na }
//Technically not allowed, but here for scss compatibility
negation_arg = (selectors_group):na -> { na }
negation_arg = selectors_group:sg -> { sg.toString() }
}

CssSelector.initialize = function() {
var s;

this.toString = function() {
return s;
var hasBeenStringified = false,
self = this;

this.selector;
this.selectors = [];
this.commaSeparatedSelectors = [];

var resetFields = function() {
self.selector = null;
self.selectors = [];
self.commaSeparatedSelectors = [];
hasBeenStrigified = false;
};

this.addSelector = function(sel) {
if(hasBeenStringified) {
resetFields();
}
this.selector = sel;
};

this.addCommaSeparatedSelector = function(commaAndSpacing, selector) {
if(hasBeenStringified) {
resetFields();
}
this.selectors.push(selector);
this.commaSeparatedSelectors.push({
commaAndSpacing: commaAndSpacing,
selector: selector
});
};

this.add = function(st) {
s = st;
this.toString = function() {
hasBeenStringified = true;
return this.selector + this.commaSeparatedSelectors.map(function(csSel) {
return csSel.commaAndSpacing + csSel.selector;
}).join('');
};
};

Expand Down
20 changes: 12 additions & 8 deletions src/index.js
Expand Up @@ -6,19 +6,23 @@ var sys = require('sys'),

var getParser = function(callback) {
if(!createdParser) {
fs.readFile(__dirname + '/scss.ometa', function(err, contents) {
if(err) {
callback(err);
} else {
ometa.createParser(contents.toString(), function(err, parser) {
fs.readFile(__dirname + '/CssSelector.ometa', function(err, cssSelectorContents) {
ometa.createParser(cssSelectorContents.toString(), function(err, cssSelectorParser) {
fs.readFile(__dirname + '/scss.ometa', function(err, contents) {
if(err) {
callback(err);
} else {
createdParser = parser;
callback(null, createdParser);
ometa.createParser(contents.toString(), function(err, parser) {
if(err) {
callback(err);
} else {
createdParser = parser;
callback(null, createdParser);
}
});
}
});
}
});
});
} else {
callback(null, createdParser);
Expand Down
27 changes: 11 additions & 16 deletions src/scss.ometa
@@ -1,17 +1,12 @@
ometa Scss <: Parser {
selectorCharacters = ('-' | letterOrDigit)+:chars -> chars.join(''),
selector = ('#' | '.' | ':'):pre selectorCharacters:sel -> (pre + sel)
| selectorCharacters:sel -> sel,
spacedSelector = space+ selector:sel -> (' ' + sel),
fullSelector = selector:first (spacedSelector | selector)*:rest -> { self.addSelector(first + rest.join('')); },
secondarySelector = "," spaces selector:first (spacedSelector | selector)*:rest -> { self.addAdditionalSelector(first + rest.join('')); },
selector = foreign(CssSelector, 'selectors_group'):sel -> { self.addSelector(sel) },

propertyValue = '$' letterOrDigit+:val -> ('$' + val.join(''))
| ('#' | '.' | ',' | '-' | space | letterOrDigit)+:val -> val.join(''),
property = spaces ('-' | letter)+:prop ":" spaces propertyValue:val ";" -> { self.addProperty({ name: prop.join(''), val: val }); },

blockBody = "{" (mixinInclude | property | scssBlock)* "}" -> { self.endBlock(); },
scssBlock = spaces fullSelector secondarySelector* blockBody,
scssBlock = spaces selector blockBody,

scssVariable = "$" letter+:name ":" spaces propertyValue:val ";" -> { self.addVariable({ name: name.join(''), val: val }); },

Expand Down Expand Up @@ -42,11 +37,8 @@ Scss.initialize = function() {
};
};

this.addAdditionalSelector = function(selector) {
currentBlock.selectors.push(selector);
};

this.addMixin = function(mixinName) {
console.log('mixin:' + mixinName);
var block = createBlock();
mixins[mixinName] = block;
currentBlock = block;
Expand All @@ -61,11 +53,14 @@ Scss.initialize = function() {
};

this.addSelector = function(sel) {
var block = createBlock();
block.selector = sel;
block.selectors = [];
currentBlock.blocks.push(block);
currentBlock = block;
var block;
if(sel.selector) {
block = createBlock();
block.selector = sel.selector;
block.selectors = sel.selectors;
currentBlock.blocks.push(block);
currentBlock = block;
}
};

this.endBlock = function() {
Expand Down

0 comments on commit 73cfa00

Please sign in to comment.