You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

c.go 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package c
  2. import (
  3. . "github.com/alecthomas/chroma" // nolint
  4. "github.com/alecthomas/chroma/lexers/internal"
  5. )
  6. // C lexer.
  7. var C = internal.Register(MustNewLexer(
  8. &Config{
  9. Name: "C",
  10. Aliases: []string{"c"},
  11. Filenames: []string{"*.c", "*.h", "*.idc"},
  12. MimeTypes: []string{"text/x-chdr", "text/x-csrc"},
  13. },
  14. Rules{
  15. "whitespace": {
  16. {`^#if\s+0`, CommentPreproc, Push("if0")},
  17. {`^#`, CommentPreproc, Push("macro")},
  18. {`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")},
  19. {`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")},
  20. {`\n`, Text, nil},
  21. {`\s+`, Text, nil},
  22. {`\\\n`, Text, nil},
  23. {`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
  24. {`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
  25. {`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
  26. },
  27. "statements": {
  28. {`(L?)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")},
  29. {`(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')`, ByGroups(LiteralStringAffix, LiteralStringChar, LiteralStringChar, LiteralStringChar), nil},
  30. {`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
  31. {`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
  32. {`0x[0-9a-fA-F]+[LlUu]*`, LiteralNumberHex, nil},
  33. {`0[0-7]+[LlUu]*`, LiteralNumberOct, nil},
  34. {`\d+[LlUu]*`, LiteralNumberInteger, nil},
  35. {`\*/`, Error, nil},
  36. {`[~!%^&*+=|?:<>/-]`, Operator, nil},
  37. {`[()\[\],.]`, Punctuation, nil},
  38. {Words(``, `\b`, `asm`, `auto`, `break`, `case`, `const`, `continue`, `default`, `do`, `else`, `enum`, `extern`, `for`, `goto`, `if`, `register`, `restricted`, `return`, `sizeof`, `static`, `struct`, `switch`, `typedef`, `union`, `volatile`, `while`), Keyword, nil},
  39. {`(bool|int|long|float|short|double|char|unsigned|signed|void)\b`, KeywordType, nil},
  40. {Words(``, `\b`, `inline`, `_inline`, `__inline`, `naked`, `restrict`, `thread`, `typename`), KeywordReserved, nil},
  41. {`(__m(128i|128d|128|64))\b`, KeywordReserved, nil},
  42. {Words(`__`, `\b`, `asm`, `int8`, `based`, `except`, `int16`, `stdcall`, `cdecl`, `fastcall`, `int32`, `declspec`, `finally`, `int64`, `try`, `leave`, `wchar_t`, `w64`, `unaligned`, `raise`, `noop`, `identifier`, `forceinline`, `assume`), KeywordReserved, nil},
  43. {`(true|false|NULL)\b`, NameBuiltin, nil},
  44. {`([a-zA-Z_]\w*)(\s*)(:)(?!:)`, ByGroups(NameLabel, Text, Punctuation), nil},
  45. {`[a-zA-Z_]\w*`, Name, nil},
  46. },
  47. "root": {
  48. Include("whitespace"),
  49. {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")},
  50. {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil},
  51. Default(Push("statement")),
  52. },
  53. "statement": {
  54. Include("whitespace"),
  55. Include("statements"),
  56. {`[{}]`, Punctuation, nil},
  57. {`;`, Punctuation, Pop(1)},
  58. },
  59. "function": {
  60. Include("whitespace"),
  61. Include("statements"),
  62. {`;`, Punctuation, nil},
  63. {`\{`, Punctuation, Push()},
  64. {`\}`, Punctuation, Pop(1)},
  65. },
  66. "string": {
  67. {`"`, LiteralString, Pop(1)},
  68. {`\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})`, LiteralStringEscape, nil},
  69. {`[^\\"\n]+`, LiteralString, nil},
  70. {`\\\n`, LiteralString, nil},
  71. {`\\`, LiteralString, nil},
  72. },
  73. "macro": {
  74. {`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil},
  75. {`[^/\n]+`, CommentPreproc, nil},
  76. {`/[*](.|\n)*?[*]/`, CommentMultiline, nil},
  77. {`//.*?\n`, CommentSingle, Pop(1)},
  78. {`/`, CommentPreproc, nil},
  79. {`(?<=\\)\n`, CommentPreproc, nil},
  80. {`\n`, CommentPreproc, Pop(1)},
  81. },
  82. "if0": {
  83. {`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()},
  84. {`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)},
  85. {`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)},
  86. {`.*?\n`, Comment, nil},
  87. },
  88. },
  89. ))