1

Long time lurker, first time poster.

I'm trying to write a vscode syntax for TeraTerm (ttl), of which has been quite the learning experience, and have hit a few walls. Biggest problem I have is writing a regex for ttl's if/then/endif. Normally I enjoy the satisfaction of figuring things out for myself, but every time I mess up on this I end up locking things up pretty hard. ttl's format for if then is:

if expression then
    statement
elseif expression then
    statement
endif

A single if/then is easy to work out in tmlanguage, but putting a regex together for nested if/thens has resulted in several ctrl-alt-deletes. I've gotten pretty good at lookaheads/behinds within a single line, however multiple lines have been a royal pain. This is kinda how I've been doing it:

"if-endif":{
    "patterns": [
        {
            "begin":"(?im)(?<ifthen>(^\\s*if\\s+(\\w+|\\s+|=)+\\s+then$))(?=(g<ifthen>).*^\\s*endif$)*.*^\\s*endif$)",
            "beginCaptures":{"1":{"name":"keyword.control.ifthen.ttl"}},
            "end":"(?i)(?<=(ditto above lookahead)*)(^\\s*endif$))",
            "endCaptures":{"1":{"name":"keyword.control.endif.ttl"}}
            "patterns":[{"include":"#if-endif"},{"include":"#alltheotherthings"}]
        }
    ]
}

Somehow I need to get the if/then and its matching endif while not accidently grabbing an endif that belongs do a different if. My biggest issue is I really have no idea how to deal with multiple lines in a regex apart from including (?m).

My other problem I'm not sure is even possible within the syntax. I'd like a way to see if a variable has already been defined in the above script so I can markup appropriately if I'm trying to use a string/integer in the wrong place, or if it hasn't been defined. However I'm not aware of any way to compare one capture group to another inside a regex. If anyone knows of a good example or documentation I'd be overjoyed. My primary docs I've been using for this are MS's vscode stuff and macromates.

Any pointers/help/links would be greatly appreciated. This is pretty much the last regex holding me up as everything else are single line affairs.

Cohors
  • 11
  • 2

0 Answers0