oleg-shilo / codemap.vscode

Code map (syntax tree) of the active document
MIT License
85 stars 28 forks source link

Multi-line Regexp #20

Closed faulknermano closed 5 years ago

faulknermano commented 5 years ago

Hi, I'm trying to to add a custom mapper for a very old scripting language (LScript). It doesn't have a any sort of function keyword. A function is always at the topmost/global scope, and is defined if it precedes { and }.

So I wrote a simple regexp:

"^[A-z].*(?=\\n{)"

But this one doesn't work -- no result in CodeMap.

So I tried removing the newline lookahead:

"^[A-z].*"

And this one 'worked', but not my intended result. It seems like CodeMap is not able to use Regexp to process multiple lines? Is this possible to do?

This is an example of LScript code:

some_global_var=1;
some_global_string="Hello world.";
my_function
{
  info(some_global_var);
  call_other_function(some_global_string+some_global_var);
}
my_function: input
{
  info(input);
}
main // entry point
{
  my_function();
}
oleg-shilo commented 5 years ago

You you are right. The regex based parsing is performed line by line. Thus one cannot use multi-line regex expressions.

I think for the cases like yours a dedicated simple mapper is a better approach: https://github.com/oleg-shilo/codemap.vscode/wiki/Adding-custom-mappers.

It is actually much simpler than one may think. Have a look at Markdown syntax parser sample at the link above. It might be the approach that would want to go with.

faulknermano commented 5 years ago

@oleg-shilo Thanks! Yes it was easy and I eventually did my own custom mapper that way. Thanks very much for this useful extension. I never thought this would ever come around. Now I can finally do LS development in VS Code (I used to have to do it in Notepad++). :-)

oleg-shilo commented 5 years ago

Glad you sorted it al out 👍

niciki-niciki commented 5 months ago

@faulknermano, @oleg-shilo I am also interested to make a multi-line mapper. This time for Verilog. Could you paste here a simple, working example how you did it? Thank you.

oleg-shilo commented 5 months ago

This is what you need: https://github.com/oleg-shilo/codemap.vscode/wiki/Adding-custom-mappers#mapping-with-a-dedicated-mapper

faulknermano commented 5 months ago

For what it's worth this is what I did for LScript (LightWave3D):

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");

class mapper {

    static read_all_lines(file) {
        let text = fs.readFileSync(file, 'utf8');
        return text.split(/\r?\n/g);
    }

    static generate(file) {
        let members = [];
        try
        {
            let line_num = 0; 
            let prev_line = "";
            mapper
                .read_all_lines(file)
                .forEach(line => {
                    line_num++;
                    // line = line.trimStart();
                    if (line.startsWith("// SECTION:"))
                        members.push(`${line}|${line_num}|level1`);
                    else if (line.startsWith("{"))
                        members.push(`${prev_line}|${line_num-1}|function`);
                    prev_line = line;
            });
        }
        catch (error) {
        }
        return members;
    }
}
exports.mapper = mapper;
oleg-shilo commented 5 months ago

Do you mind if I include it in the extension distro?

faulknermano commented 5 months ago

@oleg-shilo not at all. Please feel free to!

oleg-shilo commented 5 months ago

Done. The LScript mapper (*.ld documents) is now a part of the default CodeMap distribution. https://marketplace.visualstudio.com/items?itemName=oleg-shilo.codemap