如何建立位置? [英] How to construct a location?

查看:59
本文介绍了如何建立位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题.

假设我有一个位置

 loc l = |file:////Users/steven/tst.txt|;

我想通过选择第5-10行来使其更加详细.

and I want to make it more detailled, by selecting line 5 - 10. How do I do that?

简单地说明

l.begin.line = 5; l.begin.column = 0;

错了.

所以,我有以下问题:

  1. 如何设置 l.begin.line l.begin.column ?

如何轻松检测第10行的最后一列?

How can I easily detect the last column of line 10?

如何以一种简单的方式相应地设置 l.offset l.length ?

How can I set l.offset and l.length accordingly in an easy way?

我可以找到定义位置的Rascal模块吗?(本教程建议我查看ListRelation和Map之间的库,但这无济于事)

Were can I find the Rascal module that defines locations? (The tutorial suggests me to look in the library between ListRelation and Map, but that doesn't help)

推荐答案

感谢您的回答,根据您的回答,我可以构造所需的功能,即从文件和起始位置构造位置的功能.结束行号.

Thank for your answer, based on your answer I could construct the function that I needed, namely a function that constructs a location from a file and a start- and end-linenumber.

从Rascal建造的位置检测到 eol = 2 .

The eol=2 is detected from locations that were constructed by Rascal.

如果发现瑕疵,可以请您更正吗?

Could you please correct it if you see a flaw?


public loc getFragment(loc f, int startLine, int endLine) {

    loc fragment = |file:///null|;
    int eol = 2;

    if (exists(f)) {
        int offset = 0;
        int len = 0;
        int i = 0;
        int endCol = 0;
        for (str line <- readFileLines(f)) {
            i += 1;
            if (i < startLine) {
                offset += size(line) + eol;
            } else if (i <= endLine) {
                len += size(line);
                if (i == endLine) {
                    endCol = size(line);
                } else {
                    len += eol;
                }
            }
        }
        fragment = f(offset,len,<startLine,0>,<endLine,endCol>);
    } 
    return fragment;
}

这篇关于如何建立位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆