Skip to content

Commit

Permalink
Added support for Stata's Ado and Mata (#3383)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Mar 21, 2022
1 parent ca78cde commit 63806d5
Show file tree
Hide file tree
Showing 33 changed files with 1,195 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@
"require": "markup",
"owner": "Golmote"
},
"mata": {
"title": "Mata",
"owner": "RunDevelopment"
},
"matlab": {
"title": "MATLAB",
"owner": "Golmote"
Expand Down Expand Up @@ -1313,6 +1317,11 @@
"title": "Stan",
"owner": "RunDevelopment"
},
"stata": {
"title": "Stata Ado",
"require": ["mata", "java", "python"],
"owner": "RunDevelopment"
},
"iecst": {
"title": "Structured Text (IEC 61131-3)",
"owner": "serhioromano"
Expand Down
50 changes: 50 additions & 0 deletions components/prism-mata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// https://www.stata.com/manuals/m.pdf

(function (Prism) {

var orgType = /\b(?:(?:col|row)?vector|matrix|scalar)\b/.source;
var type = /\bvoid\b|<org>|\b(?:complex|numeric|pointer(?:\s*\([^()]*\))?|real|string|(?:class|struct)\s+\w+|transmorphic)(?:\s*<org>)?/.source
.replace(/<org>/g, orgType);

Prism.languages.mata = {
'comment': {
pattern: /\/\/.*|\/\*(?:[^*/]|\*(?!\/)|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/)*\*\//,
greedy: true
},
'string': {
pattern: /"[^"\r\n]*"|[‘`']".*?"[’`']/,
greedy: true
},

'class-name': {
pattern: /(\b(?:class|extends|struct)\s+)\w+(?=\s*(?:\{|\bextends\b))/,
lookbehind: true
},
'type': {
pattern: RegExp(type),
alias: 'class-name',
inside: {
'punctuation': /[()]/,
'keyword': /\b(?:class|function|struct|void)\b/
}
},
'keyword': /\b(?:break|class|continue|do|else|end|extends|external|final|for|function|goto|if|pragma|private|protected|public|return|static|struct|unset|unused|version|virtual|while)\b/,
'constant': /\bNULL\b/,

'number': {
pattern: /(^|[^\w.])(?:\d+(?:\.\d+)?(?:e[+-]?\d+)?|\d[a-f0-9]*(?:\.[a-f0-9]+)?x[+-]?\d+)i?(?![\w.])/i,
lookbehind: true
},
'missing': {
pattern: /(^|[^\w.])(?:\.[a-z]?)(?![\w.])/,
lookbehind: true,
alias: 'symbol'
},

'function': /\b[a-z_]\w*(?=\s*\()/i,

'operator': /\.\.|\+\+|--|&&|\|\||:?(?:[!=<>]=|[+\-*/^<>&|:])|[!?=\\#’`']/,
'punctuation': /[()[\]{},;.]/
};

}(Prism));
1 change: 1 addition & 0 deletions components/prism-mata.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions components/prism-stata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// https://www.stata.com/manuals/u.pdf
// https://www.stata.com/manuals/p.pdf

Prism.languages.stata = {
'comment': [
{
pattern: /(^[ \t]*)\*.*/m,
lookbehind: true,
greedy: true
},
{
pattern: /(^|\s)\/\/.*|\/\*[\s\S]*?\*\//,
lookbehind: true,
greedy: true
}
],
'string-literal': {
pattern: /"[^"\r\n]*"|[‘`']".*?"[’`']/,
greedy: true,
inside: {
'interpolation': {
pattern: /\$\{[^{}]*\}|[‘`']\w[^’`'\r\n]*[’`']/,
inside: {
'punctuation': /^\$\{|\}$/,
'expression': {
pattern: /[\s\S]+/,
inside: null // see below
}
}
},
'string': /[\s\S]+/
}
},

'mata': {
pattern: /(^[ \t]*mata[ \t]*:)[\s\S]+?(?=^end\b)/m,
lookbehind: true,
greedy: true,
alias: 'language-mata',
inside: Prism.languages.mata
},
'java': {
pattern: /(^[ \t]*java[ \t]*:)[\s\S]+?(?=^end\b)/m,
lookbehind: true,
greedy: true,
alias: 'language-java',
inside: Prism.languages.java
},
'python': {
pattern: /(^[ \t]*python[ \t]*:)[\s\S]+?(?=^end\b)/m,
lookbehind: true,
greedy: true,
alias: 'language-python',
inside: Prism.languages.python
},


'command': {
pattern: /(^[ \t]*(?:\.[ \t]+)?(?:(?:bayes|bootstrap|by|bysort|capture|collect|fmm|fp|frame|jackknife|mfp|mi|nestreg|noisily|permute|quietly|rolling|simulate|statsby|stepwise|svy|version|xi)\b[^:\r\n]*:[ \t]*|(?:capture|noisily|quietly|version)[ \t]+)?)[a-zA-Z]\w*/m,
lookbehind: true,
greedy: true,
alias: 'keyword'
},
'variable': /\$\w+|[‘`']\w[^’`'\r\n]*[’`']/,
'keyword': /\b(?:bayes|bootstrap|by|bysort|capture|clear|collect|fmm|fp|frame|if|in|jackknife|mi[ \t]+estimate|mfp|nestreg|noisily|of|permute|quietly|rolling|simulate|sort|statsby|stepwise|svy|varlist|version|xi)\b/,


'boolean': /\b(?:off|on)\b/,
'number': /\b\d+(?:\.\d+)?\b|\B\.\d+/,
'function': /\b[a-z_]\w*(?=\()/i,

'operator': /\+\+|--|##?|[<>!=~]=?|[+\-*^&|/]/,
'punctuation': /[(){}[\],:]/
};

Prism.languages.stata['string-literal'].inside.interpolation.inside.expression.inside = Prism.languages.stata;
1 change: 1 addition & 0 deletions components/prism-stata.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions examples/prism-mata.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h2>Full example</h2>
<pre><code>// Source: https://www.stata.com/manuals/m-1first.pdf page 12
numeric matrix tanh(numeric matrix u)
{
numeric matrix eu, emu
eu = exp(u)
emu = exp(-u)
return( (eu-emu):/(eu+emu) )
}</code></pre>
13 changes: 13 additions & 0 deletions examples/prism-stata.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2>Full example</h2>
<pre><code>// Source: https://blog.stata.com/2018/10/09/how-to-automate-common-tasks/
program normalize
version 15.1

syntax varlist [if] [in] [ , prefix(name) suffix(name) ]

foreach var in `varlist' {
summarize `var' `if' `in'
generate `prefix'`var'`suffix' = (`var' - r(mean)) / r(sd) `if' `in'
}
end
</code></pre>
5 changes: 5 additions & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
"sparql": "turtle",
"sqf": "clike",
"squirrel": "clike",
"stata": [
"mata",
"java",
"python"
],
"t4-cs": [
"t4-templating",
"csharp"
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
"splunk-spl": "Splunk SPL",
"sqf": "SQF: Status Quo Function (Arma 3)",
"sql": "SQL",
"stata": "Stata Ado",
"iecst": "Structured Text (IEC 61131-3)",
"supercollider": "SuperCollider",
"sclang": "SuperCollider",
Expand Down
Loading

0 comments on commit 63806d5

Please sign in to comment.