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

ansi_c.lua (9388B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- C LPeg lexer.
  3. local lexer = lexer
  4. local P, S, B = lpeg.P, lpeg.S, lpeg.B
  5. local lex = lexer.new(...)
  6. -- Keywords.
  7. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  8. -- Types.
  9. lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
  10. -- Functions.
  11. local builtin_func = -(B('.') + B('->')) *
  12. lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
  13. local func = lex:tag(lexer.FUNCTION, lexer.word)
  14. local method = (B('.') + B('->')) * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
  15. lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))
  16. -- Constants.
  17. lex:add_rule('constants', lex:tag(lexer.CONSTANT_BUILTIN,
  18. -(B('.') + B('->')) * lex:word_match(lexer.CONSTANT_BUILTIN)))
  19. -- Labels.
  20. lex:add_rule('label', lex:tag(lexer.LABEL, lexer.starts_line(lexer.word * ':')))
  21. -- Strings.
  22. local sq_str = lexer.range("'", true)
  23. local dq_str = lexer.range('"', true)
  24. lex:add_rule('string', lex:tag(lexer.STRING, P('L')^-1 * (sq_str + dq_str)))
  25. -- Identifiers.
  26. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
  27. -- Comments.
  28. local line_comment = lexer.to_eol('//', true)
  29. local block_comment = lexer.range('/*', '*/') +
  30. lexer.range('#if' * S(' \t')^0 * '0' * lexer.space, '#endif')
  31. lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
  32. -- Numbers.
  33. local integer = lexer.integer * lexer.word_match('u l ll ul ull lu llu', true)^-1
  34. local float = lexer.float * P('f')^-1
  35. lex:add_rule('number', lex:tag(lexer.NUMBER, float + integer))
  36. -- Preprocessor.
  37. local include = lex:tag(lexer.PREPROCESSOR, '#' * S('\t ')^0 * 'include') *
  38. (lex:get_rule('whitespace') * lex:tag(lexer.STRING, lexer.range('<', '>', true)))^-1
  39. local preproc = lex:tag(lexer.PREPROCESSOR, '#' * S('\t ')^0 * lex:word_match(lexer.PREPROCESSOR))
  40. lex:add_rule('preprocessor', include + preproc)
  41. -- Operators.
  42. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>~!=^&|?~:;,.()[]{}')))
  43. -- Fold points.
  44. lex:add_fold_point(lexer.PREPROCESSOR, '#if', '#endif')
  45. lex:add_fold_point(lexer.PREPROCESSOR, '#ifdef', '#endif')
  46. lex:add_fold_point(lexer.PREPROCESSOR, '#ifndef', '#endif')
  47. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  48. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  49. -- Word lists.
  50. lex:set_word_list(lexer.KEYWORD, {
  51. 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else', 'enum', 'extern', 'for',
  52. 'goto', 'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'static', 'switch', 'typedef',
  53. 'volatile', 'while', --
  54. 'false', 'true', -- C99
  55. 'alignas', 'alignof', '_Atomic', '_Generic', 'noreturn', '_Static_assert', 'thread_local', -- C11
  56. -- Compiler.
  57. 'asm', '__asm', '__asm__', '__restrict__', '__inline', '__inline__', '__attribute__', '__declspec'
  58. })
  59. lex:set_word_list(lexer.TYPE, {
  60. 'bool', 'char', 'double', 'float', 'int', 'long', 'short', 'signed', 'struct', 'union',
  61. 'unsigned', 'void', --
  62. 'complex', 'imaginary', '_Complex', '_Imaginary', -- complex.h C99
  63. 'lconv', -- locale.h
  64. 'div_t', -- math.h
  65. 'va_list', -- stdarg.h
  66. 'bool', '_Bool', -- stdbool.h C99
  67. -- stddef.h.
  68. 'size_t', 'ptrdiff_t', --
  69. 'max_align_t', -- C11
  70. -- stdint.h.
  71. 'int8_t', 'int16_t', 'int32_t', 'int64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t',
  72. 'int_fast64_t', 'int_least8_t', 'int_least16_t', 'int_least32_t', 'int_least64_t', 'intmax_t',
  73. 'intptr_t', 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'uint_fast8_t', 'uint_fast16_t',
  74. 'uint_fast32_t', 'uint_fast64_t', 'uint_least8_t', 'uint_least16_t', 'uint_least32_t',
  75. 'uint_least64_t', 'uintmax_t', 'uintptr_t', --
  76. 'FILE', 'fpos_t', -- stdio.h
  77. 'div_t', 'ldiv_t', -- stdlib.h
  78. -- time.h.
  79. 'tm', 'time_t', 'clock_t', --
  80. 'timespec' -- C11
  81. })
  82. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  83. 'assert', -- assert.h
  84. -- complex.h.
  85. 'CMPLX', 'creal', 'cimag', 'cabs', 'carg', 'conj', 'cproj',
  86. -- C99
  87. 'cexp', 'cpow', 'csin', 'ccos', 'ctan', 'casin', 'cacos', 'catan', 'csinh', 'ccosh', 'ctanh',
  88. 'casinh', 'cacosh', 'catanh',
  89. -- ctype.h.
  90. 'isalnum', 'isalpha', 'islower', 'isupper', 'isdigit', 'isxdigit', 'iscntrl', 'isgraph',
  91. 'isspace', 'isprint', 'ispunct', 'tolower', 'toupper', --
  92. 'isblank', -- C99
  93. -- inttypes.h.
  94. 'INT8_C', 'INT16_C', 'INT32_C', 'INT64_C', 'INTMAX_C', 'UINT8_C', 'UINT16_C', 'UINT32_C',
  95. 'UINT64_C', 'UINTMAX_C', --
  96. 'setlocale', 'localeconv', -- locale.h
  97. -- math.h.
  98. 'abs', 'div', 'fabs', 'fmod', 'exp', 'log', 'log10', 'pow', 'sqrt', 'sin', 'cos', 'tan', 'asin',
  99. 'acos', 'atan', 'atan2', 'sinh', 'cosh', 'tanh', 'ceil', 'floor', 'frexp', 'ldexp', 'modf',
  100. -- C99.
  101. 'remainder', 'remquo', 'fma', 'fmax', 'fmin', 'fdim', 'nan', 'exp2', 'expm1', 'log2', 'log1p',
  102. 'cbrt', 'hypot', 'asinh', 'acosh', 'atanh', 'erf', 'erfc', 'tgamma', 'lgamma', 'trunc', 'round',
  103. 'nearbyint', 'rint', 'scalbn', 'ilogb', 'logb', 'nextafter', 'nexttoward', 'copysign', 'isfinite',
  104. 'isinf', 'isnan', 'isnormal', 'signbit', 'isgreater', 'isgreaterequal', 'isless', 'islessequal',
  105. 'islessgreater', 'isunordered', --
  106. 'strtoimax', 'strtoumax', -- inttypes.h C99
  107. 'signal', 'raise', -- signal.h
  108. 'setjmp', 'longjmp', -- setjmp.h
  109. 'va_start', 'va_arg', 'va_end', -- stdarg.h
  110. -- stdio.h.
  111. 'fopen', 'freopen', 'fclose', 'fflush', 'setbuf', 'setvbuf', 'fwide', 'fread', 'fwrite', 'fgetc',
  112. 'getc', 'fgets', 'fputc', 'putc', 'getchar', 'gets', 'putchar', 'puts', 'ungetc', 'scanf',
  113. 'fscanf', 'sscanf', 'printf', 'fprintf', 'sprintf', 'vprintf', 'vfprintf', 'vsprintf', 'ftell',
  114. 'fgetpos', 'fseek', 'fsetpos', 'rewind', 'clearerr', 'feof', 'ferror', 'perror', 'remove',
  115. 'rename', 'tmpfile', 'tmpnam',
  116. -- stdlib.h.
  117. 'abort', 'exit', 'atexit', 'system', 'getenv', 'malloc', 'calloc', 'realloc', 'free', 'atof',
  118. 'atoi', 'atol', 'strtol', 'strtoul', 'strtod', 'mblen', 'mbsinit', 'mbrlen', 'qsort', 'bsearch',
  119. 'rand', 'srand', --
  120. 'quick_exit', '_Exit', 'at_quick_exit', 'aligned_alloc', -- C11
  121. -- string.h.
  122. 'strcpy', 'strncpy', 'strcat', 'strncat', 'strxfrm', 'strlen', 'strcmp', 'strncmp', 'strcoll',
  123. 'strchr', 'strrchr', 'strspn', 'strcspn', 'strpbrk', 'strstr', 'strtok', 'memchr', 'memcmp',
  124. 'memset', 'memcpy', 'memmove', 'strerror',
  125. -- time.h.
  126. 'difftime', 'time', 'clock', 'asctime', 'ctime', 'gmtime', 'localtime', 'mktime', --
  127. 'timespec_get' -- C11
  128. })
  129. lex:set_word_list(lexer.CONSTANT_BUILTIN, {
  130. 'NULL', --
  131. '__DATE__', '__FILE__', '__LINE__', '__TIME__', '__func__', -- preprocessor
  132. -- errno.h.
  133. 'errno', --
  134. 'E2BIG', 'EACCES', 'EADDRINUSE', 'EADDRNOTAVAIL', 'EAFNOSUPPORT', 'EAGAIN', 'EALREADY', 'EBADF',
  135. 'EBADMSG', 'EBUSY', 'ECANCELED', 'ECHILD', 'ECONNABORTED', 'ECONNREFUSED', 'ECONNRESET',
  136. 'EDEADLK', 'EDESTADDRREQ', 'EDOM', 'EDQUOT', 'EEXIST', 'EFAULT', 'EFBIG', 'EHOSTUNREACH', 'EIDRM',
  137. 'EILSEQ', 'EINPROGRESS', 'EINTR', 'EINVAL', 'EIO', 'EISCONN', 'EISDIR', 'ELOOP', 'EMFILE',
  138. 'EMLINK', 'EMSGSIZE', 'EMULTIHOP', 'ENAMETOOLONG', 'ENETDOWN', 'ENETRESET', 'ENETUNREACH',
  139. 'ENFILE', 'ENOBUFS', 'ENODATA', 'ENODEV', 'ENOENT', 'ENOEXEC', 'ENOLCK', 'ENOLINK', 'ENOMEM',
  140. 'ENOMSG', 'ENOPROTOOPT', 'ENOSPC', 'ENOSR', 'ENOSTR', 'ENOSYS', 'ENOTCONN', 'ENOTDIR',
  141. 'ENOTEMPTY', 'ENOTRECOVERABLE', 'ENOTSOCK', 'ENOTSUP', 'ENOTTY', 'ENXIO', 'EOPNOTSUPP',
  142. 'EOVERFLOW', 'EOWNERDEAD', 'EPERM', 'EPIPE', 'EPROTO', 'EPROTONOSUPPORT', 'EPROTOTYPE', 'ERANGE',
  143. 'EROFS', 'ESPIPE', 'ESRCH', 'ESTALE', 'ETIME', 'ETIMEDOUT', 'ETXTBSY', 'EWOULDBLOCK', 'EXDEV',
  144. -- float.h.
  145. 'FLT_MIN', 'DBL_MIN', 'LDBL_MIN', 'FLT_MAX', 'DBL_MAX', 'LDBL_MAX',
  146. -- limits.h.
  147. 'CHAR_BIT', 'MB_LEN_MAX', 'CHAR_MIN', 'CHAR_MAX', 'SCHAR_MIN', 'SHRT_MIN', 'INT_MIN', 'LONG_MIN',
  148. 'SCHAR_MAX', 'SHRT_MAX', 'INT_MAX', 'LONG_MAX', 'UCHAR_MAX', 'USHRT_MAX', 'UINT_MAX', 'ULONG_MAX',
  149. -- C99.
  150. 'LLONG_MIN', 'ULLONG_MAX', 'PTRDIFF_MIN', 'PTRDIFF_MAX', 'SIZE_MAX', 'SIG_ATOMIC_MIN',
  151. 'SIG_ATOMIC_MAX', 'WINT_MIN', 'WINT_MAX', 'WCHAR_MIN', 'WCHAR_MAX', --
  152. 'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', -- locale.h
  153. -- math.h.
  154. 'HUGE_VAL', --
  155. 'INFINITY', 'NAN', -- C99
  156. -- stdint.h.
  157. 'INT8_MIN', 'INT16_MIN', 'INT32_MIN', 'INT64_MIN', 'INT_FAST8_MIN', 'INT_FAST16_MIN',
  158. 'INT_FAST32_MIN', 'INT_FAST64_MIN', 'INT_LEAST8_MIN', 'INT_LEAST16_MIN', 'INT_LEAST32_MIN',
  159. 'INT_LEAST64_MIN', 'INTPTR_MIN', 'INTMAX_MIN', 'INT8_MAX', 'INT16_MAX', 'INT32_MAX', 'INT64_MAX',
  160. 'INT_FAST8_MAX', 'INT_FAST16_MAX', 'INT_FAST32_MAX', 'INT_FAST64_MAX', 'INT_LEAST8_MAX',
  161. 'INT_LEAST16_MAX', 'INT_LEAST32_MAX', 'INT_LEAST64_MAX', 'INTPTR_MAX', 'INTMAX_MAX', 'UINT8_MAX',
  162. 'UINT16_MAX', 'UINT32_MAX', 'UINT64_MAX', 'UINT_FAST8_MAX', 'UINT_FAST16_MAX', 'UINT_FAST32_MAX',
  163. 'UINT_FAST64_MAX', 'UINT_LEAST8_MAX', 'UINT_LEAST16_MAX', 'UINT_LEAST32_MAX', 'UINT_LEAST64_MAX',
  164. 'UINTPTR_MAX', 'UINTMAX_MAX',
  165. -- stdio.h
  166. 'stdin', 'stdout', 'stderr', 'EOF', 'FOPEN_MAX', 'FILENAME_MAX', 'BUFSIZ', '_IOFBF', '_IOLBF',
  167. '_IONBF', 'SEEK_SET', 'SEEK_CUR', 'SEEK_END', 'TMP_MAX', --
  168. 'EXIT_SUCCESS', 'EXIT_FAILURE', 'RAND_MAX', -- stdlib.h
  169. -- signal.h.
  170. 'SIG_DFL', 'SIG_IGN', 'SIG_ERR', 'SIGABRT', 'SIGFPE', 'SIGILL', 'SIGINT', 'SIGSEGV', 'SIGTERM', --
  171. 'CLOCKS_PER_SEC' -- time.h.
  172. })
  173. lex:set_word_list(lexer.PREPROCESSOR, {
  174. 'define', 'defined', 'elif', 'else', 'endif', 'error', 'if', 'ifdef', 'ifndef', 'line', 'pragma',
  175. 'undef'
  176. })
  177. lexer.property['scintillua.comment'] = '//'
  178. return lex