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

chuck.lua (3444B)


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