• 跳至 … +
    browser.coffee cake.coffee coffeescript.coffee command.coffee grammar.coffee helpers.coffee index.coffee lexer.coffee nodes.coffee optparse.coffee register.coffee repl.coffee rewriter.coffee scope.litcoffee sourcemap.litcoffee
  • lexer.coffee

  • §

    CoffeeScript 詞法分析器。使用一系列代幣配對正規表示法嘗試與原始碼開頭進行配對。找到配對時,會產生一個代幣,我們使用配對,然後重新開始。代幣的格式為

    [tag, value, locationData]
    

    其中 locationData 為 {first_line, first_column, last_line, last_column, last_line_exclusive, last_column_exclusive},這是一種可以直接饋入 Jison 的格式。這些內容會由 coffeescript.coffee 中定義的 parser.lexer 函數在 jison 中讀取。

    {Rewriter, INVERSES, UNFINISHED} = require './rewriter'
  • §

    匯入我們需要的輔助程式。

    {count, starts, compact, repeat, invertLiterate, merge,
    attachCommentsToNode, locationDataToString, throwSyntaxError
    replaceUnicodeCodePointEscapes, flatten, parseNumber} = require './helpers'
  • §

    詞法分析器類別

  • §
  • §

    詞法分析器類別會讀取 CoffeeScript 串流,並將其分成標記代幣。詞法分析器已加入一些額外智慧,避免語法中潛在的歧義。

    exports.Lexer = class Lexer
  • §

    tokenize 是詞法分析器的主要方法。透過嘗試使用固定在剩餘程式碼開頭的正規表示法,或自訂遞迴代幣配對方法(用於內插),逐一配對代幣來進行掃描。記錄下一個代幣後,我們會在程式碼中向前移動,超過該代幣,然後重新開始。

    每個代幣化方法負責傳回它已使用字元的數量。

    在傳回 token 串流之前,透過 Rewriter 執行它。

      tokenize: (code, opts = {}) ->
        @literate   = opts.literate  # Are we lexing literate CoffeeScript?
        @indent     = 0              # The current indentation level.
        @baseIndent = 0              # The overall minimum indentation level.
        @continuationLineAdditionalIndent = 0 # The over-indentation at the current level.
        @outdebt    = 0              # The under-outdentation at the current level.
        @indents    = []             # The stack of all current indentation levels.
        @indentLiteral = ''          # The indentation.
        @ends       = []             # The stack for pairing up tokens.
        @tokens     = []             # Stream of parsed tokens in the form `['TYPE', value, location data]`.
        @seenFor    = no             # Used to recognize `FORIN`, `FOROF` and `FORFROM` tokens.
        @seenImport = no             # Used to recognize `IMPORT FROM? AS?` tokens.
        @seenExport = no             # Used to recognize `EXPORT FROM? AS?` tokens.
        @importSpecifierList = no    # Used to identify when in an `IMPORT {...} FROM? ...`.
        @exportSpecifierList = no    # Used to identify when in an `EXPORT {...} FROM? ...`.
        @jsxDepth = 0                # Used to optimize JSX checks, how deep in JSX we are.
        @jsxObjAttribute = {}        # Used to detect if JSX attributes is wrapped in {} (<div {props...} />).
    
        @chunkLine =
          opts.line or 0             # The start line for the current @chunk.
        @chunkColumn =
          opts.column or 0           # The start column of the current @chunk.
        @chunkOffset =
          opts.offset or 0           # The start offset for the current @chunk.
        @locationDataCompensations =
          opts.locationDataCompensations or {} # The location data compensations for the current @chunk.
        code = @clean code           # The stripped, cleaned original source code.
  • §

    在每個位置,執行這個嘗試比對的清單,如果其中任何一個成功,則短路。它們的順序決定優先順序:@literalToken 是後備萬用字元。

        i = 0
        while @chunk = code[i..]
          consumed = \
               @identifierToken() or
               @commentToken()    or
               @whitespaceToken() or
               @lineToken()       or
               @stringToken()     or
               @numberToken()     or
               @jsxToken()        or
               @regexToken()      or
               @jsToken()         or
               @literalToken()
  • §

    更新位置。

          [@chunkLine, @chunkColumn, @chunkOffset] = @getLineAndColumnFromChunk consumed
    
          i += consumed
    
          return {@tokens, index: i} if opts.untilBalanced and @ends.length is 0
    
        @closeIndentation()
        @error "missing #{end.tag}", (end.origin ? end)[2] if end = @ends.pop()
        return @tokens if opts.rewrite is off
        (new Rewriter).rewrite @tokens
  • §

    預處理程式碼以移除前導和尾隨空白、換行符號等。如果我們要分析識別 CoffeeScript,請移除所有縮排少於四個空格或一個 tab 的行,以移除外部 Markdown。

      clean: (code) ->
        thusFar = 0
        if code.charCodeAt(0) is BOM
          code = code.slice 1
          @locationDataCompensations[0] = 1
          thusFar += 1
        if WHITESPACE.test code
          code = "\n#{code}"
          @chunkLine--
          @locationDataCompensations[0] ?= 0
          @locationDataCompensations[0] -= 1
        code = code
          .replace /\r/g, (match, offset) =>
            @locationDataCompensations[thusFar + offset] = 1
            ''
          .replace TRAILING_SPACES, ''
        code = invertLiterate code if @literate
        code
  • §

    Tokenizers

  • §
  • §

    比對識別文字的項目:變數、關鍵字、方法名稱等。檢查以確保 JavaScript 保留字未用作識別碼。由於 CoffeeScript 保留了少數 JavaScript 中允許的關鍵字,因此我們小心不要在這裡將它們作為屬性名稱時標記為關鍵字,所以您仍然可以執行 jQuery.is(),即使 is 否則表示 ===。

      identifierToken: ->
        inJSXTag = @atJSXTag()
        regex = if inJSXTag then JSX_ATTRIBUTE else IDENTIFIER
        return 0 unless match = regex.exec @chunk
        [input, id, colon] = match
  • §

    保留 id 的長度以供位置資料使用

        idLength = id.length
        poppedToken = undefined
        if id is 'own' and @tag() is 'FOR'
          @token 'OWN', id
          return id.length
        if id is 'from' and @tag() is 'YIELD'
          @token 'FROM', id
          return id.length
        if id is 'as' and @seenImport
          if @value() is '*'
            @tokens[@tokens.length - 1][0] = 'IMPORT_ALL'
          else if @value(yes) in COFFEE_KEYWORDS
            prev = @prev()
            [prev[0], prev[1]] = ['IDENTIFIER', @value(yes)]
          if @tag() in ['DEFAULT', 'IMPORT_ALL', 'IDENTIFIER']
            @token 'AS', id
            return id.length
        if id is 'as' and @seenExport
          if @tag() in ['IDENTIFIER', 'DEFAULT']
            @token 'AS', id
            return id.length
          if @value(yes) in COFFEE_KEYWORDS
            prev = @prev()
            [prev[0], prev[1]] = ['IDENTIFIER', @value(yes)]
            @token 'AS', id
            return id.length
        if id is 'default' and @seenExport and @tag() in ['EXPORT', 'AS']
          @token 'DEFAULT', id
          return id.length
        if id is 'assert' and (@seenImport or @seenExport) and @tag() is 'STRING'
          @token 'ASSERT', id
          return id.length
        if id is 'do' and regExSuper = /^(\s*super)(?!\(\))/.exec @chunk[3...]
          @token 'SUPER', 'super'
          @token 'CALL_START', '('
          @token 'CALL_END', ')'
          [input, sup] = regExSuper
          return sup.length + 3
    
        prev = @prev()
    
        tag =
          if colon or prev? and
             (prev[0] in ['.', '?.', '::', '?::'] or
             not prev.spaced and prev[0] is '@')
            'PROPERTY'
          else
            'IDENTIFIER'
    
        tokenData = {}
        if tag is 'IDENTIFIER' and (id in JS_KEYWORDS or id in COFFEE_KEYWORDS) and
           not (@exportSpecifierList and id in COFFEE_KEYWORDS)
          tag = id.toUpperCase()
          if tag is 'WHEN' and @tag() in LINE_BREAK
            tag = 'LEADING_WHEN'
          else if tag is 'FOR'
            @seenFor = {endsLength: @ends.length}
          else if tag is 'UNLESS'
            tag = 'IF'
          else if tag is 'IMPORT'
            @seenImport = yes
          else if tag is 'EXPORT'
            @seenExport = yes
          else if tag in UNARY
            tag = 'UNARY'
          else if tag in RELATION
            if tag isnt 'INSTANCEOF' and @seenFor
              tag = 'FOR' + tag
              @seenFor = no
            else
              tag = 'RELATION'
              if @value() is '!'
                poppedToken = @tokens.pop()
                tokenData.invert = poppedToken.data?.original ? poppedToken[1]
        else if tag is 'IDENTIFIER' and @seenFor and id is 'from' and
           isForFrom(prev)
          tag = 'FORFROM'
          @seenFor = no
  • §

    對嘗試使用 get 或 set 作為關鍵字或 CoffeeScript 通常會解釋為對稱為 get 或 set 的函式的呼叫 (例如 get({foo: function () {}})) 擲回錯誤。

        else if tag is 'PROPERTY' and prev
          if prev.spaced and prev[0] in CALLABLE and /^[gs]et$/.test(prev[1]) and
             @tokens.length > 1 and @tokens[@tokens.length - 2][0] not in ['.', '?.', '@']
            @error "'#{prev[1]}' cannot be used as a keyword, or as a function call
            without parentheses", prev[2]
          else if prev[0] is '.' and @tokens.length > 1 and (prevprev = @tokens[@tokens.length - 2])[0] is 'UNARY' and prevprev[1] is 'new'
            prevprev[0] = 'NEW_TARGET'
          else if prev[0] is '.' and @tokens.length > 1 and (prevprev = @tokens[@tokens.length - 2])[0] is 'IMPORT' and prevprev[1] is 'import'
            @seenImport = no
            prevprev[0] = 'IMPORT_META'
          else if @tokens.length > 2
            prevprev = @tokens[@tokens.length - 2]
            if prev[0] in ['@', 'THIS'] and prevprev and prevprev.spaced and
               /^[gs]et$/.test(prevprev[1]) and
               @tokens[@tokens.length - 3][0] not in ['.', '?.', '@']
              @error "'#{prevprev[1]}' cannot be used as a keyword, or as a
              function call without parentheses", prevprev[2]
    
        if tag is 'IDENTIFIER' and id in RESERVED and not inJSXTag
          @error "reserved word '#{id}'", length: id.length
    
        unless tag is 'PROPERTY' or @exportSpecifierList or @importSpecifierList
          if id in COFFEE_ALIASES
            alias = id
            id = COFFEE_ALIAS_MAP[id]
            tokenData.original = alias
          tag = switch id
            when '!'                 then 'UNARY'
            when '==', '!='          then 'COMPARE'
            when 'true', 'false'     then 'BOOL'
            when 'break', 'continue', \
                 'debugger'          then 'STATEMENT'
            when '&&', '||'          then id
            else  tag
    
        tagToken = @token tag, id, length: idLength, data: tokenData
        tagToken.origin = [tag, alias, tagToken[2]] if alias
        if poppedToken
          [tagToken[2].first_line, tagToken[2].first_column, tagToken[2].range[0]] =
            [poppedToken[2].first_line, poppedToken[2].first_column, poppedToken[2].range[0]]
        if colon
          colonOffset = input.lastIndexOf if inJSXTag then '=' else ':'
          colonToken = @token ':', ':', offset: colonOffset
          colonToken.jsxColon = yes if inJSXTag # used by rewriter
        if inJSXTag and tag is 'IDENTIFIER' and prev[0] isnt ':'
          @token ',', ',', length: 0, origin: tagToken, generated: yes
    
        input.length
  • §

    比對數字,包括小數、十六進位和指數表示法。小心不要干擾進行中的範圍。

      numberToken: ->
        return 0 unless match = NUMBER.exec @chunk
    
        number = match[0]
        lexedLength = number.length
    
        switch
          when /^0[BOX]/.test number
            @error "radix prefix in '#{number}' must be lowercase", offset: 1
          when /^0\d*[89]/.test number
            @error "decimal literal '#{number}' must not be prefixed with '0'", length: lexedLength
          when /^0\d+/.test number
            @error "octal literal '#{number}' must be prefixed with '0o'", length: lexedLength
    
        parsedValue = parseNumber number
        tokenData = {parsedValue}
    
        tag = if parsedValue is Infinity then 'INFINITY' else 'NUMBER'
        if tag is 'INFINITY'
          tokenData.original = number
        @token tag, number,
          length: lexedLength
          data: tokenData
        lexedLength
  • §

    比對字串,包括多行字串以及帶或不帶內插的 heredocs。

      stringToken: ->
        [quote] = STRING_START.exec(@chunk) || []
        return 0 unless quote
  • §

    如果前一個 token 是 from,而這是 import 或 export 陳述式,請正確標記 from。

        prev = @prev()
        if prev and @value() is 'from' and (@seenImport or @seenExport)
          prev[0] = 'FROM'
    
        regex = switch quote
          when "'"   then STRING_SINGLE
          when '"'   then STRING_DOUBLE
          when "'''" then HEREDOC_SINGLE
          when '"""' then HEREDOC_DOUBLE
    
        {tokens, index: end} = @matchWithInterpolations regex, quote
    
        heredoc = quote.length is 3
        if heredoc
  • §

    找出最小的縮排。稍後會從所有行中移除它。

          indent = null
          doc = (token[1] for token, i in tokens when token[0] is 'NEOSTRING').join '#{}'
          while match = HEREDOC_INDENT.exec doc
            attempt = match[1]
            indent = attempt if indent is null or 0 < attempt.length < indent.length
    
        delimiter = quote.charAt(0)
        @mergeInterpolationTokens tokens, {quote, indent, endOffset: end}, (value) =>
          @validateUnicodeCodePointEscapes value, delimiter: quote
    
        if @atJSXTag()
          @token ',', ',', length: 0, origin: @prev, generated: yes
    
        end
  • §

    比對並使用註解。註解會從代碼串流中取出並儲存以供稍後使用,在解析完所有內容並產生 JavaScript 程式碼後,會將註解重新插入輸出中。

      commentToken: (chunk = @chunk, {heregex, returnCommentTokens = no, offsetInChunk = 0} = {}) ->
        return 0 unless match = chunk.match COMMENT
        [commentWithSurroundingWhitespace, hereLeadingWhitespace, hereComment, hereTrailingWhitespace, lineComment] = match
        contents = null
  • §

    此註解是否與同一行中的程式碼相符?

        leadingNewline = /^\s*\n+\s*#/.test commentWithSurroundingWhitespace
        if hereComment
          matchIllegal = HERECOMMENT_ILLEGAL.exec hereComment
          if matchIllegal
            @error "block comments cannot contain #{matchIllegal[0]}",
              offset: '###'.length + matchIllegal.index, length: matchIllegal[0].length
  • §

    解析縮排或取消縮排,就像此區塊註解不存在一樣。

          chunk = chunk.replace "####{hereComment}###", ''
  • §

    移除開頭的新行,例如 Rewriter::removeLeadingNewlines,以避免產生不需要的 TERMINATOR 代碼。

          chunk = chunk.replace /^\n+/, ''
          @lineToken {chunk}
  • §

    取出 ### 風格註解的內容,並格式化它。

          content = hereComment
          contents = [{
            content
            length: commentWithSurroundingWhitespace.length - hereLeadingWhitespace.length - hereTrailingWhitespace.length
            leadingWhitespace: hereLeadingWhitespace
          }]
        else
  • §

    COMMENT 正規表示法將連續的行註解擷取為一個代碼。移除第一個註解前的所有開頭新行,但保留行註解之間的空白行。

          leadingNewlines = ''
          content = lineComment.replace /^(\n*)/, (leading) ->
            leadingNewlines = leading
            ''
          precedingNonCommentLines = ''
          hasSeenFirstCommentLine = no
          contents =
            content.split '\n'
            .map (line, index) ->
              unless line.indexOf('#') > -1
                precedingNonCommentLines += "\n#{line}"
                return
              leadingWhitespace = ''
              content = line.replace /^([ |\t]*)#/, (_, whitespace) ->
                leadingWhitespace = whitespace
                ''
              comment = {
                content
                length: '#'.length + content.length
                leadingWhitespace: "#{unless hasSeenFirstCommentLine then leadingNewlines else ''}#{precedingNonCommentLines}#{leadingWhitespace}"
                precededByBlankLine: !!precedingNonCommentLines
              }
              hasSeenFirstCommentLine = yes
              precedingNonCommentLines = ''
              comment
            .filter (comment) -> comment
    
        getIndentSize = ({leadingWhitespace, nonInitial}) ->
          lastNewlineIndex = leadingWhitespace.lastIndexOf '\n'
          if hereComment? or not nonInitial
            return null unless lastNewlineIndex > -1
          else
            lastNewlineIndex ?= -1
          leadingWhitespace.length - 1 - lastNewlineIndex
        commentAttachments = for {content, length, leadingWhitespace, precededByBlankLine}, i in contents
          nonInitial = i isnt 0
          leadingNewlineOffset = if nonInitial then 1 else 0
          offsetInChunk += leadingNewlineOffset + leadingWhitespace.length
          indentSize = getIndentSize {leadingWhitespace, nonInitial}
          noIndent = not indentSize? or indentSize is -1
          commentAttachment = {
            content
            here: hereComment?
            newLine: leadingNewline or nonInitial # Line comments after the first one start new lines, by definition.
            locationData: @makeLocationData {offsetInChunk, length}
            precededByBlankLine
            indentSize
            indented:  not noIndent and indentSize > @indent
            outdented: not noIndent and indentSize < @indent
          }
          commentAttachment.heregex = yes if heregex
          offsetInChunk += length
          commentAttachment
    
        prev = @prev()
        unless prev
  • §

    如果沒有前一個代碼,請建立一個暫存代碼來附加此註解;並加上一個新行。

          commentAttachments[0].newLine = yes
          @lineToken chunk: @chunk[commentWithSurroundingWhitespace.length..], offset: commentWithSurroundingWhitespace.length # Set the indent.
          placeholderToken = @makeToken 'JS', '', offset: commentWithSurroundingWhitespace.length, generated: yes
          placeholderToken.comments = commentAttachments
          @tokens.push placeholderToken
          @newlineToken commentWithSurroundingWhitespace.length
        else
          attachCommentsToNode commentAttachments, prev
    
        return commentAttachments if returnCommentTokens
        commentWithSurroundingWhitespace.length
  • §

    比對直接透過反引號插入來源的 JavaScript。

      jsToken: ->
        return 0 unless @chunk.charAt(0) is '`' and
          (match = (matchedHere = HERE_JSTOKEN.exec(@chunk)) or JSTOKEN.exec(@chunk))
  • §

    將跳脫的反引號轉換為反引號,以及跳脫反引號前方的跳脫反斜線轉換為反斜線

        script = match[1]
        {length} = match[0]
        @token 'JS', script, {length, data: {here: !!matchedHere}}
        length
  • §

    比對正規表示法文字,以及多行延伸正規表示法文字。正規表示法的詞法分析難以與除法區分,因此我們借用了 JavaScript 和 Ruby 的一些基本啟發法。

      regexToken: ->
        switch
          when match = REGEX_ILLEGAL.exec @chunk
            @error "regular expressions cannot begin with #{match[2]}",
              offset: match.index + match[1].length
          when match = @matchWithInterpolations HEREGEX, '///'
            {tokens, index} = match
            comments = []
            while matchedComment = HEREGEX_COMMENT.exec @chunk[0...index]
              {index: commentIndex} = matchedComment
              [fullMatch, leadingWhitespace, comment] = matchedComment
              comments.push {comment, offsetInChunk: commentIndex + leadingWhitespace.length}
            commentTokens = flatten(
              for commentOpts in comments
                @commentToken commentOpts.comment, Object.assign commentOpts, heregex: yes, returnCommentTokens: yes
            )
          when match = REGEX.exec @chunk
            [regex, body, closed] = match
            @validateEscapes body, isRegex: yes, offsetInChunk: 1
            index = regex.length
            prev = @prev()
            if prev
              if prev.spaced and prev[0] in CALLABLE
                return 0 if not closed or POSSIBLY_DIVISION.test regex
              else if prev[0] in NOT_REGEX
                return 0
            @error 'missing / (unclosed regex)' unless closed
          else
            return 0
    
        [flags] = REGEX_FLAGS.exec @chunk[index..]
        end = index + flags.length
        origin = @makeToken 'REGEX', null, length: end
        switch
          when not VALID_FLAGS.test flags
            @error "invalid regular expression flags #{flags}", offset: index, length: flags.length
          when regex or tokens.length is 1
            delimiter = if body then '/' else '///'
            body ?= tokens[0][1]
            @validateUnicodeCodePointEscapes body, {delimiter}
            @token 'REGEX', "/#{body}/#{flags}", {length: end, origin, data: {delimiter}}
          else
            @token 'REGEX_START', '(',    {length: 0, origin, generated: yes}
            @token 'IDENTIFIER', 'RegExp', length: 0, generated: yes
            @token 'CALL_START', '(',      length: 0, generated: yes
            @mergeInterpolationTokens tokens, {double: yes, heregex: {flags}, endOffset: end - flags.length, quote: '///'}, (str) =>
              @validateUnicodeCodePointEscapes str, {delimiter}
            if flags
              @token ',', ',',                    offset: index - 1, length: 0, generated: yes
              @token 'STRING', '"' + flags + '"', offset: index,     length: flags.length
            @token ')', ')',                      offset: end,       length: 0, generated: yes
            @token 'REGEX_END', ')',              offset: end,       length: 0, generated: yes
  • §

    明確地將任何 heregex 註解附加到 REGEX/REGEX_END 代碼。

        if commentTokens?.length
          addTokenData @tokens[@tokens.length - 1],
            heregexCommentTokens: commentTokens
    
        end
  • §

    比對新行、縮排和取消縮排,並判斷它們是什麼。如果我們可以偵測到目前行已繼續到下一行,則會抑制新行

    elements
      .each( ... )
      .map( ... )
    

    追蹤縮排層級,因為單一縮排取消代碼可以關閉多個縮排,所以我們需要知道我們目前在哪個層級。

      lineToken: ({chunk = @chunk, offset = 0} = {}) ->
        return 0 unless match = MULTI_DENT.exec chunk
        indent = match[0]
    
        prev = @prev()
        backslash = prev?[0] is '\\'
        @seenFor = no unless (backslash or @seenFor?.endsLength < @ends.length) and @seenFor
        @seenImport = no unless (backslash and @seenImport) or @importSpecifierList
        @seenExport = no unless (backslash and @seenExport) or @exportSpecifierList
    
        size = indent.length - 1 - indent.lastIndexOf '\n'
        noNewlines = @unfinished()
    
        newIndentLiteral = if size > 0 then indent[-size..] else ''
        unless /^(.?)\1*$/.exec newIndentLiteral
          @error 'mixed indentation', offset: indent.length
          return indent.length
    
        minLiteralLength = Math.min newIndentLiteral.length, @indentLiteral.length
        if newIndentLiteral[...minLiteralLength] isnt @indentLiteral[...minLiteralLength]
          @error 'indentation mismatch', offset: indent.length
          return indent.length
    
        if size - @continuationLineAdditionalIndent is @indent
          if noNewlines then @suppressNewlines() else @newlineToken offset
          return indent.length
    
        if size > @indent
          if noNewlines
            @continuationLineAdditionalIndent = size - @indent unless backslash
            if @continuationLineAdditionalIndent
              prev.continuationLineIndent = @indent + @continuationLineAdditionalIndent
            @suppressNewlines()
            return indent.length
          unless @tokens.length
            @baseIndent = @indent = size
            @indentLiteral = newIndentLiteral
            return indent.length
          diff = size - @indent + @outdebt
          @token 'INDENT', diff, offset: offset + indent.length - size, length: size
          @indents.push diff
          @ends.push {tag: 'OUTDENT'}
          @outdebt = @continuationLineAdditionalIndent = 0
          @indent = size
          @indentLiteral = newIndentLiteral
        else if size < @baseIndent
          @error 'missing indentation', offset: offset + indent.length
        else
          endsContinuationLineIndentation = @continuationLineAdditionalIndent > 0
          @continuationLineAdditionalIndent = 0
          @outdentToken {moveOut: @indent - size, noNewlines, outdentLength: indent.length, offset, indentSize: size, endsContinuationLineIndentation}
        indent.length
  • §

    記錄縮排取消代碼或多個代碼,如果我們剛好往回移動超過幾個已記錄的縮排。設定新的 @indent 值。

      outdentToken: ({moveOut, noNewlines, outdentLength = 0, offset = 0, indentSize, endsContinuationLineIndentation}) ->
        decreasedIndent = @indent - moveOut
        while moveOut > 0
          lastIndent = @indents[@indents.length - 1]
          if not lastIndent
            @outdebt = moveOut = 0
          else if @outdebt and moveOut <= @outdebt
            @outdebt -= moveOut
            moveOut   = 0
          else
            dent = @indents.pop() + @outdebt
            if outdentLength and @chunk[outdentLength] in INDENTABLE_CLOSERS
              decreasedIndent -= dent - moveOut
              moveOut = dent
            @outdebt = 0
  • §

    配對可能會呼叫縮排取消代碼,所以保留 decreasedIndent

            @pair 'OUTDENT'
            @token 'OUTDENT', moveOut, length: outdentLength, indentSize: indentSize + moveOut - dent
            moveOut -= dent
        @outdebt -= moveOut if dent
        @suppressSemicolons()
    
        unless @tag() is 'TERMINATOR' or noNewlines
          terminatorToken = @token 'TERMINATOR', '\n', offset: offset + outdentLength, length: 0
          terminatorToken.endsContinuationLineIndentation = {preContinuationLineIndent: @indent} if endsContinuationLineIndentation
        @indent = decreasedIndent
        @indentLiteral = @indentLiteral[...decreasedIndent]
        this
  • §

    比對並消耗沒有意義的空白。標記前一個代碼為「有空白」,因為在某些情況下會有差別。

      whitespaceToken: ->
        return 0 unless (match = WHITESPACE.exec @chunk) or
                        (nline = @chunk.charAt(0) is '\n')
        prev = @prev()
        prev[if match then 'spaced' else 'newLine'] = true if prev
        if match then match[0].length else 0
  • §

    產生換行代碼。連續換行會合併在一起。

      newlineToken: (offset) ->
        @suppressSemicolons()
        @token 'TERMINATOR', '\n', {offset, length: 0} unless @tag() is 'TERMINATOR'
        this
  • §

    在行尾使用 \ 來抑制換行。斜線在完成工作後會在此移除。

      suppressNewlines: ->
        prev = @prev()
        if prev[1] is '\\'
          if prev.comments and @tokens.length > 1
  • §

    @tokens.length 應該至少為 2(一些程式碼,然後 \)。如果某個東西在沒有任何東西後放置 \,他們應該會失去任何緊接在後的註解。

            attachCommentsToNode prev.comments, @tokens[@tokens.length - 2]
          @tokens.pop()
        this
    
      jsxToken: ->
        firstChar = @chunk[0]
  • §

    檢查前一個代碼以偵測屬性是否已展開。

        prevChar = if @tokens.length > 0 then @tokens[@tokens.length - 1][0] else ''
        if firstChar is '<'
          match = JSX_IDENTIFIER.exec(@chunk[1...]) or JSX_FRAGMENT_IDENTIFIER.exec(@chunk[1...])
          return 0 unless match and (
            @jsxDepth > 0 or
  • §

    不是沒有空白的比較運算式右邊(例如 a<b)。

            not (prev = @prev()) or
            prev.spaced or
            prev[0] not in COMPARABLE_LEFT_SIDE
          )
          [input, id] = match
          fullId = id
          if '.' in id
            [id, properties...] = id.split '.'
          else
            properties = []
          tagToken = @token 'JSX_TAG', id,
            length: id.length + 1
            data:
              openingBracketToken: @makeToken '<', '<'
              tagNameToken: @makeToken 'IDENTIFIER', id, offset: 1
          offset = id.length + 1
          for property in properties
            @token '.', '.', {offset}
            offset += 1
            @token 'PROPERTY', property, {offset}
            offset += property.length
          @token 'CALL_START', '(', generated: yes
          @token '[', '[', generated: yes
          @ends.push {tag: '/>', origin: tagToken, name: id, properties}
          @jsxDepth++
          return fullId.length + 1
        else if jsxTag = @atJSXTag()
          if @chunk[...2] is '/>' # Self-closing tag.
            @pair '/>'
            @token ']', ']',
              length: 2
              generated: yes
            @token 'CALL_END', ')',
              length: 2
              generated: yes
              data:
                selfClosingSlashToken: @makeToken '/', '/'
                closingBracketToken: @makeToken '>', '>', offset: 1
            @jsxDepth--
            return 2
          else if firstChar is '{'
            if prevChar is ':'
  • §

    這個代碼代表 JSX 屬性值的開頭,是一個表達式(例如 <div a={b} /> 中的 {b})。我們的語法將表達式的開頭表示為 ( 代碼,所以將這變成一個顯示為 { 的 ( 代碼。

              token = @token '(', '{'
              @jsxObjAttribute[@jsxDepth] = no
  • §

    標記屬性名稱為 JSX

              addTokenData @tokens[@tokens.length - 3],
                jsx: yes
            else
              token = @token '{', '{'
              @jsxObjAttribute[@jsxDepth] = yes
            @ends.push {tag: '}', origin: token}
            return 1
          else if firstChar is '>' # end of opening tag
  • §

    忽略標籤內的終止符。

            {origin: openingTagToken} = @pair '/>' # As if the current tag was self-closing.
            @token ']', ']',
              generated: yes
              data:
                closingBracketToken: @makeToken '>', '>'
            @token ',', 'JSX_COMMA', generated: yes
            {tokens, index: end} =
              @matchWithInterpolations INSIDE_JSX, '>', '</', JSX_INTERPOLATION
            @mergeInterpolationTokens tokens, {endOffset: end, jsx: yes}, (value) =>
              @validateUnicodeCodePointEscapes value, delimiter: '>'
            match = JSX_IDENTIFIER.exec(@chunk[end...]) or JSX_FRAGMENT_IDENTIFIER.exec(@chunk[end...])
            if not match or match[1] isnt "#{jsxTag.name}#{(".#{property}" for property in jsxTag.properties).join ''}"
              @error "expected corresponding JSX closing tag for #{jsxTag.name}",
                jsxTag.origin.data.tagNameToken[2]
            [, fullTagName] = match
            afterTag = end + fullTagName.length
            if @chunk[afterTag] isnt '>'
              @error "missing closing > after tag name", offset: afterTag, length: 1
  • §

    開啟 </ 為 -2/+2,關閉 > 為 +1。

            endToken = @token 'CALL_END', ')',
              offset: end - 2
              length: fullTagName.length + 3
              generated: yes
              data:
                closingTagOpeningBracketToken: @makeToken '<', '<', offset: end - 2
                closingTagSlashToken: @makeToken '/', '/', offset: end - 1
  • §

    待辦事項:複雜標籤名稱的個別標記?例如 < / A . B >

                closingTagNameToken: @makeToken 'IDENTIFIER', fullTagName, offset: end
                closingTagClosingBracketToken: @makeToken '>', '>', offset: end + fullTagName.length
  • §

    讓語法更容易存取關閉標籤的位置資料

            addTokenData openingTagToken, endToken.data
            @jsxDepth--
            return afterTag + 1
          else
            return 0
        else if @atJSXTag 1
          if firstChar is '}'
            @pair firstChar
            if @jsxObjAttribute[@jsxDepth]
              @token '}', '}'
              @jsxObjAttribute[@jsxDepth] = no
            else
              @token ')', '}'
            @token ',', ',', generated: yes
            return 1
          else
            return 0
        else
          return 0
    
      atJSXTag: (depth = 0) ->
        return no if @jsxDepth is 0
        i = @ends.length - 1
        i-- while @ends[i]?.tag is 'OUTDENT' or depth-- > 0 # Ignore indents.
        last = @ends[i]
        last?.tag is '/>' and last
  • §

    我們將所有其他單一字元視為標記。例如:( ) , . ! 多字元運算子也是文字標記,以便 Jison 可以指定適當的運算順序。我們在此特別標記了一些符號。; 和換行符號都視為 TERMINATOR,我們區分表示方法呼叫的括號和一般括號,依此類推。

      literalToken: ->
        if match = OPERATOR.exec @chunk
          [value] = match
          @tagParameters() if CODE.test value
        else
          value = @chunk.charAt 0
        tag  = value
        prev = @prev()
    
        if prev and value in ['=', COMPOUND_ASSIGN...]
          skipToken = false
          if value is '=' and prev[1] in ['||', '&&'] and not prev.spaced
            prev[0] = 'COMPOUND_ASSIGN'
            prev[1] += '='
            prev.data.original += '=' if prev.data?.original
            prev[2].range = [
              prev[2].range[0]
              prev[2].range[1] + 1
            ]
            prev[2].last_column += 1
            prev[2].last_column_exclusive += 1
            prev = @tokens[@tokens.length - 2]
            skipToken = true
          if prev and prev[0] isnt 'PROPERTY'
            origin = prev.origin ? prev
            message = isUnassignable prev[1], origin[1]
            @error message, origin[2] if message
          return value.length if skipToken
    
        if value is '(' and prev?[0] is 'IMPORT'
          prev[0] = 'DYNAMIC_IMPORT'
    
        if value is '{' and @seenImport
          @importSpecifierList = yes
        else if @importSpecifierList and value is '}'
          @importSpecifierList = no
        else if value is '{' and prev?[0] is 'EXPORT'
          @exportSpecifierList = yes
        else if @exportSpecifierList and value is '}'
          @exportSpecifierList = no
    
        if value is ';'
          @error 'unexpected ;' if prev?[0] in ['=', UNFINISHED...]
          @seenFor = @seenImport = @seenExport = no
          tag = 'TERMINATOR'
        else if value is '*' and prev?[0] is 'EXPORT'
          tag = 'EXPORT_ALL'
        else if value in MATH            then tag = 'MATH'
        else if value in COMPARE         then tag = 'COMPARE'
        else if value in COMPOUND_ASSIGN then tag = 'COMPOUND_ASSIGN'
        else if value in UNARY           then tag = 'UNARY'
        else if value in UNARY_MATH      then tag = 'UNARY_MATH'
        else if value in SHIFT           then tag = 'SHIFT'
        else if value is '?' and prev?.spaced then tag = 'BIN?'
        else if prev
          if value is '(' and not prev.spaced and prev[0] in CALLABLE
            prev[0] = 'FUNC_EXIST' if prev[0] is '?'
            tag = 'CALL_START'
          else if value is '[' and ((prev[0] in INDEXABLE and not prev.spaced) or
             (prev[0] is '::')) # `.prototype` can’t be a method you can call.
            tag = 'INDEX_START'
            switch prev[0]
              when '?'  then prev[0] = 'INDEX_SOAK'
        token = @makeToken tag, value
        switch value
          when '(', '{', '[' then @ends.push {tag: INVERSES[value], origin: token}
          when ')', '}', ']' then @pair value
        @tokens.push @makeToken tag, value
        value.length
  • §

    標記處理器

  • §
  • §

    我們的語法中,函式定義中的參數清單與函式呼叫中的引數清單之間的含糊性來源。向後移動,特別標記參數,以便讓剖析器更容易處理。

      tagParameters: ->
        return @tagDoIife() if @tag() isnt ')'
        stack = []
        {tokens} = this
        i = tokens.length
        paramEndToken = tokens[--i]
        paramEndToken[0] = 'PARAM_END'
        while tok = tokens[--i]
          switch tok[0]
            when ')'
              stack.push tok
            when '(', 'CALL_START'
              if stack.length then stack.pop()
              else if tok[0] is '('
                tok[0] = 'PARAM_START'
                return @tagDoIife i - 1
              else
                paramEndToken[0] = 'CALL_END'
                return this
        this
  • §

    標記 do 後接函式與 do 後接例如識別碼不同,以允許不同的語法優先順序

      tagDoIife: (tokenIndex) ->
        tok = @tokens[tokenIndex ? @tokens.length - 1]
        return this unless tok?[0] is 'DO'
        tok[0] = 'DO_IIFE'
        this
  • §

    在檔案結尾關閉所有剩下的開啟區塊。

      closeIndentation: ->
        @outdentToken moveOut: @indent, indentSize: 0
  • §

    比對分隔標記的內容,並使用類似 Ruby 的符號來擴充其中的變數和表達式,以取代任意表達式。

    "Hello #{name.capitalize()}."
    

    如果遇到內插,此方法將遞迴建立新的 Lexer 並進行標記化,直到 #{ 的 { 與 } 平衡為止。

    • regex 比對標記的內容(但不比對 delimiter,如果需要內插,也不比對 #{)。
    • delimiter 是標記的分隔符號。範例包括 '、"、'''、""" 和 ///。
    • closingDelimiter 僅在 JSX 中與 delimiter 不同
    • interpolators 符合內插的開頭,對於 JSX 來說,它同時為 { 和 <(即巢狀 JSX 標籤)

    此方法允許我們在字串內有內插的字串,如此反覆下去。

      matchWithInterpolations: (regex, delimiter, closingDelimiter = delimiter, interpolators = /^#\{/) ->
        tokens = []
        offsetInChunk = delimiter.length
        return null unless @chunk[...offsetInChunk] is delimiter
        str = @chunk[offsetInChunk..]
        loop
          [strPart] = regex.exec str
    
          @validateEscapes strPart, {isRegex: delimiter.charAt(0) is '/', offsetInChunk}
  • §

    推入一個假的 'NEOSTRING' 權杖,稍後它將會變成一個真正的字串。

          tokens.push @makeToken 'NEOSTRING', strPart, offset: offsetInChunk
    
          str = str[strPart.length..]
          offsetInChunk += strPart.length
    
          break unless match = interpolators.exec str
          [interpolator] = match
  • §

    移除 #{ 中的 #。

          interpolationOffset = interpolator.length - 1
          [line, column, offset] = @getLineAndColumnFromChunk offsetInChunk + interpolationOffset
          rest = str[interpolationOffset..]
          {tokens: nested, index} =
            new Lexer().tokenize rest, {line, column, offset, untilBalanced: on, @locationDataCompensations}
  • §

    考量 #{ 中的 #。

          index += interpolationOffset
    
          braceInterpolator = str[index - 1] is '}'
          if braceInterpolator
  • §

    將開頭和結尾的 { 和 } 變成括弧。不必要的括弧將會在稍後移除。

            [open, ..., close] = nested
            open[0]  = 'INTERPOLATION_START'
            open[1]  = '('
            open[2].first_column -= interpolationOffset
            open[2].range = [
              open[2].range[0] - interpolationOffset
              open[2].range[1]
            ]
            close[0]  = 'INTERPOLATION_END'
            close[1] = ')'
            close.origin = ['', 'end of interpolation', close[2]]
  • §

    移除開頭的 'TERMINATOR'(如有)。

          nested.splice 1, 1 if nested[1]?[0] is 'TERMINATOR'
  • §

    移除結尾的 'INDENT'/'OUTDENT' 配對(如有)。

          nested.splice -3, 2 if nested[nested.length - 3]?[0] is 'INDENT' and nested[nested.length - 2][0] is 'OUTDENT'
    
          unless braceInterpolator
  • §

    我們沒有使用 { 和 },所以改為包裝內插的權杖。

            open = @makeToken 'INTERPOLATION_START', '(', offset: offsetInChunk,         length: 0, generated: yes
            close = @makeToken 'INTERPOLATION_END', ')',  offset: offsetInChunk + index, length: 0, generated: yes
            nested = [open, nested..., close]
  • §

    推入一個假的 'TOKENS' 權杖,稍後它將會變成真正的權杖。

          tokens.push ['TOKENS', nested]
    
          str = str[index..]
          offsetInChunk += index
    
        unless str[...closingDelimiter.length] is closingDelimiter
          @error "missing #{closingDelimiter}", length: delimiter.length
    
        {tokens, index: offsetInChunk + closingDelimiter.length}
  • §

    將假權杖類型 'TOKENS' 和 'NEOSTRING'(由 matchWithInterpolations 傳回)的陣列 tokens 合併到權杖串流中。'NEOSTRING' 的值會使用 fn 轉換,並使用 options 先轉換成字串。

      mergeInterpolationTokens: (tokens, options, fn) ->
        {quote, indent, double, heregex, endOffset, jsx} = options
    
        if tokens.length > 1
          lparen = @token 'STRING_START', '(', length: quote?.length ? 0, data: {quote}, generated: not quote?.length
    
        firstIndex = @tokens.length
        $ = tokens.length - 1
        for token, i in tokens
          [tag, value] = token
          switch tag
            when 'TOKENS'
  • §

    此內插中有註解(沒有其他東西)。

              if value.length is 2 and (value[0].comments or value[1].comments)
                placeholderToken = @makeToken 'JS', '', generated: yes
  • §

    使用與第一個括弧相同的位址資料。

                placeholderToken[2] = value[0][2]
                for val in value when val.comments
                  placeholderToken.comments ?= []
                  placeholderToken.comments.push val.comments...
                value.splice 1, 0, placeholderToken
  • §

    推入假 'TOKENS' 權杖中的所有權杖。它們已經有健全的位址資料。

              locationToken = value[0]
              tokensToPush = value
            when 'NEOSTRING'
  • §

    將 'NEOSTRING' 轉換成 'STRING'。

              converted = fn.call this, token[1], i
              addTokenData token, initialChunk: yes if i is 0
              addTokenData token, finalChunk: yes   if i is $
              addTokenData token, {indent, quote, double}
              addTokenData token, {heregex} if heregex
              addTokenData token, {jsx} if jsx
              token[0] = 'STRING'
              token[1] = '"' + converted + '"'
              if tokens.length is 1 and quote?
                token[2].first_column -= quote.length
                if token[1].substr(-2, 1) is '\n'
                  token[2].last_line += 1
                  token[2].last_column = quote.length - 1
                else
                  token[2].last_column += quote.length
                  token[2].last_column -= 1 if token[1].length is 2
                token[2].last_column_exclusive += quote.length
                token[2].range = [
                  token[2].range[0] - quote.length
                  token[2].range[1] + quote.length
                ]
              locationToken = token
              tokensToPush = [token]
          @tokens.push tokensToPush...
    
        if lparen
          [..., lastToken] = tokens
          lparen.origin = ['STRING', null,
            first_line:            lparen[2].first_line
            first_column:          lparen[2].first_column
            last_line:             lastToken[2].last_line
            last_column:           lastToken[2].last_column
            last_line_exclusive:   lastToken[2].last_line_exclusive
            last_column_exclusive: lastToken[2].last_column_exclusive
            range: [
              lparen[2].range[0]
              lastToken[2].range[1]
            ]
          ]
          lparen[2] = lparen.origin[2] unless quote?.length
          rparen = @token 'STRING_END', ')', offset: endOffset - (quote ? '').length, length: quote?.length ? 0, generated: not quote?.length
  • §

    配對一個關閉標記,確保標記串流的過程中,所有列出的標記配對都正確平衡。

      pair: (tag) ->
        [..., prev] = @ends
        unless tag is wanted = prev?.tag
          @error "unmatched #{tag}" unless 'OUTDENT' is wanted
  • §

    自動關閉 INDENT 以支援類似這樣的語法

    el.click((event) ->
      el.hide())
    
          [..., lastIndent] = @indents
          @outdentToken moveOut: lastIndent, noNewlines: true
          return @pair tag
        @ends.pop()
  • §

    輔助函式

  • §
  • §

    補償我們最初移除的東西(例如換行符號),以便位置資料與原始來源檔案保持準確。

      getLocationDataCompensation: (start, end) ->
        totalCompensation = 0
        initialEnd = end
        current = start
        while current <= end
          break if current is end and start isnt initialEnd
          compensation = @locationDataCompensations[current]
          if compensation?
            totalCompensation += compensation
            end += compensation
          current++
        return totalCompensation
  • §

    從當前區塊的偏移量傳回行號和欄位號碼。

    offset 是 @chunk 中的字元數。

      getLineAndColumnFromChunk: (offset) ->
        compensation = @getLocationDataCompensation @chunkOffset, @chunkOffset + offset
    
        if offset is 0
          return [@chunkLine, @chunkColumn + compensation, @chunkOffset + compensation]
    
        if offset >= @chunk.length
          string = @chunk
        else
          string = @chunk[..offset-1]
    
        lineCount = count string, '\n'
    
        column = @chunkColumn
        if lineCount > 0
          [..., lastLine] = string.split '\n'
          column = lastLine.length
          previousLinesCompensation = @getLocationDataCompensation @chunkOffset, @chunkOffset + offset - column
  • §

    不要補償最初插入的新行。

          previousLinesCompensation = 0 if previousLinesCompensation < 0
          columnCompensation = @getLocationDataCompensation(
            @chunkOffset + offset + previousLinesCompensation - column
            @chunkOffset + offset + previousLinesCompensation
          )
        else
          column += string.length
          columnCompensation = compensation
    
        [@chunkLine + lineCount, column + columnCompensation, @chunkOffset + offset + compensation]
    
      makeLocationData: ({ offsetInChunk, length }) ->
        locationData = range: []
        [locationData.first_line, locationData.first_column, locationData.range[0]] =
          @getLineAndColumnFromChunk offsetInChunk
  • §

    使用長度 - 1 作為最後的偏移量 - 我們提供 last_line 和 last_column,所以如果 last_column == first_column,那麼我們正在查看長度為 1 的字元。

        lastCharacter = if length > 0 then (length - 1) else 0
        [locationData.last_line, locationData.last_column, endOffset] =
          @getLineAndColumnFromChunk offsetInChunk + lastCharacter
        [locationData.last_line_exclusive, locationData.last_column_exclusive] =
          @getLineAndColumnFromChunk offsetInChunk + lastCharacter + (if length > 0 then 1 else 0)
        locationData.range[1] = if length > 0 then endOffset + 1 else endOffset
    
        locationData
  • §

    與 token 相同,只不過這個只傳回標記,而不會將其新增到結果中。

      makeToken: (tag, value, {offset: offsetInChunk = 0, length = value.length, origin, generated, indentSize} = {}) ->
        token = [tag, value, @makeLocationData {offsetInChunk, length}]
        token.origin = origin if origin
        token.generated = yes if generated
        token.indentSize = indentSize if indentSize?
        token
  • §

    將標記新增到結果中。offset 是標記在當前 @chunk 中開始的偏移量。length 是標記在 @chunk 中的長度,在偏移量之後。如果未指定,將使用 value 的長度。

    傳回新的標記。

      token: (tag, value, {offset, length, origin, data, generated, indentSize} = {}) ->
        token = @makeToken tag, value, {offset, length, origin, generated, indentSize}
        addTokenData token, data if data
        @tokens.push token
        token
  • §

    窺視標記串流中的最後一個標籤。

      tag: ->
        [..., token] = @tokens
        token?[0]
  • §

    窺視標記串流中的最後一個值。

      value: (useOrigin = no) ->
        [..., token] = @tokens
        if useOrigin and token?.origin?
          token.origin[1]
        else
          token?[1]
  • §

    取得標記串流中的前一個標記。

      prev: ->
        @tokens[@tokens.length - 1]
  • §

    我們是否處於未完成的表達式中?

      unfinished: ->
        LINE_CONTINUER.test(@chunk) or
        @tag() in UNFINISHED
    
      validateUnicodeCodePointEscapes: (str, options) ->
        replaceUnicodeCodePointEscapes str, merge options, {@error}
  • §

    驗證字串和正規表示式中的跳脫字元。

      validateEscapes: (str, options = {}) ->
        invalidEscapeRegex =
          if options.isRegex
            REGEX_INVALID_ESCAPE
          else
            STRING_INVALID_ESCAPE
        match = invalidEscapeRegex.exec str
        return unless match
        [[], before, octal, hex, unicodeCodePoint, unicode] = match
        message =
          if octal
            "octal escape sequences are not allowed"
          else
            "invalid escape sequence"
        invalidEscape = "\\#{octal or hex or unicodeCodePoint or unicode}"
        @error "#{message} #{invalidEscape}",
          offset: (options.offsetInChunk ? 0) + match.index + before.length
          length: invalidEscape.length
    
      suppressSemicolons: ->
        while @value() is ';'
          @tokens.pop()
          @error 'unexpected ;' if @prev()?[0] in ['=', UNFINISHED...]
  • §

    在當前區塊的給定偏移量或記號位置 (token[2]) 擲回錯誤。

      error: (message, options = {}) =>
        location =
          if 'first_line' of options
            options
          else
            [first_line, first_column] = @getLineAndColumnFromChunk options.offset ? 0
            {first_line, first_column, last_column: first_column + (options.length ? 1) - 1}
        throwSyntaxError message, location
  • §

    輔助函數

  • §
    
    isUnassignable = (name, displayName = name) -> switch
      when name in [JS_KEYWORDS..., COFFEE_KEYWORDS...]
        "keyword '#{displayName}' can't be assigned"
      when name in STRICT_PROSCRIBED
        "'#{displayName}' can't be assigned"
      when name in RESERVED
        "reserved word '#{displayName}' can't be assigned"
      else
        false
    
    exports.isUnassignable = isUnassignable
  • §

    from 並非 CoffeeScript 關鍵字,但它在 import 和 export 陳述式 (如上所述) 以及 for 迴圈的宣告列中,表現得像關鍵字一樣。請嘗試偵測 from 是變數識別碼還是這個「有時」關鍵字。

    isForFrom = (prev) ->
  • §

    for i from iterable

      if prev[0] is 'IDENTIFIER'
        yes
  • §

    for from…

      else if prev[0] is 'FOR'
        no
  • §

    for {from}…, for [from]…, for {a, from}…, for {a: from}…

      else if prev[1] in ['{', '[', ',', ':']
        no
      else
        yes
    
    addTokenData = (token, data) ->
      Object.assign (token.data ?= {}), data
  • §

    常數

  • §
  • §

    CoffeeScript 與 JavaScript 共用的關鍵字。

    JS_KEYWORDS = [
      'true', 'false', 'null', 'this'
      'new', 'delete', 'typeof', 'in', 'instanceof'
      'return', 'throw', 'break', 'continue', 'debugger', 'yield', 'await'
      'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally'
      'class', 'extends', 'super'
      'import', 'export', 'default'
    ]
  • §

    僅 CoffeeScript 的關鍵字。

    COFFEE_KEYWORDS = [
      'undefined', 'Infinity', 'NaN'
      'then', 'unless', 'until', 'loop', 'of', 'by', 'when'
    ]
    
    COFFEE_ALIAS_MAP =
      and  : '&&'
      or   : '||'
      is   : '=='
      isnt : '!='
      not  : '!'
      yes  : 'true'
      no   : 'false'
      on   : 'true'
      off  : 'false'
    
    COFFEE_ALIASES  = (key for key of COFFEE_ALIAS_MAP)
    COFFEE_KEYWORDS = COFFEE_KEYWORDS.concat COFFEE_ALIASES
  • §

    JavaScript 保留但未使用的關鍵字清單,或 CoffeeScript 內部使用的關鍵字。當遇到這些關鍵字時,我們會擲回錯誤,以避免在執行階段發生 JavaScript 錯誤。

    RESERVED = [
      'case', 'function', 'var', 'void', 'with', 'const', 'let', 'enum'
      'native', 'implements', 'interface', 'package', 'private'
      'protected', 'public', 'static'
    ]
    
    STRICT_PROSCRIBED = ['arguments', 'eval']
  • §

    JavaScript 關鍵字和保留字的超集,其中任何一個都不可用作識別碼或屬性。

    exports.JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED).concat(STRICT_PROSCRIBED)
  • §

    令人討厭的 Microsoft 瘋狂行為的字元碼,也稱為 BOM。

    BOM = 65279
  • §

    記號比對正規表示式。

    IDENTIFIER = /// ^
      (?!\d)
      ( (?: (?!\s)[$\w\x7f-\uffff] )+ )
      ( [^\n\S]* : (?!:) )?  # Is this a property name?
    ///
  • §

    類似於 IDENTIFIER,但包含 -

    JSX_IDENTIFIER_PART = /// (?: (?!\s)[\-$\w\x7f-\uffff] )+ ///.source
  • §

    在 https://facebook.github.io/jsx/ 規格中,JSXElementName 可以是 JSXIdentifier、JSXNamespacedName (JSXIdentifier : JSXIdentifier) 或 JSXMemberExpression (兩個或以上的 JSXIdentifier 以 . 連接)。

    JSX_IDENTIFIER = /// ^
      (?![\d<]) # Must not start with `<`.
      ( #{JSX_IDENTIFIER_PART}
        (?: \s* : \s* #{JSX_IDENTIFIER_PART}       # JSXNamespacedName
        | (?: \s* \. \s* #{JSX_IDENTIFIER_PART} )+ # JSXMemberExpression
        )? )
    ///
  • §

    片段:<></>

    JSX_FRAGMENT_IDENTIFIER = /// ^
      ()> # Ends immediately with `>`.
    ///
  • §

    在 https://facebook.github.io/jsx/ 規格中,JSXAttributeName 可以是 JSXIdentifier 或 JSXNamespacedName,而 JSXNamespacedName 為 JSXIdentifier : JSXIdentifier

    JSX_ATTRIBUTE = /// ^
      (?!\d)
      ( #{JSX_IDENTIFIER_PART}
        (?: \s* : \s* #{JSX_IDENTIFIER_PART}       # JSXNamespacedName
        )? )
      ( [^\S]* = (?!=) )?  # Is this an attribute with a value?
    ///
    
    NUMBER     = ///
      ^ 0b[01](?:_?[01])*n?                         | # binary
      ^ 0o[0-7](?:_?[0-7])*n?                       | # octal
      ^ 0x[\da-f](?:_?[\da-f])*n?                   | # hex
      ^ \d+(?:_\d+)*n                               | # decimal bigint
      ^ (?:\d+(?:_\d+)*)?      \.? \d+(?:_\d+)*       # decimal
                         (?:e[+-]? \d+(?:_\d+)* )?
    
  • §

    十進制,不支援數字文字分隔符號,供參考:\d*.?\d+ (?:e[+-]?\d+)?

    ///i
    
    OPERATOR   = /// ^ (
      ?: [-=]>             # function
       | [-+*/%<>&|^!?=]=  # compound assign / compare
       | >>>=?             # zero-fill right shift
       | ([-+:])\1         # doubles
       | ([&|<>*/%])\2=?   # logic / shift / power / floor division / modulo
       | \?(\.|::)         # soak access
       | \.{2,3}           # range or splat
    ) ///
    
    WHITESPACE = /^[^\n\S]+/
    
    COMMENT    = /^(\s*)###([^#][\s\S]*?)(?:###([^\n\S]*)|###$)|^((?:\s*#(?!##[^#]).*)+)/
    
    CODE       = /^[-=]>/
    
    MULTI_DENT = /^(?:\n[^\n\S]*)+/
    
    JSTOKEN      = ///^ `(?!``) ((?: [^`\\] | \\[\s\S]           )*) `   ///
    HERE_JSTOKEN = ///^ ```     ((?: [^`\\] | \\[\s\S] | `(?!``) )*) ``` ///
    
    
  • §

    字串比對正規表示式。

    STRING_START   = /^(?:'''|"""|'|")/
    
    STRING_SINGLE  = /// ^(?: [^\\']  | \\[\s\S]                      )* ///
    STRING_DOUBLE  = /// ^(?: [^\\"#] | \\[\s\S] |           \#(?!\{) )* ///
    HEREDOC_SINGLE = /// ^(?: [^\\']  | \\[\s\S] | '(?!'')            )* ///
    HEREDOC_DOUBLE = /// ^(?: [^\\"#] | \\[\s\S] | "(?!"") | \#(?!\{) )* ///
    
    INSIDE_JSX = /// ^(?:
        [^
          \{ # Start of CoffeeScript interpolation.
          <  # Maybe JSX tag (`<` not allowed even if bare).
        ]
      )* /// # Similar to `HEREDOC_DOUBLE` but there is no escaping.
    JSX_INTERPOLATION = /// ^(?:
          \{       # CoffeeScript interpolation.
        | <(?!/)   # JSX opening tag.
      )///
    
    HEREDOC_INDENT     = /\n+([^\n\S]*)(?=\S)/g
  • §

    正規表示式比對正規表示式。

    REGEX = /// ^
      / (?!/) ((
      ?: [^ [ / \n \\ ]  # Every other thing.
       | \\[^\n]         # Anything but newlines escaped.
       | \[              # Character class.
           (?: \\[^\n] | [^ \] \n \\ ] )*
         \]
      )*) (/)?
    ///
    
    REGEX_FLAGS  = /^\w*/
    VALID_FLAGS  = /^(?!.*(.).*\1)[gimsuy]*$/
    
    HEREGEX      = /// ^
      (?:
    
  • §

    比對任何字元,但下列需要特殊處理的字元除外。

          [^\\/#\s]
  • §

    比對 \ 後接任何字元。

        | \\[\s\S]
  • §

    比對任何 /,但 /// 除外。

        | /(?!//)
  • §

    比對不屬於內插的一部分的 #,例如 #{}。

        | \#(?!\{)
  • §

    註解會使用到一行結尾的所有內容,包括 ///。

        | \s+(?:#(?!\{).*)?
      )*
    ///
    
    HEREGEX_COMMENT = /(\s+)(#(?!{).*)/gm
    
    REGEX_ILLEGAL = /// ^ ( / | /{3}\s*) (\*) ///
    
    POSSIBLY_DIVISION   = /// ^ /=?\s ///
  • §

    其他正則表示式。

    HERECOMMENT_ILLEGAL = /\*\//
    
    LINE_CONTINUER      = /// ^ \s* (?: , | \??\.(?![.\d]) | \??:: ) ///
    
    STRING_INVALID_ESCAPE = ///
      ( (?:^|[^\\]) (?:\\\\)* )        # Make sure the escape isn’t escaped.
      \\ (
         ?: (0\d|[1-7])                # octal escape
          | (x(?![\da-fA-F]{2}).{0,2}) # hex escape
          | (u\{(?![\da-fA-F]{1,}\})[^}]*\}?) # unicode code point escape
          | (u(?!\{|[\da-fA-F]{4}).{0,4}) # unicode escape
      )
    ///
    REGEX_INVALID_ESCAPE = ///
      ( (?:^|[^\\]) (?:\\\\)* )        # Make sure the escape isn’t escaped.
      \\ (
         ?: (0\d)                      # octal escape
          | (x(?![\da-fA-F]{2}).{0,2}) # hex escape
          | (u\{(?![\da-fA-F]{1,}\})[^}]*\}?) # unicode code point escape
          | (u(?!\{|[\da-fA-F]{4}).{0,4}) # unicode escape
      )
    ///
    
    TRAILING_SPACES     = /\s+$/
  • §

    複合指派令牌。

    COMPOUND_ASSIGN = [
      '-=', '+=', '/=', '*=', '%=', '||=', '&&=', '?=', '<<=', '>>=', '>>>='
      '&=', '^=', '|=', '**=', '//=', '%%='
    ]
  • §

    單元令牌。

    UNARY = ['NEW', 'TYPEOF', 'DELETE']
    
    UNARY_MATH = ['!', '~']
  • §

    位元移位令牌。

    SHIFT = ['<<', '>>', '>>>']
  • §

    比較令牌。

    COMPARE = ['==', '!=', '<', '>', '<=', '>=']
  • §

    數學令牌。

    MATH = ['*', '/', '%', '//', '%%']
  • §

    可以使用 not 前綴否定的關係令牌。

    RELATION = ['IN', 'OF', 'INSTANCEOF']
  • §

    布林令牌。

    BOOL = ['TRUE', 'FALSE']
  • §

    可以合法地調用或索引的令牌。這些令牌後面的開括號或方括號將被記錄為函式調用或索引運算的開頭。

    CALLABLE  = ['IDENTIFIER', 'PROPERTY', ')', ']', '?', '@', 'THIS', 'SUPER', 'DYNAMIC_IMPORT']
    INDEXABLE = CALLABLE.concat [
      'NUMBER', 'INFINITY', 'NAN', 'STRING', 'STRING_END', 'REGEX', 'REGEX_END'
      'BOOL', 'NULL', 'UNDEFINED', '}', '::'
    ]
  • §

    可以作為小於比較運算的左邊的令牌,例如 a<b。

    COMPARABLE_LEFT_SIDE = ['IDENTIFIER', ')', ']', 'NUMBER']
  • §

    正則表示式永遠不會立即遵循的令牌(在某些情況下,CALLABLE 除外),但除法運算子可以。

    請參閱:http://www-archive.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions

    NOT_REGEX = INDEXABLE.concat ['++', '--']
  • §

    在 WHEN 之前立即出現的令牌,表示 WHEN 出現在一行的開頭。我們將這些與尾隨的 WHEN 區分開來,以避免語法中的歧義。

    LINE_BREAK = ['INDENT', 'OUTDENT', 'TERMINATOR']
  • §

    忽略這些前面的額外縮排。

    INDENTABLE_CLOSERS = [')', '}', ']']