Skip to content

Commit 55cc29f

Browse files
committed
Elixir: match arrow clauses using the branch feature
1 parent db5c6a4 commit 55cc29f

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

syntaxes/Elixir.sublime-syntax

+25-27
Original file line numberDiff line numberDiff line change
@@ -120,47 +120,45 @@ contexts:
120120
fn_block:
121121
- match: fn{{no_id_key_suffix}}
122122
scope: punctuation.section.block.begin.elixir keyword.other.fn.elixir
123-
push: [fn_block_end_pop, arrow_clauses_body_pop, fn_single_body_or_pop]
124-
125-
fn_single_body_or_pop:
126-
- match: (?=->)
127-
set:
128-
- match: (?=end{{no_id_key_suffix}})
129-
pop: 1
130-
- include: if_closing_eex_pop
131-
- include: core_syntax
132-
- include: if_non_space_or_eol_pop
123+
push: [fn_block_end_pop, arrow_clauses_body_pop]
133124
134125
fn_block_end_pop:
135126
- include: block_end_pop
136127
- include: core_syntax_or_if_closing_pop
137128
138129
arrow_clauses_body_pop:
130+
- include: if_closing_token_pop
139131
- match: (?=\S)
140-
set:
141-
- match: (?=->|when{{no_id_key_suffix}})
142-
push: inlined_core_syntax_pop
143-
# NB: no default parameters in arrow clauses
144-
- match: \\\\(?!:{{no_colon_suffix}})
145-
scope: keyword.operator.default.elixir invalid.illegal.default-operator.elixir
146-
- include: parameters_or_if_closing_pop
147-
- match: $
148-
set:
149-
- include: if_closing_token_pop
150-
- match: ^(\s*)(?=[^#\s])
151-
push: [indented_core_syntax_pop, params_until_arrow_pop]
152-
- include: core_syntax
132+
branch_point: non_arrow_clause_branch
133+
branch: [non_arrow_core_syntax_pop, params_until_arrow_pop]
153134
154-
inlined_core_syntax_pop:
155-
- match: (?=;)
135+
non_arrow_core_syntax_pop:
136+
- match: (?=->(?!:)|when{{no_id_key_suffix}})
137+
fail: non_arrow_clause_branch
138+
- match: (?={{operator}}(?!:))
139+
push:
140+
- include: operator
141+
- include: if_non_space_pop
142+
- match: (?=;|$)
156143
pop: 1
157144
- include: core_syntax_or_if_closing_pop
158145
159146
params_until_arrow_pop:
160-
- match: (?=->|when{{no_id_key_suffix}})
161-
pop: 1
147+
- match: (?=when{{no_id_key_suffix}})
148+
set:
149+
- include: arrow_operator_pop
150+
- include: core_syntax_or_if_closing_pop
151+
- include: arrow_operator_pop
152+
- match: \\\\(?!:{{no_colon_suffix}})
153+
comment: no default parameters in arrow clauses
154+
scope: keyword.operator.default.elixir invalid.illegal.default-operator.elixir
162155
- include: parameters_or_if_closing_pop
163156
157+
arrow_operator_pop:
158+
- match: ->(?!:)
159+
scope: keyword.operator.arrow.elixir
160+
pop: 1
161+
164162
indented_core_syntax_pop:
165163
- match: ^(?=\1[^#\s]|(?!\1)(?!\s*(#|$)))
166164
pop: 1

tests/syntax_test_misc.ex

+24-15
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
#^ invalid.illegal.stray-closing-brace
119119

120120
(fn -> ) end)
121-
# ^ invalid.illegal.stray-closing-parenthesis
121+
# ^ invalid.illegal.stray-closing-parenthesis
122122
fn -> end
123123
# ^^^ punctuation.section.block.end keyword.context.block.end
124124
# ^^ keyword.operator.arrow
@@ -162,8 +162,17 @@
162162
# ^ variable.other -variable.parameter
163163
# ^ variable.parameter
164164

165+
fn x when
166+
# ^^^^ keyword.operator.when
167+
# ^ variable.parameter
168+
x -> x end
169+
# ^^^ keyword.context.block.end
170+
# ^ variable.other
171+
# ^ variable.other
172+
165173
fn
166-
[], acc -> acc
174+
[], acc \\ [] -> acc
175+
# ^^ keyword.operator.default invalid.illegal.default-operator
167176
# ^ punctuation.separator.sequence
168177
x, acc -> [x | acc]
169178
# ^ punctuation.separator.sequence
@@ -215,14 +224,14 @@ y -> y
215224
z -> z
216225
# ^ variable.other -variable.parameter
217226
# ^^ keyword.operator.arrow
218-
#<- variable.other -variable.parameter
227+
#<- variable.parameter
219228
end
220229
#^^ punctuation.section.block.end
221230
fn -> x; y -> z end
222231
# ^^^ punctuation.section.block.end
223232
# ^ variable.other
224233
# ^^ keyword.operator.arrow
225-
# ^ -variable.parameter
234+
# ^ variable.parameter
226235
# ^ variable.other
227236
# ^^ keyword.operator.arrow
228237
fn x -> x; y -> y end
@@ -282,7 +291,7 @@ end
282291
-> var -> var
283292
# ^^^ variable.other
284293
# ^^ keyword.operator.arrow
285-
# ^^^ variable.other
294+
# ^^^ variable.parameter
286295
#^^ keyword.operator.arrow
287296
expr
288297
#^^^ variable.other
@@ -1252,8 +1261,8 @@ fn a,,b -> end
12521261
# ^ punctuation.section.group.end
12531262
# ^ invalid.illegal.stray-closing-parenthesis
12541263
( fn -> ) end )
1255-
# ^ punctuation.section.group.end
1256-
# ^ invalid.illegal.stray-closing-parenthesis
1264+
# ^ invalid.illegal.stray-closing-parenthesis
1265+
# ^ punctuation.section.group.end
12571266

12581267
[ ( ] )
12591268
# ^ invalid.illegal.stray-closing-parenthesis
@@ -1274,8 +1283,8 @@ fn a,,b -> end
12741283
# ^ punctuation.section.brackets.end
12751284
# ^ invalid.illegal.stray-closing-bracket
12761285
[ fn -> ] end ]
1277-
# ^ punctuation.section.brackets.end
1278-
# ^ invalid.illegal.stray-closing-bracket
1286+
# ^ invalid.illegal.stray-closing-bracket
1287+
# ^ punctuation.section.brackets.end
12791288

12801289
{ ( } )
12811290
# ^ invalid.illegal.stray-closing-parenthesis
@@ -1296,8 +1305,8 @@ fn a,,b -> end
12961305
# ^ punctuation.section.sequence.end
12971306
# ^ invalid.illegal.stray-closing-brace
12981307
{ fn -> } end }
1299-
# ^ punctuation.section.sequence.end
1300-
# ^ invalid.illegal.stray-closing-brace
1308+
# ^ invalid.illegal.stray-closing-brace
1309+
# ^ punctuation.section.sequence.end
13011310

13021311
%{ ( } )
13031312
# ^ invalid.illegal.stray-closing-parenthesis
@@ -1318,8 +1327,8 @@ fn a,,b -> end
13181327
# ^ punctuation.section.mapping.end
13191328
# ^ invalid.illegal.stray-closing-brace
13201329
%{ fn -> } end }
1321-
# ^ punctuation.section.mapping.end
1322-
# ^ invalid.illegal.stray-closing-brace
1330+
# ^ invalid.illegal.stray-closing-brace
1331+
# ^ punctuation.section.mapping.end
13231332

13241333
<< ( >> )
13251334
# ^ invalid.illegal.stray-closing-parenthesis
@@ -1340,8 +1349,8 @@ fn a,,b -> end
13401349
# ^^ punctuation.definition.string.end
13411350
# ^^ invalid.illegal.stray-closing-binary
13421351
<< fn -> >> end >>
1343-
# ^^ punctuation.definition.string.end
1344-
# ^^ invalid.illegal.stray-closing-binary
1352+
# ^^ invalid.illegal.stray-closing-binary
1353+
# ^^ punctuation.definition.string.end
13451354

13461355
do ( end )
13471356
# ^ invalid.illegal.stray-closing-parenthesis

0 commit comments

Comments
 (0)