vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

spin.lua

(3735B)


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