Skip to content

Commit d6da70e

Browse files
zwiliasGolmote
authored andcommitted
Add Elm (elm-lang.org) support (#1174)
* Add Elm (elm-lang.org) support * Simplify patterns and fix a few things inherited from haskell * Add example and minified file * Clean up comment pattern, drop redundant groups, tabs for indents
1 parent bf9a314 commit d6da70e

14 files changed

+373
-0
lines changed

components.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ var components = {
197197
"title": "Elixir",
198198
"owner": "Golmote"
199199
},
200+
"elm": {
201+
"title": "Elm",
202+
"owner": "zwilias"
203+
},
200204
"erlang": {
201205
"title": "Erlang",
202206
"owner": "Golmote"

components/prism-elm.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Prism.languages.elm = {
2+
comment: /--.*|{-[\s\S]*?-}/,
3+
char: {
4+
pattern: /'(?:[^\\'\r\n]|\\(?:[abfnrtv\\']|\d+|x[0-9a-fA-F]+))'/,
5+
greedy: true
6+
},
7+
string: [
8+
{
9+
// Multiline strings are wrapped in triple ". Quotes may appear unescaped.
10+
pattern: /"""[\s\S]*?"""/,
11+
greedy: true
12+
},
13+
{
14+
pattern: /"(?:[^\\"\r\n]|\\(?:[abfnrtv\\"]|\d+|x[0-9a-fA-F]+))*"/,
15+
greedy: true
16+
}
17+
],
18+
import_statement: {
19+
// The imported or hidden names are not included in this import
20+
// statement. This is because we want to highlight those exactly like
21+
// we do for the names in the program.
22+
pattern: /^\s*import\s+[A-Z]\w*(?:\.[A-Z]\w*)*(?:\s+as\s+([A-Z]\w*)(?:\.[A-Z]\w*)*)?(?:\s+exposing\s+)?/m,
23+
inside: {
24+
keyword: /\b(?:import|as|exposing)\b/
25+
}
26+
},
27+
keyword: /\b(?:alias|as|case|else|exposing|if|in|infixl|infixr|let|module|of|then|type)\b/,
28+
// These are builtin variables only. Constructors are highlighted later as a constant.
29+
builtin: /\b(?:abs|acos|always|asin|atan|atan2|ceiling|clamp|compare|cos|curry|degrees|e|flip|floor|fromPolar|identity|isInfinite|isNaN|logBase|max|min|negate|never|not|pi|radians|rem|round|sin|sqrt|tan|toFloat|toPolar|toString|truncate|turns|uncurry|xor)\b/,
30+
// decimal integers and floating point numbers | hexadecimal integers
31+
number: /\b(?:\d+(?:\.\d+)?(?:e[+-]?\d+)?|0x[0-9a-f]+)\b/i,
32+
// Most of this is needed because of the meaning of a single '.'.
33+
// If it stands alone freely, it is the function composition.
34+
// It may also be a separator between a module name and an identifier => no
35+
// operator. If it comes together with other special characters it is an
36+
// operator too.
37+
// Valid operator characters in 0.18: +-/*=.$<>:&|^?%#@~!
38+
// Ref: https://groups.google.com/forum/#!msg/elm-dev/0AHSnDdkSkQ/E0SVU70JEQAJ
39+
operator: /\s\.\s|[+\-/*=.$<>:&|^?%#@~!]{2,}|[+\-/*=$<>:&|^?%#@~!]/,
40+
// In Elm, nearly everything is a variable, do not highlight these.
41+
hvariable: /\b(?:[A-Z]\w*\.)*[a-z]\w*\b/,
42+
constant: /\b(?:[A-Z]\w*\.)*[A-Z]\w*\b/,
43+
punctuation: /[{}[\]|(),.:]/
44+
};

components/prism-elm.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-elm.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<h1>Elm</h1>
2+
<p>To user this language, use the class "language-elm".</p>
3+
4+
<h2>Comments</h2>
5+
<pre><code>-- Single line comment
6+
{- Multi-line
7+
comment -}</code></pre>
8+
9+
<h2>Strings and characters</h2>
10+
<pre><code>'a'
11+
'\n'
12+
'\x03'
13+
"foo \" bar"
14+
"""
15+
"multiline strings" are also
16+
supported!
17+
"""</code></pre>
18+
19+
<h2>Full example</h2>
20+
<pre><code>module Main exposing (..)
21+
22+
import Html exposing (Html)
23+
import Svg exposing (..)
24+
import Svg.Attributes exposing (..)
25+
import Time exposing (Time, second)
26+
27+
28+
main =
29+
Html.program
30+
{ init = init
31+
, view = view
32+
, update = update
33+
, subscriptions = subscriptions
34+
}
35+
36+
37+
38+
-- MODEL
39+
40+
41+
type alias Model =
42+
Time
43+
44+
45+
init : ( Model, Cmd Msg )
46+
init =
47+
( 0, Cmd.none )
48+
49+
50+
51+
-- UPDATE
52+
53+
54+
type Msg
55+
= Tick Time
56+
57+
58+
update : Msg -> Model -> ( Model, Cmd Msg )
59+
update msg model =
60+
case msg of
61+
Tick newTime ->
62+
( newTime, Cmd.none )
63+
64+
65+
66+
-- SUBSCRIPTIONS
67+
68+
69+
subscriptions : Model -> Sub Msg
70+
subscriptions model =
71+
Time.every second (\time -> Tick time)
72+
73+
74+
75+
-- VIEW
76+
77+
78+
view : Model -> Html Msg
79+
view model =
80+
let
81+
angle =
82+
turns (Time.inMinutes model)
83+
84+
handX =
85+
toString (50 + 40 * cos angle)
86+
87+
handY =
88+
toString (50 + 40 * sin angle)
89+
in
90+
svg [ viewBox "0 0 100 100", width "300px" ]
91+
[ circle [ cx "50", cy "50", r "45", fill "#0B79CE" ] []
92+
, line [ x1 "50", y1 "50", x2 handX, y2 handY, stroke "#023963" ] []
93+
]
94+
</code></pre>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
abs acos always asin atan atan2
2+
ceiling clamp compare cos curry
3+
degrees e flip floor fromPolar
4+
identity isInfinite isNaN
5+
logBase max min negate never
6+
not pi radians rem round sin
7+
sqrt tan toFloat toPolar toString
8+
truncate turns uncurry xor
9+
10+
----------------------------------------------------
11+
12+
[
13+
["builtin", "abs"], ["builtin", "acos"], ["builtin", "always"], ["builtin", "asin"], ["builtin", "atan"], ["builtin", "atan2"],
14+
["builtin", "ceiling"], ["builtin", "clamp"], ["builtin", "compare"], ["builtin", "cos"], ["builtin", "curry"],
15+
["builtin", "degrees"], ["builtin", "e"], ["builtin", "flip"], ["builtin", "floor"], ["builtin", "fromPolar"],
16+
["builtin", "identity"], ["builtin", "isInfinite"], ["builtin", "isNaN"],
17+
["builtin", "logBase"], ["builtin", "max"], ["builtin", "min"], ["builtin", "negate"], ["builtin", "never"],
18+
["builtin", "not"], ["builtin", "pi"], ["builtin", "radians"], ["builtin", "rem"], ["builtin", "round"], ["builtin", "sin"],
19+
["builtin", "sqrt"], ["builtin", "tan"], ["builtin", "toFloat"], ["builtin", "toPolar"], ["builtin", "toString"],
20+
["builtin", "truncate"], ["builtin", "turns"], ["builtin", "uncurry"], ["builtin", "xor"]
21+
]
22+
23+
----------------------------------------------------
24+
25+
Checks for all builtin.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'a'
2+
'\''
3+
'\n'
4+
'\23'
5+
'\xFE'
6+
7+
----------------------------------------------------
8+
9+
[
10+
["char", "'a'"],
11+
["char", "'\\''"],
12+
["char", "'\\n'"],
13+
["char", "'\\23'"],
14+
["char", "'\\xFE'"]
15+
]
16+
17+
----------------------------------------------------
18+
19+
Checks for chars.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- foo
2+
{- foo
3+
bar -}
4+
5+
----------------------------------------------------
6+
7+
[
8+
["comment", "-- foo"],
9+
["comment", "{- foo\r\nbar -}"]
10+
]
11+
12+
----------------------------------------------------
13+
14+
Checks for single-line and multi-line comments.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Foo
2+
Foo.Bar
3+
Baz.Foobar_42
4+
5+
----------------------------------------------------
6+
7+
[
8+
["constant", "Foo"],
9+
["constant", "Foo.Bar"],
10+
["constant", "Baz.Foobar_42"]
11+
]
12+
13+
----------------------------------------------------
14+
15+
Checks for constants.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
foo
2+
Foo.bar
3+
Baz.foobar_42
4+
5+
----------------------------------------------------
6+
7+
[
8+
["hvariable", "foo"],
9+
["hvariable", "Foo.bar"],
10+
["hvariable", "Baz.foobar_42"]
11+
]
12+
13+
----------------------------------------------------
14+
15+
Checks for hvariables.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Foo
2+
import Foo_42.Bar as Foobar
3+
import Foo.Bar as Foo.Baz
4+
import List exposing (map)
5+
import Json.Decode as Json exposing (Decoder)
6+
7+
----------------------------------------------------
8+
9+
[
10+
["import_statement", [
11+
["keyword", "import"],
12+
" Foo"
13+
]],
14+
["import_statement", [
15+
["keyword", "import"],
16+
" Foo_42.Bar ",
17+
["keyword", "as"],
18+
" Foobar"
19+
]],
20+
["import_statement", [
21+
["keyword", "import"],
22+
" Foo.Bar ",
23+
["keyword", "as"],
24+
" Foo.Baz"
25+
]],
26+
["import_statement", [
27+
["keyword", "import"],
28+
" List ",
29+
["keyword", "exposing"]
30+
]],
31+
["punctuation", "("],
32+
["hvariable", "map"],
33+
["punctuation", ")"],
34+
["import_statement", [
35+
["keyword", "import"],
36+
" Json.Decode ",
37+
["keyword", "as"],
38+
" Json ",
39+
["keyword", "exposing"]
40+
]],
41+
["punctuation", "("],
42+
["constant", "Decoder"],
43+
["punctuation", ")"]
44+
]
45+
46+
----------------------------------------------------
47+
48+
Checks for import statement.

0 commit comments

Comments
 (0)