lexer.mll 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {
  2. open Lexing
  3. open Parser
  4. exception SyntaxError of string
  5. let next_line lexbuf =
  6. let pos = lexbuf.lex_curr_p in
  7. lexbuf.lex_curr_p <- {
  8. pos with pos_bol = lexbuf.lex_curr_pos;
  9. pos_lnum = pos.pos_lnum + 1
  10. }
  11. }
  12. rule token = parse
  13. | '(' { LPAREN }
  14. | ')' { RPAREN }
  15. | '{' { LBRACE }
  16. | '}' { RBRACE }
  17. | ';' { SEMICOL }
  18. | ',' { COMMA }
  19. | ':' { COLON }
  20. | "@media" { MEDIA }
  21. | "@import" { IMPORT }
  22. | "@charset" { CHARSET }
  23. | "@page" { PAGE }
  24. | "@font-face" { FONTFACE }
  25. | "@namespace" { NAMESPACE }
  26. | "!important" { IMPORTANT }
  27. | ['A'-'Z''a'-'z''0'-'9''_''-''#''.']+ as id { ID id }
  28. | ['.''#'':']['A'-'Z''a'-'z''_''-']['A'-'Z''a'-'z''0'-'9''_''-''.''#'':']* as id { SELECTOR id }
  29. | '\r' | '\n' | "\r\n" { next_line lexbuf; token lexbuf }
  30. | [' ''\t']+ { token lexbuf }
  31. | "/*" { comment lexbuf }
  32. | '"' { str (Buffer.create 17) lexbuf }
  33. | eof | '\000' { EOF }
  34. | _ as chr { raise (SyntaxError ("unexpected char: " ^ Char.escaped chr)) }
  35. (* Multi-line comments *)
  36. and comment = parse
  37. | '\r' | '\n' | "\r\n" { next_line lexbuf; comment lexbuf }
  38. | "*/" { token lexbuf }
  39. | _ { comment lexbuf }
  40. (* Strings *)
  41. and str buf = parse
  42. | '"' { STRING (Buffer.contents buf) }
  43. | '\\''/' { Buffer.add_char buf '/'; str buf lexbuf }
  44. | '\\''\\' { Buffer.add_char buf '\\'; str buf lexbuf }
  45. | '\\''b' { Buffer.add_char buf '\b'; str buf lexbuf }
  46. | '\\''f' { Buffer.add_char buf '\012'; str buf lexbuf }
  47. | '\\''n' { Buffer.add_char buf '\n'; str buf lexbuf }
  48. | '\\''r' { Buffer.add_char buf '\r'; str buf lexbuf }
  49. | '\\''t' { Buffer.add_char buf '\t'; str buf lexbuf }
  50. | [^'"''\\']+ as s { Buffer.add_string buf s; str buf lexbuf }
  51. | eof { raise (SyntaxError "unterminated string") }
  52. | _ as c { raise (SyntaxError ("illegal string character: " ^ Char.escaped c)) }