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

go.lua (2363B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- Go LPeg lexer.
  3. local lexer = lexer
  4. local P, S = lpeg.P, lpeg.S
  5. local lex = lexer.new(...)
  6. -- Keywords.
  7. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  8. -- Constants.
  9. lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
  10. -- Types.
  11. lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
  12. -- Functions.
  13. local builtin_func = -lpeg.B('.') *
  14. lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
  15. local func = lex:tag(lexer.FUNCTION, lexer.word)
  16. local method = lpeg.B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
  17. lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))
  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('"', true)
  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. local line_comment = lexer.to_eol('//')
  27. local block_comment = lexer.range('/*', '*/')
  28. lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
  29. -- Numbers.
  30. lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number * P('i')^-1))
  31. -- Operators.
  32. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-*/%&|^<>=!~:;.,()[]{}')))
  33. -- Fold points.
  34. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  35. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  36. -- Word lists.
  37. lex:set_word_list(lexer.KEYWORD, {
  38. 'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else', 'fallthrough', 'for',
  39. 'func', 'go', 'goto', 'if', 'import', 'interface', 'map', 'package', 'range', 'return', 'select',
  40. 'struct', 'switch', 'type', 'var'
  41. })
  42. lex:set_word_list(lexer.CONSTANT_BUILTIN, 'true false iota nil')
  43. lex:set_word_list(lexer.TYPE, {
  44. 'any', 'bool', 'byte', 'comparable', 'complex64', 'complex128', 'error', 'float32', 'float64',
  45. 'int', 'int8', 'int16', 'int32', 'int64', 'rune', 'string', 'uint', 'uint8', 'uint16', 'uint32',
  46. 'uint64', 'uintptr'
  47. })
  48. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  49. 'append', 'cap', 'close', 'complex', 'copy', 'delete', 'imag', 'len', 'make', 'new', 'panic',
  50. 'print', 'println', 'real', 'recover'
  51. })
  52. lexer.property['scintillua.comment'] = '//'
  53. return lex