vis

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

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

chuck.lua

(3429B)


      1 -- Copyright 2010-2025 Martin Morawetz. See LICENSE.
      2 -- ChucK LPeg lexer.
      3 
      4 local lexer = lexer
      5 local word_match = lexer.word_match
      6 local P, S = lpeg.P, lpeg.S
      7 
      8 local lex = lexer.new(...)
      9 
     10 -- Keywords.
     11 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
     12 
     13 -- Constants.
     14 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
     15 
     16 -- Types.
     17 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
     18 
     19 -- Classes.
     20 lex:add_rule('class', lex:tag(lexer.CLASS, lex:word_match(lexer.CLASS)))
     21 
     22 -- Functions.
     23 local std = 'Std.' * lex:word_match(lexer.FUNCTION_BUILTIN)
     24 local machine = 'Machine.' * lex:word_match(lexer.FUNCTION_BUILTIN .. '.machine')
     25 local math = 'Math.' * lex:word_match(lexer.FUNCTION_BUILTIN .. '.math')
     26 local func = lex:tag(lexer.FUNCTION, lexer.word) * #P('(')
     27 lex:add_rule('function', lex:tag(lexer.FUNCTION_BUILTIN, std + machine + math) + func)
     28 
     29 -- Constants.
     30 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN,
     31 	'Math.' * lex:word_match(lexer.CONSTANT_BUILTIN .. '.math')))
     32 
     33 -- Global ugens.
     34 lex:add_rule('ugen', lex:tag(lexer.CONSTANT_BUILTIN .. '.ugen', word_match('dac adc blackhole')))
     35 
     36 -- Times.
     37 lex:add_rule('time', lex:tag(lexer.NUMBER, word_match('samp ms second minute hour day week')))
     38 
     39 -- Special special value.
     40 lex:add_rule('now', lex:tag(lexer.CONSTANT_BUILTIN .. '.now', word_match('now')))
     41 
     42 -- Strings.
     43 local sq_str = P('L')^-1 * lexer.range("'", true)
     44 local dq_str = P('L')^-1 * lexer.range('"', true)
     45 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str))
     46 
     47 -- Identifiers.
     48 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
     49 
     50 -- Comments.
     51 local line_comment = lexer.to_eol('//', true)
     52 local block_comment = lexer.range('/*', '*/')
     53 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
     54 
     55 -- Numbers.
     56 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))
     57 
     58 -- Operators.
     59 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}@')))
     60 
     61 -- Word lists.
     62 lex:set_word_list(lexer.KEYWORD, {
     63 	-- Control structures.
     64 	'break', 'continue', 'else', 'for', 'if', 'repeat', 'return', 'switch', 'until', 'while',
     65 	-- Other chuck keywords.
     66 	'function', 'fun', 'spork', 'const', 'new'
     67 })
     68 
     69 lex:set_word_list(lexer.CONSTANT_BUILTIN, {
     70 	'false', 'maybe', 'me', 'null', 'NULL', 'pi', 'true' -- special values
     71 })
     72 
     73 lex:set_word_list(lexer.TYPE, 'float int time dur void same')
     74 
     75 -- Class keywords.
     76 lex:set_word_list(lexer.CLASS, {
     77 	'class', 'extends', 'implements', 'interface', 'private', 'protected', 'public', 'pure', 'static',
     78 	'super', 'this'
     79 })
     80 
     81 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
     82 	'abs', 'fabs', 'sgn', 'system', 'atoi', 'atof', 'getenv', 'setenv', 'mtof', 'ftom', 'powtodb',
     83 	'rmstodb', 'dbtopow', 'dbtorms'
     84 })
     85 
     86 lex:set_word_list(lexer.FUNCTION_BUILTIN .. '.machine', {
     87 	'add', 'spork', 'remove', 'replace', 'status', 'crash'
     88 })
     89 
     90 lex:set_word_list(lexer.FUNCTION_BUILTIN .. '.math', {
     91 	'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'sinh', 'cosh', 'tanh', 'hypot', 'pow',
     92 	'sqrt', 'exp', 'log', 'log2', 'log10', 'random', 'random2', 'randomf', 'random2f', 'srandom',
     93 	'floor', 'ceil', 'round', 'trunc', 'fmod', 'remainder', 'min', 'max', 'nextpow2', 'isinf', 'isnan'
     94 })
     95 
     96 lex:set_word_list(lexer.CONSTANT_BUILTIN .. '.math', {
     97 	'PI', 'TWO_PI', 'e', 'E', 'i', 'I', 'j', 'J', 'RANDOM_MAX'
     98 })
     99 
    100 lexer.property['scintillua.comment'] = '//'
    101 
    102 return lex