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

snobol4.lua (2600B)


  1. -- Copyright 2013-2024 Michael T. Richter. See LICENSE.
  2. -- SNOBOL4 lexer.
  3. -- This lexer works with classic SNOBOL4 as well as the CSNOBOL4 extensions.
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local B, P, S = lpeg.B, lpeg.P, lpeg.S
  7. local lex = lexer.new('snobol4')
  8. -- Whitespace.
  9. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  10. -- Keywords.
  11. lex:add_rule('keyword', token(lexer.KEYWORD, word_match({
  12. 'ABORT', 'ARRAY', 'CONTINUE', 'DEFINE', 'END', 'FRETURN', 'INPUT', 'NRETURN', 'OUTPUT', 'PUNCH',
  13. 'RETURN', 'SCONTINUE', 'TABLE'
  14. }, true) + '&' * lexer.word))
  15. -- Helper patterns.
  16. local dotted_id = lexer.word * ('.' * lexer.word)^0
  17. -- Labels.
  18. lex:add_rule('label', token(lexer.LABEL, lexer.starts_line(dotted_id)))
  19. -- Targets.
  20. local branch = B(lexer.space * ':(') * dotted_id * #P(')')
  21. local sbranch = B(lexer.space * ':' * S('SsFf') * '(') * dotted_id * #P(')')
  22. local sbranchx = B(')' * S('SsFf') * '(') * dotted_id * #P(')')
  23. lex:add_rule('target', token(lexer.LABEL, branch + sbranch + sbranchx))
  24. -- Patterns.
  25. lex:add_rule('pattern', lexer.token(lexer.CLASS, word_match({
  26. -- Keep distinct.
  27. 'ABORT', 'ANY', 'ARB', 'ARBNO', 'BAL', 'BREAK', 'BREAKX', 'FAIL', 'FENCE', 'LEN', 'NOTANY', 'POS',
  28. 'REM', 'RPOS', 'RTAB', 'SPAN', 'SUCCEED', 'TAB'
  29. }, true) * #P('(')))
  30. -- Token definitions.
  31. lex:add_rule('built-in', token(lexer.FUNCTION, word_match({
  32. 'APPLY', 'ARRAY', 'CHAR', 'CONVERT', 'COPY', 'DATA', 'DATE', 'DIFFER', 'DUPL', 'EQ', 'EVAL',
  33. 'FILE_ABSPATH', 'FILE_ISDIR', 'FREEZE', 'FUNCTION', 'GE', 'GT', 'HOST', 'IDENT', 'INTEGER',
  34. 'IO_FINDUNIT', 'ITEM', 'LABEL', 'LOAD', 'LPAD', 'LE', 'LGT', 'LT', 'NE', 'OPSYN', 'ORD',
  35. 'PROTOTYPE', 'REMDR', 'REPLACE', 'REVERSE', 'RPAD', 'RSORT', 'SERV_LISTEN', 'SET', 'SETEXIT',
  36. 'SIZE', 'SORT', 'SQRT', 'SSET', 'SUBSTR', 'TABLE', 'THAW', 'TIME', 'TRACE', 'TRIM', 'UNLOAD',
  37. 'VALUE', 'VDIFFER'
  38. }, true) * #P('(')))
  39. -- Identifiers.
  40. lex:add_rule('identifier', token(lexer.DEFAULT, dotted_id))
  41. -- Strings.
  42. local dq_str = lexer.range('"', true, false)
  43. local sq_str = lexer.range("'", true, false)
  44. lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
  45. -- Comments.
  46. lex:add_rule('comment', token(lexer.COMMENT, lexer.starts_line(lexer.to_eol(S('*#|;!')))))
  47. -- Numbers.
  48. lex:add_rule('number', token(lexer.NUMBER, lexer.number))
  49. -- Control.
  50. lex:add_rule('control', token(lexer.PREPROCESSOR, lexer.starts_line('-' * lexer.word)))
  51. -- Operators.
  52. lex:add_rule('operator', token(lexer.OPERATOR, S'¬?$.!%*/#+-@⊥&^~\\='))
  53. lexer.property['scintillua.comment'] = '#'
  54. return lex