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.

cl.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package c
  2. import (
  3. . "github.com/alecthomas/chroma" // nolint
  4. "github.com/alecthomas/chroma/lexers/internal"
  5. )
  6. var (
  7. clBuiltinFunctions = []string{
  8. "<", "<=", "=", ">", ">=", "-", "/", "/=", "*", "+", "1-", "1+",
  9. "abort", "abs", "acons", "acos", "acosh", "add-method", "adjoin",
  10. "adjustable-array-p", "adjust-array", "allocate-instance",
  11. "alpha-char-p", "alphanumericp", "append", "apply", "apropos",
  12. "apropos-list", "aref", "arithmetic-error-operands",
  13. "arithmetic-error-operation", "array-dimension", "array-dimensions",
  14. "array-displacement", "array-element-type", "array-has-fill-pointer-p",
  15. "array-in-bounds-p", "arrayp", "array-rank", "array-row-major-index",
  16. "array-total-size", "ash", "asin", "asinh", "assoc", "assoc-if",
  17. "assoc-if-not", "atan", "atanh", "atom", "bit", "bit-and", "bit-andc1",
  18. "bit-andc2", "bit-eqv", "bit-ior", "bit-nand", "bit-nor", "bit-not",
  19. "bit-orc1", "bit-orc2", "bit-vector-p", "bit-xor", "boole",
  20. "both-case-p", "boundp", "break", "broadcast-stream-streams",
  21. "butlast", "byte", "byte-position", "byte-size", "caaaar", "caaadr",
  22. "caaar", "caadar", "caaddr", "caadr", "caar", "cadaar", "cadadr",
  23. "cadar", "caddar", "cadddr", "caddr", "cadr", "call-next-method", "car",
  24. "cdaaar", "cdaadr", "cdaar", "cdadar", "cdaddr", "cdadr", "cdar",
  25. "cddaar", "cddadr", "cddar", "cdddar", "cddddr", "cdddr", "cddr", "cdr",
  26. "ceiling", "cell-error-name", "cerror", "change-class", "char", "char<",
  27. "char<=", "char=", "char>", "char>=", "char/=", "character",
  28. "characterp", "char-code", "char-downcase", "char-equal",
  29. "char-greaterp", "char-int", "char-lessp", "char-name",
  30. "char-not-equal", "char-not-greaterp", "char-not-lessp", "char-upcase",
  31. "cis", "class-name", "class-of", "clear-input", "clear-output",
  32. "close", "clrhash", "code-char", "coerce", "compile",
  33. "compiled-function-p", "compile-file", "compile-file-pathname",
  34. "compiler-macro-function", "complement", "complex", "complexp",
  35. "compute-applicable-methods", "compute-restarts", "concatenate",
  36. "concatenated-stream-streams", "conjugate", "cons", "consp",
  37. "constantly", "constantp", "continue", "copy-alist", "copy-list",
  38. "copy-pprint-dispatch", "copy-readtable", "copy-seq", "copy-structure",
  39. "copy-symbol", "copy-tree", "cos", "cosh", "count", "count-if",
  40. "count-if-not", "decode-float", "decode-universal-time", "delete",
  41. "delete-duplicates", "delete-file", "delete-if", "delete-if-not",
  42. "delete-package", "denominator", "deposit-field", "describe",
  43. "describe-object", "digit-char", "digit-char-p", "directory",
  44. "directory-namestring", "disassemble", "documentation", "dpb",
  45. "dribble", "echo-stream-input-stream", "echo-stream-output-stream",
  46. "ed", "eighth", "elt", "encode-universal-time", "endp",
  47. "enough-namestring", "ensure-directories-exist",
  48. "ensure-generic-function", "eq", "eql", "equal", "equalp", "error",
  49. "eval", "evenp", "every", "exp", "export", "expt", "fboundp",
  50. "fceiling", "fdefinition", "ffloor", "fifth", "file-author",
  51. "file-error-pathname", "file-length", "file-namestring",
  52. "file-position", "file-string-length", "file-write-date",
  53. "fill", "fill-pointer", "find", "find-all-symbols", "find-class",
  54. "find-if", "find-if-not", "find-method", "find-package", "find-restart",
  55. "find-symbol", "finish-output", "first", "float", "float-digits",
  56. "floatp", "float-precision", "float-radix", "float-sign", "floor",
  57. "fmakunbound", "force-output", "format", "fourth", "fresh-line",
  58. "fround", "ftruncate", "funcall", "function-keywords",
  59. "function-lambda-expression", "functionp", "gcd", "gensym", "gentemp",
  60. "get", "get-decoded-time", "get-dispatch-macro-character", "getf",
  61. "gethash", "get-internal-real-time", "get-internal-run-time",
  62. "get-macro-character", "get-output-stream-string", "get-properties",
  63. "get-setf-expansion", "get-universal-time", "graphic-char-p",
  64. "hash-table-count", "hash-table-p", "hash-table-rehash-size",
  65. "hash-table-rehash-threshold", "hash-table-size", "hash-table-test",
  66. "host-namestring", "identity", "imagpart", "import",
  67. "initialize-instance", "input-stream-p", "inspect",
  68. "integer-decode-float", "integer-length", "integerp",
  69. "interactive-stream-p", "intern", "intersection",
  70. "invalid-method-error", "invoke-debugger", "invoke-restart",
  71. "invoke-restart-interactively", "isqrt", "keywordp", "last", "lcm",
  72. "ldb", "ldb-test", "ldiff", "length", "lisp-implementation-type",
  73. "lisp-implementation-version", "list", "list*", "list-all-packages",
  74. "listen", "list-length", "listp", "load",
  75. "load-logical-pathname-translations", "log", "logand", "logandc1",
  76. "logandc2", "logbitp", "logcount", "logeqv", "logical-pathname",
  77. "logical-pathname-translations", "logior", "lognand", "lognor",
  78. "lognot", "logorc1", "logorc2", "logtest", "logxor", "long-site-name",
  79. "lower-case-p", "machine-instance", "machine-type", "machine-version",
  80. "macroexpand", "macroexpand-1", "macro-function", "make-array",
  81. "make-broadcast-stream", "make-concatenated-stream", "make-condition",
  82. "make-dispatch-macro-character", "make-echo-stream", "make-hash-table",
  83. "make-instance", "make-instances-obsolete", "make-list",
  84. "make-load-form", "make-load-form-saving-slots", "make-package",
  85. "make-pathname", "make-random-state", "make-sequence", "make-string",
  86. "make-string-input-stream", "make-string-output-stream", "make-symbol",
  87. "make-synonym-stream", "make-two-way-stream", "makunbound", "map",
  88. "mapc", "mapcan", "mapcar", "mapcon", "maphash", "map-into", "mapl",
  89. "maplist", "mask-field", "max", "member", "member-if", "member-if-not",
  90. "merge", "merge-pathnames", "method-combination-error",
  91. "method-qualifiers", "min", "minusp", "mismatch", "mod",
  92. "muffle-warning", "name-char", "namestring", "nbutlast", "nconc",
  93. "next-method-p", "nintersection", "ninth", "no-applicable-method",
  94. "no-next-method", "not", "notany", "notevery", "nreconc", "nreverse",
  95. "nset-difference", "nset-exclusive-or", "nstring-capitalize",
  96. "nstring-downcase", "nstring-upcase", "nsublis", "nsubst", "nsubst-if",
  97. "nsubst-if-not", "nsubstitute", "nsubstitute-if", "nsubstitute-if-not",
  98. "nth", "nthcdr", "null", "numberp", "numerator", "nunion", "oddp",
  99. "open", "open-stream-p", "output-stream-p", "package-error-package",
  100. "package-name", "package-nicknames", "packagep",
  101. "package-shadowing-symbols", "package-used-by-list", "package-use-list",
  102. "pairlis", "parse-integer", "parse-namestring", "pathname",
  103. "pathname-device", "pathname-directory", "pathname-host",
  104. "pathname-match-p", "pathname-name", "pathnamep", "pathname-type",
  105. "pathname-version", "peek-char", "phase", "plusp", "position",
  106. "position-if", "position-if-not", "pprint", "pprint-dispatch",
  107. "pprint-fill", "pprint-indent", "pprint-linear", "pprint-newline",
  108. "pprint-tab", "pprint-tabular", "prin1", "prin1-to-string", "princ",
  109. "princ-to-string", "print", "print-object", "probe-file", "proclaim",
  110. "provide", "random", "random-state-p", "rassoc", "rassoc-if",
  111. "rassoc-if-not", "rational", "rationalize", "rationalp", "read",
  112. "read-byte", "read-char", "read-char-no-hang", "read-delimited-list",
  113. "read-from-string", "read-line", "read-preserving-whitespace",
  114. "read-sequence", "readtable-case", "readtablep", "realp", "realpart",
  115. "reduce", "reinitialize-instance", "rem", "remhash", "remove",
  116. "remove-duplicates", "remove-if", "remove-if-not", "remove-method",
  117. "remprop", "rename-file", "rename-package", "replace", "require",
  118. "rest", "restart-name", "revappend", "reverse", "room", "round",
  119. "row-major-aref", "rplaca", "rplacd", "sbit", "scale-float", "schar",
  120. "search", "second", "set", "set-difference",
  121. "set-dispatch-macro-character", "set-exclusive-or",
  122. "set-macro-character", "set-pprint-dispatch", "set-syntax-from-char",
  123. "seventh", "shadow", "shadowing-import", "shared-initialize",
  124. "short-site-name", "signal", "signum", "simple-bit-vector-p",
  125. "simple-condition-format-arguments", "simple-condition-format-control",
  126. "simple-string-p", "simple-vector-p", "sin", "sinh", "sixth", "sleep",
  127. "slot-boundp", "slot-exists-p", "slot-makunbound", "slot-missing",
  128. "slot-unbound", "slot-value", "software-type", "software-version",
  129. "some", "sort", "special-operator-p", "sqrt", "stable-sort",
  130. "standard-char-p", "store-value", "stream-element-type",
  131. "stream-error-stream", "stream-external-format", "streamp", "string",
  132. "string<", "string<=", "string=", "string>", "string>=", "string/=",
  133. "string-capitalize", "string-downcase", "string-equal",
  134. "string-greaterp", "string-left-trim", "string-lessp",
  135. "string-not-equal", "string-not-greaterp", "string-not-lessp",
  136. "stringp", "string-right-trim", "string-trim", "string-upcase",
  137. "sublis", "subseq", "subsetp", "subst", "subst-if", "subst-if-not",
  138. "substitute", "substitute-if", "substitute-if-not", "subtypep", "svref",
  139. "sxhash", "symbol-function", "symbol-name", "symbolp", "symbol-package",
  140. "symbol-plist", "symbol-value", "synonym-stream-symbol", "syntax:",
  141. "tailp", "tan", "tanh", "tenth", "terpri", "third",
  142. "translate-logical-pathname", "translate-pathname", "tree-equal",
  143. "truename", "truncate", "two-way-stream-input-stream",
  144. "two-way-stream-output-stream", "type-error-datum",
  145. "type-error-expected-type", "type-of", "typep", "unbound-slot-instance",
  146. "unexport", "unintern", "union", "unread-char", "unuse-package",
  147. "update-instance-for-different-class",
  148. "update-instance-for-redefined-class", "upgraded-array-element-type",
  149. "upgraded-complex-part-type", "upper-case-p", "use-package",
  150. "user-homedir-pathname", "use-value", "values", "values-list", "vector",
  151. "vectorp", "vector-pop", "vector-push", "vector-push-extend", "warn",
  152. "wild-pathname-p", "write", "write-byte", "write-char", "write-line",
  153. "write-sequence", "write-string", "write-to-string", "yes-or-no-p",
  154. "y-or-n-p", "zerop",
  155. }
  156. clSpecialForms = []string{
  157. "block", "catch", "declare", "eval-when", "flet", "function", "go", "if",
  158. "labels", "lambda", "let", "let*", "load-time-value", "locally", "macrolet",
  159. "multiple-value-call", "multiple-value-prog1", "progn", "progv", "quote",
  160. "return-from", "setq", "symbol-macrolet", "tagbody", "the", "throw",
  161. "unwind-protect",
  162. }
  163. clMacros = []string{
  164. "and", "assert", "call-method", "case", "ccase", "check-type", "cond",
  165. "ctypecase", "decf", "declaim", "defclass", "defconstant", "defgeneric",
  166. "define-compiler-macro", "define-condition", "define-method-combination",
  167. "define-modify-macro", "define-setf-expander", "define-symbol-macro",
  168. "defmacro", "defmethod", "defpackage", "defparameter", "defsetf",
  169. "defstruct", "deftype", "defun", "defvar", "destructuring-bind", "do",
  170. "do*", "do-all-symbols", "do-external-symbols", "dolist", "do-symbols",
  171. "dotimes", "ecase", "etypecase", "formatter", "handler-bind",
  172. "handler-case", "ignore-errors", "incf", "in-package", "lambda", "loop",
  173. "loop-finish", "make-method", "multiple-value-bind", "multiple-value-list",
  174. "multiple-value-setq", "nth-value", "or", "pop",
  175. "pprint-exit-if-list-exhausted", "pprint-logical-block", "pprint-pop",
  176. "print-unreadable-object", "prog", "prog*", "prog1", "prog2", "psetf",
  177. "psetq", "push", "pushnew", "remf", "restart-bind", "restart-case",
  178. "return", "rotatef", "setf", "shiftf", "step", "time", "trace", "typecase",
  179. "unless", "untrace", "when", "with-accessors", "with-compilation-unit",
  180. "with-condition-restarts", "with-hash-table-iterator",
  181. "with-input-from-string", "with-open-file", "with-open-stream",
  182. "with-output-to-string", "with-package-iterator", "with-simple-restart",
  183. "with-slots", "with-standard-io-syntax",
  184. }
  185. clLambdaListKeywords = []string{
  186. "&allow-other-keys", "&aux", "&body", "&environment", "&key", "&optional",
  187. "&rest", "&whole",
  188. }
  189. clDeclarations = []string{
  190. "dynamic-extent", "ignore", "optimize", "ftype", "inline", "special",
  191. "ignorable", "notinline", "type",
  192. }
  193. clBuiltinTypes = []string{
  194. "atom", "boolean", "base-char", "base-string", "bignum", "bit",
  195. "compiled-function", "extended-char", "fixnum", "keyword", "nil",
  196. "signed-byte", "short-float", "single-float", "double-float", "long-float",
  197. "simple-array", "simple-base-string", "simple-bit-vector", "simple-string",
  198. "simple-vector", "standard-char", "unsigned-byte",
  199. // Condition Types
  200. "arithmetic-error", "cell-error", "condition", "control-error",
  201. "division-by-zero", "end-of-file", "error", "file-error",
  202. "floating-point-inexact", "floating-point-overflow",
  203. "floating-point-underflow", "floating-point-invalid-operation",
  204. "parse-error", "package-error", "print-not-readable", "program-error",
  205. "reader-error", "serious-condition", "simple-condition", "simple-error",
  206. "simple-type-error", "simple-warning", "stream-error", "storage-condition",
  207. "style-warning", "type-error", "unbound-variable", "unbound-slot",
  208. "undefined-function", "warning",
  209. }
  210. clBuiltinClasses = []string{
  211. "array", "broadcast-stream", "bit-vector", "built-in-class", "character",
  212. "class", "complex", "concatenated-stream", "cons", "echo-stream",
  213. "file-stream", "float", "function", "generic-function", "hash-table",
  214. "integer", "list", "logical-pathname", "method-combination", "method",
  215. "null", "number", "package", "pathname", "ratio", "rational", "readtable",
  216. "real", "random-state", "restart", "sequence", "standard-class",
  217. "standard-generic-function", "standard-method", "standard-object",
  218. "string-stream", "stream", "string", "structure-class", "structure-object",
  219. "symbol", "synonym-stream", "t", "two-way-stream", "vector",
  220. }
  221. )
  222. // Common Lisp lexer.
  223. var CommonLisp = internal.Register(TypeRemappingLexer(MustNewLexer(
  224. &Config{
  225. Name: "Common Lisp",
  226. Aliases: []string{"common-lisp", "cl", "lisp"},
  227. Filenames: []string{"*.cl", "*.lisp"},
  228. MimeTypes: []string{"text/x-common-lisp"},
  229. CaseInsensitive: true,
  230. },
  231. Rules{
  232. "root": {
  233. Default(Push("body")),
  234. },
  235. "multiline-comment": {
  236. {`#\|`, CommentMultiline, Push()},
  237. {`\|#`, CommentMultiline, Pop(1)},
  238. {`[^|#]+`, CommentMultiline, nil},
  239. {`[|#]`, CommentMultiline, nil},
  240. },
  241. "commented-form": {
  242. {`\(`, CommentPreproc, Push()},
  243. {`\)`, CommentPreproc, Pop(1)},
  244. {`[^()]+`, CommentPreproc, nil},
  245. },
  246. "body": {
  247. {`\s+`, Text, nil},
  248. {`;.*$`, CommentSingle, nil},
  249. {`#\|`, CommentMultiline, Push("multiline-comment")},
  250. {`#\d*Y.*$`, CommentSpecial, nil},
  251. {`"(\\.|\\\n|[^"\\])*"`, LiteralString, nil},
  252. {`:(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, LiteralStringSymbol, nil},
  253. {`::(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, LiteralStringSymbol, nil},
  254. {`:#(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, LiteralStringSymbol, nil},
  255. {`'(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, LiteralStringSymbol, nil},
  256. {`'`, Operator, nil},
  257. {"`", Operator, nil},
  258. {"[-+]?\\d+\\.?(?=[ \"()\\'\\n,;`])", LiteralNumberInteger, nil},
  259. {"[-+]?\\d+/\\d+(?=[ \"()\\'\\n,;`])", LiteralNumber, nil},
  260. {"[-+]?(\\d*\\.\\d+([defls][-+]?\\d+)?|\\d+(\\.\\d*)?[defls][-+]?\\d+)(?=[ \"()\\'\\n,;`])", LiteralNumberFloat, nil},
  261. {"#\\\\.(?=[ \"()\\'\\n,;`])", LiteralStringChar, nil},
  262. {`#\\(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, LiteralStringChar, nil},
  263. {`#\(`, Operator, Push("body")},
  264. {`#\d*\*[01]*`, LiteralOther, nil},
  265. {`#:(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, LiteralStringSymbol, nil},
  266. {`#[.,]`, Operator, nil},
  267. {`#\'`, NameFunction, nil},
  268. {`#b[+-]?[01]+(/[01]+)?`, LiteralNumberBin, nil},
  269. {`#o[+-]?[0-7]+(/[0-7]+)?`, LiteralNumberOct, nil},
  270. {`#x[+-]?[0-9a-f]+(/[0-9a-f]+)?`, LiteralNumberHex, nil},
  271. {`#\d+r[+-]?[0-9a-z]+(/[0-9a-z]+)?`, LiteralNumber, nil},
  272. {`(#c)(\()`, ByGroups(LiteralNumber, Punctuation), Push("body")},
  273. {`(#\d+a)(\()`, ByGroups(LiteralOther, Punctuation), Push("body")},
  274. {`(#s)(\()`, ByGroups(LiteralOther, Punctuation), Push("body")},
  275. {`#p?"(\\.|[^"])*"`, LiteralOther, nil},
  276. {`#\d+=`, Operator, nil},
  277. {`#\d+#`, Operator, nil},
  278. {"#+nil(?=[ \"()\\'\\n,;`])\\s*\\(", CommentPreproc, Push("commented-form")},
  279. {`#[+-]`, Operator, nil},
  280. {`(,@|,|\.)`, Operator, nil},
  281. {"(t|nil)(?=[ \"()\\'\\n,;`])", NameConstant, nil},
  282. {`\*(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)\*`, NameVariableGlobal, nil},
  283. {`(\|[^|]+\||(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~])(?:\\.|[\w!$%&*+-/<=>?@\[\]^{}~]|[#.:])*)`, NameVariable, nil},
  284. {`\(`, Punctuation, Push("body")},
  285. {`\)`, Punctuation, Pop(1)},
  286. },
  287. },
  288. ), TypeMapping{
  289. {NameVariable, NameFunction, clBuiltinFunctions},
  290. {NameVariable, Keyword, clSpecialForms},
  291. {NameVariable, NameBuiltin, clMacros},
  292. {NameVariable, Keyword, clLambdaListKeywords},
  293. {NameVariable, Keyword, clDeclarations},
  294. {NameVariable, KeywordType, clBuiltinTypes},
  295. {NameVariable, NameClass, clBuiltinClasses},
  296. }))