lexer.mll 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. {
  2. (* Tokenizer according to definition at
  3. * http://www.w3.org/TR/CSS2/syndata.html#tokenization *)
  4. open Lexing
  5. open Parser
  6. exception SyntaxError of string
  7. let next_line lexbuf =
  8. let pos = lexbuf.lex_curr_p in
  9. lexbuf.lex_curr_p <- {
  10. pos with pos_bol = lexbuf.lex_curr_pos;
  11. pos_lnum = pos.pos_lnum + 1
  12. }
  13. }
  14. let h = ['0'-'9''a'-'f']
  15. let wc = '\r''\n' | [' ''\t''\r''\n''\012']
  16. let nonascii = ['\160'-'\255']
  17. let s = [' ''\t''\r''\n''\012']+
  18. let w = s?
  19. let nl = '\n' | '\r''\n' | '\r' | '\012'
  20. let unicode = '\\' h(h(h(h(h(h)?)?)?)?)? wc?
  21. let escape = unicode | '\\'[^'\r''\n''\012''0'-'9''a'-'f']
  22. let nmstart = ['_''a'-'z'] | nonascii | escape
  23. let nmchar = ['_''a'-'z''0'-'9''-'] | nonascii | escape
  24. let string1 = '"'([^'\n''\r''\012''"'] | '\\'nl | escape)*'"'
  25. let string2 = '\''([^'\n''\r''\012''\''] | '\\'nl | escape)*'\''
  26. let mystring = string1 | string2
  27. let badstring1 = '"'([^'\n''\r''\012''"'] | '\\'nl | escape)*'\\'?
  28. let badstring2 = '\''([^'\n''\r''\012''\''] | '\\'nl | escape)*'\\'?
  29. let badstring = badstring1 | badstring2
  30. let badcomment1 = '/''*'[^'*']*'*'+([^'/''*'][^'*']*'*'+)*
  31. let badcomment2 = '/''*'[^'*']*('*'+[^'/''*'][^'*']*)*
  32. let badcomment = badcomment1 | badcomment2
  33. let baduri1 = "url("w(['!''#''$''%''&''*'-'['']'-'~'] | nonascii | escape)*w
  34. let baduri2 = "url("w mystring w
  35. let baduri3 = "url("w badstring
  36. let baduri = baduri1 | baduri2 | baduri3
  37. let comment = '/''*'[^'*']*'*'+([^'/''*'][^'*']*'*'+)'*''/'
  38. let ident = '-'? nmstart nmchar*
  39. let name = nmchar+
  40. let num = ['0'-'9']+ | ['0'-'9']*'.'['0'-'9']+
  41. let url = (['!''#''$''%''&''*''-''~'] | nonascii | escape)*
  42. rule token = parse
  43. | s { S }
  44. | comment (* ignore comments *)
  45. | badcomment (* unclosed comment at EOF *)
  46. | "<!--" { CDO }
  47. | "-->" { CDC }
  48. | "~=" { INCLUDES }
  49. | "|=" { DASHMATCH }
  50. | mystring { STRING }
  51. | badstring { BAD_STRING }
  52. | ident as id { IDENT id }
  53. | '#' (name as name) { HASH name }
  54. | "@import" { IMPORT_SYM }
  55. | "@page" { PAGE_SYM }
  56. | "@media" { MEDIA_SYM }
  57. | "@charset" { CHARSET_SYM }
  58. | '!' (w | comment)* "important" { IMPORTANT_SYM }
  59. | (num as n) "em" { EMS (int_of_string n) }
  60. | (num as n) "ex" { EXS (int_of_string n) }
  61. | (num as n) "px" { LENGTH (int_of_string n, "px") }
  62. | (num as n) "cm" { LENGTH (int_of_string n, "cm") }
  63. | (num as n) "mm" { LENGTH (int_of_string n, "mm") }
  64. | (num as n) "in" { LENGTH (int_of_string n, "in") }
  65. | (num as n) "pt" { LENGTH (int_of_string n, "pt") }
  66. | (num as n) "pc" { LENGTH (int_of_string n, "pc") }
  67. | (num as n) "deg" { ANGLE (int_of_string n, "deg") }
  68. | (num as n) "rad" { ANGLE (int_of_string n, "rad") }
  69. | (num as n) "grad" { ANGLE (int_of_string n, "grad") }
  70. | (num as n) "ms" { TIME (int_of_string n, "ms") }
  71. | (num as n) "s" { TIME (int_of_string n, "s") }
  72. | (num as n) "hz" { FREQ (int_of_string n, "hz") }
  73. | (num as n) "khz" { FREQ (int_of_string n, "khz") }
  74. | (num as n) "%" { PERCENTAGE (int_of_string n) }
  75. | (num as n) (ident as dim) { DIMENSION (int_of_string n, dim) }
  76. | num as n { NUMBER (int_of_string n) }
  77. | "url(" w (mystring as uri) w ")" { URI uri }
  78. | "url(" w (url as uri) w ")" { URI uri }
  79. | baduri as uri { BAD_URI uri }
  80. | (ident as fn) '(' { FUNCTION fn }
  81. | '(' { LPAREN }
  82. | ')' { RPAREN }
  83. | '{' { LBRACE }
  84. | '}' { RBRACE }
  85. | '[' { LBRACK }
  86. | ']' { RBRACK }
  87. | ';' { SEMICOL }
  88. | ':' { COLON }
  89. (*
  90. | _ as c { raise (SyntaxError ("illegal string character: " ^ Char.escaped c)) }
  91. *)