Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lexer for the Odin programming language #116

Open
wants to merge 2 commits into
base: default
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lexers/lexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ function M.detect(filename, line)
nim = 'nim', --
nsh = 'nsis', nsi = 'nsis', nsis = 'nsis', --
obs = 'objeck', --
odin = 'odin', --
m = 'objective_c', mm = 'objective_c', objc = 'objective_c', --
caml = 'caml', ml = 'caml', mli = 'caml', mll = 'caml', mly = 'caml', --
dpk = 'pascal', dpr = 'pascal', p = 'pascal', pas = 'pascal', --
Expand Down
93 changes: 93 additions & 0 deletions lexers/odin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-- Copyright 2006-2024 Mitchell. See LICENSE.
-- Copyright 2024 Brett Mahar. See LICENSE.
-- Odin LPeg lexer.

local lexer = lexer
local P, S = lpeg.P, lpeg.S

local lex = lexer.new(...)

-- Keywords.
lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))

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

-- Types.
lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))

-- Functions.
local builtin_func = -lpeg.B('.') *
lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
local func = lex:tag(lexer.FUNCTION, lexer.word)
local method = lpeg.B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))

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

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

-- Comments.
local line_comment = lexer.to_eol('//')
local block_comment = lexer.range('/*', '*/')
lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))

-- Numbers.
lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number * P('i')^-1))

-- Operators.
lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('@?+-#*/$%&|^<>=!~:;.,()[]{}')))

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

Copy link
Owner

@orbitalquark orbitalquark Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this entire range is just a copy of the Go lexer, I would suggest replacing it with

local lex = lexer.new(..., {inherit = lexer.load('go')})

-- Word lists.
lex:set_word_list(lexer.KEYWORD, {
-- keyword control
'asm', 'yield', 'await', 'using', 'do', 'inline', 'no_inline', 'fallthrough', 'break', 'continue',
'case', 'vector', 'static', 'dynamic', 'atomic', 'push_allocator', 'push_context', 'if', 'else',
'when', 'for', 'in', 'not_in', 'defer', 'switch', 'return', 'const', 'import', 'export', 'foreign',
'package', 'import_load', 'foreign_library', 'foreign_system_library', 'or_else', 'or_return',
'or_break','or_continue', 'where', 'expect', 'syscall',
-- keyword operator
'distinct', 'context',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

distinct appears more related to types than keywords (kind of like your storage type section). What do you think about moving it to the types word list?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context seems more like a built-in variable than a keyword. We could probably modify the 'constant' rule to add an option for built-in variables and add this to its list. Something like:

lex:modify_rule('constant', lex:get_rule('constant') + lex:word_match(lexer.VARIABLE_BUILTIN))

lex:set_word_list(lexer.VARIABLE_BUILTIN, 'context')

Does this sound reasonable?

-- keyword function
'size_of', 'align_of', 'offset_of', 'type_of', 'type_info', 'type_info_of', 'typeid_of', 'identifier',
'cast', 'transmute', 'auto_cast', 'down_cast', 'union_cast', 'accumulator', 'offset_of_selector',
'offset_of_member', 'offset_of_by_string', 'swizzle', 'min', 'max', 'abs', 'clamp',
'is_package_imported', 'sqrt', 'valgrind_client_request'
Comment on lines +61 to +64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally built-in functions like these would go into the built-in functions word list. Is there a particular reason they should be labeled as keywords?

})

lex:set_word_list(lexer.CONSTANT_BUILTIN, 'true false nil iota')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://pkg.odin-lang.org/base/builtin/ there are more constants. Are they worth adding?


lex:set_word_list(lexer.TYPE, {
-- type
'i8', 'i16', 'i32', 'i64', 'i128', 'int', 'u8', 'u32', 'u64', 'u128', 'uint', 'uintptr',
'f16', 'f32', 'f64', 'complex32', 'complex64', 'complex128', 'bool', 'b8', 'b16', 'b32', 'b64',
'string', 'rune', 'rawptr', 'any', 'byte', 'cstring', 'complex', 'quaternion', 'real', 'imag',
'jmag', 'kmag', 'conj',
Comment on lines +71 to +74
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://pkg.odin-lang.org/base/builtin/ there appear to be more types. Are they worth adding here?

-- storage type
'type', 'var', 'dynamic', 'struct', 'enum', 'union', 'map', 'set', 'bit_set', 'bit_field', 'typeid',
'matrix', 'rawunion', 'proc', 'macro', 'soa_struct'
})

lex:set_word_list(lexer.FUNCTION_BUILTIN, {
'len', 'cap', 'make', 'resize', 'reserve', 'append', 'delete', 'assertf?', 'panicf?',
-- package annotations
'thread_local', 'test', 'static', 'require_target_feature', 'require_results', 'require', 'private',
'optimization_mode', 'objc_type', 'objc_name', 'objc_is_class_method', 'objc_class',
'no_instrumentation', 'linkage', 'link_prefix', 'link_name', 'instrumentation_enter',
'instrumentation_exit', 'init', 'fini', 'extra_linker_flags', 'disabled', 'entry_point_only',
'enable_target_feature', 'enable_target_features', 'deprecated', 'deferred_out', 'deferred_in_out',
'deferred_in', 'builtin', 'cold', 'default_calling_convention'
Comment on lines +83 to +88
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These do not feel like functions to me. We could extend the lexer:modify_rule() I proposed earlier to also include a word list for these, like lexer.ATTRIBUTE. For example:

lex:modify_rule('constant', lex:get_rule('constant') +
  lex:word_match(lexer.VARIABLE_BUILTIN) +
  lex:word_match(lexer.ATTRIBUTE)

[...]

lex:set_word_list(lexer.ATTRIBUTE, { ... })

What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanos for reviewing! I may have some spare time later in the week to include these suggestions. I'm happy for you to go ahead and make the changes if you beat me to it :-)

})

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

return lex