我已經寫了一個生成器來實現這個技巧,但我想知道實現偏離規則的最佳方法。你將如何執行偏離規則?
不久: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