aboutsummaryrefslogtreecommitdiff
path: root/lua/lexers/elixir.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/lexers/elixir.lua')
-rw-r--r--lua/lexers/elixir.lua186
1 files changed, 80 insertions, 106 deletions
diff --git a/lua/lexers/elixir.lua b/lua/lexers/elixir.lua
index 4863e4a..b06e6c0 100644
--- a/lua/lexers/elixir.lua
+++ b/lua/lexers/elixir.lua
@@ -1,123 +1,97 @@
--- Copyright 2015-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
+-- Copyright 2015-2022 Mitchell. See LICENSE.
-- Contributed by Richard Philips.
--- Elixer LPeg lexer.
+-- Elixir LPeg lexer.
-local l = require('lexer')
-local token, style, color, word_match = l.token, l.style, l.color, l.word_match
-local B, P, R, S = lpeg.B, lpeg.P, lpeg.R, lpeg.S
+local lexer = require('lexer')
+local token, word_match = lexer.token, lexer.word_match
+local B, P, S = lpeg.B, lpeg.P, lpeg.S
-local M = {_NAME = 'elixir'}
+local lex = lexer.new('elixir', {fold_by_indentation = true})
-- Whitespace.
-local ws = token(l.WHITESPACE, l.space^1)
-
--- Comments.
-local comment = token(l.COMMENT, '#' * l.nonnewline_esc^0)
+lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
+
+-- Sigils.
+local sigil11 = '~' * S('CRSW') * lexer.range('<', '>')
+local sigil12 = '~' * S('CRSW') * lexer.range('{', '}')
+local sigil13 = '~' * S('CRSW') * lexer.range('[', ']')
+local sigil14 = '~' * S('CRSW') * lexer.range('(', ')')
+local sigil15 = '~' * S('CRSW') * lexer.range('|', false, false)
+local sigil16 = '~' * S('CRSW') * lexer.range('/', false, false)
+local sigil17 = '~' * S('CRSW') * lexer.range('"', false, false)
+local sigil18 = '~' * S('CRSW') * lexer.range("'", false, false)
+local sigil19 = '~' * S('CRSW') * lexer.range('"""')
+local sigil10 = '~' * S('CRSW') * lexer.range("'''")
+local sigil21 = '~' * S('crsw') * lexer.range('<', '>')
+local sigil22 = '~' * S('crsw') * lexer.range('{', '}')
+local sigil23 = '~' * S('crsw') * lexer.range('[', ']')
+local sigil24 = '~' * S('crsw') * lexer.range('(', ')')
+local sigil25 = '~' * S('crsw') * lexer.range('|')
+local sigil26 = '~' * S('crsw') * lexer.range('/')
+local sigil27 = '~' * S('crsw') * lexer.range('"')
+local sigil28 = '~' * S('crsw') * lexer.range("'")
+local sigil29 = '~' * S('crsw') * lexer.range('"""')
+local sigil20 = '~' * S('crsw') * lexer.range("'''")
+local sigil_token = token(lexer.REGEX,
+ sigil10 + sigil19 + sigil11 + sigil12 + sigil13 + sigil14 + sigil15 + sigil16 + sigil17 + sigil18 +
+ sigil20 + sigil29 + sigil21 + sigil22 + sigil23 + sigil24 + sigil25 + sigil26 + sigil27 +
+ sigil28)
+local sigiladdon_token = token(lexer.EMBEDDED, lexer.alpha^0)
+lex:add_rule('sigil', sigil_token * sigiladdon_token)
+
+-- Atoms.
+local atom1 = B(1 - P(':')) * ':' * lexer.range('"')
+local atom2 = B(1 - P(':')) * ':' * lexer.alpha * (lexer.alnum + S('_@'))^0 * S('?!')^-1
+local atom3 = B(1 - (lexer.alnum + S('_:'))) * lexer.upper * (lexer.alnum + S('_@'))^0 * S('?!')^-1
+lex:add_rule('atom', token(lexer.CONSTANT, atom1 + atom2 + atom3))
-- Strings.
-local dq_str = l.delimited_range('"', false)
-local triple_dq_str = '"""' * (l.any - '"""')^0 * P('"""')^-1
-local string = token(l.STRING, triple_dq_str + dq_str)
-
--- Numbers
-local dec = l.digit * (l.digit + P("_"))^0
-local bin = '0b' * S('01')^1
-local oct = '0o' * R('07')^1
-local integer = bin + l.hex_num + oct + dec
-local float = l.digit^1 * P(".") * l.digit^1 * S("eE") *
- (S('+-')^-1 * l.digit^1)^-1
-local number_token = B(1 - R('az', 'AZ', '__')) *
- (S('+-')^-1) * token(l.NUMBER, (float + integer))
-
--- Keywords.
-local keyword_token = token(l.KEYWORD, word_match{
- "is_atom", "is_binary", "is_bitstring", "is_boolean", "is_float",
- "is_function", "is_integer", "is_list", "is_map", "is_number", "is_pid",
- "is_port", "is_record", "is_reference", "is_tuple", "is_exception", "case",
- "when", "cond", "for", "if", "unless", "try", "receive", "send", "exit",
- "raise", "throw", "after", "rescue", "catch", "else", "do", "end", "quote",
- "unquote", "super", "import", "require", "alias", "use", "self", "with", "fn"
-})
+local dq_str = lexer.range('"')
+local triple_dq_str = lexer.range('"""')
+lex:add_rule('string', token(lexer.STRING, triple_dq_str + dq_str))
--- Functions
-local function_token = token(l.FUNCTION, word_match{
- "defstruct", "defrecordp", "defrecord", "defprotocol", "defp",
- "defoverridable", "defmodule", "defmacrop", "defmacro", "defimpl",
- "defexception", "defdelegate", "defcallback", "def"
-})
-
--- Sigils
-local sigil11 = P("~") * S("CRSW") * l.delimited_range('<>', false, true)
-local sigil12 = P("~") * S("CRSW") * l.delimited_range('{}', false, true)
-local sigil13 = P("~") * S("CRSW") * l.delimited_range('[]', false, true)
-local sigil14 = P("~") * S("CRSW") * l.delimited_range('()', false, true)
-local sigil15 = P("~") * S("CRSW") * l.delimited_range('|', false, true)
-local sigil16 = P("~") * S("CRSW") * l.delimited_range('/', false, true)
-local sigil17 = P("~") * S("CRSW") * l.delimited_range('"', false, true)
-local sigil18 = P("~") * S("CRSW") * l.delimited_range("'", false, true)
-local sigil19 = P("~") * S("CRSW") * '"""' * (l.any - '"""')^0 * P('"""')^-1
-local sigil10 = P("~") * S("CRSW") * "'''" * (l.any - "'''")^0 * P("'''")^-1
-local sigil21 = P("~") * S("crsw") * l.delimited_range('<>', false, false)
-local sigil22 = P("~") * S("crsw") * l.delimited_range('{}', false, false)
-local sigil23 = P("~") * S("crsw") * l.delimited_range('[]', false, false)
-local sigil24 = P("~") * S("crsw") * l.delimited_range('()', false, false)
-local sigil25 = P("~") * S("crsw") * l.delimited_range('|', false, false)
-local sigil26 = P("~") * S("crsw") * l.delimited_range('/', false, false)
-local sigil27 = P("~") * S("crsw") * l.delimited_range('"', false, false)
-local sigil28 = P("~") * S("crsw") * l.delimited_range("'", false, false)
-local sigil29 = P("~") * S("csrw") * '"""' * (l.any - '"""')^0 * P('"""')^-1
-local sigil20 = P("~") * S("csrw") * "'''" * (l.any - "'''")^0 * P("'''")^-1
-local sigil_token = token(l.REGEX, sigil10 + sigil19 + sigil11 + sigil12 +
- sigil13 + sigil14 + sigil15 + sigil16 +
- sigil17 + sigil18 + sigil20 + sigil29 +
- sigil21 + sigil22 + sigil23 + sigil24 +
- sigil25 + sigil26 + sigil27 + sigil28)
-local sigiladdon_token = token(l.EMBEDDED, R('az', 'AZ')^0)
+-- Comments.
+lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))
--- Attributes
-local attribute_token = token(l.LABEL, B(1 - R('az', 'AZ', '__')) * P('@') *
- R('az','AZ') * R('az','AZ','09','__')^0)
+-- Attributes.
+lex:add_rule('attribute', token(lexer.LABEL, B(1 - (lexer.alnum + '_')) * '@' * lexer.alpha *
+ (lexer.alnum + '_')^0))
--- Booleans
-local boolean_token = token(l.NUMBER,
- P(':')^-1 * word_match{"true", "false", "nil"})
+-- Booleans.
+lex:add_rule('boolean', token(lexer.NUMBER, P(':')^-1 * word_match('true false nil')))
--- Identifiers
-local identifier = token(l.IDENTIFIER, R('az', '__') *
- R('az', 'AZ', '__', '09')^0 * S('?!')^-1)
+-- Functions.
+lex:add_rule('function', token(lexer.FUNCTION, word_match{
+ 'defstruct', 'defrecordp', 'defrecord', 'defprotocol', 'defp', 'defoverridable', 'defmodule',
+ 'defmacrop', 'defmacro', 'defimpl', 'defexception', 'defdelegate', 'defcallback', 'def'
+}))
--- Atoms
-local atom1 = B(1 - P(':')) * P(':') * dq_str
-local atom2 = B(1 - P(':')) * P(':') * R('az', 'AZ') *
- R('az', 'AZ', '__', '@@', '09')^0 * S('?!')^-1
-local atom3 = B(1 - R('az', 'AZ', '__', '09', '::')) *
- R('AZ') * R('az', 'AZ', '__', '@@', '09')^0 * S('?!')^-1
-local atom_token = token(l.CONSTANT, atom1 + atom2 + atom3)
+-- Keywords.
+lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
+ 'is_atom', 'is_binary', 'is_bitstring', 'is_boolean', 'is_float', 'is_function', 'is_integer',
+ 'is_list', 'is_map', 'is_number', 'is_pid', 'is_port', 'is_record', 'is_reference', 'is_tuple',
+ 'is_exception', 'case', 'when', 'cond', 'for', 'if', 'unless', 'try', 'receive', 'send', 'exit',
+ 'raise', 'throw', 'after', 'rescue', 'catch', 'else', 'do', 'end', 'quote', 'unquote', 'super',
+ 'import', 'require', 'alias', 'use', 'self', 'with', 'fn'
+}))
-- Operators
-local operator1 = word_match{"and", "or", "not", "when", "xor", "in"}
-local operator2 = P('!==') + '!=' + '!' + '=~' + '===' + '==' + '=' + '<<<' +
- '<<' + '<=' + '<-' + '<' + '>>>' + '>>' + '>=' + '>' + '->' +
- '--' + '-' + '++' + '+' + '&&&' + '&&' + '&' + '|||' + '||' +
- '|>' + '|' + '..' + '.' + '^^^' + '^' + '\\\\' + '::' + '*' +
- '/' + '~~~' + '@'
-local operator_token = token(l.OPERATOR, operator1 + operator2)
+local operator1 = word_match('and or not when xor in')
+local operator2 = P('!==') + '!=' + '!' + '=~' + '===' + '==' + '=' + '<<<' + '<<' + '<=' + '<-' +
+ '<' + '>>>' + '>>' + '>=' + '>' + '->' + '--' + '-' + '++' + '+' + '&&&' + '&&' + '&' + '|||' +
+ '||' + '|>' + '|' + '..' + '.' + '^^^' + '^' + '\\\\' + '::' + '*' + '/' + '~~~' + '@'
+lex:add_rule('operator', token(lexer.OPERATOR, operator1 + operator2))
-M._rules = {
- {'whitespace', ws},
- {'sigil', sigil_token * sigiladdon_token},
- {'atom', atom_token},
- {'string', string},
- {'comment', comment},
- {'attribute', attribute_token},
- {'boolean', boolean_token},
- {'function', function_token},
- {'keyword', keyword_token},
- {'operator', operator_token},
- {'identifier', identifier},
- {'number', number_token},
-}
+-- Identifiers
+lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word * S('?!')^-1))
-M._FOLDBYINDENTATION = true
+-- Numbers
+local dec = lexer.digit * (lexer.digit + '_')^0
+local bin = '0b' * S('01')^1
+local oct = '0o' * lpeg.R('07')^1
+local integer = bin + lexer.hex_num + oct + dec
+local float = lexer.digit^1 * '.' * lexer.digit^1 * S('eE') * (S('+-')^-1 * lexer.digit^1)^-1
+lex:add_rule('number',
+ B(1 - (lexer.alpha + '_')) * S('+-')^-1 * token(lexer.NUMBER, float + integer))
-return M
+return lex