Skip to content

Commit

Permalink
Migrated Meson lexer.
Browse files Browse the repository at this point in the history
Thanks to Randy Palamar.
  • Loading branch information
orbitalquark committed Oct 30, 2023
1 parent 50485b5 commit 48a6fc9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
104 changes: 53 additions & 51 deletions lexers/meson.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
-- Copyright 2020-2023 Florian Fischer. See LICENSE.
-- Meson file LPeg lexer.

local lexer = require('lexer')
local token, word_match = lexer.token, lexer.word_match
local P, R, S = lpeg.P, lpeg.R, lpeg.S
local lexer = lexer
local S = lpeg.S

local lex = lexer.new('meson', {fold_by_indentation = true})

-- Whitespace.
lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
local lex = lexer.new(..., {fold_by_indentation = true})

-- Keywords.
lex:add_rule('keyword', token(lexer.KEYWORD, word_match(
'and or not if elif else endif foreach break continue endforeach')))
lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))

-- Methods.
-- Functions.
-- https://mesonbuild.com/Reference-manual.html#builtin-objects
-- https://mesonbuild.com/Reference-manual.html#returned-objects
local method_names = word_match{
local method = lex:tag(lexer.FUNCTION_METHOD, lex:word_match(lexer.FUNCTION_METHOD))
-- https://mesonbuild.com/Reference-manual.html#functions
local func = lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
-- A function call must be followed by an opening parenthesis. The matching of function calls
-- instead of just their names is needed to not falsely highlight function names which can also
-- be keyword arguments. For example 'include_directories' can be a function call itself or a
-- keyword argument of an 'executable' or 'library' function call.
lex:add_rule('function', (method + func) * #(lexer.space^0 * '('))

-- Builtin objects.
-- https://mesonbuild.com/Reference-manual.html#builtin-objects
lex:add_rule('object', lex:tag(lexer.VARIABLE_BUILTIN, lex:word_match(lexer.VARIABLE_BUILTIN)))

-- Constants.
lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))

-- Identifiers.
lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))

-- Strings.
local str = lexer.range("'", true)
local multiline_str = lexer.range("'''")
lex:add_rule('string', lex:tag(lexer.STRING, multiline_str + str))

-- Comments.
lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.to_eol('#', true)))

-- Numbers.
local oct_num = '0o' * lpeg.R('07')
local integer = S('+-')^-1 * (lexer.hex_num + lexer.bin_num + oct_num + lexer.dec_num)
lex:add_rule('number', lex:tag(lexer.NUMBER, integer))

-- Operators.
lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('()[]{}-=+/%:.,?<>')))

-- Word lists
lex:set_word_list(lexer.KEYWORD, {
'and', 'or', 'not', 'if', 'elif', 'else', 'endif', 'foreach', 'break', 'continue', 'endforeach'
})

lex:set_word_list(lexer.FUNCTION_METHOD, {
-- array --
'contains', 'get', 'length',
-- boolean --
Expand Down Expand Up @@ -69,13 +104,9 @@ local method_names = word_match{
'found', 'get_variable',
-- run result object --
'compiled', 'returncode', 'stderr', 'stdout'
}
-- A method call must be followed by an opening parenthesis.
lex:add_rule('method', token(lexer.FUNCTION_METHOD, method_names * #(lexer.space^0 * '(')))
})

-- Function.
-- https://mesonbuild.com/Reference-manual.html#functions
local func_names = word_match{
lex:set_word_list(lexer.FUNCTION_BUILTIN, {
'add_global_arguments', 'add_global_link_arguments', 'add_languages', 'add_project_arguments',
'add_project_link_arguments', 'add_test_setup', 'alias_targ', 'assert', 'benchmark',
'both_libraries', 'build_target', 'configuration_data', 'configure_file', 'custom_target',
Expand All @@ -85,42 +116,13 @@ local func_names = word_match{
'is_disabler', 'is_variable', 'jar', 'join_paths', 'library', 'message', 'warning', 'summary',
'project', 'run_command', 'run_targ', 'set_variable', 'shared_library', 'shared_module',
'static_library', 'subdir', 'subdir_done', 'subproject', 'test', 'vcs_tag'
}
-- A function call must be followed by an opening parenthesis. The matching of function calls
-- instead of just their names is needed to not falsely highlight function names which can also
-- be keyword arguments. For example 'include_directories' can be a function call itself or a
-- keyword argument of an 'executable' or 'library' function call.
lex:add_rule('function', token(lexer.FUNCTION, func_names * #(lexer.space^0 * '(')))

-- Builtin objects.
-- https://mesonbuild.com/Reference-manual.html#builtin-objects
lex:add_rule('object',
token('object', word_match('meson build_machine host_machine target_machine')))
lex:add_style('object', lexer.styles.type)

-- Constants.
lex:add_rule('constant', token(lexer.CONSTANT, word_match('false true')))

-- Identifiers.
lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
})

-- Strings.
local str = lexer.range("'", true)
local multiline_str = lexer.range("'''")
lex:add_rule('string', token(lexer.STRING, multiline_str + str))
lex:set_word_list(lexer.VARIABLE_BUILTIN, {
'meson', 'build_machine', 'host_machine', 'target_machine'
})

-- Comments.
lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#', true)))

-- Numbers.
local dec = R('19')^1 * R('09')^0
local bin = '0b' * S('01')^1
local oct = '0o' * R('07')^1
local integer = S('+-')^-1 * (bin + lexer.hex_num + oct + dec)
lex:add_rule('number', token(lexer.NUMBER, integer))

-- Operators.
lex:add_rule('operator', token(lexer.OPERATOR, S('()[]{}-=+/%:.,?<>')))
lex:set_word_list(lexer.CONSTANT_BUILTIN, {'false', 'true'})

lexer.property['scintillua.comment'] = '#'

Expand Down
3 changes: 0 additions & 3 deletions themes/dark.properties
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ scintillua.styles.character=$(scintillua.styles.constant)
# Mediawiki
scintillua.styles.behavior_switch=$(scintillua.styles.keyword)

# Meson
scintillua.styles.object=$(scintillua.styles.type)

# Moonscript
scintillua.styles.tbl_key=$(scintillua.styles.regex)
scintillua.styles.self_ref=$(scintillua.styles.label)
Expand Down
3 changes: 0 additions & 3 deletions themes/light.properties
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ scintillua.styles.character=$(scintillua.styles.constant)
# Mediawiki
scintillua.styles.behavior_switch=$(scintillua.styles.keyword)

# Meson
scintillua.styles.object=$(scintillua.styles.type)

# Moonscript
scintillua.styles.tbl_key=$(scintillua.styles.regex)
scintillua.styles.self_ref=$(scintillua.styles.label)
Expand Down
3 changes: 0 additions & 3 deletions themes/scite.properties
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ scintillua.styles.character=$(scintillua.styles.constant)
# Mediawiki
scintillua.styles.behavior_switch=$(scintillua.styles.keyword)

# Meson
scintillua.styles.object=$(scintillua.styles.type)

# Moonscript
scintillua.styles.tbl_key=$(scintillua.styles.regex)
scintillua.styles.self_ref=$(scintillua.styles.label)
Expand Down

0 comments on commit 48a6fc9

Please sign in to comment.