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

pure.lua (1663B)


  1. -- Copyright 2015-2024 David B. Lamkins <david@lamkins.net>. See LICENSE.
  2. -- pure LPeg lexer, see http://purelang.bitbucket.org/
  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('pure')
  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. 'namespace', 'with', 'end', 'using', 'interface', 'extern', 'let', 'const', 'def', 'type',
  12. 'public', 'private', 'nonfix', 'outfix', 'infix', 'infixl', 'infixr', 'prefix', 'postfix', 'if',
  13. 'otherwise', 'when', 'case', 'of', 'then', 'else'
  14. }))
  15. -- Identifiers.
  16. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  17. -- Strings.
  18. lex:add_rule('string', token(lexer.STRING, lexer.range('"', true)))
  19. -- Comments.
  20. local line_comment = lexer.to_eol('//')
  21. local block_comment = lexer.range('/*', '*/')
  22. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  23. -- Numbers.
  24. local bin = '0' * S('Bb') * S('01')^1
  25. local hex = lexer.hex_num
  26. local dec = lexer.dec_num
  27. local int = (bin + hex + dec) * P('L')^-1
  28. local rad = P('.') - '..'
  29. local exp = (S('Ee') * S('+-')^-1 * int)^-1
  30. local flt = int * (rad * dec)^-1 * exp + int^-1 * rad * dec * exp
  31. lex:add_rule('number', token(lexer.NUMBER, flt + int))
  32. -- Pragmas.
  33. local hashbang = lexer.starts_line('#!') * (lexer.nonnewline - '//')^0
  34. lex:add_rule('pragma', token(lexer.PREPROCESSOR, hashbang))
  35. -- Operators.
  36. lex:add_rule('operator', token(lexer.OPERATOR, '..' + S('+-/*%<>~!=^&|?~:;,.()[]{}@#$`\\\'')))
  37. lexer.property['scintillua.comment'] = '//'
  38. return lex