Skip to content

Instantly share code, notes, and snippets.

@conartist6
Last active January 22, 2023 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conartist6/50c3c3d7158891020b1f5f1e4aa785d3 to your computer and use it in GitHub Desktop.
Save conartist6/50c3c3d7158891020b1f5f1e4aa785d3 to your computer and use it in GitHub Desktop.
Tokenizer ranges
class Tokenizer {
constructor(source) {
this.source = source;
this.result = null; // result is a token, i.e. a stack of tokens
this.pathRangesByToken = new WeakMap();
this.prevTokensByToken = new WeakMap();
}
ownTokensFor(path) {
const { prevTokensByToken, pathRangesByToken } = this;
const [startNodeToken, endNodeToken] = path.outerRange;
const cstTokens = [];
for (
let token = prevTokensByToken.get(endNodeToken);
token !== startNodeToken;
token = prevTokensByToken.get(token)
) {
if (token.type === 'EndNode') {
const range = pathRangesByToken.get(token);
token = range[0];
} else {
cstTokens.push(createToken(token.type, token.value));
}
}
cstTokens.reverse();
return cstTokens;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment