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

myrddin.lua (1838B)


  1. -- Copyright 2017-2024 Michael Forney. See LICENSE
  2. -- Myrddin LPeg lexer.
  3. local lexer = require('lexer')
  4. local token, word_match = lexer.token, lexer.word_match
  5. local P, S = lpeg.P, lpeg.S
  6. local lex = lexer.new('myrddin')
  7. -- Whitespace.
  8. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  9. -- Keywords.
  10. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  11. 'break', 'const', 'continue', 'elif', 'else', 'extern', 'false', 'for', 'generic', 'goto', 'if',
  12. 'impl', 'in', 'match', 'pkg', 'pkglocal', 'sizeof', 'struct', 'trait', 'true', 'type', 'union',
  13. 'use', 'var', 'while'
  14. }))
  15. -- Types.
  16. lex:add_rule('type', token(lexer.TYPE, word_match{
  17. 'void', 'bool', 'char', 'byte', 'int', 'uint', 'int8', 'uint8', 'int16', 'uint16', 'int32',
  18. 'uint32', 'int64', 'uint64', 'flt32', 'flt64'
  19. } + '@' * lexer.word))
  20. -- Identifiers.
  21. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  22. -- Comments.
  23. local line_comment = lexer.to_eol('//', true)
  24. local block_comment = lexer.range('/*', '*/', false, false, true)
  25. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  26. -- Strings.
  27. local sq_str = lexer.range("'", true)
  28. local dq_str = lexer.range('"', true)
  29. lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
  30. -- Numbers.
  31. local digit = lexer.digit + '_'
  32. local bdigit = S('01') + '_'
  33. local xdigit = lexer.xdigit + '_'
  34. local odigit = lpeg.R('07') + '_'
  35. local integer = '0x' * xdigit^1 + '0o' * odigit^1 + '0b' * bdigit^1 + digit^1
  36. local float = digit^1 * ((('.' * digit^1) * (S('eE') * S('+-')^-1 * digit^1)^-1) +
  37. (('.' * digit^1)^-1 * S('eE') * S('+-')^-1 * digit^1))
  38. lex:add_rule('number', token(lexer.NUMBER, float + integer))
  39. -- Operators.
  40. lex:add_rule('operator', token(lexer.OPERATOR, S('`#_+-/*%<>~!=^&|~:;,.()[]{}')))
  41. lexer.property['scintillua.comment'] = '//'
  42. return lex