-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Pygment.ex
230 lines (209 loc) · 4.33 KB
/
Pygment.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Numbers
0b0101011
1234 ; 0x1A ; 0xbeef ; 0763 ; 0o123
3.14 ; 5.0e21 ; 0.5e-12
100_000_000
# these are not valid numbers
0b012 ; 0xboar ; 0o888
0B01 ; 0XAF ; 0O123
# Characters
?a ; ?1 ; ?\n ; ?\s ; ?\c ; ? ; ?,
?\x{12} ; ?\x{abcd}
?\x34 ; ?\xF
# these show that only the first digit is part of the character
?\123 ; ?\12 ; ?\7
# Atoms
:this ; :that
:'complex atom'
:"with' \"\" 'quotes"
:... ; :<<>> ; :%{} ; :% ; :{}
:++; :--; :*; :~~~; :::
:% ; :. ; :<-
# Strings
"Hello world"
"Interspersed \x{ff} codes \7 \8 \65 \016 and \t\s\\s\z\+ \\ escapes"
"Quotes ' inside \" \123 the \"\" \xF \\xF string \\\" end"
"Multiline
string"
# Char lists
'this is a list'
'escapes \' \t \\\''
'Multiline
char
list
'
# Binaries
<<1, 2, 3>>
<<"hello"::binary, c :: utf8, x::[4, unit(2)]>> = "hello™1"
# Sigils
~r/this + i\s "a" regex/
~R'this + i\s "a" regex too'
~w(hello #{ ["has" <> "123", '\c\d', "\123 interpol" | []] } world)s
~W(hello #{no "123" \c\d \123 interpol} world)s
~s{Escapes terminators \{ and \}, but no {balancing} # outside of sigil here }
~S"No escapes \s\t\n and no #{interpolation}"
:"atoms work #{"to" <> "o"}"
# Operators
x = 1 + 2.0 * 3
y = true and false; z = false or true
... = 144
... == !x && y || z
"hello" |> String.upcase |> String.downcase()
{^z, a} = {true, x}
# Free operators (added in 1.0.0)
p ~>> f = bind(p, f)
p1 ~> p2 = pair_right(p1, p2)
p1 <~ p2 = pair_left(p1, p2)
p1 <~> p2 = pair_both(p1, p2)
p |~> f = map(p, f)
p1 <|> p2 = either(p1, p2)
# Lists, tuples, maps, keywords
[1, :a, 'hello'] ++ [2, 3]
[:head | [?t, ?a, ?i, ?l]]
{:one, 2.0, "three"}
[...: "this", <<>>: "is", %{}: "a keyword", %: "list", {}: "too"]
["this is an atom too": 1, "so is this": 2]
[option: "value", key: :word]
[++: "operator", ~~~: :&&&]
map = %{shortcut: "syntax"}
%{map | "update" => "me"}
%{ 12 => 13, :weird => ['thing'] }
# Comprehensions
for x <- 1..10, x < 5, do: {x, x}
pixels = "12345678"
for << <<r::4, g::4, b::4, a::size(4)>> <- pixels >> do
[r, {g, %{"b" => a}}]
end
# String interpolation
"String #{inspect "interpolation"} is quite #{1+4+7} difficult"
# Identifiers
abc_123 = 1
_018OP = 2
A__0 == 3
## Modules
#defmodule Long.Module.Name do
# @moduledoc "Simple module docstring"
#
# @doc """
# Multiline docstring
# "with quotes"
# and #{ inspect %{"interpolation" => "in" <> "action"} }
# now with #{ {:a, 'tuple'} }
# and #{ inspect {
# :tuple,
# %{ with: "nested #{ inspect %{ :interpolation => %{} } }" }
# } }
# """
# defstruct [:a, :name, :height]
#
# @doc ~S'''
# No #{interpolation} of any kind.
# \000 \x{ff}
#
# \n #{\x{ff}}
# '''
# def func(a, b \\ []), do: :ok
#
# @doc false
# def __before_compile__(_) do
# :ok
# end
#end
#
## Structs
#defmodule Second.Module do
# s = %Long.Module.Name{name: "Silly"}
# %Long.Module.Name{s | height: {192, :cm}}
# ".. #{%Long.Module.Name{s | height: {192, :cm}}} .."
#end
#
## Types, pseudo-vars, attributes
#defmodule M do
# @custom_attr :some_constant
#
# @before_compile Long.Module.Name
#
# @typedoc "This is a type"
# @type typ :: integer
#
# @typedoc """
# Another type
# """
# @opaque typtyp :: 1..10
#
# @spec func(typ, typtyp) :: :ok | :fail
# def func(a, b) do
# a || b || :ok || :fail
# Path.expand("..", __DIR__)
# IO.inspect __ENV__
# __NOTAPSEUDOVAR__ = 11
# __MODULE__.func(b, a)
# end
#
# defmacro m() do
# __CALLER__
# end
#end
#
## Functions
#anon = fn x, y, z ->
# fn(a, b, c) ->
# &(x + y - z * a / &1 + b + div(&2, c))
# end
#end
#
#&Set.put(&1, &2) ; & Set.put(&1, &2) ; &( Set.put(&1, &1) )
#
## Function calls
#anon.(1, 2, 3); self; hd([1,2,3])
#Kernel.spawn(fn -> :ok end)
#IO.ANSI.black
#
## Control flow
#if :this do
# :that
#else
# :otherwise
#end
#
#pid = self
#receive do
# {:EXIT, _} -> :done
# {^pid, :_} -> nil
# after 100 -> :no_luck
#end
#
#case __ENV__.line do
# x when is_integer(x) -> x
# x when x in 1..12 -> -x
#end
#
#cond do
# false -> "too bad"
# 4 > 5 -> "oops"
# true -> nil
#end
#
## Lexical scope modifiers
#import Kernel, except: [spawn: 1, +: 2, /: 2, Unless: 2]
#alias Long.Module.Name, as: N0men123_and4
#use Bitwise
#
#4 &&& 5
#2 <<< 3
#
## Protocols
#defprotocol Useless do
# def func1(this)
# def func2(that)
#end
#
#defimpl Useless, for: Atom do
#end
#
## Exceptions
#defmodule NotAnError do
# defexception [:message]
#end
#
#raise NotAnError, message: "This is not an error"