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

julia.lua (4487B)


  1. -- Copyright 2020-2024 Tobias Frilling. See LICENSE.
  2. -- Julia lexer.
  3. local lexer = require('lexer')
  4. local token, word_match = lexer.token, lexer.word_match
  5. local B, P, S = lpeg.B, lpeg.P, lpeg.S
  6. local lex = lexer.new('julia')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. local id = lexer.word * P('!')^0
  10. -- Keyword
  11. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  12. 'baremodule', 'begin', 'break', 'catch', 'const', 'continue', 'do', 'else', 'elseif', 'end',
  13. 'export', 'finally', 'for', 'function', 'global', 'if', 'in', 'isa', 'import', 'let', 'local',
  14. 'macro', 'module', 'quote', 'return', 'struct', 'try', 'using', 'where', 'while'
  15. } + 'abstract type' + 'mutable struct' + 'primitive type'))
  16. -- Constant
  17. local const_bool = word_match('true false')
  18. local const_numerical = (P('Inf') + 'NaN') * (P('16') + '32' + '64')^-1 * -lexer.alnum
  19. local const_special = word_match('nothing undef missing')
  20. local const_env = word_match('ARGS ENV ENDIAN_BOM LOAD_PATH VERSION PROGRAM_FILE DEPOT_PATH')
  21. local const_io = word_match('stdout stdin stderr devnull')
  22. lex:add_rule('constant', token(lexer.CONSTANT,
  23. const_bool + const_numerical + const_special + const_env + const_io))
  24. -- Type
  25. local type_annotated = (B('::') + B(':: ')) * id
  26. local type_para = id * #P('{')
  27. local type_subtyping = id * #(lexer.space^0 * '<:') + (B('<:') + B('<: ')) * id
  28. local type_struct = B('struct ') * id
  29. -- LuaFormatter off
  30. local type_builtin_numerical = ((P('Abstract') + 'Big') * 'Float' +
  31. 'Float' * (P('16') + '32' + '64') +
  32. P('U')^-1 * 'Int' * (P('8') + '16' + '32' + '64' + '128')^-1 +
  33. P('Abstract')^-1 * 'Irrational'
  34. ) * -lexer.alnum + word_match('Number Complex Real Integer Bool Signed Unsigned Rational')
  35. -- LuaFormatter on
  36. local type_builtin_range = ((P('Lin') + 'Ordinal' + (P('Abstract')^-1 * P('Unit')^-1)) * 'Range' +
  37. 'StepRange' * P('Len')^-1 - 'Range'
  38. ) * -lexer.alnum
  39. local type_builtin_array = ((P('Abstract') + 'Bit' + 'Dense' + 'PermutedDims' + 'Sub')^-1 *
  40. word_match('Array Vector Matrix VecOrMat') +
  41. (P('Abstract') + 'Sym' + (P('Unit')^-1 * (P('Lower') + 'Upper')))^-1 * 'Triangular'
  42. ) * -lexer.alnum +
  43. word_match('Adjoint Bidiagonal Diagonal Hermitian LQPackedQ Symmetric Transpose UpperHessenberg')
  44. lex:add_rule('type', token(lexer.TYPE,
  45. type_para + type_annotated + type_subtyping + type_struct + type_builtin_numerical +
  46. type_builtin_range + type_builtin_array))
  47. -- Macro
  48. lex:add_rule('macro', token(lexer.PREPROCESSOR, '@' * (id + '.')))
  49. -- Symbol
  50. lex:add_rule('symbol', token('symbol', -B(P(':') + '<') * ':' * id))
  51. lex:add_style('symbol', lexer.styles.string)
  52. -- Function
  53. lex:add_rule('function', token(lexer.FUNCTION, id * #(P('.')^-1 * '(')))
  54. -- Identifier
  55. lex:add_rule('identifier', token(lexer.IDENTIFIER, id))
  56. -- Comment
  57. local line_comment = lexer.to_eol('#')
  58. local block_comment = lexer.range('#=', '=#')
  59. lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment))
  60. -- Number
  61. local decimal = lexer.digit^1 * ('_' * lexer.digit^1)^0
  62. local hex_digits = lexer.xdigit^1 * ('_' * lexer.xdigit^1)^0
  63. local hexadecimal = '0x' * hex_digits
  64. local binary = '0b' * S('01')^1 * ('_' * S('01')^1)^0
  65. local integer = binary + hexadecimal + decimal
  66. local float_dec_coeff = decimal^0 * '.' * decimal + decimal * '.' * decimal^0
  67. local float_dec_expon = S('eEf') * S('+-')^-1 * lexer.digit^1
  68. local float_dec = float_dec_coeff * float_dec_expon^-1 + decimal * float_dec_expon
  69. local float_hex_coeff = '0x' * (hex_digits^0 * '.' * hex_digits + hex_digits * '.' * hex_digits^0)
  70. local float_hex_expon = 'p' * S('+-')^-1 * lexer.digit^1
  71. local float_hex = float_hex_coeff * float_hex_expon^-1 + hexadecimal * float_hex_expon
  72. local float = float_dec + float_hex
  73. local imaginary = (float_dec + decimal) * 'im'
  74. lex:add_rule('number',
  75. token(lexer.NUMBER, S('+-')^-1 * (imaginary + float + integer) * -lexer.alpha))
  76. -- String & Character
  77. local doc_str = lexer.range('"""')
  78. local str = lexer.range('"')
  79. lex:add_rule('string', token(lexer.STRING, doc_str + str))
  80. local c_esc = '\\' * S('\\"\'nrbtfav')
  81. local unicode = '\\' * S('uU') * lexer.xdigit^1
  82. local char = "'" * (lexer.alnum + c_esc + unicode) * "'"
  83. lex:add_rule('character', token('character', char))
  84. lex:add_style('character', lexer.styles.constant)
  85. -- Operator
  86. lex:add_rule('operator', token(lexer.OPERATOR, S('+-*/<>=!%^&|~\\\':?.') + '÷' + '≠' + '≈' +
  87. '≤' + '≥' + '⊻' + '√'))
  88. lexer.property['scintillua.comment'] = '#'
  89. return lex