forked from alexwarth/ometa-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
standalone.html
154 lines (139 loc) · 6.19 KB
/
standalone.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<html>
<head>
<script src="lib.js"></script>
<script src="ometa-base.js"></script>
<script src="parser.js"></script>
<script src="bs-js-compiler.js"></script>
<script src="bs-ometa-compiler.js"></script>
<script src="bs-ometa-optimizer.js"></script>
<script src="bs-ometa-js-compiler.js"></script>
<script src="ometa-script-tag.js"></script>
<script type="text/x-ometa-js">
ometa CssSelector <: Parser {
crChar = '\r',
ffChar = '\f',
nlChar = '\n',
tabChar = '\t',
lineEnding = crChar | ffChar | nlChar,
tabOrLineEnding = tabChar | lineEnding,
ident = '-' nmstart:s nmchar*:cs -> { '-' + s + cs.join('') }
| nmstart:s nmchar*:cs -> { s + cs.join('') },
name = nmchar+:n -> { n.join('') },
nmstart = ('_' | letter | nonascii | escape):n -> { n },
nonascii = '',
unicode = '',
escape = unicode | '',
nmchar = '_' | '-' | letterOrDigit | nonascii | escape,
num = digit+: d -> d.join('')
| digit* '.' digit+,
string = (string1 | string2):s -> { s },
//string1 = '\"' (~(lineEnding | '\"') | '\\' nl | nonascii | escape)*:s '\"' -> { '\"' + s.join('') + '\"' },
string1 = '"' letter*:s '"' -> { '"' + s.join('') + '"' },
string2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*:s '\'' -> { '\'' + s.join('') + '\'' },
//invalid = invalid1 | invalid2,
//invalid1 = '\"' (~(lineEnding | '\"') | '\\' nl | nonascii | escape)*,
//invalid2 = '\'' (~(lineEnding | '\'') | '\\' nl | nonascii | escape)*,
nl = crChar nlChar
| lineEnding,
D = 'd' | 'D',
E = 'e' | 'E',
N = 'n' | 'N',
O = 'o' | 'O',
T = 't' | 'T',
V = 'v' | 'V',
S = ' ' -> { ' ' }
| '\n' -> { '\n' },
INCLUDES = '~' '=' -> { '~=' },
DASHMATCH = '|' '=' -> { '|=' },
PREFIXMATCH = '^' '=' -> { '^=' },
SUFFIXMATCH = '$' '=' -> { '$=' },
SUBSTRINGMATCH = '*' '=' -> { '*=' },
IDENT = ident,
STRING = string:s -> { s },
FUNCTION = ident:i '(' -> { i + '(' },
NUMBER = num:n -> { n },
HASH = '#' name:n -> { '#' + n },
PLUS = S+ '+' -> { ' +' }
| '+' -> { '+' },
// First line of next selector is a Css Hack
GREATER = '>' '>' -> { '> >' }
| S+ '>' -> { ' >' }
| '>' -> { '>' },
COMMA = S+ ',' -> { ' ,' }
| ',' -> { ',' },
TILDE = S+ '~' -> { ' ~' }
| '~' -> { '~' },
NOT = ':' N O T '(' -> { ':not(' },
ATKEYWORD = '@' ident,
INVALID = invalid,
PERCENTAGE = num:n '%' -> { n + '%' },
DIMENSION = num:n ident:i -> { n + i },
CDO = '<' '!' '-' '-',
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 },
selector = simple_selector_sequence:sim (combined_sequence)*:additional -> { sim + additional.join('') }
// Css Hack
| combined_sequence*:comb -> { comb.join('') },
combinator = PLUS:p S+ -> { p + ' ' }
| PLUS:p -> { p }
| GREATER:g S+ -> { g + ' ' }
| GREATER:g -> { g }
| 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 },
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('') }
| '*' non_namespaced_selector*:post -> { '*' + post.join('') }
| element_name:ele non_namespaced_selector*:post -> { ele + post.join('') }
| non_namespaced_selector+:sels -> { sels.join('') }
// Css Hack
| expression:ex -> { ex },
namespace_prefix = ('*' | IDENT):pre '|' -> { pre + '|' }
| '|' -> { '|' },
// First line of the next selector is a Css Hack
element_name = IDENT:i '*' -> { i + '*' }
| IDENT:i -> { i },
class = '.' IDENT:i -> { '.' + i },
attrib = '[' S* possible_namespaced_attrib:att ']' -> { '[' + att + ']' },
possible_namespaced_attrib = namespace_prefix:pre ident_with_possible_postfix:post -> { pre + post }
| ident_with_possible_postfix:post -> { post },
ident_with_possible_postfix = IDENT:left S* attrib_match:match S* (IDENT | STRING):right S* -> { left + match + right }
| IDENT:i S* -> { i },
attrib_match = (PREFIXMATCH | SUFFIXMATCH | SUBSTRINGMATCH | equals_match | INCLUDES | DASHMATCH):m -> { m },
equals_match = '=' -> { '=' },
pseudo = ':' ':' (functional_pseudo | IDENT):i -> { '::' + i }
| ':' (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 + ')' },
expression_content = (PLUS | '-' | PERCENTAGE | DIMENSION | NUMBER | STRING | IDENT):e -> { e },
expression = expression_content:ec S+ expression:e -> { ec + ' ' + e }
| expression_content:ec expression:e -> { ec + e }
| expression_content:ec S* -> { ec },
full_expression = (expression)+:ea -> { ea.join('') },
negation = NOT:n S* negation_arg:na S* ')' -> { n + na + ')' },
//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 }
}
CssSelector.initialize = function() {
var s;
this.toString = function() {
return s;
};
this.add = function(st) {
s = st;
};
};
console.log(CssSelector.matchAll("E,\nF", "selectors_group").toString());
console.log(CssSelector.matchAll("E\nF", "selectors_group").toString());
console.log(CssSelector.matchAll("E, F\nG, H", "selectors_group").toString());
</script>
<body>
hello world
</body>
</html>