Skip to content

Commit

Permalink
fix: modernize mediawiki lexer
Browse files Browse the repository at this point in the history
References: orbitalquark#76
  • Loading branch information
mcepl committed Jan 16, 2025
1 parent b187d6e commit 1642cfe
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions lexers/mediawiki.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ local P, S, B = lpeg.P, lpeg.S, lpeg.B

local lex = lexer.new(...)

-- Comments.
lex:add_rule('comment', token(lexer.COMMENT, lexer.range('<!--', '-->')))

-- HTML-like tags
local tag_start = token(lexer.TAG, '<' * P('/')^-1 * lexer.alnum^1 * lexer.space^0)
local tag_start = lex:tag(lexer.TAG, '<' * P('/')^-1 * lexer.alnum^1 * lexer.space^0)
local dq_str = '"' * ((lexer.any - S('>"\\')) + ('\\' * lexer.any))^0 * '"'
local tag_attr = token(lexer.ATTRIBUTE, lexer.alpha^1 * lexer.space^0 *
local tag_attr = lex:tag(lexer.ATTRIBUTE, lexer.alpha^1 * lexer.space^0 *
('=' * lexer.space^0 * (dq_str + (lexer.any - lexer.space - '>')^0)^-1)^0 * lexer.space^0)
local tag_end = token(lexer.TAG, P('/')^-1 * '>')
local tag_end = lex:tag(lexer.TAG, P('/')^-1 * '>')
lex:add_rule('tag', tag_start * tag_attr^0 * tag_end)

-- Link
lex:add_rule('link', token(lexer.STRING, S('[]')))
lex:add_rule('internal_link', B('[[') * token(lexer.LINK, (lexer.any - '|' - ']]')^1))
lex:add_rule('link', lex:tag(lexer.STRING, S('[]')))
lex:add_rule('internal_link', B('[[') * lex:tag(lexer.LINK, (lexer.any - '|' - ']]')^1))

-- Templates and parser functions.
lex:add_rule('template', token(lexer.OPERATOR, S('{}')))
lex:add_rule('template', lex:tag(lexer.OPERATOR, S('{}')))
lex:add_rule('parser_func',
B('{{') * token(lexer.FUNCTION, '#' * lexer.alpha^1 + lexer.upper^1 * ':'))
lex:add_rule('template_name', B('{{') * token(lexer.LINK, (lexer.any - S('{}|'))^1))
B('{{') * lex:tag(lexer.FUNCTION, '#' * lexer.alpha^1 + lexer.upper^1 * ':'))
lex:add_rule('template_name', B('{{') * lex:tag(lexer.LINK, (lexer.any - S('{}|'))^1))

-- Operators.
lex:add_rule('operator', token(lexer.OPERATOR, S('-=|#~!')))
lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('-=|#~!')))

-- Behavior switches
local start_pat = P(function(_, pos) return pos == 1 end)
lex:add_rule('behavior_switch', (B(lexer.space) + start_pat) * token('behavior_switch', word_match(
'__TOC__ __FORCETOC__ __NOTOC__ __NOEDITSECTION__ __NOCC__ __NOINDEX__')) * #lexer.space)
lex:add_style('behavior_switch', lexer.styles.keyword)
lex:add_rule('behavior_switch', ((B(lexer.space) + start_pat) * lex:word_match('behavior_switch') * #lexer.space))
lex:set_word_list('behavior_switch',
{'__TOC__', '__FORCETOC__', '__NOTOC__', '__NOEDITSECTION__', '__NOCC__', '__NOINDEX__'})

-- Comments.
lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.range('<!--', '-->')))

lexer.property['scintillua.comment'] = '<!--|-->'
lexer.property['scintillua.angle.braces'] = '1'
Expand Down

0 comments on commit 1642cfe

Please sign in to comment.