vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

forth.lua

(2731B)


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