Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tokenizer
- {
- tokens = [];
- index = 0;
- consume_type_text(type, text) {
- if(tokens[index].type === type && tokens[index].text === text) {
- index += 1;
- return tokens[index - 1];
- }
- return null;
- }
- consume_type(type)
- {
- if(tokens[index].type === type) {
- index += 1;
- return tokens[index - 1];
- }
- return null;
- }
- };
- function parseTag(tokenizer) {
- if(!tokenizer.consume_type_text("symbol", "<")) { return {null, null}; }
- let token = tokenizer.consume_type("identifier", "<")
- if(token === null) { return {null, null}; }
- // ...
- // On success, something like
- return {node, tokenizer}
- }
- function parse(tokens) {
- let tokenizer = new Tokenizer();
- tokenizer.tokens = tokens;
- tokenizer.index = 0;
- // We don't want to pass a reference, because it will get modified even it we don't successfully parse a tag.
- parseTag({...tokenizer});
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement