vis

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

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

fish.lua

(2156B)


      1 -- Copyright 2015-2025 Jason Schindler. See LICENSE.
      2 -- Fish (http://fishshell.com/) script 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('fish')
      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 	'alias', 'and', 'begin', 'bg', 'bind', 'block', 'break', 'breakpoint', 'builtin', 'case', 'cd',
     16 	'command', 'commandline', 'complete', 'contains', 'continue', 'count', 'dirh', 'dirs', 'echo',
     17 	'else', 'emit', 'end', 'eval', 'exec', 'exit', 'fg', 'fish', 'fish_config', 'fishd',
     18 	'fish_indent', 'fish_pager', 'fish_prompt', 'fish_right_prompt', 'fish_update_completions', 'for',
     19 	'funced', 'funcsave', 'function', 'functions', 'help', 'history', 'if', 'in', 'isatty', 'jobs',
     20 	'math', 'mimedb', 'nextd', 'not', 'open', 'or', 'popd', 'prevd', 'psub', 'pushd', 'pwd', 'random',
     21 	'read', 'return', 'set', 'set_color', 'source', 'status', 'switch', 'test', 'trap', 'type',
     22 	'ulimit', 'umask', 'vared', 'while'
     23 }))
     24 
     25 -- Identifiers.
     26 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     27 
     28 -- Variables.
     29 lex:add_rule('variable', token(lexer.VARIABLE, '$' * (lexer.word + lexer.range('{', '}', true))))
     30 
     31 -- Strings.
     32 local sq_str = lexer.range("'", false, false)
     33 local dq_str = lexer.range('"')
     34 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     35 
     36 -- Shebang.
     37 lex:add_rule('shebang', token(lexer.COMMENT, lexer.to_eol('#!/')))
     38 
     39 -- Comments.
     40 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
     41 
     42 -- Numbers.
     43 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     44 
     45 -- Operators.
     46 lex:add_rule('operator', token(lexer.OPERATOR, S('=!<>+-/*^&|~.,:;?()[]{}')))
     47 
     48 -- Fold points.
     49 lex:add_fold_point(lexer.KEYWORD, 'begin', 'end')
     50 lex:add_fold_point(lexer.KEYWORD, 'for', 'end')
     51 lex:add_fold_point(lexer.KEYWORD, 'function', 'end')
     52 lex:add_fold_point(lexer.KEYWORD, 'if', 'end')
     53 lex:add_fold_point(lexer.KEYWORD, 'switch', 'end')
     54 lex:add_fold_point(lexer.KEYWORD, 'while', 'end')
     55 
     56 lexer.property['scintillua.comment'] = '#'
     57 
     58 return lex