austin-schick

parse_utils.js

Jan 2nd, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($B) {
  2.     "use strict";
  3.     var _b_ = $B.builtins
  4.  
  5.     $B._PyPegen_lookahead = function(p, positive, rule) {
  6.         let mark = p.mark;
  7.         let res = rule.apply(p);
  8.         p.mark = mark;
  9.         return (res != null) == positive;
  10.     }
  11.  
  12.     $B._PyPegen_lookahead_with_arg = function (p, positive, rule, target, arg) {
  13.         let mark = p.mark;
  14.         let res;
  15.         if (target != null) {
  16.             res = rule(target, arg)
  17.         } else {
  18.             res = rule.apply(p, [arg]);
  19.         }
  20.         p.mark = mark;
  21.         return (res != null) == positive;
  22.     }
  23.  
  24.     $B._PyPegen_expect_token = function(p, type) {
  25.         if (p.mark == p.fill) {
  26.             if ($B._PyPegen_fill_token(p) < 0) {
  27.                 p.error_indicator = 1;
  28.                 return null;
  29.             }
  30.         }
  31.         let t = p.tokens[p.mark];
  32.         // console.log(t, type);
  33.         if (t.type == 'NUMBER' && type == 'NUMBER') {
  34.             p.mark += 1;
  35.             return t;  
  36.         }
  37.         if (t.string != type) {
  38.             return null;
  39.         }
  40.         p.mark += 1;
  41.         return t;
  42.     }
  43.  
  44.     $B._PyPegen_is_memoized = function (p, type) {
  45.         if (p.mark == p.fill) {
  46.             if ($B._PyPegen_fill_token(p) < 0) {
  47.                 p.error_indicator = 1;
  48.                 return null;
  49.             }
  50.         }
  51.         let t = p.tokens[p.mark];
  52.         if (t.memo == null) {
  53.             return [null, false]
  54.         }
  55.         if (t.memo[type] === undefined) {
  56.             return [null, false]
  57.         }
  58.         return [t.memo[type], true];
  59.     }
  60.  
  61.     $B._PyPegen_insert_memo = $B._PyPegen_update_memo = function (p, mark, type, value) {
  62.         let t = p.tokens[mark];
  63.         if (t.memo == null) {
  64.             t.memo = {};
  65.         }
  66.         return t.memo[type] = value;
  67.     }
  68.  
  69.     $B._PyPegen_name_from_token = function (p, t) {
  70.         throw new Error('fail for now');
  71.     }
  72.  
  73.     // expr_ty
  74.     // _PyPegen_name_token(Parser * p)
  75.     // {
  76.     //     Token * t = _PyPegen_expect_token(p, NAME);
  77.     //     return _PyPegen_name_from_token(p, t);
  78.     // }
  79.  
  80.     $B._PyPegen_expect_soft_keyword = function(p, keyword) {
  81.         if (p.mark == p.fill) {
  82.             if ($B._PyPegen_fill_token(p) < 0) {
  83.                 p.error_indicator = 1;
  84.                 return null;
  85.             }
  86.         }
  87.         let t = p.tokens[p.mark];
  88.         if (t.type != 'NAME') {
  89.             return null;
  90.         }
  91.         if (t.string == keyword) {
  92.             p.mark += 1;
  93.             return t;
  94.         }
  95.         return null;
  96.     }
  97.  
  98.     $B._PyPegen_fill_token = function(p) {
  99.         while (true) {
  100.             var next = p.tokenizer.next();
  101.             if (!next.done) {
  102.                 var value = next.value;
  103.                 if (!['ENCODING', 'NL', 'COMMENT'].includes(value.type)) {
  104.                     p.tokens.push(value);
  105.                     p.fill += 1;
  106.                     break;
  107.                 }
  108.             } else {
  109.                 throw Error('tokenizer exhausted')
  110.             }
  111.         }
  112.         return 1;
  113.     }
  114. })(__BRYTHON__)
Advertisement
Add Comment
Please, Sign In to add comment