我已经写了一个生成器来实现这个技巧,但我想知道实现偏离规则的最佳方法。你将如何执行偏离规则?
不久:Off-side rule意味着在这种情况下,缩进被认为是一个句法元素。
这里是伪越位规则进行断词,在可用的形式捕捉压痕,我不想用语言来限制答案:
token NEWLINE
matches r"\n\ *"
increase line count
pick up and store the indentation level
remember to also record the current level of parenthesis
procedure layout tokens
level = stack of indentation levels
push 0 to level
last_newline = none
per each token
if it is NEWLINE put it to last_newline and get next token
if last_newline contains something
extract new_level and parenthesis_count from last_newline
- if newline was inside parentheses, do nothing
- if new_level > level.top
push new_level to level
emit last_newline as INDENT token and clear last_newline
- if new_level == level.top
emit last_newline and clear last_newline
- otherwise
while new_level < level.top
pop from level
if new_level > level.top
freak out, indentation is broken.
emit last_newline as DEDENT token
clear last_newline
emit token
while level.top != 0
emit token as DEDENT token
pop from level
comments are ignored before they are getting into the layouter
layouter lies between a lexer and a parser
这制图器在不产生超过一个NEWLINE时间,并且在缩进出现时不会生成NEWLINE。因此解析规则很简单。我觉得这很不错,但是告知是否有更好的方法来完成它。
虽然使用了这一段时间,但我注意到,在DEDENTs之后,无论如何都可以很好地发出newline,这样,您可以将NEWLINE的表达式分开,同时将INDENT DEDENT作为表达的预告片。
您的代码无法发出多个DEDENT,它在EOF之前都没有考虑过缩进。它对某些事物可能有用,但这些事情比括号支持更重要。 – Cheery 2009-06-04 09:31:51