sas.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // SAS mode copyright (c) 2016 Jared Dean, SAS Institute
  4. // Created by Jared Dean
  5. // TODO
  6. // indent and de-indent
  7. // identify macro variables
  8. //Definitions
  9. // comment -- text within * ; or /* */
  10. // keyword -- SAS language variable
  11. // variable -- macro variables starts with '&' or variable formats
  12. // variable-2 -- DATA Step, proc, or macro names
  13. // string -- text within ' ' or " "
  14. // operator -- numeric operator + / - * ** le eq ge ... and so on
  15. // builtin -- proc %macro data run mend
  16. // atom
  17. // def
  18. ;(function (mod) {
  19. if (typeof exports == 'object' && typeof module == 'object')
  20. // CommonJS
  21. mod(require('../../lib/codemirror'))
  22. else if (typeof define == 'function' && define.amd)
  23. // AMD
  24. define(['../../lib/codemirror'], mod)
  25. // Plain browser env
  26. else mod(CodeMirror)
  27. })(function (CodeMirror) {
  28. 'use strict'
  29. CodeMirror.defineMode('sas', function () {
  30. var words = {}
  31. var isDoubleOperatorSym = {
  32. eq: 'operator',
  33. lt: 'operator',
  34. le: 'operator',
  35. gt: 'operator',
  36. ge: 'operator',
  37. in: 'operator',
  38. ne: 'operator',
  39. or: 'operator',
  40. }
  41. var isDoubleOperatorChar = /(<=|>=|!=|<>)/
  42. var isSingleOperatorChar = /[=\(:\),{}.*<>+\-\/^\[\]]/
  43. // Takes a string of words separated by spaces and adds them as
  44. // keys with the value of the first argument 'style'
  45. function define(style, string, context) {
  46. if (context) {
  47. var split = string.split(' ')
  48. for (var i = 0; i < split.length; i++) {
  49. words[split[i]] = { style: style, state: context }
  50. }
  51. }
  52. }
  53. //datastep
  54. define('def', 'stack pgm view source debug nesting nolist', ['inDataStep'])
  55. define('def', 'if while until for do do; end end; then else cancel', ['inDataStep'])
  56. define('def', 'label format _n_ _error_', ['inDataStep'])
  57. define(
  58. 'def',
  59. 'ALTER BUFNO BUFSIZE CNTLLEV COMPRESS DLDMGACTION ENCRYPT ENCRYPTKEY EXTENDOBSCOUNTER GENMAX GENNUM INDEX LABEL OBSBUF OUTREP PW PWREQ READ REPEMPTY REPLACE REUSE ROLE SORTEDBY SPILL TOBSNO TYPE WRITE FILECLOSE FIRSTOBS IN OBS POINTOBS WHERE WHEREUP IDXNAME IDXWHERE DROP KEEP RENAME',
  60. ['inDataStep']
  61. )
  62. define('def', 'filevar finfo finv fipname fipnamel fipstate first firstobs floor', ['inDataStep'])
  63. define(
  64. 'def',
  65. 'varfmt varinfmt varlabel varlen varname varnum varray varrayx vartype verify vformat vformatd vformatdx vformatn vformatnx vformatw vformatwx vformatx vinarray vinarrayx vinformat vinformatd vinformatdx vinformatn vinformatnx vinformatw vinformatwx vinformatx vlabel vlabelx vlength vlengthx vname vnamex vnferr vtype vtypex weekday',
  66. ['inDataStep']
  67. )
  68. define('def', 'zipfips zipname zipnamel zipstate', ['inDataStep'])
  69. define('def', 'put putc putn', ['inDataStep'])
  70. define('builtin', 'data run', ['inDataStep'])
  71. //proc
  72. define('def', 'data', ['inProc'])
  73. // flow control for macros
  74. define('def', '%if %end %end; %else %else; %do %do; %then', ['inMacro'])
  75. //everywhere
  76. define('builtin', 'proc run; quit; libname filename %macro %mend option options', ['ALL'])
  77. define('def', 'footnote title libname ods', ['ALL'])
  78. define('def', '%let %put %global %sysfunc %eval ', ['ALL'])
  79. // automatic macro variables http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a003167023.htm
  80. define(
  81. 'variable',
  82. '&sysbuffr &syscc &syscharwidth &syscmd &sysdate &sysdate9 &sysday &sysdevic &sysdmg &sysdsn &sysencoding &sysenv &syserr &syserrortext &sysfilrc &syshostname &sysindex &sysinfo &sysjobid &syslast &syslckrc &syslibrc &syslogapplname &sysmacroname &sysmenv &sysmsg &sysncpu &sysodspath &sysparm &syspbuff &sysprocessid &sysprocessname &sysprocname &sysrc &sysscp &sysscpl &sysscpl &syssite &sysstartid &sysstartname &systcpiphostname &systime &sysuserid &sysver &sysvlong &sysvlong4 &syswarningtext',
  83. ['ALL']
  84. )
  85. //footnote[1-9]? title[1-9]?
  86. //options statement
  87. define('def', 'source2 nosource2 page pageno pagesize', ['ALL'])
  88. //proc and datastep
  89. define(
  90. 'def',
  91. '_all_ _character_ _cmd_ _freq_ _i_ _infile_ _last_ _msg_ _null_ _numeric_ _temporary_ _type_ abort abs addr adjrsq airy alpha alter altlog altprint and arcos array arsin as atan attrc attrib attrn authserver autoexec awscontrol awsdef awsmenu awsmenumerge awstitle backward band base betainv between blocksize blshift bnot bor brshift bufno bufsize bxor by byerr byline byte calculated call cards cards4 catcache cbufno cdf ceil center cexist change chisq cinv class cleanup close cnonct cntllev coalesce codegen col collate collin column comamid comaux1 comaux2 comdef compbl compound compress config continue convert cos cosh cpuid create cross crosstab css curobs cv daccdb daccdbsl daccsl daccsyd dacctab dairy datalines datalines4 datejul datepart datetime day dbcslang dbcstype dclose ddfm ddm delete delimiter depdb depdbsl depsl depsyd deptab dequote descending descript design= device dflang dhms dif digamma dim dinfo display distinct dkricond dkrocond dlm dnum do dopen doptname doptnum dread drop dropnote dsname dsnferr echo else emaildlg emailid emailpw emailserver emailsys encrypt end endsas engine eof eov erf erfc error errorcheck errors exist exp fappend fclose fcol fdelete feedback fetch fetchobs fexist fget file fileclose fileexist filefmt filename fileref fmterr fmtsearch fnonct fnote font fontalias fopen foptname foptnum force formatted formchar formdelim formdlim forward fpoint fpos fput fread frewind frlen from fsep fuzz fwrite gaminv gamma getoption getvarc getvarn go goto group gwindow hbar hbound helpenv helploc hms honorappearance hosthelp hostprint hour hpct html hvar ibessel ibr id if index indexc indexw initcmd initstmt inner input inputc inputn inr insert int intck intnx into intrr invaliddata irr is jbessel join juldate keep kentb kurtosis label lag last lbound leave left length levels lgamma lib library libref line linesize link list log log10 log2 logpdf logpmf logsdf lostcard lowcase lrecl ls macro macrogen maps mautosource max maxdec maxr mdy mean measures median memtype merge merror min minute missing missover mlogic mod mode model modify month mopen mort mprint mrecall msglevel msymtabmax mvarsize myy n nest netpv new news nmiss no nobatch nobs nocaps nocardimage nocenter nocharcode nocmdmac nocol nocum nodate nodbcs nodetails nodmr nodms nodmsbatch nodup nodupkey noduplicates noechoauto noequals noerrorabend noexitwindows nofullstimer noicon noimplmac noint nolist noloadlist nomiss nomlogic nomprint nomrecall nomsgcase nomstored nomultenvappl nonotes nonumber noobs noovp nopad nopercent noprint noprintinit normal norow norsasuser nosetinit nosplash nosymbolgen note notes notitle notitles notsorted noverbose noxsync noxwait npv null number numkeys nummousekeys nway obs on open order ordinal otherwise out outer outp= output over ovp p(1 5 10 25 50 75 90 95 99) pad pad2 paired parm parmcards path pathdll pathname pdf peek peekc pfkey pmf point poisson poke position printer probbeta probbnml probchi probf probgam probhypr probit probnegb probnorm probsig probt procleave prt ps pw pwreq qtr quote r ranbin rancau random ranexp rangam range ranks rannor ranpoi rantbl rantri ranuni rcorr read recfm register regr remote remove rename repeat repeated replace resolve retain return reuse reverse rewind right round rsquare rtf rtrace rtraceloc s s2 samploc sasautos sascontrol sasfrscr sasmsg sasmstore sasscript sasuser saving scan sdf second select selection separated seq serror set setcomm setot sign simple sin sinh siteinfo skewness skip sle sls sortedby sortpgm sortseq sortsize soundex spedis splashlocation split spool sqrt start std stderr stdin stfips stimer stname stnamel stop stopover sub subgroup subpopn substr sum sumwgt symbol symbolgen symget symput sysget sysin sysleave sysmsg sysparm sysprint sysprintfont sysprod sysrc system t table tables tan tanh tapeclose tbufsize terminal test then timepart tinv tnonct to today tol tooldef totper transformout translate trantab tranwrd trigamma trim trimn trunc truncover type unformatted uniform union until upcase update user usericon uss validate value var weight when where while wincharset window work workinit workterm write wsum xsync xwait yearcutoff yes yyq min max',
  92. ['inDataStep', 'inProc']
  93. )
  94. define('operator', 'and not ', ['inDataStep', 'inProc'])
  95. // Main function
  96. function tokenize(stream, state) {
  97. // Finally advance the stream
  98. var ch = stream.next()
  99. // BLOCKCOMMENT
  100. if (ch === '/' && stream.eat('*')) {
  101. state.continueComment = true
  102. return 'comment'
  103. } else if (state.continueComment === true) {
  104. // in comment block
  105. //comment ends at the beginning of the line
  106. if (ch === '*' && stream.peek() === '/') {
  107. stream.next()
  108. state.continueComment = false
  109. } else if (stream.skipTo('*')) {
  110. //comment is potentially later in line
  111. stream.skipTo('*')
  112. stream.next()
  113. if (stream.eat('/')) state.continueComment = false
  114. } else {
  115. stream.skipToEnd()
  116. }
  117. return 'comment'
  118. }
  119. if (ch == '*' && stream.column() == stream.indentation()) {
  120. stream.skipToEnd()
  121. return 'comment'
  122. }
  123. // DoubleOperator match
  124. var doubleOperator = ch + stream.peek()
  125. if ((ch === '"' || ch === "'") && !state.continueString) {
  126. state.continueString = ch
  127. return 'string'
  128. } else if (state.continueString) {
  129. if (state.continueString == ch) {
  130. state.continueString = null
  131. } else if (stream.skipTo(state.continueString)) {
  132. // quote found on this line
  133. stream.next()
  134. state.continueString = null
  135. } else {
  136. stream.skipToEnd()
  137. }
  138. return 'string'
  139. } else if (state.continueString !== null && stream.eol()) {
  140. stream.skipTo(state.continueString) || stream.skipToEnd()
  141. return 'string'
  142. } else if (/[\d\.]/.test(ch)) {
  143. //find numbers
  144. if (ch === '.') stream.match(/^[0-9]+([eE][\-+]?[0-9]+)?/)
  145. else if (ch === '0') stream.match(/^[xX][0-9a-fA-F]+/) || stream.match(/^0[0-7]+/)
  146. else stream.match(/^[0-9]*\.?[0-9]*([eE][\-+]?[0-9]+)?/)
  147. return 'number'
  148. } else if (isDoubleOperatorChar.test(ch + stream.peek())) {
  149. // TWO SYMBOL TOKENS
  150. stream.next()
  151. return 'operator'
  152. } else if (isDoubleOperatorSym.hasOwnProperty(doubleOperator)) {
  153. stream.next()
  154. if (stream.peek() === ' ') return isDoubleOperatorSym[doubleOperator.toLowerCase()]
  155. } else if (isSingleOperatorChar.test(ch)) {
  156. // SINGLE SYMBOL TOKENS
  157. return 'operator'
  158. }
  159. // Matches one whole word -- even if the word is a character
  160. var word
  161. if (stream.match(/[%&;\w]+/, false) != null) {
  162. word = ch + stream.match(/[%&;\w]+/, true)
  163. if (/&/.test(word)) return 'variable'
  164. } else {
  165. word = ch
  166. }
  167. // the word after DATA PROC or MACRO
  168. if (state.nextword) {
  169. stream.match(/[\w]+/)
  170. // match memname.libname
  171. if (stream.peek() === '.') stream.skipTo(' ')
  172. state.nextword = false
  173. return 'variable-2'
  174. }
  175. word = word.toLowerCase()
  176. // Are we in a DATA Step?
  177. if (state.inDataStep) {
  178. if (word === 'run;' || stream.match(/run\s;/)) {
  179. state.inDataStep = false
  180. return 'builtin'
  181. }
  182. // variable formats
  183. if (word && stream.next() === '.') {
  184. //either a format or libname.memname
  185. if (/\w/.test(stream.peek())) return 'variable-2'
  186. else return 'variable'
  187. }
  188. // do we have a DATA Step keyword
  189. if (word && words.hasOwnProperty(word) && (words[word].state.indexOf('inDataStep') !== -1 || words[word].state.indexOf('ALL') !== -1)) {
  190. //backup to the start of the word
  191. if (stream.start < stream.pos) stream.backUp(stream.pos - stream.start)
  192. //advance the length of the word and return
  193. for (var i = 0; i < word.length; ++i) stream.next()
  194. return words[word].style
  195. }
  196. }
  197. // Are we in an Proc statement?
  198. if (state.inProc) {
  199. if (word === 'run;' || word === 'quit;') {
  200. state.inProc = false
  201. return 'builtin'
  202. }
  203. // do we have a proc keyword
  204. if (word && words.hasOwnProperty(word) && (words[word].state.indexOf('inProc') !== -1 || words[word].state.indexOf('ALL') !== -1)) {
  205. stream.match(/[\w]+/)
  206. return words[word].style
  207. }
  208. }
  209. // Are we in a Macro statement?
  210. if (state.inMacro) {
  211. if (word === '%mend') {
  212. if (stream.peek() === ';') stream.next()
  213. state.inMacro = false
  214. return 'builtin'
  215. }
  216. if (word && words.hasOwnProperty(word) && (words[word].state.indexOf('inMacro') !== -1 || words[word].state.indexOf('ALL') !== -1)) {
  217. stream.match(/[\w]+/)
  218. return words[word].style
  219. }
  220. return 'atom'
  221. }
  222. // Do we have Keywords specific words?
  223. if (word && words.hasOwnProperty(word)) {
  224. // Negates the initial next()
  225. stream.backUp(1)
  226. // Actually move the stream
  227. stream.match(/[\w]+/)
  228. if (word === 'data' && /=/.test(stream.peek()) === false) {
  229. state.inDataStep = true
  230. state.nextword = true
  231. return 'builtin'
  232. }
  233. if (word === 'proc') {
  234. state.inProc = true
  235. state.nextword = true
  236. return 'builtin'
  237. }
  238. if (word === '%macro') {
  239. state.inMacro = true
  240. state.nextword = true
  241. return 'builtin'
  242. }
  243. if (/title[1-9]/.test(word)) return 'def'
  244. if (word === 'footnote') {
  245. stream.eat(/[1-9]/)
  246. return 'def'
  247. }
  248. // Returns their value as state in the prior define methods
  249. if (state.inDataStep === true && words[word].state.indexOf('inDataStep') !== -1) return words[word].style
  250. if (state.inProc === true && words[word].state.indexOf('inProc') !== -1) return words[word].style
  251. if (state.inMacro === true && words[word].state.indexOf('inMacro') !== -1) return words[word].style
  252. if (words[word].state.indexOf('ALL') !== -1) return words[word].style
  253. return null
  254. }
  255. // Unrecognized syntax
  256. return null
  257. }
  258. return {
  259. startState: function () {
  260. return {
  261. inDataStep: false,
  262. inProc: false,
  263. inMacro: false,
  264. nextword: false,
  265. continueString: null,
  266. continueComment: false,
  267. }
  268. },
  269. token: function (stream, state) {
  270. // Strip the spaces, but regex will account for them either way
  271. if (stream.eatSpace()) return null
  272. // Go through the main process
  273. return tokenize(stream, state)
  274. },
  275. blockCommentStart: '/*',
  276. blockCommentEnd: '*/',
  277. }
  278. })
  279. CodeMirror.defineMIME('text/x-sas', 'sas')
  280. })