vis

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

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

pike.lua

(1742B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- Pike 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('pike')
      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 	'break', 'case', 'catch', 'continue', 'default', 'do', 'else', 'for', 'foreach', 'gauge', 'if',
     16 	'lambda', 'return', 'sscanf', 'switch', 'while', 'import', 'inherit',
     17 	-- Type modifiers.
     18 	'constant', 'extern', 'final', 'inline', 'local', 'nomask', 'optional', 'private', 'protected',
     19 	'public', 'static', 'variant'
     20 }))
     21 
     22 -- Types.
     23 lex:add_rule('type', token(lexer.TYPE, word_match(
     24 	'array class float function int mapping mixed multiset object program string void')))
     25 
     26 -- Identifiers.
     27 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     28 
     29 -- Strings.
     30 local sq_str = lexer.range("'", true)
     31 local dq_str = P('#')^-1 * lexer.range('"', true)
     32 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     33 
     34 -- Comments.
     35 local line_comment = lexer.to_eol('//', true)
     36 local block_comment = lexer.range('/*', '*/', false, false, true)
     37 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     38 
     39 -- Numbers.
     40 lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('lLdDfF')^-1))
     41 
     42 -- Preprocessors.
     43 lex:add_rule('preprocessor', token(lexer.PREPROCESSOR, lexer.to_eol(lexer.starts_line('#'))))
     44 
     45 -- Operators.
     46 lex:add_rule('operator', token(lexer.OPERATOR, S('<>=!+-/*%&|^~@`.,:;()[]{}')))
     47 
     48 -- Fold points.
     49 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     50 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     51 
     52 lexer.property['scintillua.comment'] = '//'
     53 
     54 return lex