@@ -17,6 +17,7 @@ def any(name, alternates):
1717def make_pat ():
1818 kw = r"\b" + any ("KEYWORD" , keyword .kwlist ) + r"\b"
1919 match_softkw = (
20+ r"(?<!\\\n)" + # last line doesn't end in slash
2021 r"^[ \t]*" + # at beginning of line + possible indentation
2122 r"(?P<MATCH_SOFTKW>match)\b" +
2223 r"(?![ \t]*(?:" + "|" .join ([ # not followed by ...
@@ -27,11 +28,13 @@ def make_pat():
2728 r"))"
2829 )
2930 case_default = (
31+ r"(?<!\\\n)" + # last line doesn't end in slash
3032 r"^[ \t]*" + # at beginning of line + possible indentation
3133 r"(?P<CASE_SOFTKW>case)" +
3234 r"[ \t]+(?P<CASE_DEFAULT_UNDERSCORE>_\b)"
3335 )
3436 case_softkw_and_pattern = (
37+ r"(?<!\\\n)" + # last line doesn't end in slash
3538 r"^[ \t]*" + # at beginning of line + possible indentation
3639 r"(?P<CASE_SOFTKW2>case)\b" +
3740 r"(?![ \t]*(?:" + "|" .join ([ # not followed by ...
@@ -45,19 +48,43 @@ def make_pat():
4548 builtinlist = [str (name ) for name in dir (builtins )
4649 if not name .startswith ('_' ) and
4750 name not in keyword .kwlist ]
48- builtin = r"([^.'\"\\#]\b|^)" + any ("BUILTIN" , builtinlist ) + r"\b"
51+ builtin = (
52+ r"(?<!" + # make sure there isn't
53+ r"\\\n" + # a slash followed by a newline
54+ r")" +
55+ r"(?<!" + # make sure that there also isn't
56+ r"|" .join ([
57+ r"\." , # a dot or
58+ r" " , # a space
59+ ]) +
60+ r")" +
61+ r"(" + # match any number of
62+ r"[ \t]*" + # spaces/tabs followed by
63+ r"(\\\\)*\\" + # an odd number of slashes followed by
64+ r"\n" + # a newline
65+ r")*" +
66+ r"(" + # also match
67+ r"|" .join ([ # either
68+ r"[ \t]+" , # indentation or
69+ r"\b" , # a word boundary
70+ ]) +
71+ r")" +
72+ any ("BUILTIN" , builtinlist ) + # followed by a builtin
73+ r"\b" # followed by another word boundary
74+ )
4975 comment = any ("COMMENT" , [r"#[^\n]*" ])
5076 stringprefix = r"(?i:r|u|f|fr|rf|b|br|rb|t|rt|tr)?"
5177 sqstring = stringprefix + r"'[^'\\\n]*(\\.[^'\\\n]*)*'?"
5278 dqstring = stringprefix + r'"[^"\\\n]*(\\.[^"\\\n]*)*"?'
5379 sq3string = stringprefix + r"'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
5480 dq3string = stringprefix + r'"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
5581 string = any ("STRING" , [sq3string , dq3string , sqstring , dqstring ])
82+ sync = any ("SYNC" , [r"(?<!\\)\n" ]) # no sync if line ends with slash
5683 prog = re .compile ("|" .join ([
5784 builtin , comment , string , kw ,
5885 match_softkw , case_default ,
5986 case_softkw_and_pattern ,
60- any ( "SYNC" , [ r"\n" ]) ,
87+ sync ,
6188 ]),
6289 re .DOTALL | re .MULTILINE )
6390 return prog
0 commit comments