113 lines
2.9 KiB
TypeScript
113 lines
2.9 KiB
TypeScript
import type { FactBlock, Node, PropertyRow, SourceDocument, Translator } from '../contract'
|
|
import { enumRows, functionRows, implRows, importRows, moduleRows, plainSay, structRows, traitRows } from '../rules/rustRows'
|
|
import { createZhTemplateTranslator, KIND_ZH, ZH_ATTRIBUTE_NAMES } from './zhTemplate'
|
|
|
|
const say = plainSay()
|
|
|
|
const template = createZhTemplateTranslator({
|
|
id: 'rust.zh-template',
|
|
label: '中文直译',
|
|
languageIds: ['rust'],
|
|
keywords: {
|
|
fn: '函数',
|
|
let: '令',
|
|
mut: '可变',
|
|
pub: '公开',
|
|
struct: '结构体',
|
|
enum: '枚举',
|
|
impl: '实现',
|
|
trait: '特质',
|
|
use: '导入',
|
|
mod: '模块',
|
|
match: '匹配',
|
|
if: '如果',
|
|
else: '否则',
|
|
for: '遍历',
|
|
while: '当',
|
|
loop: '循环',
|
|
return: '返回',
|
|
async: '异步',
|
|
await: '等待',
|
|
break: '跳出',
|
|
continue: '继续',
|
|
where: '其中',
|
|
type: '类型',
|
|
const: '常量',
|
|
static: '静态',
|
|
unsafe: '不安全',
|
|
as: '作为',
|
|
in: '在',
|
|
ref: '引用',
|
|
move: '移动',
|
|
dyn: '动态',
|
|
crate: '本 crate',
|
|
super: '父模块',
|
|
self: '自己',
|
|
Self: '本类型',
|
|
},
|
|
literals: { true: '真', false: '假' },
|
|
builtinTypes: {},
|
|
isTypePosition: () => false,
|
|
operators: {},
|
|
operatorParents: [],
|
|
emptyLiterals: {},
|
|
mapLiterals: {},
|
|
stringNodes: ['string_literal'],
|
|
commentText,
|
|
nodeTemplates: {},
|
|
})
|
|
|
|
/**
|
|
* Rust 的中文直译。属性表的用词按代码阅读的 Rust 原型:结构体、可见性、变体、分情况。
|
|
* 关键字和注释仍走中文直译那一套。常量和变量的行不另写一套,走模板里现成的那套。
|
|
*/
|
|
export const rustZhTranslator: Translator = {
|
|
...template,
|
|
attributeNames: {
|
|
attributes: { ...ZH_ATTRIBUTE_NAMES.attributes, access: '可见性' },
|
|
values: {
|
|
...ZH_ATTRIBUTE_NAMES.values,
|
|
kind: { ...KIND_ZH, class: '结构体' },
|
|
access: {
|
|
...ZH_ATTRIBUTE_NAMES.values.access,
|
|
public: '公开',
|
|
module: '本模块',
|
|
super: '父模块',
|
|
crate: '本 crate',
|
|
},
|
|
},
|
|
},
|
|
describe,
|
|
}
|
|
|
|
function describe(block: FactBlock, document: SourceDocument): PropertyRow[] {
|
|
switch (block.kind) {
|
|
case 'import':
|
|
return importRows(block, say)
|
|
case 'class':
|
|
return structRows(block, say)
|
|
case 'enum':
|
|
return enumRows(block, say)
|
|
case 'function':
|
|
return functionRows(block, say)
|
|
case 'trait':
|
|
return traitRows(block, say)
|
|
case 'impl':
|
|
return implRows(block, say)
|
|
case 'module':
|
|
return moduleRows(block, say)
|
|
case 'const':
|
|
case 'variable':
|
|
return template.describe(block, document)
|
|
default:
|
|
return []
|
|
}
|
|
}
|
|
|
|
function commentText(node: Node, text: string): string {
|
|
const doc = node.childForFieldName('doc')
|
|
if (doc)
|
|
return doc.text.trim()
|
|
return text.replace(/^\/\/[/!]?\s?/, '').replace(/^\/\*[*!]?\s?/, '').replace(/\s*\*\/$/, '').trim()
|
|
}
|