logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

spin.lua (3763B)


  1. -- Copyright 2017-2024 David B. Lamkins <david@lamkins.net>. See LICENSE.
  2. -- Spin LPeg lexer, see https://www.parallax.com/microcontrollers/propeller.
  3. local lexer = require('lexer')
  4. local token, word_match = lexer.token, lexer.word_match
  5. local P, R, S = lpeg.P, lpeg.R, lpeg.S
  6. local lex = lexer.new('spin')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. -- Keywords.
  10. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  11. '_clkfreq', '_clkmode', '_free', '_stack', '_xinfreq', 'abort', 'abs', 'absneg', 'add', 'addabs',
  12. 'adds', 'addsx', 'addx', 'and', 'andn', 'byte', 'bytefill', 'bytemove', 'call', 'case', 'chipver',
  13. 'clkfreq', 'clkmode', 'clkset', 'cmp', 'cmps', 'cmpsub', 'cmpsx', 'cmpx', 'cnt', 'cogid',
  14. 'coginit', 'cognew', 'cogstop', 'con', 'constant', 'ctra', 'ctrb', 'dat', 'dira', 'dirb', 'djnz',
  15. 'else', 'elseif', 'elseifnot', 'enc', 'false', 'file', 'fit', 'float', 'from', 'frqa', 'frqb',
  16. 'hubop', 'if', 'ifnot', 'if_a', 'if_ae', 'if_always', 'if_b', 'if_be', 'if_c', 'if_c_and_nz',
  17. 'if_c_and_z', 'if_c_eq_z', 'if_c_ne_z', 'if_c_or_nz', 'if_c_or_z', 'if_e', 'if_nc',
  18. 'if_nc_and_nz', 'if_nc_and_z', 'if_nc_or_nz', 'if_nc_or_z', 'if_ne', 'if_never', 'if_nz',
  19. 'if_nz_and_c', 'if_nz_and_nc', 'if_nz_or_c', 'if_nz_or_nc', 'if_z', 'if_z_and_c', 'if_z_and_nc',
  20. 'if_z_eq_c', 'if_z_ne_c', 'if_z_or_c', 'if_z_or_nc', 'ina', 'inb', 'jmp', 'jmpret', 'lockclr',
  21. 'locknew', 'lockret', 'lockset', 'long', 'longfill', 'longmove', 'lookdown', 'lookdownz',
  22. 'lookup', 'lookupz', 'max', 'maxs', 'min', 'mins', 'mov', 'movd', 'movi', 'movs', 'mul', 'muls',
  23. 'muxc', 'muxnc', 'muxnz', 'muxz', 'neg', 'negc', 'negnc', 'negnz', 'negx', 'negz', 'next', 'nop',
  24. 'not', 'nr', 'obj', 'ones', 'or', 'org', 'other', 'outa', 'outb', 'par', 'phsa', 'phsb', 'pi',
  25. 'pll1x', 'pll2x', 'pll4x', 'pll8x', 'pll16x', 'posx', 'pri', 'pub', 'quit', 'rcfast', 'rcl',
  26. 'rcr', 'rcslow', 'rdbyte', 'rdlong', 'rdword', 'reboot', 'repeat', 'res', 'result', 'ret',
  27. 'return', 'rev', 'rol', 'ror', 'round', 'sar', 'shl', 'shr', 'spr', 'step', 'strcomp', 'string',
  28. 'strsize', 'sub', 'subabs', 'subs', 'subsx', 'subx', 'sumc', 'sumnc', 'sumnz', 'sumz', 'test',
  29. 'testn', 'tjnz', 'tjz', 'to', 'true', 'trunc', 'until', 'var', 'vcfg', 'vscl', 'waitcnt',
  30. 'waitpeq', 'waitpne', 'waitvid', 'wc', 'while', 'word', 'wordfill', 'wordmove', 'wr', 'wrbyte',
  31. 'wrlong', 'wz', 'xinput', 'xor', 'xtal1', 'xtal2', 'xtal3'
  32. }))
  33. -- Identifiers.
  34. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  35. -- Strings.
  36. lex:add_rule('string', token(lexer.STRING, lexer.range('"', true)))
  37. -- Comments.
  38. local line_comment = lexer.to_eol(P("''") + "'")
  39. local block_comment = lexer.range('{', '}')
  40. local block_doc_comment = lexer.range('{{', '}}')
  41. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_doc_comment + block_comment))
  42. -- Numbers.
  43. local bin = '%' * S('01_')^1
  44. local ter = '%%' * (R('03') + '_')^1
  45. local hex = '$' * (lexer.xdigit + '_')^1
  46. local dec = (lexer.digit + '_')^1
  47. local int = bin + ter + dec + hex
  48. local rad = P('.') - '..'
  49. local exp = (S('Ee') * S('+-')^-1 * int)^-1
  50. local flt = dec * (rad * dec)^-1 * exp + dec^-1 * rad * dec * exp
  51. lex:add_rule('number', token(lexer.NUMBER, flt + int))
  52. -- Operators.
  53. lex:add_rule('operator', token(lexer.OPERATOR,
  54. P('--') + '++' + '^^' + '||' + '~~' + '|<' + '>|' + '@@' + ':=' + '+=' + '-=' + '*=' + '/=' + '**' +
  55. '**=' + '//' + '//=' + '#>' + '#>=' + '<#' + '<#=' + '~>' + '~>=' + '<<' + '<<=' + '>>' + '>>=' +
  56. '<-' + '<-=' + '->' + '->=' + '><' + '><=' + '&=' + '|=' + 'and=' + 'or=' + '==' + '===' + '<>' +
  57. '<>=' + '<=' + '>=' + '=<' + '=<=' + '=>' + '=>=' + '..' + S('+-/*<>~!&=^|?:.()[]@#\\')))
  58. lexer.property['scintillua.comment'] = "'"
  59. return lex