Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented CssSelector grammar in Scss grammar

  • Loading branch information...
commit 73cfa000736042ffe9c5dcfff2c8ad23d81c79f2 1 parent 75a0f0c
@bmavity authored
View
2  examples/grammarInvoker.js
@@ -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));
});
View
52 src/cssSelector.ometa
@@ -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('') },
@@ -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('') }
@@ -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 }
@@ -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('');
};
};
View
20 src/index.js
@@ -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);
View
27 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 }); },
@@ -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;
@@ -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() {
Please sign in to comment.
Something went wrong with that request. Please try again.