Skip to content

Commit

Permalink
Added support for ReScript (#3435)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarcosp committed Apr 15, 2022
1 parent c2d2c4b commit cbef9af
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Expand Up @@ -1212,6 +1212,11 @@
"alias": "rpy",
"owner": "HyuchiaDiego"
},
"rescript": {
"title": "ReScript",
"alias": "res",
"owner": "vmarcosp"
},
"rest": {
"title": "reST (reStructuredText)",
"owner": "Golmote"
Expand Down
60 changes: 60 additions & 0 deletions components/prism-rescript.js
@@ -0,0 +1,60 @@
Prism.languages.rescript = {
'comment': {
pattern: /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
greedy: true
},
'char': { pattern: /'(?:[^\r\n\\]|\\(?:.|\w+))'/, greedy: true },
'string': {
pattern: /"(?:\\(?:\r\n|[\s\S])|[^\\\r\n"])*"/,
greedy: true
},
'class-name': /\b[A-Z]\w*|@[a-z.]*|#[A-Za-z]\w*|#\d/,
'function': {
pattern: /[a-zA-Z]\w*(?=\()|(\.)[a-z]\w*/,
lookbehind: true,
},
'number': /(?:\b0x(?:[\da-f]+(?:\.[\da-f]*)?|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?)[ful]{0,4}/i,
'boolean': /\b(?:false|true)\b/,
'attr-value': /[A-Za-z]\w*(?==)/,
'constant': {
pattern: /(\btype\s+)[a-z]\w*/,
lookbehind: true
},
'tag': {
pattern: /(<)[a-z]\w*|(?:<\/)[a-z]\w*/,
lookbehind: true,
inside: {
'operator': /<|>|\//,
},
},
'keyword': /\b(?:and|as|assert|begin|bool|class|constraint|do|done|downto|else|end|exception|external|float|for|fun|function|if|in|include|inherit|initializer|int|lazy|let|method|module|mutable|new|nonrec|object|of|open|or|private|rec|string|switch|then|to|try|type|when|while|with)\b/,
'operator': /\.{3}|:[:=]?|\|>|->|=(?:==?|>)?|<=?|>=?|[|^?'#!~`]|[+\-*\/]\.?|\b(?:asr|land|lor|lsl|lsr|lxor|mod)\b/,
'punctuation': /[(){}[\],;.]/
};

Prism.languages.insertBefore('rescript', 'string', {
'template-string': {
pattern: /`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,
greedy: true,
inside: {
'template-punctuation': {
pattern: /^`|`$/,
alias: 'string'
},
'interpolation': {
pattern: /((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,
lookbehind: true,
inside: {
'interpolation-punctuation': {
pattern: /^\$\{|\}$/,
alias: 'tag'
},
rest: Prism.languages.rescript
}
},
'string': /[\s\S]+/
}
},
});

Prism.languages.res = Prism.languages.rescript;
1 change: 1 addition & 0 deletions components/prism-rescript.min.js

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

52 changes: 52 additions & 0 deletions examples/prism-rescript.html
@@ -0,0 +1,52 @@
<h2>Comments</h2>
<pre><code>/* This is a comment */
// Another comment
</code></pre>

<h2>Let bindings</h2>
<pre><code>let name = "Alonzo Church"
let message = `Hello ${name}`
let message2 = `Hello ${person.name}`
let result = `Testing => ${Module.Another.value}`
let value = 1
let result = 1 + 1
let langs = ["ReScript", "JavaScript"]
let person = ("Alonzo", 32)</code></pre>

<h2>Type declarations & Functions</h2>
<pre><code>type role = | Admin | Editor | Viewer
type numeric = #1 | #2 | #3
type normal = #Poly | #Variant
type myPolyVar = [ #"poly-variant" | #"test-chars" ]

type person = {
name: string,
age: int,
role: role
}

type record = {
"field": string
}

let sum = (a, b) => a + b
let sum2 = (a:int, b: int) => a + b</code></pre>

<h2>Modules, JSX & Components</h2>
<pre><code>%%raw(``)
module Button = {
@react.component
let make = (~count: int) =>{
let times = switch count {
| 1 => "once"
| 2 => "twice"
| n => Belt.Int.toString(n) ++ " times"
}
let msg = "Click me " ++ times

&lt;button onClick={Js.log}>{msg->React.string}&lt;/button>;
}
}

@react.component
let make = () => &lt;MyModule.Button count=1 /></code></pre>
1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Expand Up @@ -244,6 +244,7 @@
"rkt": "racket",
"razor": "cshtml",
"rpy": "renpy",
"res": "rescript",
"robot": "robotframework",
"rb": "ruby",
"sh-session": "shell-session",
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
Expand Up @@ -213,6 +213,7 @@
"tsx": "React TSX",
"renpy": "Ren'py",
"rpy": "Ren'py",
"res": "ReScript",
"rest": "reST (reStructuredText)",
"robotframework": "Robot Framework",
"robot": "Robot Framework",
Expand Down

0 comments on commit cbef9af

Please sign in to comment.