♻️ 重构
This commit is contained in:
@@ -2,6 +2,54 @@
|
||||
# test_tsf.sh - TSF 语法综合测试
|
||||
echo "=== TSF 基础语法验证 ==="
|
||||
|
||||
# 配置 tree-sitter 使用当前语法库
|
||||
SCRIPT_DIR=$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
GRAMMAR_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
|
||||
PARSER_PARENT=$(cd "$GRAMMAR_ROOT/.." && pwd)
|
||||
|
||||
# 本地配置目录,独立于用户全局配置
|
||||
CONFIG_DIR="$GRAMMAR_ROOT/.tree-sitter-config"
|
||||
mkdir -p "$CONFIG_DIR/tree-sitter"
|
||||
cat >"$CONFIG_DIR/tree-sitter/config.json" <<EOF
|
||||
{
|
||||
"grammars": [
|
||||
{
|
||||
"name": "tsf",
|
||||
"scope": "source.tsf",
|
||||
"file-types": ["tsf", "tsl"],
|
||||
"path": "$GRAMMAR_ROOT"
|
||||
}
|
||||
],
|
||||
"parser-directories": ["$PARSER_PARENT"]
|
||||
}
|
||||
EOF
|
||||
export XDG_CONFIG_HOME="$CONFIG_DIR"
|
||||
export TREE_SITTER_GRAMMAR_DIR="$GRAMMAR_ROOT"
|
||||
|
||||
# 覆盖 tree-sitter 命令,parse 时自动带上 scope 和 config
|
||||
tree-sitter() {
|
||||
if [ "$1" = "parse" ]; then
|
||||
shift
|
||||
local tmp_input=""
|
||||
|
||||
# 如果从 stdin 读入代码,则先落盘到临时文件
|
||||
if [ $# -eq 0 ] && [ ! -t 0 ]; then
|
||||
tmp_input=$(mktemp)
|
||||
cat >"$tmp_input"
|
||||
set -- "$tmp_input"
|
||||
fi
|
||||
|
||||
command tree-sitter parse --config-path "$CONFIG_DIR/tree-sitter/config.json" --scope source.tsf "$@"
|
||||
local status=$?
|
||||
if [ -n "$tmp_input" ]; then
|
||||
rm -f "$tmp_input"
|
||||
fi
|
||||
return $status
|
||||
else
|
||||
command tree-sitter "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "步骤1: 生成解析器"
|
||||
# tree-sitter generate
|
||||
# if [ $? -ne 0 ]; then
|
||||
|
||||
Reference in New Issue
Block a user