Skip to content

[Request] Detect a keyword defined inside the input string #4304

@paulrosen

Description

@paulrosen

I'm creating a grammar for a custom language that has some interesting quirks. I have figured out how to do most of it, but I'm not sure if this one case is possible.

The language allows defining a keyword before its use. (Sort of like the #define statement in C) I'd like to target those defined keywords as if they were built in keywords.

Here is an example of the problem. The first line defines the new keyword "blah". From then on that can appear. In this example assume that "ABCD" is a predefined keyword and "oops" is a malformed string.

U:blah=something
...
ABCD blah oops

Here's some of my grammar:

{
	scope: "define",
	match: /^U:/
},
{
	scope: "new-keyword",
	match: /(?<=^U:)([^=]+)*/
},
{
	scope: "definition",
	match: /(?<=^U:([^=]+))=[^\n]*/
},
{
	scope: "error",
	match: /./
}

That is, if anything doesn't match, it gets the scope "error" and I can highlight it in red.

So, the output I'd like to get is:

<span class="hljs-define">U:</span>
<span class="hljs-new-keyword">blah</span>
<span class="hljs-definition">=something</span>
<span class="hljs-keyword">ABCD</span>
<span class="hljs-keyword">blah</span>
<span class="hljs-error">oops</span>

So this would require injecting something into the grammar after it starts parsing.

In addition, if there are two of these on the page the second one should not have the new keyword, so it must be scoped only to that string.

Is there any thing I can do?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions