Last active
January 22, 2023 18:10
-
-
Save conartist6/50c3c3d7158891020b1f5f1e4aa785d3 to your computer and use it in GitHub Desktop.
Tokenizer ranges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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