daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Nov 8th, 2018 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. lexems = {
  2.   // keyword: /^(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with)/,
  3.   literal: /^[a-z]\w*/,
  4.   number: /^\d+(\.\d+)?/,
  5.   space: /^\s+/,
  6.   line: ';',
  7.   assign: '=',
  8.   or: '|',
  9.   plus: '+',
  10.   repeat: '[',
  11.   endrepeat: ']',
  12.   brace: '(',
  13.   endbrace: ')',
  14.   bracket: '{',
  15.   endbracket: '}',
  16.   string: {
  17.     pattern: /^("([^"](\\")?)*"|'([^'](\\')?)*')/,
  18.     parse(string) {
  19.       return JSON.parse(string)
  20.     }
  21.   }
  22. };
  23.  
  24. stackTokens = ['repeat', 'brace', 'bracket'];
  25. binary = ['or', 'plus', 'assign', 'line'];
  26.  
  27. function scan(string) {
  28.   if (0 === string.length) {
  29.     return [];
  30.   }
  31.   for (const [name, entry] of Object.entries(lexems)) {
  32.     if ('string' === typeof entry) {
  33.       if (string.indexOf(entry) === 0) {
  34.         return [[name], ...scan(string.slice(entry.length))]
  35.       }
  36.       continue;
  37.     }
  38.     const m = (entry.pattern || entry).exec(string);
  39.     if (m) {
  40.       return [
  41.         [name, entry.parse ? entry.parse(m[0]) : m[0]],
  42.         ...scan(string.slice(m[0].length))
  43.       ]
  44.     }
  45.   }
  46.   const result = scan(string.slice(1));
  47.   if ('unknown' === result[0][0]) {
  48.     result[0][1] = string[0] + result[0][1];
  49.     return result
  50.   }
  51.   return [
  52.     ['unknown', string[0]],
  53.     ...result
  54.   ];
  55. }
  56.  
  57. function ignore(tokens, list = ['unknown', 'space']) {
  58.   return tokens.filter(t => list.indexOf(t[0]) < 0)
  59. }
  60.  
  61. function single(array) {
  62.   return 1 === array.length ? array[0] : array;
  63. }
  64.  
  65. function parse(tokens) {
  66.   let i = 0;
  67.  
  68.   function _parse() {
  69.     let result = [];
  70.     for (; i < tokens.length; i++) {
  71.       const token = tokens[i];
  72.       const name = token[0];
  73.       if (stackTokens.indexOf(name) >= 0) {
  74.         i++;
  75.         const stack = _parse();
  76.         if (name instanceof Array) {
  77.           console.error('Array ', name)
  78.         }
  79.         if (binary.indexOf(stack[0]) >= 0) {
  80.           result.push([name, stack]);
  81.         }
  82.         else {
  83.           result.push([name, ...stack]);
  84.         }
  85.       }
  86.       // if (binary.indexOf(name) >= 0) {
  87.       //   i++;
  88.       //   const left = result.slice(0);
  89.       //   const [first, ...right] = _parse();
  90.         // if (name === first) {
  91.         //   return [name, left, ...right];
  92.         // }
  93.         // if (binary.indexOf(first) > binary.indexOf(name)) {
  94.         //   return [name, [first, left, ...right]]
  95.         // }
  96.         // return [name, left, [first, ...right]];
  97.       // }
  98.       // else {
  99.         if (name.indexOf('end') === 0) {
  100.           break;
  101.         }
  102.         else {
  103.           result.push(token);
  104.         }
  105.       // }
  106.     }
  107.     return result;
  108.   }
  109.  
  110.   return _parse();
  111. }
  112.  
  113. Array.prototype.out = function () {
  114.   return this.map(t => t.join(':')).join(' ')
  115. };
  116.  
  117. Array.prototype.json = function () {
  118.   return JSON.stringify(this, null, '\t')
  119. };
  120.  
  121. Array.prototype.pretty = function () {
  122.   return this.map(t => stackTokens.indexOf(t[0]) >= 0 ? t.pretty() : ('string' === typeof t ? t : t.join(':')))
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top