aboutsummaryrefslogtreecommitdiff
path: root/lua/lexers/apl.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/lexers/apl.lua')
-rw-r--r--lua/lexers/apl.lua62
1 files changed, 24 insertions, 38 deletions
diff --git a/lua/lexers/apl.lua b/lua/lexers/apl.lua
index 32e1f3a..9ee22cf 100644
--- a/lua/lexers/apl.lua
+++ b/lua/lexers/apl.lua
@@ -1,69 +1,55 @@
--- Copyright 2015-2017 David B. Lamkins <david@lamkins.net>. See LICENSE.
+-- Copyright 2015-2022 David B. Lamkins <david@lamkins.net>. See LICENSE.
-- APL 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 lexer = require('lexer')
+local token, word_match = lexer.token, lexer.word_match
+local P, S = lpeg.P, lpeg.S
-local M = {_NAME = 'apl'}
+local lex = lexer.new('apl')
-- Whitespace.
-local ws = token(l.WHITESPACE, l.space^1)
+lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
-- Comments.
-local comment = token(l.COMMENT, (P('⍝') + P('#')) * l.nonnewline^0)
+lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol(P('⍝') + '#')))
-- Strings.
-local sq_str = l.delimited_range("'", false, true)
-local dq_str = l.delimited_range('"')
-
-local string = token(l.STRING, sq_str + dq_str)
+local sq_str = lexer.range("'", false, false)
+local dq_str = lexer.range('"')
+lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
-- Numbers.
-local dig = R('09')
+local dig = lexer.digit
local rad = P('.')
local exp = S('eE')
local img = S('jJ')
local sgn = P('¯')^-1
-local float = sgn * (dig^0 * rad * dig^1 + dig^1 * rad * dig^0 + dig^1) *
- (exp * sgn *dig^1)^-1
-local number = token(l.NUMBER, float * img * float + float)
+local float = sgn * (dig^0 * rad * dig^1 + dig^1 * rad * dig^0 + dig^1) * (exp * sgn * dig^1)^-1
+lex:add_rule('number', token(lexer.NUMBER, float * img * float + float))
-- Keywords.
-local keyword = token(l.KEYWORD, P('⍞') + P('χ') + P('⍺') + P('⍶') + P('⍵') +
- P('⍹') + P('⎕') * R('AZ', 'az')^0)
+lex:add_rule('keyword', token(lexer.KEYWORD,
+ P('⍞') + 'χ' + '⍺' + '⍶' + '⍵' + '⍹' + '⎕' * lexer.alpha^0))
-- Names.
-local n1l = R('AZ', 'az')
-local n1b = P('_') + P('∆') + P('⍙')
-local n2l = n1l + R('09')
-local n2b = n1b + P('¯')
+local n1l = lexer.alpha
+local n1b = P('_') + '∆' + '⍙'
+local n2l = n1l + lexer.digit
+local n2b = n1b + '¯'
local n1 = n1l + n1b
local n2 = n2l + n2b
local name = n1 * n2^0
-- Labels.
-local label = token(l.LABEL, name * P(':'))
+lex:add_rule('label', token(lexer.LABEL, name * ':'))
-- Variables.
-local variable = token(l.VARIABLE, name)
+lex:add_rule('variable', token(lexer.VARIABLE, name))
-- Special.
-local special = token(l.TYPE, S('{}[]();') + P('←') + P('→') + P('◊'))
+lex:add_rule('special', token(lexer.TYPE, S('{}[]();') + '←' + '→' + '◊'))
-- Nabla.
-local nabla = token(l.PREPROCESSOR, P('∇') + P('⍫'))
-
-M._rules = {
- {'whitespace', ws},
- {'comment', comment},
- {'string', string},
- {'number', number},
- {'keyword', keyword},
- {'label', label},
- {'variable', variable},
- {'special', special},
- {'nabla', nabla},
-}
+lex:add_rule('nabla', token(lexer.PREPROCESSOR, P('∇') + '⍫'))
-return M
+return lex