Repository navigation
Expand file tree
/
Copy pathcss.lua
More file actions
208 lines (162 loc) · 4.24 KB
/
Copy pathcss.lua
File metadata and controls
208 lines (162 loc) · 4.24 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
local M = {}
local function trim(str)
return str:match("^%s*(.-)%s*$")
end
local COMBINATORS = {
DESCENDANT = {},
DIRECT_DESCENDANT = {},
NEXT_SIBLING = {},
SUBSEQUENT_SIBLING = {},
}
M.COMBINATORS = COMBINATORS
local COMBINATOR_CHARS = {
[">"] = COMBINATORS.DIRECT_DESCENDANT,
["+"] = COMBINATORS.NEXT_SIBLING,
["~"] = COMBINATORS.SUBSEQUENT_SIBLING
}
local function create_tokeniser(input)
local pos = 1
local len = #input
local function peek()
if pos > len then return nil end
return input:sub(pos, pos)
end
local function next()
local char = peek()
if char then pos = pos + 1 end
return char
end
local function read_identifier()
local result = ""
while pos <= len do
local char = peek()
if char and char:match("[%w-]") then
result = result .. next()
else
break
end
end
return result
end
return {
peek = peek,
next = next,
read_identifier = read_identifier,
pos = function() return pos end
}
end
local function parse_compound_selector( tokeniser )
local selector = {
tag_name = nil,
id = nil,
class = {},
attributes_values = {},
attributes_present = {},
}
--local selectors = {}
-- Parse first part (type or universal)
local char = tokeniser.peek()
if char == "*" then
tokeniser.next()
--table.insert(selectors, {type = "universal"})
selector.tag_name = "*"
elseif char and char:match("[%w-]") then
local name = tokeniser.read_identifier()
if name ~= "" then
--table.insert(selectors, {type = "type", value = name})
selector.tag_name = name
end
end
-- Parse additional class or ID selectors
while true do
char = tokeniser.peek()
if not char then break end
if char == "." then
tokeniser.next() -- consume '.'
local name = tokeniser.read_identifier()
if name == "" then
error("Expected class name at position " .. tokeniser.pos())
end
--table.insert(selectors, {type = "class", value = name})
table.insert( selector.class, name )
elseif char == "#" then
tokeniser.next() -- consume '#'
local name = tokeniser.read_identifier()
if name == "" then
error("Expected id at position " .. tokeniser.pos())
end
--table.insert(selectors, {type = "id", value = name})
selector.id = name
elseif char == "[" then
tokeniser.next() -- consume leading [
local name = tokeniser.read_identifier()
if tokeniser.peek() == "=" then
tokeniser.next()
if tokeniser.peek() ~= "\"" then
error("Expected opening quote \" at pos " .. tokeniser.pos() )
end
tokeniser.next() -- consume leading "
local value = ""
while tokeniser.peek() ~= "\"" do
value = value .. tokeniser.peek()
tokeniser.next()
end
tokeniser.next() -- consume trailing "
selector.attributes_values[name] = value
else
table.insert( selector.attributes_present, name )
end
if tokeniser.peek() ~= "]" then
error("Expected closing bracket (']') at " .. tokeniser.pos())
end
tokeniser.next() -- consume trailing ]
else
break
end
end
return selector
end
local function parse_combinator( tokeniser )
-- Skip leading whitespace
while tokeniser.peek() and tokeniser.peek():match("%s") do
tokeniser.next()
end
local char = tokeniser.peek()
if not char then return nil end
if char == ">" or char == "+" or char == "~" then
tokeniser.next()
-- Skip trailing whitespace
while tokeniser.peek() and tokeniser.peek():match("%s") do
tokeniser.next()
end
return COMBINATOR_CHARS[char]
else
-- Make sure next character isn't an explicit combinator
char = tokeniser.peek()
if char and not (char == ">" or char == "+" or char == "~") then
return COMBINATORS.DESCENDANT
end
end
return nil
end
function M.parse( input )
input = trim( input )
local tokeniser = create_tokeniser( input )
local output = { selector = parse_compound_selector( tokeniser ) }
local current = output
-- Parse combinations of combinators and compound selectors
while true do
local combinator = parse_combinator( tokeniser )
if not combinator then
current.combinator = nil
current.next = nil
break
end
local next_selector = parse_compound_selector( tokeniser )
current.combinator = combinator
current.next = { selector = next_selector }
current = current.next
end
return output
end
return M