exports.starts = (string, literal, start) ->
literal is string.substr start, literal.length
此文件包含我们希望在 **Lexer**、**Rewriter** 和 **Nodes** 之间共享的通用辅助函数。合并对象、扁平化数组、计算字符数,诸如此类。
查看给定字符串的开头,以查看它是否与一个序列匹配。
exports.starts = (string, literal, start) ->
literal is string.substr start, literal.length
查看给定字符串的结尾,以查看它是否与一个序列匹配。
exports.ends = (string, literal, back) ->
len = literal.length
literal is string.substr string.length - len - (back or 0), len
重复一个字符串 n
次。
exports.repeat = repeat = (str, n) ->
使用巧妙的算法来实现 O(log(n)) 字符串连接操作。
res = ''
while n > 0
res += str if n & 1
n >>>= 1
str += str
res
从数组中删除所有假值。
exports.compact = (array) ->
item for item in array when item
计算一个字符串在一个字符串中出现的次数。
exports.count = (string, substr) ->
num = pos = 0
return 1/0 unless substr.length
num++ while pos = 1 + string.indexOf substr, pos
num
合并对象,返回一个包含两边属性的新副本。每次调用 Base#compile
时都会使用它,以允许选项哈希中的属性传播到树中,而不会污染其他分支。
exports.merge = (options, overrides) ->
extend (extend {}, options), overrides
使用另一个对象的属性扩展源对象(浅拷贝)。
extend = exports.extend = (object, properties) ->
for key, val of properties
object[key] = val
object
返回数组的扁平化版本。对于从节点获取 children
列表非常方便。
exports.flatten = flatten = (array) ->
flattened = []
for element in array
if '[object Array]' is Object::toString.call element
flattened = flattened.concat flatten element
else
flattened.push element
flattened
从对象中删除一个键,返回该值。当节点在选项哈希中查找特定方法时很有用。
exports.del = (obj, key) ->
val = obj[key]
delete obj[key]
val
典型的 Array::some
exports.some = Array::some ? (fn) ->
return true for e in this when fn e
false
一个简单的函数,用于通过将文档放在注释中来反转 Literate CoffeeScript 代码,生成可以“正常”编译的 CoffeeScript 代码字符串。
exports.invertLiterate = (code) ->
maybe_code = true
lines = for line in code.split('\n')
if maybe_code and /^([ ]{4}|[ ]{0,3}\t)/.test line
line
else if maybe_code = /^\s*$/.test line
line
else
'# ' + line
lines.join '\n'
将两个 jison 风格的定位数据对象合并在一起。如果没有提供 last
,这将简单地返回 first
。
buildLocationData = (first, last) ->
if not last
first
else
first_line: first.first_line
first_column: first.first_column
last_line: last.last_line
last_column: last.last_column
这将返回一个函数,该函数以一个对象作为参数,如果该对象是一个 AST 节点,则更新该对象的 locationData。无论如何都会返回该对象。
exports.addLocationDataFn = (first, last) ->
(obj) ->
if ((typeof obj) is 'object') and (!!obj['updateLocationDataIfMissing'])
obj.updateLocationDataIfMissing buildLocationData(first, last)
return obj
将 jison 定位数据转换为字符串。obj
可以是令牌或 locationData。
exports.locationDataToString = (obj) ->
if ("2" of obj) and ("first_line" of obj[2]) then locationData = obj[2]
else if "first_line" of obj then locationData = obj
if locationData
"#{locationData.first_line + 1}:#{locationData.first_column + 1}-" +
"#{locationData.last_line + 1}:#{locationData.last_column + 1}"
else
"No location data"
.coffee.md
的兼容版本 basename
,它返回没有扩展名的文件。
exports.baseFileName = (file, stripExt = no, useWinPathSep = no) ->
pathSep = if useWinPathSep then /\\|\// else /\//
parts = file.split(pathSep)
file = parts[parts.length - 1]
return file unless stripExt and file.indexOf('.') >= 0
parts = file.split('.')
parts.pop()
parts.pop() if parts[parts.length - 1] is 'coffee' and parts.length > 1
parts.join('.')
确定文件名是否代表 CoffeeScript 文件。
exports.isCoffee = (file) -> /\.((lit)?coffee|coffee\.md)$/.test file
确定文件名是否代表 Literate CoffeeScript 文件。
exports.isLiterate = (file) -> /\.(litcoffee|coffee\.md)$/.test file
从给定位置抛出 SyntaxError。该错误的 toString
将返回一个错误消息,遵循“标准”格式 <filename>:<line>:<col>: <message>
,加上包含错误的行和一个显示错误位置的标记。
exports.throwSyntaxError = (message, location) ->
error = new SyntaxError message
error.location = location
error.toString = syntaxErrorToString
不要显示编译器的堆栈跟踪,而是显示我们自定义的错误消息(当错误在编译 CoffeeScript 的 Node.js 应用程序中冒泡时,这很有用)。
error.stack = error.toString()
throw error
如果编译器 SyntaxError 还没有源代码信息,则使用源代码信息更新它。
exports.updateSyntaxError = (error, code, filename) ->
避免搞乱其他错误的 stack
属性(即可能的错误)。
if error.toString is syntaxErrorToString
error.code or= code
error.filename or= filename
error.stack = error.toString()
error
syntaxErrorToString = ->
return Error::toString.call @ unless @code and @location
{first_line, first_column, last_line, last_column} = @location
last_line ?= first_line
last_column ?= first_column
filename = @filename or '[stdin]'
codeLine = @code.split('\n')[first_line]
start = first_column
仅显示多行错误的第一行。
end = if first_line is last_line then last_column + 1 else codeLine.length
marker = codeLine[...start].replace(/[^\s]/g, ' ') + repeat('^', end - start)
检查我们是否在支持颜色的 TTY 上运行。
if process?
colorsEnabled = process.stdout?.isTTY and not process.env?.NODE_DISABLE_COLORS
if @colorful ? colorsEnabled
colorize = (str) -> "\x1B[1;31m#{str}\x1B[0m"
codeLine = codeLine[...start] + colorize(codeLine[start...end]) + codeLine[end..]
marker = colorize marker
"""
#{filename}:#{first_line + 1}:#{first_column + 1}: error: #{@message}
#{codeLine}
#{marker}
"""
exports.nameWhitespaceCharacter = (string) ->
switch string
when ' ' then 'space'
when '\n' then 'newline'
when '\r' then 'carriage return'
when '\t' then 'tab'
else string