vis

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

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

elm.lua

(1367B)


      1 -- Copyright 2020-2025 Mitchell. See LICENSE.
      2 -- Elm LPeg lexer
      3 -- Adapted from Haskell LPeg lexer by Karl Schultheisz.
      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('elm', {fold_by_indentation = true})
     10 
     11 -- Whitespace.
     12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     13 
     14 -- Keywords.
     15 lex:add_rule('keyword', token(lexer.KEYWORD, word_match(
     16 	'if then else case of let in module import as exposing type alias port')))
     17 
     18 -- Types & type constructors.
     19 local word = (lexer.alnum + S("._'#"))^0
     20 local op = lexer.punct - S('()[]{}')
     21 lex:add_rule('type', token(lexer.TYPE, lexer.upper * word + ':' * (op^1 - ':')))
     22 
     23 -- Identifiers.
     24 lex:add_rule('identifier', token(lexer.IDENTIFIER, (lexer.alpha + '_') * word))
     25 
     26 -- Strings.
     27 lex:add_rule('string', token(lexer.STRING, lexer.range('"')))
     28 
     29 -- Chars.
     30 lex:add_rule('character', token(lexer.STRING, lexer.range("'", true)))
     31 
     32 -- Comments.
     33 local line_comment = lexer.to_eol('--', true)
     34 local block_comment = lexer.range('{-', '-}', false, false, true)
     35 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     36 
     37 -- Numbers.
     38 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     39 
     40 -- Operators.
     41 lex:add_rule('operator', token(lexer.OPERATOR, op))
     42 
     43 lexer.property['scintillua.comment'] = '--'
     44 
     45 return lex