scss.ometa 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ometa Scss <: Parser {
  2. selector = foreign(CssSelector, 'selectors_group'):sel -> { self.addSelector(sel) },
  3. propertyValue = '$' letterOrDigit+:val -> ('$' + val.join(''))
  4. | ('#' | '.' | ',' | '-' | space | letterOrDigit)+:val -> val.join(''),
  5. property = spaces ('-' | letter)+:prop ":" spaces propertyValue:val ";" -> { self.addProperty({ name: prop.join(''), val: val }); },
  6. blockBody = "{" (mixinInclude | property | scssBlock)* "}" -> { self.endBlock(); },
  7. scssBlock = spaces selector blockBody,
  8. scssVariable = "$" letter+:name ":" spaces propertyValue:val ";" -> { self.addVariable({ name: name.join(''), val: val }); },
  9. mixinName = spaces ('-' | '_' | letterOrDigit)+:name -> name.join(''),
  10. mixinInclude = "@include" mixinName:name ";" -> { self.addInclude(name); },
  11. mixinDeclaration = "@mixin" mixinName:name -> { self.addMixin(name); },
  12. scssMixin = mixinDeclaration blockBody,
  13. scssFile = (scssVariable | scssMixin | scssBlock)* -> { self.getFile(); }
  14. }
  15. Scss.initialize = function() {
  16. var blocks = [],
  17. mixins = {},
  18. variables = {},
  19. file,
  20. currentBlock;
  21. var createBlock = function() {
  22. var parentBlock = currentBlock;
  23. return {
  24. blocks: [],
  25. includes: [],
  26. properties: [],
  27. end: function() {
  28. currentBlock = parentBlock;
  29. }
  30. };
  31. };
  32. this.addMixin = function(mixinName) {
  33. var block = createBlock();
  34. mixins[mixinName] = block;
  35. currentBlock = block;
  36. };
  37. this.addProperty = function(prop) {
  38. currentBlock.properties.push(prop);
  39. };
  40. this.addInclude = function(include) {
  41. currentBlock.includes.push(include);
  42. };
  43. this.addSelector = function(sel) {
  44. var block;
  45. if(sel.selector) {
  46. block = createBlock();
  47. block.selector = sel.selector;
  48. block.selectors = sel.selectors;
  49. currentBlock.blocks.push(block);
  50. currentBlock = block;
  51. }
  52. };
  53. this.endBlock = function() {
  54. currentBlock.end();
  55. };
  56. this.addVariable = function(variable) {
  57. variables[variable.name] = variable.val;
  58. };
  59. this.getFile = function() {
  60. return file;
  61. };
  62. file = {
  63. blocks: [],
  64. end: function() {
  65. currentBlock = file;
  66. },
  67. getMixin: function(mixinName) {
  68. return mixins[mixinName];
  69. },
  70. getValue: function(variableName) {
  71. return variables[variableName];
  72. }
  73. };
  74. currentBlock = file;
  75. };
  76. Scss