Advertisement
Tkap1

Untitled

Jan 21st, 2024
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. class Tokenizer
  4. {
  5.     tokens = [];
  6.     index = 0;
  7.     consume_type_text(type, text) {
  8.         if(tokens[index].type === type && tokens[index].text === text) {
  9.             index += 1;
  10.             return tokens[index - 1];
  11.         }
  12.         return null;
  13.     }
  14.     consume_type(type)
  15.     {
  16.         if(tokens[index].type === type) {
  17.             index += 1;
  18.             return tokens[index - 1];
  19.         }
  20.         return null;
  21.     }
  22. };
  23.  
  24.  
  25. function parseTag(tokenizer) {
  26.     if(!tokenizer.consume_type_text("symbol", "<")) { return {null, null}; }
  27.  
  28.     let token = tokenizer.consume_type("identifier", "<")
  29.     if(token === null) { return {null, null}; }
  30.  
  31.     // ...
  32.     // On success, something like
  33.     return {node, tokenizer}
  34.  
  35. }
  36.  
  37. function parse(tokens) {
  38.     let tokenizer = new Tokenizer();
  39.     tokenizer.tokens = tokens;
  40.     tokenizer.index = 0;
  41.  
  42.     // We don't want to pass a reference, because it will get modified even it we don't successfully parse a tag.
  43.     parseTag({...tokenizer});
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement