Compiles all the rules from the environment into a list of rules.
(environment: "Environment")
| 209 | |
| 210 | |
| 211 | def compile_rules(environment: "Environment") -> t.List[t.Tuple[str, str]]: |
| 212 | """Compiles all the rules from the environment into a list of rules.""" |
| 213 | e = re.escape |
| 214 | rules = [ |
| 215 | ( |
| 216 | len(environment.comment_start_string), |
| 217 | TOKEN_COMMENT_BEGIN, |
| 218 | e(environment.comment_start_string), |
| 219 | ), |
| 220 | ( |
| 221 | len(environment.block_start_string), |
| 222 | TOKEN_BLOCK_BEGIN, |
| 223 | e(environment.block_start_string), |
| 224 | ), |
| 225 | ( |
| 226 | len(environment.variable_start_string), |
| 227 | TOKEN_VARIABLE_BEGIN, |
| 228 | e(environment.variable_start_string), |
| 229 | ), |
| 230 | ] |
| 231 | |
| 232 | if environment.line_statement_prefix is not None: |
| 233 | rules.append( |
| 234 | ( |
| 235 | len(environment.line_statement_prefix), |
| 236 | TOKEN_LINESTATEMENT_BEGIN, |
| 237 | r"^[ \t\v]*" + e(environment.line_statement_prefix), |
| 238 | ) |
| 239 | ) |
| 240 | if environment.line_comment_prefix is not None: |
| 241 | rules.append( |
| 242 | ( |
| 243 | len(environment.line_comment_prefix), |
| 244 | TOKEN_LINECOMMENT_BEGIN, |
| 245 | r"(?:^|(?<=\S))[^\S\r\n]*" + e(environment.line_comment_prefix), |
| 246 | ) |
| 247 | ) |
| 248 | |
| 249 | return [x[1:] for x in sorted(rules, reverse=True)] |
| 250 | |
| 251 | |
| 252 | class Failure: |