如何转换以下代码段:
let myVar: string = 'test';到以下产出:
// type {string}
var myVar = 'test';用甜品?
更新
我正在寻找一种方法来将确切的第一个代码片段转换为第二个代码片段。包括// type {string}的评论。
我想用它来创建一个简单的DSL来生成一个代码,用google闭包编译器进行检查。
发布于 2014-07-31 03:28:55
这应该可以做到:
let let = macro {
case { _ $name $[:] $type = $init:expr } => {
var typeStr = unwrapSyntax(#{$type});
var varStx = makeKeyword("var", #{here});
varStx.token.leadingComments = [{
type: "Line",
value: " type {" + typeStr + "}"
}];
letstx $var = [varStx];
return #{
$var $name = $init
}
}
}
let myVar: string = 'test';扩展到:
// type {string}
var myVar = 'test';发布于 2014-07-30 23:39:54
我不知道你在找什么,但也许这段话:
macro m {
case {_ () } => {
var x = makeValue(0, #{here});
x.token.leadingComments = [{
type: "Line",
value: " type {string}"
}];
return withSyntax ($x = [x]) #{
$x
}
}
}
m()
macro foo {
rule { $id = $init } => {
var $id = $init
}
rule { $init } => { var myVar = $init }
}
foo "test";遗憾的是,如果没有令牌,就无法输出注释。
输出
// type {string}
0;
var myVar = 'test';还可以查看标题“卫生”的这页面,它位于页面的3/4左右。
希望这能有所帮助。
更新
阅读一些文章,指出一个简单的规则宏应该完成这项工作:
macro foo {
rule { $id = $init } => {
var $id = $init
}
rule { $init } => { var myVar = $init }
}
// type {string}
foo "test";https://stackoverflow.com/questions/25048665
复制相似问题