haxe:从haxe.macro.Position获取行号和行位置

时间:2016-06-28 13:23:36

标签: macros haxe

在每个表达式的haxe宏中,我们可以以http://api.haxe.org/haxe/macro/Position.html的形式获得它的位置:

 {
   file:String, // filename - relative to source path
   min:Int, // position of first character in file
   max:Int // position of last character in file
 }

我想获得最小和最大变量的行号和位置。

我绝对可以通过打开文件

来做到这一点
FileSystem.absolutePath(Context.resolvePath(posInfo.file));

并计算行号,但haxe已经这样做,从编译器获取此信息要好得多。有可能吗?

3 个答案:

答案 0 :(得分:1)

在当前版本的Haxe中,您可以使用 mfcc_feat = mfcc.mfcc(audio,rate, 0.025, 0.01, 20, appendEnergy = True

PositionTools.toLocation

class Macro { public static macro function log(args:Array<Expr>):Expr { var loc = PositionTools.toLocation(Context.currentPos()); var locStr = loc.file + ":" + loc.range.start.line; args.unshift(macro $v{locStr}); return macro SomeExtern.logFunc($a{args}); } } 转换为Macro.log("hi!")

答案 1 :(得分:0)

我知道有些项目是手动完成的(比如checkstyle)

加载文件内容,找到回车符(\ n,\ r或\ n \ r \ n)标记每个新行的字符位置,针对这些位置查找pos.min

如果你在文件中有多字节字符,我想这可能会有问题...

答案 2 :(得分:0)

在haxe 4 PositionTools.toLocation中添加了功能。

相关问题