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

hare.lua (2766B)


  1. -- Copyright 2021-2024 Mitchell. See LICENSE.
  2. -- Hare LPeg lexer
  3. local lexer = lexer
  4. local P, R, S = lpeg.P, lpeg.R, lpeg.S
  5. local lex = lexer.new(...)
  6. -- Keywords.
  7. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  8. -- Types.
  9. lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
  10. -- Functions.
  11. local builtin_func = lex:tag(lexer.FUNCTION_BUILTIN,
  12. lex:word_match(lexer.FUNCTION_BUILTIN) + 'size' * #(lexer.space^0 * '('))
  13. local func = lex:tag(lexer.FUNCTION, lex:tag(lexer.FUNCTION, lexer.word * ('::' * lexer.word)^0 *
  14. #(lexer.space^0 * '(')))
  15. lex:add_rule('function', builtin_func + func)
  16. -- Constants.
  17. lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
  18. -- Identifiers.
  19. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
  20. -- Strings.
  21. local sq_str = lexer.range("'", true)
  22. local dq_str = lexer.range('"')
  23. local raw_str = lexer.range('`', false, false)
  24. lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + raw_str))
  25. -- Comments.
  26. lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.to_eol('//')))
  27. -- Numbers.
  28. local bin_num = '0b' * R('01')^1 * -lexer.xdigit
  29. local oct_num = '0o' * R('07')^1 * -lexer.xdigit
  30. local hex_num = '0x' * lexer.xdigit^1
  31. local int_suffix = lexer.word_match('i u z i8 i16 i32 i64 u8 u16 u32 u64')
  32. local float_suffix = lexer.word_match('f32 f64')
  33. local suffix = int_suffix + float_suffix
  34. local integer = S('+-')^-1 *
  35. ((hex_num + oct_num + bin_num) * int_suffix^-1 + lexer.dec_num * suffix^-1)
  36. local float = lexer.float * float_suffix^-1
  37. lex:add_rule('number', lex:tag(lexer.NUMBER, integer + float))
  38. -- Error assertions
  39. lex:add_rule('error_assert', lex:tag(lexer.ERROR .. '.assert', lpeg.B(')') * P('!')))
  40. -- Operators.
  41. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%^!=&|?~:;,.()[]{}<>')))
  42. -- Attributes.
  43. lex:add_rule('attribute', lex:tag(lexer.ANNOTATION, '@' * lexer.word))
  44. -- Fold points.
  45. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  46. -- Word lists.
  47. lex:set_word_list(lexer.KEYWORD, {
  48. 'as', 'break', 'case', 'const', 'continue', 'def', 'defer', 'else', 'export', 'fn', 'for', 'if',
  49. 'is', 'let', 'match', 'nullable', 'return', 'static', 'switch', 'type', 'use', 'yield', '_'
  50. })
  51. lex:set_word_list(lexer.TYPE, {
  52. 'bool', 'enum', 'f32', 'f64', 'i16', 'i32', 'i64', 'i8', 'int', 'opaque', 'never', 'rune', 'size',
  53. 'str', 'struct', 'u16', 'u32', 'u64', 'u8', 'uint', 'uintptr', 'union', 'valist'
  54. })
  55. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  56. 'abort', 'align', 'alloc', 'append', 'assert', 'delete', 'free', 'insert', 'len', 'offset',
  57. 'vaarg', 'vaend', 'vastart'
  58. })
  59. lex:set_word_list(lexer.CONSTANT_BUILTIN, 'false null true void')
  60. lexer.property['scintillua.comment'] = '//'
  61. return lex