vis

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

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

ps.lua

(1545B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- Postscript LPeg lexer.
      3 
      4 local lexer = require('lexer')
      5 local token, word_match = lexer.token, lexer.word_match
      6 local P, S = lpeg.P, lpeg.S
      7 
      8 local lex = lexer.new('ps')
      9 
     10 -- Whitespace.
     11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     12 
     13 -- Keywords.
     14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     15 	'pop', 'exch', 'dup', 'copy', 'roll', 'clear', 'count', 'mark', 'cleartomark', 'counttomark',
     16 	'exec', 'if', 'ifelse', 'for', 'repeat', 'loop', 'exit', 'stop', 'stopped', 'countexecstack',
     17 	'execstack', 'quit', 'start', 'true', 'false', 'NULL'
     18 }))
     19 
     20 -- Functions.
     21 lex:add_rule('function', token(lexer.FUNCTION, word_match{
     22 	'add', 'div', 'idiv', 'mod', 'mul', 'sub', 'abs', 'ned', 'ceiling', 'floor', 'round', 'truncate',
     23 	'sqrt', 'atan', 'cos', 'sin', 'exp', 'ln', 'log', 'rand', 'srand', 'rrand'
     24 }))
     25 
     26 -- Identifiers.
     27 local word = (lexer.alpha + '-') * (lexer.alnum + '-')^0
     28 lex:add_rule('identifier', token(lexer.IDENTIFIER, word))
     29 
     30 -- Strings.
     31 local arrow_string = lexer.range('<', '>')
     32 local nested_string = lexer.range('(', ')', false, false, true)
     33 lex:add_rule('string', token(lexer.STRING, arrow_string + nested_string))
     34 
     35 -- Comments.
     36 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('%')))
     37 
     38 -- Numbers.
     39 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     40 
     41 -- Labels.
     42 lex:add_rule('label', token(lexer.LABEL, '/' * word))
     43 
     44 -- Operators.
     45 lex:add_rule('operator', token(lexer.OPERATOR, S('[]{}')))
     46 
     47 lexer.property['scintillua.comment'] = '%'
     48 
     49 return lex