Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added grammar invoker as well as cssSelector grammar file

  • Loading branch information...
commit 17c9390126026037c8f8027d4e9551d7c385c692 1 parent 63e9250
@bmavity authored
Showing with 132 additions and 0 deletions.
  1. +43 −0 examples/grammarInvoker.js
  2. +89 −0 src/cssSelector.ometa
View
43 examples/grammarInvoker.js
@@ -0,0 +1,43 @@
+var sys = require('sys'),
+ fs = require('fs'),
+ ometa = require('ometa'),
+ createdParser;
+
+var getParser = function(callback) {
+ if(!createdParser) {
+ fs.readFile(__dirname + '/../src/cssSelector.ometa', function(err, contents) {
+ if(err) {
+ callback(err);
+ } else {
+ ometa.createParser(contents.toString(), function(err, parser) {
+ if(err) {
+ callback(err);
+ } else {
+ createdParser = parser;
+ callback(null, createdParser);
+ }
+ }, 'selectors_group');
+ }
+ });
+ } else {
+ callback(null, createdParser);
+ }
+};
+
+var parse = function(scssFile, callback) {
+ getParser(function(err, parser) {
+ if(err) {
+ callback(err);
+ } else {
+ parser.parse(scssFile, function(err, css) {
+ sys.puts(css.toString());
+ });
+ }
+ });
+};
+
+
+module.exports.parse = parse;
+parse('a:nth-child(2)', function(err) {
+ sys.puts(sys.inspect(err, true, null));
+});
View
89 src/cssSelector.ometa
@@ -0,0 +1,89 @@
+ometa CssSelector <: Parser {
+ crChar = '\r',
+ ffChar = '\f',
+ nlChar = '\n',
+ tabChar = '\t',
+ lineEnding = crChar | ffChar | nlChar,
+ tabOrLineEnding = tabChar | lineEnding,
+
+
+ ident = '-' nmstart nmchar*
+ | nmstart nmchar*,
+ name = nmchar+,
+ nmstart = '_' | letter | nonascii | escape,
+ nonascii = '',
+ unicode = '',
+ escape = unicode | '',
+ nmchar = '_' | '-' | letterOrDigit | nonascii | escape,
+ num = digit+: d -> d.join('')
+ | digit* '.' digit+,
+ string = string1 | string2,
+ string1 = '"' (~(lineEnding | '"') | '\\' nl | nonascii | escape)* '"',
+ string2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)* '\'',
+ invalid = invalid1 | invalid2,
+ invalid1 = '"' (~(lineEnding | '"') | '\\' nl | nonascii | escape)*,
+ invalid2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*,
+ nl = crChar nlChar
+ | lineEnding,
+ w = (' ' | tabOrLineEnding)*,
+ D = 'd' | 'D',
+ E = 'e' | 'E',
+ N = 'n' | 'N',
+ O = 'o' | 'O',
+ T = 't' | 'T',
+ V = 'v' | 'V',
+
+
+ S = ' ' | tabOrLineEnding,
+ INCLUDES = '~' '=',
+ DASHMATCH = '|' '=',
+ PREFIXMATCH = '^' '=',
+ SUFFIXMATCH = '$' '=',
+ SUBSTRINGMATCH = '*' '=',
+ IDENT = ident,
+ STRING = string,
+ FUNCTION = ident '(',
+ NUMBER = num,
+ HASH = '#' name,
+ PLUS = w '+',
+ GREATER = w '>',
+ COMMA = w ',',
+ TILDE = w '~',
+ NOT = ':' N O T '(',
+ ATKEYWORD = '@' ident,
+ INVALID = invalid,
+ PERCENTAGE = num '%',
+ DIMENSION = num ident,
+ CDO = '<' '!' '-' '-',
+ CDC = '-' '-' '>',
+
+
+ selectors_group = selector (COMMA S* selector)*,
+ selector = simple_selector_sequence (combinator simple_selector_sequence)*,
+ combinator = PLUS S*
+ | GREATER S*
+ | TILDE S*
+ | S+,
+ simple_selector_sequence = (type_selector | universal) (HASH | class | attrib | pseudo | negation)*
+ | (HASH | class | attrib | pseudo | negation)+,
+ type_selector = namespace_prefix element_name
+ | element_name,
+ namespace_prefix = (IDENT | '*') '|'
+ | '|',
+ element_name = IDENT,
+ universal = namespace_prefix '*'
+ | '*',
+ class = '.' IDENT,
+
+ attrib = '[' S* possible_namespaced_attrib ']',
+ possible_namespaced_attrib = namespace_prefix ident_with_possible_postfix
+ | ident_with_possible_postfix,
+ ident_with_possible_postfix = IDENT S* (PREFIXMATCH | SUFFIXMATCH | SUBSTRINGMATCH | '=' | INCLUDES | DASHMATCH) S* (IDENT | STRING) S*
+ | IDENT S*,
+ pseudo = ':' ':' (IDENT | functional_pseudo)
+ | ':' (IDENT | functional_pseudo),
+ functional_pseudo = FUNCTION S* expression ')' -> 'h',
+ expression = ((PLUS | '-' | DIMENSION | NUMBER | STRING | IDENT) S*)+,
+ negation = NOT S* negation_arg S* ')',
+ negation_arg = type_selector | universal | HASH | class | attrib | pseudo
+}
Please sign in to comment.
Something went wrong with that request. Please try again.