Package cssutils :: Module cssproductions
[hide private]
[frames] | no frames]

Source Code for Module cssutils.cssproductions

  1  """productions for cssutils based on a mix of CSS 2.1 and CSS 3 Syntax 
  2  productions 
  3   
  4  - http://www.w3.org/TR/css3-syntax 
  5  - http://www.w3.org/TR/css3-syntax/#grammar0 
  6   
  7  open issues 
  8      - numbers contain "-" if present 
  9      - HASH: #aaa is, #000 is not anymore, 
 10              CSS2.1: 'nmchar': r'[_a-z0-9-]|{nonascii}|{escape}', 
 11              CSS3: 'nmchar': r'[_a-z-]|{nonascii}|{escape}', 
 12  """ 
 13  __all__ = ['CSSProductions', 'MACROS', 'PRODUCTIONS'] 
 14  __docformat__ = 'restructuredtext' 
 15  __version__ = '$Id: cssproductions.py 1378 2008-07-15 20:02:19Z cthedot $' 
 16   
 17  # a complete list of css3 macros 
 18  MACROS = { 
 19      'ident': r'[-]?{nmstart}{nmchar}*', 
 20      'name': r'{nmchar}+', 
 21      'nmstart': r'[_a-zA-Z]|{nonascii}|{escape}', 
 22      'nonascii': r'[^\0-\177]', 
 23      'unicode': r'\\[0-9a-f]{1,6}(?:{nl}|{wc})?', 
 24      'escape': r'{unicode}|\\[ -~\200-\777]', 
 25      #   'escape': r'{unicode}|\\[ -~\200-\4177777]', 
 26      'nmchar': r'[-_a-zA-Z0-9]|{nonascii}|{escape}', 
 27   
 28      'num': r'[0-9]*\.[0-9]+|[0-9]+', #r'[-]?\d+|[-]?\d*\.\d+', 
 29      'string':  r"""\'({stringesc1}|{stringchar}|")*\'""" + "|" + '''\"({stringesc2}|{stringchar}|')*\"''', 
 30      # seems an error in CSS 3 but is allowed in CSS 2.1 
 31      'stringesc1' : r"\\'", 
 32      'stringesc2' : r'\\"', 
 33   
 34      'stringchar':  r'{urlchar}| |\\{nl}', 
 35   
 36      # urlchar  ::= [#x9#x21#x23-#x26#x27-#x7E] | nonascii | escape 
 37      # 0x27 is "'" which should not be in here..., should ) be in here??? 
 38      'urlchar':  r'[\x09\x21\x23-\x26\x28-\x7E]|{nonascii}|{escape}', 
 39   
 40      # from CSS2.1 
 41      'invalid': r'{invalid1}|{invalid2}', 
 42      'invalid1': r'\"([^\n\r\f\\"]|\\{nl}|{escape})*', 
 43      'invalid2': r"\'([^\n\r\f\\']|\\{nl}|{escape})*", 
 44   
 45      # \r\n should be counted as one char see unicode above 
 46      'nl': r'\n|\r\n|\r|\f', 
 47      'w': r'{wc}*', 
 48      'wc': r'\t|\r|\n|\f|\x20', 
 49   
 50      'comment': r'\/\*[^*]*\*+([^/][^*]*\*+)*\/', 
 51   
 52      'A': r'A|a|\\0{0,4}(?:41|61)(?:\r\n|[ \t\r\n\f])?', 
 53      'C': r'C|c|\\0{0,4}(?:43|63)(?:\r\n|[ \t\r\n\f])?', 
 54      'D': r'D|d|\\0{0,4}(?:44|64)(?:\r\n|[ \t\r\n\f])?', 
 55      'E': r'E|e|\\0{0,4}(?:45|65)(?:\r\n|[ \t\r\n\f])?', 
 56      'F': r'F|f|\\0{0,4}(?:46|66)(?:\r\n|[ \t\r\n\f])?', 
 57      'G': r'G|g|\\0{0,4}(?:47|67)(?:\r\n|[ \t\r\n\f])?|\\G|\\g', 
 58      'H': r'H|h|\\0{0,4}(?:48|68)(?:\r\n|[ \t\r\n\f])?|\\H|\\h', 
 59      'I': r'I|i|\\0{0,4}(?:49|69)(?:\r\n|[ \t\r\n\f])?|\\I|\\i', 
 60      'K': r'K|k|\\0{0,4}(?:4b|6b)(?:\r\n|[ \t\r\n\f])?|\\K|\\k', 
 61      'L': r'L|l|\\0{0,4}(?:4c|6c)(?:\r\n|[ \t\r\n\f])?|\\L|\\l', 
 62      'M': r'M|m|\\0{0,4}(?:4d|6d)(?:\r\n|[ \t\r\n\f])?|\\M|\\m', 
 63      'N': r'N|n|\\0{0,4}(?:4e|6e)(?:\r\n|[ \t\r\n\f])?|\\N|\\n', 
 64      'O': r'O|o|\\0{0,4}(?:4f|6f)(?:\r\n|[ \t\r\n\f])?|\\O|\\o', 
 65      'P': r'P|p|\\0{0,4}(?:50|70)(?:\r\n|[ \t\r\n\f])?|\\P|\\p', 
 66      'R': r'R|r|\\0{0,4}(?:52|72)(?:\r\n|[ \t\r\n\f])?|\\R|\\r', 
 67      'S': r'S|s|\\0{0,4}(?:53|73)(?:\r\n|[ \t\r\n\f])?|\\S|\\s', 
 68      'T': r'T|t|\\0{0,4}(?:54|74)(?:\r\n|[ \t\r\n\f])?|\\T|\\t', 
 69      'U': r'U|u|\\0{0,4}(?:55|75)(?:\r\n|[ \t\r\n\f])?|\\U|\\u', 
 70      'X': r'X|x|\\0{0,4}(?:58|78)(?:\r\n|[ \t\r\n\f])?|\\X|\\x', 
 71      'Z': r'Z|z|\\0{0,4}(?:5a|7a)(?:\r\n|[ \t\r\n\f])?|\\Z|\\z', 
 72      } 
 73   
 74  # The following productions are the complete list of tokens 
 75  # used by cssutils, a mix of CSS3 and some CSS2.1 productions. 
 76  # The productions are **ordered**: 
 77  PRODUCTIONS = [ 
 78      ('BOM', r'\xFEFF'), # will only be checked at beginning of CSS 
 79       
 80      ('S', r'{wc}+'), # 1st in list of general productions 
 81      ('URI', r'{U}{R}{L}\({w}({string}|{urlchar}*){w}\)'), 
 82      ('FUNCTION', r'{ident}\('), 
 83      ('IDENT', r'{ident}'), 
 84      ('STRING', r'{string}'), 
 85      ('INVALID', r'{invalid}'), # from CSS2.1 
 86      ('HASH', r'\#{name}'), 
 87      ('PERCENTAGE', r'{num}\%'), 
 88      ('DIMENSION', r'{num}{ident}'), 
 89      ('NUMBER', r'{num}'), 
 90      # valid ony at start so not checked everytime 
 91      #('CHARSET_SYM', r'@charset '), # from Errata includes ending space! 
 92      ('ATKEYWORD', r'@{ident}'), # other keywords are done in the tokenizer 
 93      #('UNICODE-RANGE', r'[0-9A-F?]{1,6}(\-[0-9A-F]{1,6})?'), #??? 
 94      ('CDO', r'\<\!\-\-'), 
 95      ('CDC', r'\-\-\>'), 
 96      ('INCLUDES', '\~\='), 
 97      ('DASHMATCH', r'\|\='), 
 98      ('PREFIXMATCH', r'\^\='), 
 99      ('SUFFIXMATCH', r'\$\='), 
100      ('SUBSTRINGMATCH', r'\*\='), 
101      # checked specially if fullsheet is parsed 
102      ('COMMENT', r'{comment}'), #r'\/\*[^*]*\*+([^/][^*]*\*+)*\/'), 
103      ('CHAR', r'[^"\']') # MUST always be last 
104      ] 
105   
106 -class CSSProductions(object):
107 """ 108 most attributes are set later 109 """ 110 EOF = True 111 # removed from productions as they simply are ATKEYWORD until 112 # tokenizing 113 CHARSET_SYM = 'CHARSET_SYM' 114 FONT_FACE_SYM = 'FONT_FACE_SYM' 115 MEDIA_SYM = 'MEDIA_SYM' 116 IMPORT_SYM = 'IMPORT_SYM' 117 NAMESPACE_SYM = 'NAMESPACE_SYM' 118 PAGE_SYM = 'PAGE_SYM'
119 120 for i, t in enumerate(PRODUCTIONS): 121 setattr(CSSProductions, t[0].replace('-', '_'), t[0]) 122