prism-stan.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (function (Prism) {
  2. // https://mc-stan.org/docs/2_28/reference-manual/bnf-grammars.html
  3. var higherOrderFunctions = /\b(?:algebra_solver|algebra_solver_newton|integrate_1d|integrate_ode|integrate_ode_bdf|integrate_ode_rk45|map_rect|ode_(?:adams|bdf|ckrk|rk45)(?:_tol)?|ode_adjoint_tol_ctl|reduce_sum|reduce_sum_static)\b/;
  4. Prism.languages.stan = {
  5. 'comment': /\/\/.*|\/\*[\s\S]*?\*\/|#(?!include).*/,
  6. 'string': {
  7. // String literals can contain spaces and any printable ASCII characters except for " and \
  8. // https://mc-stan.org/docs/2_24/reference-manual/print-statements-section.html#string-literals
  9. pattern: /"[\x20\x21\x23-\x5B\x5D-\x7E]*"/,
  10. greedy: true
  11. },
  12. 'directive': {
  13. pattern: /^([ \t]*)#include\b.*/m,
  14. lookbehind: true,
  15. alias: 'property'
  16. },
  17. 'function-arg': {
  18. pattern: RegExp(
  19. '(' +
  20. higherOrderFunctions.source +
  21. /\s*\(\s*/.source +
  22. ')' +
  23. /[a-zA-Z]\w*/.source
  24. ),
  25. lookbehind: true,
  26. alias: 'function'
  27. },
  28. 'constraint': {
  29. pattern: /(\b(?:int|matrix|real|row_vector|vector)\s*)<[^<>]*>/,
  30. lookbehind: true,
  31. inside: {
  32. 'expression': {
  33. pattern: /(=\s*)\S(?:\S|\s+(?!\s))*?(?=\s*(?:>$|,\s*\w+\s*=))/,
  34. lookbehind: true,
  35. inside: null // see below
  36. },
  37. 'property': /\b[a-z]\w*(?=\s*=)/i,
  38. 'operator': /=/,
  39. 'punctuation': /^<|>$|,/
  40. }
  41. },
  42. 'keyword': [
  43. {
  44. pattern: /\bdata(?=\s*\{)|\b(?:functions|generated|model|parameters|quantities|transformed)\b/,
  45. alias: 'program-block'
  46. },
  47. /\b(?:array|break|cholesky_factor_corr|cholesky_factor_cov|complex|continue|corr_matrix|cov_matrix|data|else|for|if|in|increment_log_prob|int|matrix|ordered|positive_ordered|print|real|reject|return|row_vector|simplex|target|unit_vector|vector|void|while)\b/,
  48. // these are functions that are known to take another function as their first argument.
  49. higherOrderFunctions
  50. ],
  51. 'function': /\b[a-z]\w*(?=\s*\()/i,
  52. 'number': /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:E[+-]?\d+(?:_\d+)*)?i?(?!\w)/i,
  53. 'boolean': /\b(?:false|true)\b/,
  54. 'operator': /<-|\.[*/]=?|\|\|?|&&|[!=<>+\-*/]=?|['^%~?:]/,
  55. 'punctuation': /[()\[\]{},;]/
  56. };
  57. Prism.languages.stan.constraint.inside.expression.inside = Prism.languages.stan;
  58. }(Prism));