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

forth.lua (2746B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- Forth LPeg lexer.
  3. -- Contributions from Joseph Eib.
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local P, S = lpeg.P, lpeg.S
  7. local lex = lexer.new('forth')
  8. -- Whitespace.
  9. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  10. -- Strings.
  11. local c_str = 'c' * lexer.range('"', true, false)
  12. local s_str = 's' * lexer.range('"', true, false)
  13. local s_bs_str = 's\\' * lexer.range('"', true)
  14. local dot_str = '.' * lexer.range('"', true, false)
  15. local dot_paren_str = '.' * lexer.range('(', ')', true)
  16. local abort_str = 'abort' * lexer.range('"', true, false)
  17. lex:add_rule('string',
  18. token(lexer.STRING, c_str + s_str + s_bs_str + dot_str + dot_paren_str + abort_str))
  19. -- Keywords.
  20. lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
  21. '#>', '#s', '*/', '*/mod', '+loop', ',', '.', '.r', '/mod', '0<', '0<>', '0>', '0=', '1+', '1-',
  22. '2!', '2*', '2/', '2>r', '2@', '2drop', '2dup', '2over', '2r>', '2r@', '2swap', ':noname', '<#',
  23. '<>', '>body', '>in', '>number', '>r', '?do', '?dup', '@', 'abort', 'abs', 'accept', 'action-of',
  24. 'again', 'align', 'aligned', 'allot', 'and', 'base', 'begin', 'bl', 'buffer:', 'c!', 'c,', 'c@',
  25. 'case', 'cell+', 'cells', 'char', 'char+', 'chars', 'compile,', 'constant,', 'count', 'cr',
  26. 'create', 'decimal', 'defer', 'defer!', 'defer@', 'depth', 'do', 'does>', 'drop', 'dup', 'else',
  27. 'emit', 'endcase', 'endof', 'environment?', 'erase', 'evaluate', 'execute', 'exit', 'false',
  28. 'fill', 'find', 'fm/mod', 'here', 'hex', 'hold', 'holds', 'i', 'if', 'immediate', 'invert', 'is',
  29. 'j', 'key', 'leave', 'literal', 'loop', 'lshift', 'm*', 'marker', 'max', 'min', 'mod', 'move',
  30. 'negate', 'nip', 'of', 'or', 'over', 'pad', 'parse', 'parse-name', 'pick', 'postpone', 'quit',
  31. 'r>', 'r@', 'recurse', 'refill', 'restore-input', 'roll', 'rot', 'rshift', 's>d', 'save-input',
  32. 'sign', 'sm/rem', 'source', 'source-id', 'space', 'spaces', 'state', 'swap', 'to', 'then', 'true',
  33. 'tuck', 'type', 'u.', 'u.r', 'u>', 'u<', 'um*', 'um/mod', 'unloop', 'until', 'unused', 'value',
  34. 'variable', 'while', 'within', 'word', 'xor', "[']", '[char]', '[compile]'
  35. }, true)))
  36. -- Identifiers.
  37. lex:add_rule('identifier', token(lexer.IDENTIFIER, (lexer.alnum + S('+-*=<>.?/\'%,_$#'))^1))
  38. -- Comments.
  39. local line_comment = lexer.to_eol(S('|\\'))
  40. local block_comment = lexer.range('(', ')')
  41. lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
  42. -- Numbers.
  43. lex:add_rule('number', token(lexer.NUMBER, P('-')^-1 * lexer.digit^1 * (S('./') * lexer.digit^1)^-1))
  44. -- Operators.
  45. lex:add_rule('operator', token(lexer.OPERATOR, S(':;<>+*-/[]#')))
  46. lexer.property['scintillua.comment'] = '\\'
  47. return lex