~yerinalexey/dotfiles

a67ca4d9307f2a9c134aac09ffd109f4bb4d38cf — Alexey Yerin 9 months ago 7872a26
vis: import new and updates hare lexer
1 files changed, 25 insertions(+), 22 deletions(-)

M vis/lexers/hare.lua
M vis/lexers/hare.lua => vis/lexers/hare.lua +25 -22
@@ 1,4 1,4 @@
-- Copyright 2021-2022 Mitchell. See LICENSE.
-- Copyright 2021-2023 Mitchell. See LICENSE.
-- Hare LPeg lexer
-- https://harelang.org
-- Contributed by Qiu


@@ 14,45 14,49 @@ lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))

-- Keywords.
lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  'as', 'break', 'case', 'const', 'continue', 'def', 'defer', 'else', 'export', 'false', 'fn',
  'for', 'if', 'is', 'let', 'match', 'null', 'nullable', 'return', 'static', 'struct', 'switch',
  'true', 'type', 'use', 'yield'
  'as', 'break', 'case', 'const', 'continue', 'def', 'defer', 'else', 'export', 'false', 'fn', 'for',
  'if', 'is', 'let', 'match', 'null', 'nullable', 'return', 'static', 'struct', 'switch', 'true',
  'type', 'use', 'yield'
}))

-- Functions.
local size_function = 'size' * #P('(')
local size_builtin = 'size' * #(lexer.space^0 * '(')
lex:add_rule('function', token(lexer.FUNCTION, word_match{
  'len', 'alloc', 'free', 'assert', 'abort', 'append', 'insert', 'delete', 'vastart', 'vaarg',
  'vaend'
} + size_function))
  'abort', 'align', 'alloc', 'append', 'assert', 'cap', 'delete', 'free', 'insert', 'len', 'offset',
  'vaarg', 'vaend', 'vastart'
} + size_builtin))

-- Types.
lex:add_rule('type', token(lexer.TYPE, word_match{
  'bool', 'enum', 'f32', 'f64', 'i16', 'i32', 'i64', 'i8', 'int', 'u16', 'u32', 'u64', 'u8', 'uint',
  'uintptr', 'union', 'void', 'rune', 'size', 'str'
  'bool', 'enum', 'f32', 'f64', 'i16', 'i32', 'i64', 'i8', 'int', 'rune', 'size', 'str', 'u16',
  'u32', 'u64', 'u8', 'uint', 'uintptr', 'union', 'valist', 'void',
}))

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

-- Strings.
local sq_str = lexer.range("'")
local sq_str = lexer.range("'", true)
local dq_str = lexer.range('"')
local raw_str = lexer.range('`', false, false)
local raw_str = lexer.range('`')
lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + raw_str))

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

-- Numbers.
local literal_suffix = word_match{
	"i", "u", "z", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64",
local integer_suffix = word_match{
	"i", "u", "z", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64",
}
local integer_constant = (P("0x") * lexer.xdigit^1
	+ P("0o") * R("07")^1
	+ P("0b") * R("01")^1
	+ R("09")^1)
lex:add_rule('number', token(lexer.NUMBER, (integer_constant + lexer.number) * literal_suffix^-1))
local float_suffix = word_match{ "f32", "f64" }
local suffix = integer_suffix + float_suffix

local bin_num = '0b' * R('01')^1 * -lexer.xdigit
local oct_num = '0o' * R('07')^1 * -lexer.xdigit
local hex_num = '0x' * lexer.xdigit^1
local integer_literal = S('+-')^-1 * ((hex_num + oct_num + bin_num) * integer_suffix^-1 + lexer.dec_num * suffix^-1)
local float_literal = lexer.float * float_suffix^-1
lex:add_rule('number', token(lexer.NUMBER, integer_literal + float_literal))

-- Error assertions
lex:add_rule('error_assert', token('error_assert', lpeg.B(')') * P('!')))


@@ 61,9 65,8 @@ lex:add_style('error_assert', lexer.styles.error)
-- Operators.
lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%^!=&|?~:;,.()[]{}<>')))

-- At rule.
lex:add_rule('at_rule', token('at_rule', '@' * lexer.word))
lex:add_style('at_rule', lexer.styles.preprocessor)
-- Attributes.
lex:add_rule('attribute', token(lexer.PREPROCESSOR, '@' * lexer.word))

-- Fold points.
lex:add_fold_point(lexer.OPERATOR, '{', '}')