@@ -40,8 +40,11 @@ let token_to_string = function
4040 | LEFT_BRACKET -> " ["
4141 | RIGHT_BRACKET -> " ]"
4242 | COLON -> " :"
43- | WHITESPACE_BEFORE_COLON -> " *"
44- | WHITESPACE_COLON -> " *:"
43+ | DOT -> " ."
44+ (* Whitespaces are detected only in selectors, before ":", ".", and "#", to
45+ * disambiguate between "p :first-child" and "p:first-child", these
46+ * whitespaces are replaced with "*" *)
47+ | WHITESPACE -> " *"
4548 | SEMI_COLON -> " ;"
4649 | PERCENTAGE -> " %"
4750 | IMPORTANT -> " !important"
@@ -262,34 +265,39 @@ let discard_comments_and_white_spaces buf =
262265 in
263266 discard_white_spaces buf false
264267
265- let rec get_next_token buf spaces_detected =
268+ let rec get_next_tokens buf spaces_detected =
266269 let open Menhir_parser in
267270 match % sedlex buf with
268- | eof -> EOF
269- | ';' -> SEMI_COLON
270- | '}' -> RIGHT_BRACE
271- | '{' -> LEFT_BRACE
272- | ':' -> if spaces_detected then WHITESPACE_COLON else COLON
273- | '(' -> LEFT_PAREN
274- | ')' -> RIGHT_PAREN
275- | '[' -> LEFT_BRACKET
276- | ']' -> RIGHT_BRACKET
277- | '%' -> PERCENTAGE
278- | operator -> OPERATOR (Lex_buffer. latin1 buf)
279- | string -> STRING (Lex_buffer. latin1 ~skip: 1 ~drop: 1 buf)
280- | "url(" -> get_url " " buf
281- | important -> IMPORTANT
282- | nested_at_rule -> NESTED_AT_RULE (Lex_buffer. latin1 ~skip: 1 buf)
283- | at_rule_without_body -> AT_RULE_WITHOUT_BODY (Lex_buffer. latin1 ~skip: 1 buf)
284- | at_rule -> AT_RULE (Lex_buffer. latin1 ~skip: 1 buf)
271+ | eof -> [ EOF ]
272+ | ';' -> [ SEMI_COLON ]
273+ | '}' -> [ RIGHT_BRACE ]
274+ | '{' -> [ LEFT_BRACE ]
275+ | ':' -> if spaces_detected then [ WHITESPACE ; COLON ] else [ COLON ]
276+ | '.' -> if spaces_detected then [ WHITESPACE ; DOT ] else [ DOT ]
277+ | '(' -> [ LEFT_PAREN ]
278+ | ')' -> [ RIGHT_PAREN ]
279+ | '[' -> [ LEFT_BRACKET ]
280+ | ']' -> [ RIGHT_BRACKET ]
281+ | '%' -> [ PERCENTAGE ]
282+ | operator -> [ OPERATOR (Lex_buffer. latin1 buf) ]
283+ | string -> [ STRING (Lex_buffer. latin1 ~skip: 1 ~drop: 1 buf) ]
284+ | "url(" -> [ get_url " " buf ]
285+ | important -> [ IMPORTANT ]
286+ | nested_at_rule -> [ NESTED_AT_RULE (Lex_buffer. latin1 ~skip: 1 buf) ]
287+ | at_rule_without_body ->
288+ [ AT_RULE_WITHOUT_BODY (Lex_buffer. latin1 ~skip: 1 buf) ]
289+ | at_rule -> [ AT_RULE (Lex_buffer. latin1 ~skip: 1 buf) ]
285290 (* NOTE: should be placed above ident, otherwise pattern with
286291 * '-[0-9a-z]{1,6}' cannot be matched *)
287- | _u , '+' , unicode_range -> UNICODE_RANGE (Lex_buffer. latin1 buf)
288- | ident , '(' -> FUNCTION (Lex_buffer. latin1 ~drop: 1 buf)
289- | ident -> IDENT (Lex_buffer. latin1 buf)
290- | '#' , name -> HASH (Lex_buffer. latin1 ~skip: 1 buf)
291- | number -> get_dimension (Lex_buffer. latin1 buf) buf
292- | any -> DELIM (Lex_buffer. latin1 buf)
292+ | _u , '+' , unicode_range -> [ UNICODE_RANGE (Lex_buffer. latin1 buf) ]
293+ | ident , '(' -> [ FUNCTION (Lex_buffer. latin1 ~drop: 1 buf) ]
294+ | ident -> [ IDENT (Lex_buffer. latin1 buf) ]
295+ | '#' , name ->
296+ if spaces_detected then
297+ [ WHITESPACE ; HASH (Lex_buffer. latin1 ~skip: 1 buf) ]
298+ else [ HASH (Lex_buffer. latin1 ~skip: 1 buf) ]
299+ | number -> [ get_dimension (Lex_buffer. latin1 buf) buf ]
300+ | any -> [ DELIM (Lex_buffer. latin1 buf) ]
293301 | _ -> assert false
294302
295303and get_dimension n buf =
@@ -316,25 +324,19 @@ and get_url url buf =
316324
317325let token_queue = Queue. create ()
318326
319- let queue_next_token_with_location buf =
327+ let queue_next_tokens_with_location buf =
320328 let spaces_detected = discard_comments_and_white_spaces buf in
321329 let loc_start = Lex_buffer. next_loc buf in
322- let token = get_next_token buf spaces_detected in
330+ let tokens = get_next_tokens buf spaces_detected in
323331 let loc_end = Lex_buffer. next_loc buf in
324- match token with
325- | Menhir_parser. WHITESPACE_COLON ->
326- Queue. add
327- (Menhir_parser. WHITESPACE_BEFORE_COLON , loc_start, loc_end)
328- token_queue;
329- Queue. add (Menhir_parser. COLON , loc_start, loc_end) token_queue
330- | _ -> Queue. add (token, loc_start, loc_end) token_queue
332+ List. iter (fun t -> Queue. add (t, loc_start, loc_end) token_queue) tokens
331333
332334let parse buf p =
333335 let last_token =
334336 ref (Menhir_parser. EOF , Lexing. dummy_pos, Lexing. dummy_pos)
335337 in
336338 let next_token () =
337- if Queue. is_empty token_queue then queue_next_token_with_location buf;
339+ if Queue. is_empty token_queue then queue_next_tokens_with_location buf;
338340 last_token := Queue. take token_queue;
339341 ! last_token
340342 in
0 commit comments