Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/tokenize/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function _matchGrammar (
// Without the global flag, lastIndex won't work
flagsToAdd += 'g';
}
if (pattern.source.includes('(?<') && pattern.hasIndices === false) {
if (pattern.source?.includes('(?<') && pattern.hasIndices === false) {
// Has named groups, we need to be able to capture their indices
flagsToAdd += 'd';
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/tokenize/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function resolve (
let ret = reference ?? undefined;

if (typeof ret === 'string') {
ret = prism.languageRegistry.getLanguage(ret)?.grammar;
ret = prism.languageRegistry.getLanguage(ret)?.resolvedGrammar;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still not fully resolving cases like:

{
  id: "foo",
  require: bar
  grammar: {
    token: {
      ...
      inside: "bar"
    }
  }
}

The bar language is imported and can be accessed via the languages array associated with the foo language. However, languages["bar"] is evaluated lazily. Until it's accessed for the first time, the bar language won't be added to the languageRegistry, and resolve(prism, "bar") will always return undefined.

Honestly, I don't know how to solve it without providing some extra info on the grammar we resolve. 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need to traverse the grammar for resolvedGrammar and resolve anything that can be resolved without context. That will help with many things, e.g. self as well. It won't help with optional languages though. But we can replace it with a getter that overwrites itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a plan 👍

}

if (typeof ret === 'function' && ret.length === 0) {
Expand All @@ -20,6 +20,7 @@ export function resolve (

if (typeof ret === 'object' && ret.$rest) {
let restGrammar = resolve.call(prism, ret.$rest) ?? {};
delete ret.$rest;

if (typeof restGrammar === 'object') {
ret = { ...ret, ...restGrammar };
Expand Down