sas.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """
  2. pygments.lexers.sas
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexer for SAS.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, words
  10. from pygments.token import Comment, Keyword, Name, Number, String, Text, \
  11. Other, Generic
  12. __all__ = ['SASLexer']
  13. class SASLexer(RegexLexer):
  14. """
  15. For `SAS <http://www.sas.com/>`_ files.
  16. .. versionadded:: 2.2
  17. """
  18. # Syntax from syntax/sas.vim by James Kidd <james.kidd@covance.com>
  19. name = 'SAS'
  20. aliases = ['sas']
  21. filenames = ['*.SAS', '*.sas']
  22. mimetypes = ['text/x-sas', 'text/sas', 'application/x-sas']
  23. flags = re.IGNORECASE | re.MULTILINE
  24. builtins_macros = (
  25. "bquote", "nrbquote", "cmpres", "qcmpres", "compstor", "datatyp",
  26. "display", "do", "else", "end", "eval", "global", "goto", "if",
  27. "index", "input", "keydef", "label", "left", "length", "let",
  28. "local", "lowcase", "macro", "mend", "nrquote",
  29. "nrstr", "put", "qleft", "qlowcase", "qscan",
  30. "qsubstr", "qsysfunc", "qtrim", "quote", "qupcase", "scan",
  31. "str", "substr", "superq", "syscall", "sysevalf", "sysexec",
  32. "sysfunc", "sysget", "syslput", "sysprod", "sysrc", "sysrput",
  33. "then", "to", "trim", "unquote", "until", "upcase", "verify",
  34. "while", "window"
  35. )
  36. builtins_conditionals = (
  37. "do", "if", "then", "else", "end", "until", "while"
  38. )
  39. builtins_statements = (
  40. "abort", "array", "attrib", "by", "call", "cards", "cards4",
  41. "catname", "continue", "datalines", "datalines4", "delete", "delim",
  42. "delimiter", "display", "dm", "drop", "endsas", "error", "file",
  43. "filename", "footnote", "format", "goto", "in", "infile", "informat",
  44. "input", "keep", "label", "leave", "length", "libname", "link",
  45. "list", "lostcard", "merge", "missing", "modify", "options", "output",
  46. "out", "page", "put", "redirect", "remove", "rename", "replace",
  47. "retain", "return", "select", "set", "skip", "startsas", "stop",
  48. "title", "update", "waitsas", "where", "window", "x", "systask"
  49. )
  50. builtins_sql = (
  51. "add", "and", "alter", "as", "cascade", "check", "create",
  52. "delete", "describe", "distinct", "drop", "foreign", "from",
  53. "group", "having", "index", "insert", "into", "in", "key", "like",
  54. "message", "modify", "msgtype", "not", "null", "on", "or",
  55. "order", "primary", "references", "reset", "restrict", "select",
  56. "set", "table", "unique", "update", "validate", "view", "where"
  57. )
  58. builtins_functions = (
  59. "abs", "addr", "airy", "arcos", "arsin", "atan", "attrc",
  60. "attrn", "band", "betainv", "blshift", "bnot", "bor",
  61. "brshift", "bxor", "byte", "cdf", "ceil", "cexist", "cinv",
  62. "close", "cnonct", "collate", "compbl", "compound",
  63. "compress", "cos", "cosh", "css", "curobs", "cv", "daccdb",
  64. "daccdbsl", "daccsl", "daccsyd", "dacctab", "dairy", "date",
  65. "datejul", "datepart", "datetime", "day", "dclose", "depdb",
  66. "depdbsl", "depsl", "depsyd",
  67. "deptab", "dequote", "dhms", "dif", "digamma",
  68. "dim", "dinfo", "dnum", "dopen", "doptname", "doptnum",
  69. "dread", "dropnote", "dsname", "erf", "erfc", "exist", "exp",
  70. "fappend", "fclose", "fcol", "fdelete", "fetch", "fetchobs",
  71. "fexist", "fget", "fileexist", "filename", "fileref",
  72. "finfo", "finv", "fipname", "fipnamel", "fipstate", "floor",
  73. "fnonct", "fnote", "fopen", "foptname", "foptnum", "fpoint",
  74. "fpos", "fput", "fread", "frewind", "frlen", "fsep", "fuzz",
  75. "fwrite", "gaminv", "gamma", "getoption", "getvarc", "getvarn",
  76. "hbound", "hms", "hosthelp", "hour", "ibessel", "index",
  77. "indexc", "indexw", "input", "inputc", "inputn", "int",
  78. "intck", "intnx", "intrr", "irr", "jbessel", "juldate",
  79. "kurtosis", "lag", "lbound", "left", "length", "lgamma",
  80. "libname", "libref", "log", "log10", "log2", "logpdf", "logpmf",
  81. "logsdf", "lowcase", "max", "mdy", "mean", "min", "minute",
  82. "mod", "month", "mopen", "mort", "n", "netpv", "nmiss",
  83. "normal", "note", "npv", "open", "ordinal", "pathname",
  84. "pdf", "peek", "peekc", "pmf", "point", "poisson", "poke",
  85. "probbeta", "probbnml", "probchi", "probf", "probgam",
  86. "probhypr", "probit", "probnegb", "probnorm", "probt",
  87. "put", "putc", "putn", "qtr", "quote", "ranbin", "rancau",
  88. "ranexp", "rangam", "range", "rank", "rannor", "ranpoi",
  89. "rantbl", "rantri", "ranuni", "repeat", "resolve", "reverse",
  90. "rewind", "right", "round", "saving", "scan", "sdf", "second",
  91. "sign", "sin", "sinh", "skewness", "soundex", "spedis",
  92. "sqrt", "std", "stderr", "stfips", "stname", "stnamel",
  93. "substr", "sum", "symget", "sysget", "sysmsg", "sysprod",
  94. "sysrc", "system", "tan", "tanh", "time", "timepart", "tinv",
  95. "tnonct", "today", "translate", "tranwrd", "trigamma",
  96. "trim", "trimn", "trunc", "uniform", "upcase", "uss", "var",
  97. "varfmt", "varinfmt", "varlabel", "varlen", "varname",
  98. "varnum", "varray", "varrayx", "vartype", "verify", "vformat",
  99. "vformatd", "vformatdx", "vformatn", "vformatnx", "vformatw",
  100. "vformatwx", "vformatx", "vinarray", "vinarrayx", "vinformat",
  101. "vinformatd", "vinformatdx", "vinformatn", "vinformatnx",
  102. "vinformatw", "vinformatwx", "vinformatx", "vlabel",
  103. "vlabelx", "vlength", "vlengthx", "vname", "vnamex", "vtype",
  104. "vtypex", "weekday", "year", "yyq", "zipfips", "zipname",
  105. "zipnamel", "zipstate"
  106. )
  107. tokens = {
  108. 'root': [
  109. include('comments'),
  110. include('proc-data'),
  111. include('cards-datalines'),
  112. include('logs'),
  113. include('general'),
  114. (r'.', Text),
  115. ],
  116. # SAS is multi-line regardless, but * is ended by ;
  117. 'comments': [
  118. (r'^\s*\*.*?;', Comment),
  119. (r'/\*.*?\*/', Comment),
  120. (r'^\s*\*(.|\n)*?;', Comment.Multiline),
  121. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  122. ],
  123. # Special highlight for proc, data, quit, run
  124. 'proc-data': [
  125. (r'(^|;)\s*(proc \w+|data|run|quit)[\s;]',
  126. Keyword.Reserved),
  127. ],
  128. # Special highlight cards and datalines
  129. 'cards-datalines': [
  130. (r'^\s*(datalines|cards)\s*;\s*$', Keyword, 'data'),
  131. ],
  132. 'data': [
  133. (r'(.|\n)*^\s*;\s*$', Other, '#pop'),
  134. ],
  135. # Special highlight for put NOTE|ERROR|WARNING (order matters)
  136. 'logs': [
  137. (r'\n?^\s*%?put ', Keyword, 'log-messages'),
  138. ],
  139. 'log-messages': [
  140. (r'NOTE(:|-).*', Generic, '#pop'),
  141. (r'WARNING(:|-).*', Generic.Emph, '#pop'),
  142. (r'ERROR(:|-).*', Generic.Error, '#pop'),
  143. include('general'),
  144. ],
  145. 'general': [
  146. include('keywords'),
  147. include('vars-strings'),
  148. include('special'),
  149. include('numbers'),
  150. ],
  151. # Keywords, statements, functions, macros
  152. 'keywords': [
  153. (words(builtins_statements,
  154. prefix = r'\b',
  155. suffix = r'\b'),
  156. Keyword),
  157. (words(builtins_sql,
  158. prefix = r'\b',
  159. suffix = r'\b'),
  160. Keyword),
  161. (words(builtins_conditionals,
  162. prefix = r'\b',
  163. suffix = r'\b'),
  164. Keyword),
  165. (words(builtins_macros,
  166. prefix = r'%',
  167. suffix = r'\b'),
  168. Name.Builtin),
  169. (words(builtins_functions,
  170. prefix = r'\b',
  171. suffix = r'\('),
  172. Name.Builtin),
  173. ],
  174. # Strings and user-defined variables and macros (order matters)
  175. 'vars-strings': [
  176. (r'&[a-z_]\w{0,31}\.?', Name.Variable),
  177. (r'%[a-z_]\w{0,31}', Name.Function),
  178. (r'\'', String, 'string_squote'),
  179. (r'"', String, 'string_dquote'),
  180. ],
  181. 'string_squote': [
  182. ('\'', String, '#pop'),
  183. (r'\\\\|\\"|\\\n', String.Escape),
  184. # AFAIK, macro variables are not evaluated in single quotes
  185. # (r'&', Name.Variable, 'validvar'),
  186. (r'[^$\'\\]+', String),
  187. (r'[$\'\\]', String),
  188. ],
  189. 'string_dquote': [
  190. (r'"', String, '#pop'),
  191. (r'\\\\|\\"|\\\n', String.Escape),
  192. (r'&', Name.Variable, 'validvar'),
  193. (r'[^$&"\\]+', String),
  194. (r'[$"\\]', String),
  195. ],
  196. 'validvar': [
  197. (r'[a-z_]\w{0,31}\.?', Name.Variable, '#pop'),
  198. ],
  199. # SAS numbers and special variables
  200. 'numbers': [
  201. (r'\b[+-]?([0-9]+(\.[0-9]+)?|\.[0-9]+|\.)(E[+-]?[0-9]+)?i?\b',
  202. Number),
  203. ],
  204. 'special': [
  205. (r'(null|missing|_all_|_automatic_|_character_|_n_|'
  206. r'_infile_|_name_|_null_|_numeric_|_user_|_webout_)',
  207. Keyword.Constant),
  208. ],
  209. # 'operators': [
  210. # (r'(-|=|<=|>=|<|>|<>|&|!=|'
  211. # r'\||\*|\+|\^|/|!|~|~=)', Operator)
  212. # ],
  213. }