Sublime Forum

File_regex help needed


I’m trying to develop a build system for the tniASM assembly assembler. However, I’ve hit a roadblock with getting a working file_regex line. Here’s an example of the output from tniASM upon finding an error:

Pass 1…
Syntax Error in line 28 (D:\Users\Name\Documents\Test.asm): Illegal Text
Exiting…[Finished in 0.2s with exit code 1]

Unfortunately, from the Sublime Text documentation I have been unable to determine exactly what file_regex parses for. For example, does it matter which order the groups are? Can named groups be used? etc. There just seem to be so little detail / clarity in the documentation.

So, can anyone help with what a working file_regex could be for the above output?




For example, does it matter which order the groups are?

Yes, it does.

Can named groups be used?

No they can’t.

You can use "line_regex" to get the line number and "file_regex" to get the file.



Thanks for your help. Is it the case that file_regex MUST capture 2 or more groups? The documentation states “should” so it’s not clear to me. If it does need 2 or more then I assume that I can add the “message” group but how does one specify empty groups, i.e. “line number” and “column number”?



The captures can’t be named and are assigned in the order listed, so if you need to “skip” one you need to use an empty capture that captures nothing.



Okay, I’m beginning to get there and should be able to match the filename with the following:


However, Sublime is flagging the escaped period \. as being an error (i.e. it is highlighted in pink). I’m also getting this when trying to use other escaped characters such as \:

Why does Sublime not accept these escaped sequences when Perl regex does?



Okay, I’ve figured out that I need to also escape the \ for it to be recognised so to escape a period I need to enter \.

So, I think I’m getting there with the following but it still does nothing:

"file_regex": "([^\\]+\\.asm),(),(),((\\: ).+)",
"line_regex": "line ([0-9]*)",

Any suggestions would be gratefully received. By way of example the above should capture something like the following groups from the message in my first post:

filename = Test.asm
message = Illegal Text
line number = 28



"file_regex": "([^\\]+\\.asm),(),(),((\\: ).+)",

That would match Test.asm,,,: Illegal Text. Try something like ([^\\]+\\.asm)()()\\): (.+) instead. You can test whether your regexes work using find.



There is also a helper sublime function to debug file_regex & line_regex

def log_result_regex(flag):
    Enables or disables result regex logging. This is useful when trying to
    debug file_regex and line_regex in build systems

Though I am not exactly sure how this works.



sublime.log_result_regex() generates logs in the console that tell you where and how anything that uses file_regex and line_regex are matching. For example:

>>> sublime.log_result_regex(True)
Running gcc "/home/tmartin/test.c" -o "/home/tmartin/test"
found result file, line, col of [/home/tmartin/test.c], [8], [13] full path: /home/tmartin/test.c
found result file, line, col of [/home/tmartin/test.c], [8], [13] full path: /home/tmartin/test.c
found result file, line, col of [/home/tmartin/test.c], [8], [13] full path: /home/tmartin/test.c

The log about what’s being executed comes from the exec command, but the other parts are indicating the file, line and column of the error, as well as what the full path is (which is synthesized from the matched file name and the working_dir if the file is relative, which here it’s not so they’re both the same).

If the result also matched a message, that would be displayed as well. However the build system I’m using hasn’t been modified to work with what appears to be a new error output style in gcc.