logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

zig.lua (3812B)


  1. -- Copyright 2020-2024 Karchnu karchnu@karchnu.fr. See LICENSE.
  2. -- Zig LPeg lexer.
  3. -- (Based on the C++ LPeg lexer from Mitchell.)
  4. local lexer = require('lexer')
  5. local token, word_match = lexer.token, lexer.word_match
  6. local P, S = lpeg.P, lpeg.S
  7. local lex = lexer.new('zig')
  8. -- Whitespace.
  9. lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
  10. -- Keywords.
  11. lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
  12. -- Keywords.
  13. 'inline', 'pub', 'fn', 'comptime', 'const', 'extern', 'return', 'var', 'usingnamespace',
  14. -- Defering code blocks.
  15. 'defer', 'errdefer',
  16. -- Functions and structures related keywords.
  17. 'align', 'allowzero', 'noalias', 'noinline', 'callconv', 'packed', 'linksection', 'unreachable',
  18. 'test', 'asm', 'volatile',
  19. -- Parallelism and concurrency related keywords.
  20. 'async', 'await', 'noasync', 'suspend', 'nosuspend', 'resume', 'threadlocalanyframe',
  21. -- Control flow: conditions and loops.
  22. 'if', 'else', 'orelse', 'or', 'and', 'while', 'for', 'switch', 'continue', 'break', 'catch',
  23. 'try',
  24. -- Not keyword but overly used variable name with always the same semantic.
  25. 'self'
  26. }))
  27. -- Types.
  28. lex:add_rule('type', token(lexer.TYPE, word_match{
  29. 'enum', 'struct', 'union', --
  30. 'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128', 'u128', --
  31. 'isize', 'usize', --
  32. 'c_short', 'c_ushort', 'c_int', 'c_uint', --
  33. 'c_long', 'c_ulong', 'c_longlong', 'c_ulonglong', 'c_longdouble', --
  34. 'c_void', --
  35. 'f16', 'f32', 'f64', 'f128', --
  36. 'bool', 'void', 'noreturn', 'type', 'anytype', 'error', 'anyerror', --
  37. 'comptime_int', 'comptime_float'
  38. }))
  39. -- Constants.
  40. lex:add_rule('constant', token(lexer.CONSTANT, word_match{
  41. -- Special values.
  42. 'false', 'true', 'null', 'undefined'
  43. }))
  44. -- Built-in functions.
  45. lex:add_rule('function', token(lexer.FUNCTION, '@' * word_match{
  46. 'addWithOverflow', 'alignCast', 'alignOf', 'as', 'asyncCall', 'atomicLoad', 'atomicRmw',
  47. 'atomicStore', 'bitCast', 'bitOffsetOf', 'boolToInt', 'bitSizeOf', 'breakpoint', 'mulAdd',
  48. 'byteSwap', 'bitReverse', 'byteOffsetOf', 'call', 'cDefine', 'cImport', 'cInclude', 'clz',
  49. 'cmpxchgStrong', 'cmpxchgWeak', 'compileError', 'compileLog', 'ctz', 'cUndef', 'divExact',
  50. 'divFloor', 'divTrunc', 'embedFile', 'enumToInt', 'errorName', 'errorReturnTrace', 'errorToInt',
  51. 'errSetCast', 'export', 'fence', 'field', 'fieldParentPtr', 'floatCast', 'floatToInt', 'frame',
  52. 'Frame', 'frameAddress', 'frameSize', 'hasDecl', 'hasField', 'import', 'intCast', 'intToEnum',
  53. 'intToError', 'intToFloat', 'intToPtr', 'memcpy', 'memset', 'wasmMemorySize', 'wasmMemoryGrow',
  54. 'mod', 'mulWithOverflow', 'panic', 'popCount', 'ptrCast', 'ptrToInt', 'rem', 'returnAddress',
  55. 'setAlignStack', 'setCold', 'setEvalBranchQuota', 'setFloatMode', 'setRuntimeSafety', 'shlExact',
  56. 'shlWithOverflow', 'shrExact', 'shuffle', 'sizeOf', 'splat', 'reduce', 'src', 'sqrt', 'sin',
  57. 'cos', 'exp', 'exp2', 'log', 'log2', 'log10', 'fabs', 'floor', 'ceil', 'trunc', 'round',
  58. 'subWithOverflow', 'tagName', 'TagType', 'This', 'truncate', 'Type', 'typeInfo', 'typeName',
  59. 'TypeOf', 'unionInit'
  60. }))
  61. -- Strings.
  62. local sq_str = P('L')^-1 * lexer.range("'", true)
  63. local dq_str = P('L')^-1 * lexer.range('"', true)
  64. lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
  65. -- Identifiers.
  66. lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
  67. -- Comments.
  68. local doc_comment = lexer.to_eol('///', true)
  69. local comment = lexer.to_eol('//', true)
  70. lex:add_rule('comment', token(lexer.COMMENT, doc_comment + comment))
  71. -- Numbers.
  72. lex:add_rule('number', token(lexer.NUMBER, lexer.number))
  73. -- Operators.
  74. lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}')))
  75. -- Fold points.
  76. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  77. lexer.property['scintillua.comment'] = '//'
  78. return lex