summaryrefslogtreecommitdiff
path: root/.config/vis/lexers
diff options
context:
space:
mode:
Diffstat (limited to '.config/vis/lexers')
-rw-r--r--.config/vis/lexers/execline.lua64
-rw-r--r--.config/vis/lexers/hare.lua82
-rw-r--r--.config/vis/lexers/roff.lua32
3 files changed, 178 insertions, 0 deletions
diff --git a/.config/vis/lexers/execline.lua b/.config/vis/lexers/execline.lua
new file mode 100644
index 0000000..cb79c8d
--- /dev/null
+++ b/.config/vis/lexers/execline.lua
@@ -0,0 +1,64 @@
+-- Copyright 2023 Sam Nystrom <sam@samnystrom.dev>
+-- Execline LPeg lexer.
+
+local l = require('lexer')
+local token, word_match = l.token, l.word_match
+local P, R, S = lpeg.P, lpeg.R, lpeg.S
+
+local M = {_NAME = 'execline'}
+
+-- Whitespace.
+local ws = token(l.WHITESPACE, l.space^1)
+
+-- Comments.
+local comment = token(l.COMMENT, l.starts_line(S('#')) * l.nonnewline^0)
+
+-- Strings.
+local sq_str = l.range("'")
+local dq_str = l.range('"')
+local string = token(l.STRING, sq_str + dq_str)
+
+-- Numbers.
+local dec = l.digit^1 * ('_' * l.digit^1)^0
+local oct_num = '0' * S('01234567_')^1
+local integer = S('+-')^-1 * (l.hex_num + oct_num + dec)
+local number = token(l.NUMBER, (l.float + integer))
+
+-- Keywords.
+local keyword = token(l.KEYWORD, word_match{
+ 'execlineb', 'execline-cd', 'posix-cd', 'execline-umask', 'posix-umask',
+ 'emptyenv', 'envfile', 'export', 'unexport', 'fdclose', 'fdblock', 'fdmove',
+ 'fdswap', 'fdreserve', 'redirfd', 'piperw', 'heredoc', 'wait', 'getcwd',
+ 'getpid', 'exec', 'tryexec', 'exit', 'trap', 'withstdinas',
+ 'foreground', 'background', 'case', 'if', 'ifelse', 'ifte', 'ifthenelse',
+ 'backtick', 'pipeline', 'runblock',
+ 'define', 'importas', 'elglob', 'elgetpositionals', 'multidefine',
+ 'multisubstitute',
+ 'forx', 'forstdin', 'forbacktickx', 'loopwhilex',
+ 'elgetopt', 'shift', 'dollarat',
+ 'eltest', 'homeof',
+ 'execline'
+})
+
+-- Identifiers.
+local identifier = token(l.IDENTIFIER, l.word)
+
+local variable = token(l.VARIABLE, '$' * (l.word + l.range('{}', true, true, true)))
+
+-- Operators.
+local operator = token(l.OPERATOR, S('{}'))
+
+M._rules = {
+ {'whitespace', ws},
+ {'keyword', keyword},
+ {'identifier', identifier},
+ {'string', string},
+ {'comment', comment},
+ {'number', number},
+ {'variable', variable},
+ {'operator', operator},
+}
+
+M._LEXBYLINE = true
+
+return M
diff --git a/.config/vis/lexers/hare.lua b/.config/vis/lexers/hare.lua
new file mode 100644
index 0000000..0aacad3
--- /dev/null
+++ b/.config/vis/lexers/hare.lua
@@ -0,0 +1,82 @@
+-- Copyright 2023 Sam Nystrom <sam@samnystrom.dev>
+-- Hare LPeg lexer
+
+local l = require('lexer')
+local C, Cmt, P, R, S = lpeg.C, lpeg.Cmt, lpeg.P, lpeg.R, lpeg.S
+
+local lex = l.new('hare')
+
+-- Whitespace
+lex:add_rule('whitespace', l.token(l.WHITESPACE, l.space^1))
+
+-- Comments
+lex:add_rule('comment', l.token(l.COMMENT, l.to_eol('//', true)))
+
+-- Keywords
+lex:add_rule('keyword', l.token(l.KEYWORD, l.word_match{
+ 'as', 'is',
+ 'if', 'else', 'match', 'switch',
+ 'break', 'continue', 'defer', 'return', 'yield',
+ 'const', 'def', 'let',
+ 'fn',
+ 'case',
+ 'for',
+ 'export', 'static',
+ 'enum', 'struct', 'union',
+ 'type',
+}))
+
+-- Builtins
+lex:add_rule('function', l.token(l.FUNCTION, l.word_match{
+ 'abort', 'assert',
+ 'align', 'len', 'offset',
+ 'alloc', 'free',
+ 'append', 'insert', 'delete',
+ -- C ABI
+ 'vastart', 'vaarg', 'vaend',
+}))
+
+-- Types
+lex:add_rule('type', l.token(l.TYPE, l.word_match{
+ 'bool',
+ 'f32', 'f64',
+ 'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64',
+ 'size', 'int', 'uint', 'uintptr',
+ 'never',
+ 'nullable',
+ 'opaque',
+ 'rune', 'str',
+ 'void',
+ -- C ABI
+ 'valist',
+}))
+
+-- Constants
+lex:add_rule('constant', l.token(l.CONSTANT, l.word_match{'true', 'false', 'null'}))
+
+-- Numbers
+--[[local identifier = P('r#')^-1 * l.word
+local function opt_cap(patt) return C(patt^-1) end
+local float = l.digit *
+ (Cmt(opt_cap('.' * l.digit) * opt_cap(S('eE') * S('+-')^-1 * l.digit) *
+ opt_cap(P('f32') + 'f64'), function(input, index, decimals, exponent, type)
+ return decimals ~= "" or exponent ~= "" or type ~= ""
+ end) + '.' * -(S('._') + identifier))
+local bin = P('0b') * S('01')
+local oct = P('0o') * lpeg.R('07')
+local hex = P('0x') * l.xdigit
+local integer = (bin + oct + hex + decimal_literal) *
+ (S('iu') * (P('8') + '16' + '32' + '64' + '128' + 'size'))^-1
+--]]
+lex:add_rule('number', l.token(l.NUMBER, l.float + l.integer))
+
+-- Strings
+lex:add_rule('string', l.token(l.STRING, l.range('"') + l.range('\'')))
+
+-- Operators
+lex:add_rule('operator', l.token(l.OPERATOR, S('+-/*<>!=@~&|^?:;,.()[]{}')))
+
+lex:add_fold_point(l.OPERATOR, '{', '}')
+lex:add_fold_point(l.COMMENT, l.fold_consecutive_lines('//'))
+
+return lex
diff --git a/.config/vis/lexers/roff.lua b/.config/vis/lexers/roff.lua
new file mode 100644
index 0000000..59657a7
--- /dev/null
+++ b/.config/vis/lexers/roff.lua
@@ -0,0 +1,32 @@
+-- Copyright 2023 Sam Nystrom <sam@samnystrom.dev>
+-- Roff LPeg lexer.
+
+local l = require('lexer')
+local token, word_match = l.token, l.word_match
+local P, R, S = lpeg.P, lpeg.R, lpeg.S
+
+local M = {_NAME = 'roff'}
+
+-- Whitespace
+local ws = token(l.WHITESPACE, l.space^1)
+
+-- Comments
+local comment = token(l.COMMENT, '\\"' * l.nonnewline^0)
+
+-- Strings
+local string = token(l.STRING, l.delimited_range('"'))
+
+-- Keywords
+local word = (l.alpha + '_') * (l.alnum + S('_.'))^0
+local keyword = token(l.KEYWORD, l.starts_line('.') * l.word)
+
+M._rules = {
+ {'whitespace', ws},
+ {'keyword', keyword},
+ {'string', string},
+ {'comment', comment},
+}
+
+M._LEXBYLINE = true
+
+return M