aboutsummaryrefslogtreecommitdiff
path: root/lexers/rust.lua
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-10-16 12:36:47 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-11-08 13:35:36 +0100
commit039042f2e323c1f982f1de61b702c88fb33d6ccb (patch)
tree67dea69de9462e0c27ea2a743b4c5d1798eaa057 /lexers/rust.lua
parentb1ec60061623601ca6185a16d77c6c6c62135e95 (diff)
downloadvis-039042f2e323c1f982f1de61b702c88fb33d6ccb.tar.gz
vis-039042f2e323c1f982f1de61b702c88fb33d6ccb.tar.xz
Import LPeg based lexers from Scintillua 3.6.1-1
These are Copyright (c) 2007-2015 Mitchell and released under the MIT license.
Diffstat (limited to 'lexers/rust.lua')
-rw-r--r--lexers/rust.lua87
1 files changed, 87 insertions, 0 deletions
diff --git a/lexers/rust.lua b/lexers/rust.lua
new file mode 100644
index 0000000..4fef3ae
--- /dev/null
+++ b/lexers/rust.lua
@@ -0,0 +1,87 @@
+-- Copyright 2015 Alejandro Baez (https://twitter.com/a_baez). See LICENSE.
+-- Rust LPeg lexer.
+
+local l = require("lexer")
+local token, word_match = l.token, l.word_match
+local P, R, S = lpeg.P, lpeg.R, lpeg.S
+
+local M = {_NAME = 'rust'}
+
+-- Whitespace.
+local ws = token(l.WHITESPACE, l.space^1)
+
+-- Comments.
+local line_comment = '//' * l.nonnewline_esc^0
+local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
+local comment = token(l.COMMENT, line_comment + block_comment)
+
+-- Strings.
+local sq_str = P('L')^-1 * l.delimited_range("'")
+local dq_str = P('L')^-1 * l.delimited_range('"')
+local raw_str = "##" * (l.any - '##')^0 * P("##")^-1
+local string = token(l.STRING, dq_str + raw_str)
+
+-- Numbers.
+local number = token(l.NUMBER, l.float +
+ "0b" * (l.dec_num + "_")^1 + l.integer)
+
+-- Keywords.
+local keyword = token(l.KEYWORD, word_match{
+ 'abstract', 'alignof', 'as', 'become', 'box',
+ 'break', 'const', 'continue', 'crate', 'do',
+ 'else', 'enum', 'extern', 'false', 'final',
+ 'fn', 'for', 'if', 'impl', 'in',
+ 'let', 'loop', 'macro', 'match', 'mod',
+ 'move', 'mut', "offsetof", 'override', 'priv',
+ 'pub', 'pure', 'ref', 'return', 'sizeof',
+ 'static', 'self', 'struct', 'super', 'true',
+ 'trait', 'type', 'typeof', 'unsafe', 'unsized',
+ 'use', 'virtual', 'where', 'while', 'yield'
+})
+
+-- Library types
+local library = token(l.LABEL, l.upper * (l.lower + l.dec_num)^1)
+
+-- syntax extensions
+local extension = l.word^1 * S("!")
+
+local func = token(l.FUNCTION, extension)
+
+-- Types.
+local type = token(l.TYPE, word_match{
+ '()', 'bool', 'isize', 'usize', 'char', 'str',
+ 'u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64',
+ 'f32','f64',
+})
+
+-- Identifiers.
+local identifier = token(l.IDENTIFIER, l.word)
+
+-- Operators.
+local operator = token(l.OPERATOR, S('+-/*%<>!=`^~@&|?#~:;,.()[]{}'))
+
+-- Attributes.
+local attribute = token(l.PREPROCESSOR, "#[" *
+ (l.nonnewline - ']')^0 * P("]")^-1)
+
+M._rules = {
+ {'whitespace', ws},
+ {'keyword', keyword},
+ {'function', func},
+ {'library', library},
+ {'type', type},
+ {'identifier', identifier},
+ {'string', string},
+ {'comment', comment},
+ {'number', number},
+ {'operator', operator},
+ {'preprocessor', attribute},
+}
+
+M._foldsymbols = {
+ _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
+ [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')},
+ [l.OPERATOR] = {['('] = 1, ['{'] = 1, [')'] = -1, ['}'] = -1}
+}
+
+return M