Advertisement
LoganDark

Input Example Program 2

Feb 19th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. if not fs.exists('lex') then
  2.     shell.run('pastebin get edyuQ5xY lex')
  3. end
  4.  
  5. local lexSuccess = os.loadAPI('lex')
  6.  
  7. if not lexSuccess or not lex or not lex.lex then
  8.     print('A valid lexer is required (file `lex` seems to be broken or not a lexer)')
  9.     return
  10. end
  11.  
  12. local input = dofile('input')
  13. local lex = dofile('lex')
  14.  
  15. local apis = {
  16.     -- tables
  17.     bit = true,
  18.     bit32 = true,
  19.     bitop = true,
  20.     colors = true,
  21.     colours = true,
  22.     coroutine = true,
  23.     disk = true,
  24.     fs = true,
  25.     gps = true,
  26.     help = true,
  27.     http = true,
  28.     io = true,
  29.     keys = true,
  30.     math = true,
  31.     os = true,
  32.     paintutils = true,
  33.     parallel = true,
  34.     peripheral = true,
  35.     rednet = true,
  36.     redstone = true,
  37.     rs = true,
  38.     settings = true,
  39.     shell = true,
  40.     socket = true,
  41.     string = true,
  42.     table = true,
  43.     term = true,
  44.     textutils = true,
  45.     vector = true,
  46.     window = true,
  47.  
  48.     -- functions
  49.     assert = true,
  50.     collectgarbage = true,
  51.     dofile = true,
  52.     error = true,
  53.     getfenv = true,
  54.     getmetatable = true,
  55.     ipairs = true,
  56.     loadfile = true,
  57.     loadstring = true,
  58.     module = true,
  59.     next = true,
  60.     pairs = true,
  61.     pcall = true,
  62.     print = true,
  63.     rawequal = true,
  64.     rawget = true,
  65.     rawset = true,
  66.     require = true,
  67.     select = true,
  68.     setfenv = true,
  69.     setmetatable = true,
  70.     tonumber = true,
  71.     tostring = true,
  72.     type = true,
  73.     unpack = true,
  74.     xpcall =  true,
  75.     printError = true,
  76.     write = true
  77. }
  78.  
  79. input(nil, nil, nil, nil, nil, nil, nil, function(_sLine, _nPos, _nScroll, _bLast, _sCompletion)
  80.     -- _sLine, _nPos, _nScroll, _bLast, _sCompletion
  81.    
  82.     local fg = ''
  83.     local bg = ''
  84.     local tokens = lex(_sLine)[1]
  85.  
  86.     if #tokens > 0 then
  87.         for t = 1, #tokens do
  88.             local color = '0'
  89.             local token = tokens[t]
  90.            
  91.             if token.type == 'keyword' or token.type == 'operator' then
  92.                 color = '4' -- yellow
  93.             elseif token.type == 'number' or token.type == 'value' or token.type == 'escape' then
  94.                 color = 'a' -- purple
  95.             elseif token.type == 'comment' then
  96.                 color = 'd' -- green
  97.             elseif token.type == 'ident' and apis[token.data] then
  98.                 color = '8' -- lightGray
  99.             elseif token.type == 'string' then
  100.                 color = '9' -- cyan
  101.             elseif token.type == 'unidentified' then
  102.                 color = 'e' -- red
  103.             end
  104.            
  105.             for i = 1, #token.data do      
  106.                 fg = fg .. color
  107.                 bg = bg .. 'f'
  108.             end
  109.         end
  110.     end
  111.    
  112.     for i = 1, #_sCompletion do
  113.         fg = fg .. 'f'
  114.         bg = bg .. '7'
  115.     end
  116.    
  117.     return fg, bg
  118. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement