From 4af2de4fb0483d388c7a6e3d43d8a5b8bf78042e Mon Sep 17 00:00:00 2001 From: Murray Calavera Date: Mon, 25 Dec 2017 22:20:58 +0000 Subject: lexers: add pony --- lua/lexers/pony.lua | 114 +++++++++++++++++++++++++++++++++++++++++++++++ lua/lexers/sml.lua | 2 +- lua/plugins/filetype.lua | 3 ++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 lua/lexers/pony.lua diff --git a/lua/lexers/pony.lua b/lua/lexers/pony.lua new file mode 100644 index 0000000..f1d4e59 --- /dev/null +++ b/lua/lexers/pony.lua @@ -0,0 +1,114 @@ +-- Copyright 2017 Murray Calavera. See LICENSE. +-- Pony LPeg lexer. + +local l = require('lexer') +local token = l.token +local P, S = lpeg.P, lpeg.S + +local function pword(words) + return l.word_match(words, "'") +end + +local ws = token(l.WHITESPACE, l.space^1) + +local comment_line = '//' * l.nonnewline^0 +local comment_block = l.nested_pair('/*', '*/') +local comment = token(l.COMMENT, comment_line + comment_block) + +local annotation = token(l.PREPROCESSOR, l.delimited_range('\\', false, true)) + +local lit_bool = token(l.CONSTANT, pword{'true', 'false'}) + +local lit_str = token(l.STRING, + l.delimited_range('"') -- this covers triple quoted strings + + l.delimited_range("'") +) + +local function num(digit) + return digit * (digit^0 * P'_')^0 * digit^1 + digit +end + +local int = num(l.digit) +local frac = P('.') * int +local exp = S('eE') * (P('-') + P('+'))^-1 * int + +local lit_num = token(l.NUMBER, + P('0x') * num(l.xdigit) + + P('0b') * num(S('01')) + + int * frac^-1 * exp^-1 +) + +local keyword = token(l.KEYWORD, pword{ + 'actor', 'as', 'be', 'break', 'class', 'compile_error', 'compile_intrinsic', + 'continue', 'consume', 'do', 'else', 'elseif', 'embed', 'end', 'error', + 'for', 'fun', 'if', 'ifdef', 'iftype', 'in', 'interface', 'is', 'isnt', + 'lambda', 'let', 'match', 'new', 'object', 'primitive', 'recover', 'repeat', + 'return', 'struct', 'then', 'this', 'trait', 'try', 'type', 'until', 'use', + 'var', 'where', 'while', 'with'}) +local capability = token(l.LABEL, pword{ + 'box', 'iso', 'ref', 'tag', 'trn', 'val'}) +local qualifier = token(l.LABEL, + P'#' * pword{'read', 'send', 'share', 'any', 'alias'}) + +local operator = token(l.OPERATOR, + pword{'and', 'or', 'xor', 'not', 'addressof', 'digestof'} + + lpeg.Cmt(S('+-*/%<>=!')^1, function(input, index, op) + local ops = { + ['+'] = true, ['-'] = true, ['*'] = true, ['/'] = true, ['%'] = true, + ['+~'] = true, ['-~'] = true, ['*~'] = true, ['/~'] = true, + ['%~'] = true, ['<<'] = true, ['>>'] = true, ['<<~'] = true, + ['>>~'] = true, ['=='] = true, ['!='] = true, ['<'] = true, + ['<='] = true, ['>='] = true, ['>'] = true, ['==~'] = true, + ['!=~'] = true, ['<~'] = true, ['<=~'] = true, ['>=~'] = true, + ['>~'] = true + } + return ops[op] and index or nil + end) +) + +-- there is no suitable token name for this, change this if ever one is added +local punctuation = token(l.OPERATOR, + P'=>' + P'.>' + P'<:' + P'->' + + S('=.,:;()[]{}!?~^&|_@')) + +-- highlight functions with syntax sugar at declaration +local func + = token(l.KEYWORD, pword{'fun', 'new', 'be'}) * ws^-1 + * annotation^-1 * ws^-1 + * capability^-1 * ws^-1 + * token(l.FUNCTION, pword{ + 'create', 'dispose', '_final', 'apply', 'update', + 'add', 'sub', 'mul', 'div', 'mod', 'add_unsafe', 'sub_unsafe', + 'mul_unsafe', 'div_unsafe', 'mod_unsafe', 'shl', 'shr', 'shl_unsafe', + 'shr_unsafe', 'op_and', 'op_or', 'op_xor', 'eq', 'ne', 'lt', 'le', 'ge', + 'gt', 'eq_unsafe', 'ne_unsafe', 'lt_unsafe', 'le_unsafe', 'ge_unsafe', + 'gt_unsafe', 'neg', 'neg_unsafe', 'op_not', + 'has_next', 'next', + '_serialise_space', '_serialise', '_deserialise'}) + +local id_suffix = (l.alnum + P("'") + P('_'))^0 +local type = token(l.TYPE, P('_')^-1 * l.upper * id_suffix) +local identifier = token(l.IDENTIFIER, P('_')^-1 * l.lower * id_suffix) +local tuple_lookup = token(l.IDENTIFIER, P('_') * l.digit^1) + +local M = {_NAME = 'pony'} + +M._rules = { + {'whitespace', ws}, + {'comment', comment}, + {'annotation', annotation}, + {'boolean', lit_bool}, + {'number', lit_num}, + {'string', lit_str}, + {'function', func}, + {'keyword', keyword}, + {'capability', capability}, + {'qualifier', qualifier}, + {'operator', operator}, + {'type', type}, + {'identifier', identifier}, + {'lookup', tuple_lookup}, + {'punctuation', punctuation} +} + +return M diff --git a/lua/lexers/sml.lua b/lua/lexers/sml.lua index e734afd..093e67c 100644 --- a/lua/lexers/sml.lua +++ b/lua/lexers/sml.lua @@ -4,7 +4,7 @@ local l = require('lexer') local token = l.token -function mlword(words) +local function mlword(words) return l.word_match(words, "'") end diff --git a/lua/plugins/filetype.lua b/lua/plugins/filetype.lua index e4062bf..f5c0800 100644 --- a/lua/plugins/filetype.lua +++ b/lua/plugins/filetype.lua @@ -278,6 +278,9 @@ vis.ftdetect.filetypes = { pkgbuild = { ext = { "PKGBUILD" }, }, + pony = { + ext = { "%.pony$" }, + }, powershell = { ext = { "%.ps1$" }, }, -- cgit v1.2.3