-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added grammar invoker as well as cssSelector grammar file
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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)); | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
} |