{throwSyntaxError, extractAllCommentTokens} = require './helpers'
CoffeeScript 语言包含大量的可选语法、隐式语法和简写语法。这会极大地复杂化语法并膨胀生成的解析表。我们没有让解析器处理所有这些,而是对标记流进行一系列的处理,使用这个 **Rewriter** 将简写转换为明确的长格式,添加隐式缩进和括号,并进行一些清理工作。
{throwSyntaxError, extractAllCommentTokens} = require './helpers'
将附加的注释从一个标记移动到另一个标记。
moveComments = (fromToken, toToken) ->
return unless fromToken.comments
if toToken.comments and toToken.comments.length isnt 0
unshiftedComments = []
for comment in fromToken.comments
if comment.unshift
unshiftedComments.push comment
else
toToken.comments.push comment
toToken.comments = unshiftedComments.concat toToken.comments
else
toToken.comments = fromToken.comments
delete fromToken.comments
创建一个生成的标记:由于使用隐式语法而存在的标记。可以选择让这个新标记从另一个标记中获取附加的注释。
generate = (tag, value, origin, commentsToken) ->
token = [tag, value]
token.generated = yes
token.origin = origin if origin
moveComments commentsToken, token if commentsToken
token
exports.Rewriter = class Rewriter
以多个步骤重写标记流,每次处理一个逻辑过滤器。当然,这可以更改为对流进行单次处理,使用一个大型高效的 switch 语句,但这样处理起来要好得多。这些步骤的顺序很重要——必须先修正缩进,然后才能用括号将代码块括起来。
rewrite: (@tokens) ->
将环境变量 DEBUG_TOKEN_STREAM
设置为 true
以输出标记调试信息。还可以将 DEBUG_REWRITTEN_TOKEN_STREAM
设置为 true
以输出重写后的标记流。
if process?.env?.DEBUG_TOKEN_STREAM
console.log 'Initial token stream:' if process.env.DEBUG_REWRITTEN_TOKEN_STREAM
console.log (t[0] + '/' + t[1] + (if t.comments then '*' else '') for t in @tokens).join ' '
@removeLeadingNewlines()
@closeOpenCalls()
@closeOpenIndexes()
@normalizeLines()
@tagPostfixConditionals()
@addImplicitBracesAndParens()
@rescueStowawayComments()
@addLocationDataToGeneratedTokens()
@enforceValidJSXAttributes()
@fixIndentationLocationData()
@exposeTokenDataToGrammar()
if process?.env?.DEBUG_REWRITTEN_TOKEN_STREAM
console.log 'Rewritten token stream:' if process.env.DEBUG_TOKEN_STREAM
console.log (t[0] + '/' + t[1] + (if t.comments then '*' else '') for t in @tokens).join ' '
@tokens
重写标记流,向前和向后查看一个标记。允许块的返回值告诉我们向前(或向后)移动标记流中的多少个标记,以确保在插入和删除标记时不会错过任何内容,并且流在我们的操作下改变长度。
scanTokens: (block) ->
{tokens} = this
i = 0
i += block.call this, token, i, tokens while token = tokens[i]
true
detectEnd: (i, condition, action, opts = {}) ->
{tokens} = this
levels = 0
while token = tokens[i]
return action.call this, token, i if levels is 0 and condition.call this, token, i
if token[0] in EXPRESSION_START
levels += 1
else if token[0] in EXPRESSION_END
levels -= 1
if levels < 0
return if opts.returnOnNegativeLevel
return action.call this, token, i
i += 1
i - 1
前导换行符会在语法中引入歧义,因此我们在这里进行处理。
removeLeadingNewlines: ->
查找第一个非 TERMINATOR
标记的索引。
break for [tag], i in @tokens when tag isnt 'TERMINATOR'
return if i is 0
如果我们要丢弃的标记有任何附加的注释,则将它们向前移动到将成为新第一个标记的位置。
for leadingNewlineToken in @tokens[0...i]
moveComments leadingNewlineToken, @tokens[i]
丢弃所有前导换行符标记。
@tokens.splice 0, i
词法分析器已标记方法调用的左括号。将其与配对的右括号匹配。
closeOpenCalls: ->
condition = (token, i) ->
token[0] in [')', 'CALL_END']
action = (token, i) ->
token[0] = 'CALL_END'
@scanTokens (token, i) ->
@detectEnd i + 1, condition, action if token[0] is 'CALL_START'
1
词法分析器已标记索引操作调用的左括号。将其与配对的右括号匹配。
closeOpenIndexes: ->
startToken = null
condition = (token, i) ->
token[0] in [']', 'INDEX_END']
action = (token, i) ->
if @tokens.length >= i and @tokens[i + 1][0] is ':'
startToken[0] = '['
token[0] = ']'
else
token[0] = 'INDEX_END'
@scanTokens (token, i) ->
if token[0] is 'INDEX_START'
startToken = token
@detectEnd i + 1, condition, action
1
从 i
开始的标记流中,将标记与 pattern
匹配。pattern
可以包含字符串(相等)、字符串数组(其中之一)或 null(通配符)。返回匹配项的索引,如果未匹配则返回 -1。
indexOfTag: (i, pattern...) ->
fuzz = 0
for j in [0 ... pattern.length]
continue if not pattern[j]?
pattern[j] = [pattern[j]] if typeof pattern[j] is 'string'
return -1 if @tag(i + j + fuzz) not in pattern[j]
i + j + fuzz - 1
如果位于类似于 @<x>:
、<x>:
或 <EXPRESSION_START><x>...<EXPRESSION_END>:
的内容之前,则返回 yes
。
looksObjectish: (j) ->
return yes if @indexOfTag(j, '@', null, ':') isnt -1 or @indexOfTag(j, null, ':') isnt -1
index = @indexOfTag j, EXPRESSION_START
if index isnt -1
end = null
@detectEnd index + 1, ((token) -> token[0] in EXPRESSION_END), ((token, i) -> end = i)
return yes if @tag(end + 1) is ':'
no
如果当前行的标记包含相同表达式级别的标记元素,则返回 yes
。在 LINEBREAKS
或包含的平衡表达式的显式开始处停止搜索。
findTagsBackwards: (i, tags) ->
backStack = []
while i >= 0 and (backStack.length or
@tag(i) not in tags and
(@tag(i) not in EXPRESSION_START or @tokens[i].generated) and
@tag(i) not in LINEBREAKS)
backStack.push @tag(i) if @tag(i) in EXPRESSION_END
backStack.pop() if @tag(i) in EXPRESSION_START and backStack.length
i -= 1
@tag(i) in tags
在标记流中查找隐式调用和对象的迹象,并添加它们。
addImplicitBracesAndParens: ->
在堆栈上跟踪当前的平衡深度(隐式和显式)。
stack = []
start = null
@scanTokens (token, i, tokens) ->
[tag] = token
[prevTag] = prevToken = if i > 0 then tokens[i - 1] else []
[nextTag] = nextToken = if i < tokens.length - 1 then tokens[i + 1] else []
stackTop = -> stack[stack.length - 1]
startIdx = i
辅助函数,用于在返回以获取新标记时跟踪已使用和拼接的标记数量。
forward = (n) -> i - startIdx + n
辅助函数
isImplicit = (stackItem) -> stackItem?[2]?.ours
isImplicitObject = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '{'
isImplicitCall = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '('
inImplicit = -> isImplicit stackTop()
inImplicitCall = -> isImplicitCall stackTop()
inImplicitObject = -> isImplicitObject stackTop()
隐式括号内的未闭合控制语句(如类声明或 if 条件语句)。
inImplicitControl = -> inImplicit() and stackTop()?[0] is 'CONTROL'
startImplicitCall = (idx) ->
stack.push ['(', idx, ours: yes]
tokens.splice idx, 0, generate 'CALL_START', '(', ['', 'implicit function call', token[2]], prevToken
endImplicitCall = ->
stack.pop()
tokens.splice i, 0, generate 'CALL_END', ')', ['', 'end of input', token[2]], prevToken
i += 1
startImplicitObject = (idx, {startsLine = yes, continuationLineIndent} = {}) ->
stack.push ['{', idx, sameLine: yes, startsLine: startsLine, ours: yes, continuationLineIndent: continuationLineIndent]
val = new String '{'
val.generated = yes
tokens.splice idx, 0, generate '{', val, token, prevToken
endImplicitObject = (j) ->
j = j ? i
stack.pop()
tokens.splice j, 0, generate '}', '}', token, prevToken
i += 1
implicitObjectContinues = (j) =>
nextTerminatorIdx = null
@detectEnd j,
(token) -> token[0] is 'TERMINATOR'
(token, i) -> nextTerminatorIdx = i
returnOnNegativeLevel: yes
return no unless nextTerminatorIdx?
@looksObjectish nextTerminatorIdx + 1
如果以下任何内容位于参数/值中,则不要在下一个缩进处结束隐式调用/对象。
if (
(inImplicitCall() or inImplicitObject()) and tag in CONTROL_IN_IMPLICIT or
inImplicitObject() and prevTag is ':' and tag is 'FOR'
)
stack.push ['CONTROL', i, ours: yes]
return forward(1)
if tag is 'INDENT' and inImplicit()
if prevTag not in ['=>', '->', '[', '(', ',', '{', 'ELSE', '=']
while inImplicitCall() or inImplicitObject() and prevTag isnt ':'
if inImplicitCall()
endImplicitCall()
else
endImplicitObject()
stack.pop() if inImplicitControl()
stack.push [tag, i]
return forward(1)
显式表达式的直接开始。
if tag in EXPRESSION_START
stack.push [tag, i]
return forward(1)
关闭显式闭合表达式内的所有隐式表达式。
if tag in EXPRESSION_END
while inImplicit()
if inImplicitCall()
endImplicitCall()
else if inImplicitObject()
endImplicitObject()
else
stack.pop()
start = stack.pop()
inControlFlow = =>
seenFor = @findTagsBackwards(i, ['FOR']) and @findTagsBackwards(i, ['FORIN', 'FOROF', 'FORFROM'])
controlFlow = seenFor or @findTagsBackwards i, ['WHILE', 'UNTIL', 'LOOP', 'LEADING_WHEN']
return no unless controlFlow
isFunc = no
tagCurrentLine = token[2].first_line
@detectEnd i,
(token, i) -> token[0] in LINEBREAKS
(token, i) ->
[prevTag, ,{first_line}] = tokens[i - 1] || []
isFunc = tagCurrentLine is first_line and prevTag in ['->', '=>']
returnOnNegativeLevel: yes
isFunc
识别标准的隐式调用,如 f a、f() b、f? c、h[0] d 等。添加了对左侧的扩展点支持:f …a
if (tag in IMPLICIT_FUNC and token.spaced or
tag is '?' and i > 0 and not tokens[i - 1].spaced) and
(nextTag in IMPLICIT_CALL or
(nextTag is '...' and @tag(i + 2) in IMPLICIT_CALL and not @findTagsBackwards(i, ['INDEX_START', '['])) or
nextTag in IMPLICIT_UNSPACED_CALL and
not nextToken.spaced and not nextToken.newLine) and
not inControlFlow()
tag = token[0] = 'FUNC_EXIST' if tag is '?'
startImplicitCall i + 1
return forward(2)
隐式调用,以隐式缩进的对象作为第一个参数。
f
a: b
c: d
当位于以下控制结构的同一行上时,不要接受此类型的隐式调用,因为这可能会误解以下结构
if f
a: 1
为
if f(a: 1)
这可能总是无意的。此外,不要在文字数组或显式对象的第 1 行中允许这样做,因为这会造成语法歧义(#5368)。
if tag in IMPLICIT_FUNC and
@indexOfTag(i + 1, 'INDENT') > -1 and @looksObjectish(i + 2) and
not @findTagsBackwards(i, ['CLASS', 'EXTENDS', 'IF', 'CATCH',
'SWITCH', 'LEADING_WHEN', 'FOR', 'WHILE', 'UNTIL']) and
not ((s = stackTop()?[0]) in ['{', '['] and
not isImplicit(stackTop()) and
@findTagsBackwards(i, s))
startImplicitCall i + 1
stack.push ['INDENT', i + 2]
return forward(3)
隐式对象从这里开始。
if tag is ':'
返回到对象的(隐式)开始位置。
s = switch
when @tag(i - 1) in EXPRESSION_END
[startTag, startIndex] = start
if startTag is '[' and startIndex > 0 and @tag(startIndex - 1) is '@' and not tokens[startIndex - 1].spaced
startIndex - 1
else
startIndex
when @tag(i - 2) is '@' then i - 2
else i - 1
startsLine = s <= 0 or @tag(s - 1) in LINEBREAKS or tokens[s - 1].newLine
我们只是在继续声明一个已经存在的对象吗?包括在显式“{”之后缩进的情况。
if stackTop()
[stackTag, stackIdx] = stackTop()
stackNext = stack[stack.length - 2]
if (stackTag is '{' or
stackTag is 'INDENT' and stackNext?[0] is '{' and
not isImplicit(stackNext) and
@findTagsBackwards(stackIdx-1, ['{'])) and
(startsLine or @tag(s - 1) is ',' or @tag(s - 1) is '{') and
@tag(s - 1) not in UNFINISHED
return forward(1)
preObjectToken = if i > 1 then tokens[i - 2] else []
startImplicitObject(s, {startsLine: !!startsLine, continuationLineIndent: preObjectToken.continuationLineIndent})
return forward(2)
将所有封闭对象标记为非 sameLine
if tag in LINEBREAKS
for stackItem in stack by -1
break unless isImplicit stackItem
stackItem[2].sameLine = no if isImplicitObject stackItem
缩进继续行隐式对象在缩进结束时结束。
if tag is 'TERMINATOR' and token.endsContinuationLineIndentation
{preContinuationLineIndent} = token.endsContinuationLineIndentation
while inImplicitObject() and (implicitObjectIndent = stackTop()[2].continuationLineIndent)? and implicitObjectIndent > preContinuationLineIndent
endImplicitObject()
newLine = prevTag is 'OUTDENT' or prevToken.newLine
if tag in IMPLICIT_END or
(tag in CALL_CLOSERS and newLine) or
(tag in ['..', '...'] and @findTagsBackwards(i, ["INDEX_START"]))
while inImplicit()
[stackTag, stackIdx, {sameLine, startsLine}] = stackTop()
在到达参数列表的末尾时关闭隐式调用
if inImplicitCall() and prevTag isnt ',' or
(prevTag is ',' and tag is 'TERMINATOR' and not nextTag?)
endImplicitCall()
关闭隐式对象,例如:return a: 1, b: 2 unless true
else if inImplicitObject() and sameLine and
tag isnt 'TERMINATOR' and prevTag isnt ':' and
not (tag in ['POST_IF', 'FOR', 'WHILE', 'UNTIL'] and startsLine and implicitObjectContinues(i + 1))
endImplicitObject()
在行尾关闭隐式对象,行没有以逗号结尾,并且隐式对象没有开始该行,或者下一行看起来不像对象的延续。
else if inImplicitObject() and tag is 'TERMINATOR' and prevTag isnt ',' and
not (startsLine and @looksObjectish(i + 1))
endImplicitObject()
else if inImplicitControl() and tokens[stackTop()[1]][0] is 'CLASS' and tag is 'TERMINATOR'
stack.pop()
else
break
如果逗号是最后一个字符,并且后面的内容看起来不像属于该对象,则关闭隐式对象。这用于尾随逗号和调用,例如
x =
a: b,
c: d,
e = 2
以及
f a, b: c, d: e, f, g: h: i, j
if tag is ',' and not @looksObjectish(i + 1) and inImplicitObject() and not (@tag(i + 2) in ['FOROF', 'FORIN']) and
(nextTag isnt 'TERMINATOR' or not @looksObjectish(i + 2))
offset = if nextTag is 'OUTDENT' then 1 else 0
while inImplicitObject()
endImplicitObject i + offset
return forward(1)
确保在 JSX 属性中仅使用字符串和包装的表达式。
enforceValidJSXAttributes: ->
@scanTokens (token, i, tokens) ->
if token.jsxColon
next = tokens[i + 1]
if next[0] not in ['STRING_START', 'STRING', '(']
throwSyntaxError 'expected wrapped or quoted JSX attribute', next[2]
return 1
并非所有标记都能通过解析器的处理。为了避免注释丢失到无形中,请找到附加到注定要消失的标记的注释,并将它们移动到能够到达另一边的标记。
rescueStowawayComments: ->
insertPlaceholder = (token, j, tokens, method) ->
tokens[method] generate 'TERMINATOR', '\n', tokens[j] unless tokens[j][0] is 'TERMINATOR'
tokens[method] generate 'JS', '', tokens[j], token
dontShiftForward = (i, tokens) ->
j = i + 1
while j isnt tokens.length and tokens[j][0] in DISCARDED
return yes if tokens[j][0] is 'INTERPOLATION_END'
j++
no
shiftCommentsForward = (token, i, tokens) ->
找到下一个存活的标记,并将此标记的注释附加到它,并使用一个标志,我们知道要在该标记自己的编译之前输出这些注释。(否则,注释将在它们附加到的标记之后输出。)
j = i
j++ while j isnt tokens.length and tokens[j][0] in DISCARDED
unless j is tokens.length or tokens[j][0] in DISCARDED
comment.unshift = yes for comment in token.comments
moveComments token, tokens[j]
return 1
else # All following tokens are doomed!
j = tokens.length - 1
insertPlaceholder token, j, tokens, 'push'
生成的标记已添加到末尾,而不是内联,因此我们不会跳过。
return 1
shiftCommentsBackward = (token, i, tokens) ->
找到最后一个存活的标记,并将此标记的注释附加到它。
j = i
j-- while j isnt -1 and tokens[j][0] in DISCARDED
unless j is -1 or tokens[j][0] in DISCARDED
moveComments token, tokens[j]
return 1
else # All previous tokens are doomed!
insertPlaceholder token, 0, tokens, 'unshift'
我们添加了两个标记,因此向前移动以说明插入。
return 3
@scanTokens (token, i, tokens) ->
return 1 unless token.comments
ret = 1
if token[0] in DISCARDED
此标记将无法通过解析器,因此我们需要拯救其附加的标记并将它们重新分配到附近的标记。不开始新行的注释可以向后移动到最后一个安全的标记,而其他标记应该向前移动。
dummyToken = comments: []
j = token.comments.length - 1
until j is -1
if token.comments[j].newLine is no and token.comments[j].here is no
dummyToken.comments.unshift token.comments[j]
token.comments.splice j, 1
j--
if dummyToken.comments.length isnt 0
ret = shiftCommentsBackward dummyToken, i - 1, tokens
if token.comments.length isnt 0
shiftCommentsForward token, i, tokens
else unless dontShiftForward i, tokens
如果此标记的任何注释都开始一行——前一个换行符和注释的开头之间只有空格——并且这不是特殊的 JS
标记之一,则将此注释向前移动到下一个有效标记之前。Block.compileComments
还具有逻辑来确保“开始新行”注释在与注释附加到的标记相关的最近换行符之后或之前,但该换行符可能位于 }
或 )
或其他生成的标记内,我们确实希望此注释在这些标记之后输出。因此,我们需要在这里移动注释,避免这些生成的和被丢弃的标记。
dummyToken = comments: []
j = token.comments.length - 1
until j is -1
if token.comments[j].newLine and not token.comments[j].unshift and
not (token[0] is 'JS' and token.generated)
dummyToken.comments.unshift token.comments[j]
token.comments.splice j, 1
j--
if dummyToken.comments.length isnt 0
ret = shiftCommentsForward dummyToken, i + 1, tokens
delete token.comments if token.comments?.length is 0
ret
将位置数据添加到重写器生成的的所有标记。
addLocationDataToGeneratedTokens: ->
@scanTokens (token, i, tokens) ->
return 1 if token[2]
return 1 unless token.generated or token.explicit
if token.fromThen and token[0] is 'INDENT'
token[2] = token.origin[2]
return 1
if token[0] is '{' and nextLocation=tokens[i + 1]?[2]
{first_line: line, first_column: column, range: [rangeIndex]} = nextLocation
else if prevLocation = tokens[i - 1]?[2]
{last_line: line, last_column: column, range: [, rangeIndex]} = prevLocation
column += 1
else
line = column = 0
rangeIndex = 0
token[2] = {
first_line: line
first_column: column
last_line: line
last_column: column
last_line_exclusive: line
last_column_exclusive: column
range: [rangeIndex, rangeIndex]
}
return 1
OUTDENT
标记应始终位于前一个标记的最后一个字符处,以便以 OUTDENT
标记结尾的 AST 节点最终具有与节点下最后一个“真实”标记相对应的位置。
fixIndentationLocationData: ->
@allComments ?= extractAllCommentTokens @tokens
findPrecedingComment = (token, {afterPosition, indentSize, first, indented}) =>
tokenStart = token[2].range[0]
matches = (comment) ->
if comment.outdented
return no unless indentSize? and comment.indentSize > indentSize
return no if indented and not comment.indented
return no unless comment.locationData.range[0] < tokenStart
return no unless comment.locationData.range[0] > afterPosition
yes
if first
lastMatching = null
for comment in @allComments by -1
if matches comment
lastMatching = comment
else if lastMatching
return lastMatching
return lastMatching
for comment in @allComments when matches comment by -1
return comment
null
@scanTokens (token, i, tokens) ->
return 1 unless token[0] in ['INDENT', 'OUTDENT'] or
(token.generated and token[0] is 'CALL_END' and not token.data?.closingTagNameToken) or
(token.generated and token[0] is '}')
isIndent = token[0] is 'INDENT'
prevToken = token.prevToken ? tokens[i - 1]
prevLocationData = prevToken[2]
addLocationDataToGeneratedTokens() 将 outdent 的位置数据设置为前一个标记的位置数据,但为了检测空“块”内的注释,我们希望查找前一个标记之前的注释。
useNextToken = token.explicit or token.generated
if useNextToken
nextToken = token
nextTokenIndex = i
nextToken = tokens[nextTokenIndex++] while (nextToken.explicit or nextToken.generated) and nextTokenIndex isnt tokens.length - 1
precedingComment = findPrecedingComment(
if useNextToken
nextToken
else
token
afterPosition: prevLocationData.range[0]
indentSize: token.indentSize
first: isIndent
indented: useNextToken
)
if isIndent
return 1 unless precedingComment?.newLine
我们不希望例如 if
条件末尾的隐式调用包含后面的缩进注释。
return 1 if token.generated and token[0] is 'CALL_END' and precedingComment?.indented
prevLocationData = precedingComment.locationData if precedingComment?
token[2] =
first_line:
if precedingComment?
prevLocationData.first_line
else
prevLocationData.last_line
first_column:
if precedingComment?
if isIndent
0
else
prevLocationData.first_column
else
prevLocationData.last_column
last_line: prevLocationData.last_line
last_column: prevLocationData.last_column
last_line_exclusive: prevLocationData.last_line_exclusive
last_column_exclusive: prevLocationData.last_column_exclusive
range:
if isIndent and precedingComment?
[
prevLocationData.range[0] - precedingComment.indentSize
prevLocationData.range[1]
]
else
prevLocationData.range
return 1
因为我们的语法是 LALR(1),所以它无法处理一些缺少结束分隔符的单行表达式。**Rewriter** 添加了隐式块,因此它不需要这样做。为了保持语法的简洁和整洁,表达式内的尾随换行符将被删除,并且空块的缩进标记将被添加。
normalizeLines: ->
starter = indent = outdent = null
leading_switch_when = null
leading_if_then = null
计算 THEN
标记
ifThens = []
condition = (token, i) ->
token[1] isnt ';' and token[0] in SINGLE_CLOSERS and
not (token[0] is 'TERMINATOR' and @tag(i + 1) in EXPRESSION_CLOSE) and
not (token[0] is 'ELSE' and
(starter isnt 'THEN' or (leading_if_then or leading_switch_when))) and
not (token[0] in ['CATCH', 'FINALLY'] and starter in ['->', '=>']) or
token[0] in CALL_CLOSERS and
(@tokens[i - 1].newLine or @tokens[i - 1][0] is 'OUTDENT')
action = (token, i) ->
ifThens.pop() if token[0] is 'ELSE' and starter is 'THEN'
@tokens.splice (if @tag(i - 1) is ',' then i - 1 else i), 0, outdent
closeElseTag = (tokens, i) =>
tlen = ifThens.length
return i unless tlen > 0
lastThen = ifThens.pop()
[, outdentElse] = @indentation tokens[lastThen]
插入 OUTDENT
以关闭内部 IF
。
outdentElse[1] = tlen*2
tokens.splice(i, 0, outdentElse)
插入 OUTDENT
以关闭外部 IF
。
outdentElse[1] = 2
tokens.splice(i + 1, 0, outdentElse)
从末尾删除 outdent。
@detectEnd i + 2,
(token, i) -> token[0] in ['OUTDENT', 'TERMINATOR']
(token, i) ->
if @tag(i) is 'OUTDENT' and @tag(i + 1) is 'OUTDENT'
tokens.splice i, 2
i + 2
@scanTokens (token, i, tokens) ->
[tag] = token
conditionTag = tag in ['->', '=>'] and
@findTagsBackwards(i, ['IF', 'WHILE', 'FOR', 'UNTIL', 'SWITCH', 'WHEN', 'LEADING_WHEN', '[', 'INDEX_START']) and
not (@findTagsBackwards i, ['THEN', '..', '...'])
if tag is 'TERMINATOR'
if @tag(i + 1) is 'ELSE' and @tag(i - 1) isnt 'OUTDENT'
tokens.splice i, 1, @indentation()...
return 1
if @tag(i + 1) in EXPRESSION_CLOSE
if token[1] is ';' and @tag(i + 1) is 'OUTDENT'
tokens[i + 1].prevToken = token
moveComments token, tokens[i + 1]
tokens.splice i, 1
return 0
if tag is 'CATCH'
for j in [1..2] when @tag(i + j) in ['OUTDENT', 'TERMINATOR', 'FINALLY']
tokens.splice i + j, 0, @indentation()...
return 2 + j
if tag in ['->', '=>'] and (@tag(i + 1) in [',', ']'] or @tag(i + 1) is '.' and token.newLine)
[indent, outdent] = @indentation tokens[i]
tokens.splice i + 1, 0, indent, outdent
return 1
if tag in SINGLE_LINERS and @tag(i + 1) isnt 'INDENT' and
not (tag is 'ELSE' and @tag(i + 1) is 'IF') and
not conditionTag
starter = tag
[indent, outdent] = @indentation tokens[i]
indent.fromThen = true if starter is 'THEN'
if tag is 'THEN'
leading_switch_when = @findTagsBackwards(i, ['LEADING_WHEN']) and @tag(i + 1) is 'IF'
leading_if_then = @findTagsBackwards(i, ['IF']) and @tag(i + 1) is 'IF'
ifThens.push i if tag is 'THEN' and @findTagsBackwards(i, ['IF'])
ELSE
标记未关闭。
if tag is 'ELSE' and @tag(i - 1) isnt 'OUTDENT'
i = closeElseTag tokens, i
tokens.splice i + 1, 0, indent
@detectEnd i + 2, condition, action
tokens.splice i, 1 if tag is 'THEN'
return 1
return 1
将后缀条件语句标记为后缀条件语句,以便我们可以使用不同的优先级解析它们。
tagPostfixConditionals: ->
original = null
condition = (token, i) ->
[tag] = token
[prevTag] = @tokens[i - 1]
tag is 'TERMINATOR' or (tag is 'INDENT' and prevTag not in SINGLE_LINERS)
action = (token, i) ->
if token[0] isnt 'INDENT' or (token.generated and not token.fromThen)
original[0] = 'POST_' + original[0]
@scanTokens (token, i) ->
return 1 unless token[0] is 'IF'
original = token
@detectEnd i + 1, condition, action
return 1
对于具有额外数据的标记,我们希望通过将标记值包装为 String() 对象并将数据设置为该对象的属性来使语法可见。然后,语法应该负责为节点构造函数清理此操作:将标记值解包为原始字符串,并分别传递任何预期的标记数据属性
exposeTokenDataToGrammar: ->
@scanTokens (token, i) ->
if token.generated or (token.data and Object.keys(token.data).length isnt 0)
token[1] = new String token[1]
token[1][key] = val for own key, val of (token.data ? {})
token[1].generated = yes if token.generated
1
根据同一行上的另一个标记生成缩进标记。
indentation: (origin) ->
indent = ['INDENT', 2]
outdent = ['OUTDENT', 2]
if origin
indent.generated = outdent.generated = yes
indent.origin = outdent.origin = origin
else
indent.explicit = outdent.explicit = yes
[indent, outdent]
generate: generate
通过标记索引查找标记。
tag: (i) -> @tokens[i]?[0]
必须平衡的标记对列表。
BALANCED_PAIRS = [
['(', ')']
['[', ']']
['{', '}']
['INDENT', 'OUTDENT'],
['CALL_START', 'CALL_END']
['PARAM_START', 'PARAM_END']
['INDEX_START', 'INDEX_END']
['STRING_START', 'STRING_END']
['INTERPOLATION_START', 'INTERPOLATION_END']
['REGEX_START', 'REGEX_END']
]
我们正在尝试修复的 BALANCED_PAIRS
的反向映射,以便我们可以从任一端查找内容。
exports.INVERSES = INVERSES = {}
表示平衡对的开始/结束的标记。
EXPRESSION_START = []
EXPRESSION_END = []
for [left, right] in BALANCED_PAIRS
EXPRESSION_START.push INVERSES[right] = left
EXPRESSION_END .push INVERSES[left] = right
表示表达式子句结束的标记。
EXPRESSION_CLOSE = ['CATCH', 'THEN', 'ELSE', 'FINALLY'].concat EXPRESSION_END
如果后面跟着 IMPLICIT_CALL
,则表示函数调用的标记。
IMPLICIT_FUNC = ['IDENTIFIER', 'PROPERTY', 'SUPER', ')', 'CALL_END', ']', 'INDEX_END', '@', 'THIS']
如果前面是 IMPLICIT_FUNC
,则表示函数调用。
IMPLICIT_CALL = [
'IDENTIFIER', 'JSX_TAG', 'PROPERTY', 'NUMBER', 'INFINITY', 'NAN'
'STRING', 'STRING_START', 'REGEX', 'REGEX_START', 'JS'
'NEW', 'PARAM_START', 'CLASS', 'IF', 'TRY', 'SWITCH', 'THIS'
'DYNAMIC_IMPORT', 'IMPORT_META', 'NEW_TARGET'
'UNDEFINED', 'NULL', 'BOOL'
'UNARY', 'DO', 'DO_IIFE', 'YIELD', 'AWAIT', 'UNARY_MATH', 'SUPER', 'THROW'
'@', '->', '=>', '[', '(', '{', '--', '++'
]
IMPLICIT_UNSPACED_CALL = ['+', '-']
始终标记单行表达式隐式调用结束的标记。
IMPLICIT_END = ['POST_IF', 'FOR', 'WHILE', 'UNTIL', 'WHEN', 'BY',
'LOOP', 'TERMINATOR']
具有未闭合结尾的块表达式的单行风格。语法无法区分它们,因此我们插入隐式缩进。
SINGLE_LINERS = ['ELSE', '->', '=>', 'TRY', 'FINALLY', 'THEN']
SINGLE_CLOSERS = ['TERMINATOR', 'CATCH', 'FINALLY', 'ELSE', 'OUTDENT', 'LEADING_WHEN']
结束一行的标记。
LINEBREAKS = ['TERMINATOR', 'INDENT', 'OUTDENT']
在换行符之后,关闭打开的调用的标记。
CALL_CLOSERS = ['.', '?.', '::', '?::']
阻止后续缩进结束隐式调用/对象的标记
CONTROL_IN_IMPLICIT = ['IF', 'TRY', 'FINALLY', 'CATCH', 'CLASS', 'SWITCH']
被解析器吞噬的标记,永远不会导致代码生成。你可以在 grammar.coffee
中发现这些标记,因为 o
函数的第二个参数不包含这些标记的 new
调用。STRING_START
不在此列表中,因为它的 locationData
与成为 StringWithInterpolations
的节点的 locationData
相匹配,因此 addDataToNode
将 STRING_START
的标记附加到该节点。
DISCARDED = ['(', ')', '[', ']', '{', '}', ':', '.', '..', '...', ',', '=', '++', '--', '?',
'AS', 'AWAIT', 'CALL_START', 'CALL_END', 'DEFAULT', 'DO', 'DO_IIFE', 'ELSE',
'EXTENDS', 'EXPORT', 'FORIN', 'FOROF', 'FORFROM', 'IMPORT', 'INDENT', 'INDEX_SOAK',
'INTERPOLATION_START', 'INTERPOLATION_END', 'LEADING_WHEN', 'OUTDENT', 'PARAM_END',
'REGEX_START', 'REGEX_END', 'RETURN', 'STRING_END', 'THROW', 'UNARY', 'YIELD'
].concat IMPLICIT_UNSPACED_CALL.concat IMPLICIT_END.concat CALL_CLOSERS.concat CONTROL_IN_IMPLICIT
当出现在行尾时,抑制后续 TERMINATOR/INDENT 标记的标记
exports.UNFINISHED = UNFINISHED = ['\\', '.', '?.', '?::', 'UNARY', 'DO', 'DO_IIFE', 'MATH', 'UNARY_MATH', '+', '-',
'**', 'SHIFT', 'RELATION', 'COMPARE', '&', '^', '|', '&&', '||',
'BIN?', 'EXTENDS']