yacas.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Yacas mode copyright (c) 2015 by Grzegorz Mazur
  4. // Loosely based on mathematica mode by Calin Barbat
  5. ;(function (mod) {
  6. if (typeof exports == 'object' && typeof module == 'object')
  7. // CommonJS
  8. mod(require('../../lib/codemirror'))
  9. else if (typeof define == 'function' && define.amd)
  10. // AMD
  11. define(['../../lib/codemirror'], mod)
  12. // Plain browser env
  13. else mod(CodeMirror)
  14. })(function (CodeMirror) {
  15. 'use strict'
  16. CodeMirror.defineMode('yacas', function (_config, _parserConfig) {
  17. function words(str) {
  18. var obj = {},
  19. words = str.split(' ')
  20. for (var i = 0; i < words.length; ++i) obj[words[i]] = true
  21. return obj
  22. }
  23. var bodiedOps = words(
  24. 'Assert BackQuote D Defun Deriv For ForEach FromFile ' +
  25. 'FromString Function Integrate InverseTaylor Limit ' +
  26. 'LocalSymbols Macro MacroRule MacroRulePattern ' +
  27. 'NIntegrate Rule RulePattern Subst TD TExplicitSum ' +
  28. 'TSum Taylor Taylor1 Taylor2 Taylor3 ToFile ' +
  29. 'ToStdout ToString TraceRule Until While'
  30. )
  31. // patterns
  32. var pFloatForm = '(?:(?:\\.\\d+|\\d+\\.\\d*|\\d+)(?:[eE][+-]?\\d+)?)'
  33. var pIdentifier = "(?:[a-zA-Z\\$'][a-zA-Z0-9\\$']*)"
  34. // regular expressions
  35. var reFloatForm = new RegExp(pFloatForm)
  36. var reIdentifier = new RegExp(pIdentifier)
  37. var rePattern = new RegExp(pIdentifier + '?_' + pIdentifier)
  38. var reFunctionLike = new RegExp(pIdentifier + '\\s*\\(')
  39. function tokenBase(stream, state) {
  40. var ch
  41. // get next character
  42. ch = stream.next()
  43. // string
  44. if (ch === '"') {
  45. state.tokenize = tokenString
  46. return state.tokenize(stream, state)
  47. }
  48. // comment
  49. if (ch === '/') {
  50. if (stream.eat('*')) {
  51. state.tokenize = tokenComment
  52. return state.tokenize(stream, state)
  53. }
  54. if (stream.eat('/')) {
  55. stream.skipToEnd()
  56. return 'comment'
  57. }
  58. }
  59. // go back one character
  60. stream.backUp(1)
  61. // update scope info
  62. var m = stream.match(/^(\w+)\s*\(/, false)
  63. if (m !== null && bodiedOps.hasOwnProperty(m[1])) state.scopes.push('bodied')
  64. var scope = currentScope(state)
  65. if (scope === 'bodied' && ch === '[') state.scopes.pop()
  66. if (ch === '[' || ch === '{' || ch === '(') state.scopes.push(ch)
  67. scope = currentScope(state)
  68. if ((scope === '[' && ch === ']') || (scope === '{' && ch === '}') || (scope === '(' && ch === ')')) state.scopes.pop()
  69. if (ch === ';') {
  70. while (scope === 'bodied') {
  71. state.scopes.pop()
  72. scope = currentScope(state)
  73. }
  74. }
  75. // look for ordered rules
  76. if (stream.match(/\d+ *#/, true, false)) {
  77. return 'qualifier'
  78. }
  79. // look for numbers
  80. if (stream.match(reFloatForm, true, false)) {
  81. return 'number'
  82. }
  83. // look for placeholders
  84. if (stream.match(rePattern, true, false)) {
  85. return 'variable-3'
  86. }
  87. // match all braces separately
  88. if (stream.match(/(?:\[|\]|{|}|\(|\))/, true, false)) {
  89. return 'bracket'
  90. }
  91. // literals looking like function calls
  92. if (stream.match(reFunctionLike, true, false)) {
  93. stream.backUp(1)
  94. return 'variable'
  95. }
  96. // all other identifiers
  97. if (stream.match(reIdentifier, true, false)) {
  98. return 'variable-2'
  99. }
  100. // operators; note that operators like @@ or /; are matched separately for each symbol.
  101. if (stream.match(/(?:\\|\+|\-|\*|\/|,|;|\.|:|@|~|=|>|<|&|\||_|`|'|\^|\?|!|%|#)/, true, false)) {
  102. return 'operator'
  103. }
  104. // everything else is an error
  105. return 'error'
  106. }
  107. function tokenString(stream, state) {
  108. var next,
  109. end = false,
  110. escaped = false
  111. while ((next = stream.next()) != null) {
  112. if (next === '"' && !escaped) {
  113. end = true
  114. break
  115. }
  116. escaped = !escaped && next === '\\'
  117. }
  118. if (end && !escaped) {
  119. state.tokenize = tokenBase
  120. }
  121. return 'string'
  122. }
  123. function tokenComment(stream, state) {
  124. var prev, next
  125. while ((next = stream.next()) != null) {
  126. if (prev === '*' && next === '/') {
  127. state.tokenize = tokenBase
  128. break
  129. }
  130. prev = next
  131. }
  132. return 'comment'
  133. }
  134. function currentScope(state) {
  135. var scope = null
  136. if (state.scopes.length > 0) scope = state.scopes[state.scopes.length - 1]
  137. return scope
  138. }
  139. return {
  140. startState: function () {
  141. return {
  142. tokenize: tokenBase,
  143. scopes: [],
  144. }
  145. },
  146. token: function (stream, state) {
  147. if (stream.eatSpace()) return null
  148. return state.tokenize(stream, state)
  149. },
  150. indent: function (state, textAfter) {
  151. if (state.tokenize !== tokenBase && state.tokenize !== null) return CodeMirror.Pass
  152. var delta = 0
  153. if (textAfter === ']' || textAfter === '];' || textAfter === '}' || textAfter === '};' || textAfter === ');') delta = -1
  154. return (state.scopes.length + delta) * _config.indentUnit
  155. },
  156. electricChars: '{}[]();',
  157. blockCommentStart: '/*',
  158. blockCommentEnd: '*/',
  159. lineComment: '//',
  160. }
  161. })
  162. CodeMirror.defineMIME('text/x-yacas', {
  163. name: 'yacas',
  164. })
  165. })