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 Odin lexer #802

Merged
merged 3 commits into from
Jul 19, 2023
Merged
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
113 changes: 113 additions & 0 deletions lexers/embedded/odin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<lexer>
<config>
<name>Odin</name>
<alias>odin</alias>
<filename>*.odin</filename>
<mime_type>text/odin</mime_type>
</config>
<rules>
<state name="NestedComment">
<rule pattern = "/[*]">
<token type = "CommentMultiline"/>
<push/>
</rule>
<rule pattern = "[*]/">
<token type = "CommentMultiline"/>
<pop depth = "1"/>
</rule>
<rule pattern = "[\s\S]">
<token type = "CommentMultiline"/>
</rule>
</state>
<state name="root">
<rule pattern = "\n">
<token type = "TextWhitespace"/>
</rule>
<rule pattern = "\s+">
<token type = "TextWhitespace"/>
</rule>
<rule pattern = "//.*?\n">
<token type = "CommentSingle"/>
</rule>
<rule pattern = "/[*]">
<token type = "CommentMultiline"/>
<push state="NestedComment"/>
</rule>
<rule pattern = "(import|package)\b">
<token type = "KeywordNamespace"/>
</rule>
<rule pattern = "(proc|struct|map|enum|union)\b">
<token type = "KeywordDeclaration"/>
</rule>
<rule pattern = "(asm|auto_cast|bit_set|break|case|cast|context|continue|defer|distinct|do|dynamic|else|enum|fallthrough|for|foreign|if|import|in|map|not_in|or_else|or_return|package|proc|return|struct|switch|transmute|typeid|union|using|when|where|panic|real|imag|len|cap|append|copy|delete|new|make|clearpanic|real|imag|len|cap|append|copy|delete|new|make|clear)\b">
<token type = "Keyword"/>
</rule>
<rule pattern = "(true|false|nil)\b">
<token type = "KeywordConstant"/>
</rule>
<rule pattern = "(uint|u8|u16|u32|u64|int|i8|i16|i32|i64|i16le|i32le|i64le|i128le|u16le|u32le|u64le|u128le|i16be|i32be|i64be|i128be|u16be|u32be|u64be|u128be|f16|f32|f64|complex32|complex64|complex128|quaternion64|quaternion128|quaternion256|byte|rune|string|cstring|typeid|any|bool|b8|b16|b32|b64|uintptr|rawptr)\b">
<token type = "KeywordType"/>
</rule>
<rule pattern = "\#[a-zA-Z_]+\b">
<token type = "NameDecorator"/>
</rule>
<rule pattern = "\@\(?[a-zA-Z_]+\b\s*(=\s*&quot;?[0-9a-zA-Z_.]+&quot;?)?\)?">
<token type = "NameAttribute"/>
</rule>
<rule pattern="[a-zA-Z_]\w*">
<token type="Name"/>
</rule>
<rule pattern="([a-zA-Z_]\w*)(\s*)(\()">
<token type="NameFunction"/>
</rule>
<rule pattern="[^\W\d]\w*">
<token type="NameOther"/>
</rule>
<rule pattern = "\d+i">
<token type = "LiteralNumber"/>
</rule>
<rule pattern = "\d+\.\d*([Ee][-+]\d+)?i">
<token type = "LiteralNumber"/>
</rule>
<rule pattern = "\.\d+([Ee][-+]\d+)?i">
<token type = "LiteralNumber"/>
</rule>
<rule pattern = "\d+[Ee][-+]\d+i">
<token type = "LiteralNumber"/>
</rule>
<rule pattern = "\d+(\.\d+[eE][+\-]?\d+|\.\d*|[eE][+\-]?\d+)">
<token type = "LiteralNumberFloat"/>
</rule>
<rule pattern = "\.\d+([eE][+\-]?\d+)?">
<token type = "LiteralNumberFloat"/>
</rule>
<rule pattern = "0o[0-7]+">
<token type = "LiteralNumberOct"/>
</rule>
<rule pattern = "0x[0-9a-fA-F_]+">
<token type = "LiteralNumberHex"/>
</rule>
<rule pattern = "0b[01_]+">
<token type = "LiteralNumberBin"/>
</rule>
<rule pattern = "(0|[1-9][0-9_]*)">
<token type = "LiteralNumberInteger"/>
</rule>
<rule pattern = "'(\\['&quot;\\abfnrtv]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|[^\\])'" >
<token type = "LiteralStringChar"/>
</rule>
<rule pattern = "(`)([^`]*)(`)" >
<token type = "LiteralString"/>
</rule>
<rule pattern = "&quot;(\\\\|\\&quot;|[^&quot;])*&quot;" >
<token type = "LiteralString"/>
</rule>
<rule pattern = "(&lt;&lt;=|&gt;&gt;=|&lt;&lt;|&gt;&gt;|&lt;=|&gt;=|&amp;=|&amp;|\+=|-=|\*=|/=|%=|\||\^|=|&amp;&amp;|\|\||--|-&gt;|=|==|!=|:=|:|::|\.\.\&lt;|\.\.=|[+\-*/%&amp;])" >
<token type = "Operator"/>
</rule>
<rule pattern="[{}()\[\],.;]">
<token type="Punctuation"/>
</rule>
</state>
</rules>
</lexer>
90 changes: 90 additions & 0 deletions lexers/testdata/odin.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import "core:fmt"

/*
some comments in odin can
/* be nested! */
and it should still work
*/

Data :: struct {
c: complex64,
q: quaternion256,
}

/* some other comment */

E :: enum(u32) {
First,
Second,
Third,
}

E_Set :: distinct bit_set[E; u32]

foo_int :: proc(x: int) -> bool {
return false
}

foo_float :: proc(f: f32) -> bool {
return true
}

foo_en :: proc(e: E) -> bool {
return e == .Third
}

foo :: proc{foo_int, foo_float, foo_en}

SOME_CONSTANT :: #config(COMMAND_LINE_ARG, 3)
main :: proc() {
loc := #caller_location

m: map[string]struct{}
da: [dynamic]f64

len(da)
cap(da)

foo(32)

#panic("oof")

y := &da
y^ = make([dynamic]f64, 100)
defer delete(da)

x := []int{1, 2, 3, 4}
for v, i in x {
fmt.println(i, "-", v)
}

z := E_Set{.First, .Second}
z2 := E_Set{.Third}

assert(z | z2 == {.First, .Second, .Third})
}

@(test)
a_test_proc :: proc(t: ^testing.T) {
value: int
value = 3
testing.errnof("a format: %s", value)
}

@(disable = ODIN_DEBUG)
debug_thing :: proc(x, y, z: int) {
fmt.println(x, y, z)
}

@private
program := `
foo :: proc() {
fmt.println("hellope!")
}
`

@(private = "file")
pkg: int

Loading
Loading