Skip to content

Commit 0803c58

Browse files
committed
Add IPs and CIDR in strings
1 parent 6a5135f commit 0803c58

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Terraform.sublime-syntax

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,29 @@ variables:
121121
| filemd1
122122
)
123123
124+
# IPs and CIDR
125+
## IPv6 pattern stolen lovingly from https://github.com/tijn/hosts.tmLanguage,
126+
## where Michael Lyons adapted them from David M. Szydek https://stackoverflow.com/a/17871737.
127+
zero_to_32: (?:3[0-2]|[12][0-9]|[0-9])
128+
zero_to_128: (?:12[0-8]|1[01][0-9]|[1-9][0-9]|[0-9])
129+
zero_to_255: (?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9][0-9])|(?:[1-9][0-9])|[0-9])
130+
ipv4: (?:(?:{{zero_to_255}}\.){3}{{zero_to_255}})
131+
ipv6: |-
132+
(?xi:
133+
(?:::(?:ffff(?::0{1,4}){0,1}:){0,1}{{ipv4}}) # ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
134+
|(?:(?:[0-9a-f]{1,4}:){1,4}:{{ipv4}}) # 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)
135+
|(?:fe80:(?::[0-9a-f]{1,4}){0,4}%[0-9a-z]{1,}) # fe80::7:8%eth0 fe80::7:8%1 (link-local IPv6 addresses with zone index)
136+
|(?:(?:[0-9a-f]{1,4}:){7,7} [0-9a-f]{1,4}) # 1:2:3:4:5:6:7:8
137+
| (?:[0-9a-f]{1,4}: (?::[0-9a-f]{1,4}){1,6}) # 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
138+
|(?:(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}) # 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
139+
|(?:(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}) # 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
140+
|(?:(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}) # 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
141+
|(?:(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}) # 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
142+
|(?:(?:[0-9a-f]{1,4}:){1,6} :[0-9a-f]{1,4}) # 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
143+
|(?:(?:[0-9a-f]{1,4}:){1,7} :) # 1:: 1:2:3:4:5:6:7::
144+
|(?::(?:(?::[0-9a-f]{1,4}){1,7}|:)) # ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
145+
)
146+
124147
contexts:
125148
main:
126149
- include: comments
@@ -294,6 +317,8 @@ contexts:
294317
scope: constant.character.escape.terraform
295318
- include: string-interpolation
296319
- include: aws-acl
320+
- include: ip-addresses-with-cidr-at-start
321+
- include: ipv6-square-bracket-at-start
297322

298323
aws-acl:
299324
- match: (?=\barn:aws:)
@@ -319,6 +344,43 @@ contexts:
319344
- meta_content_scope: source.terraform
320345
- include: string-interpolation-body
321346

347+
ipv4-at-start:
348+
- match: \G{{ipv4}}\b
349+
scope: meta.number.integer.other.terraform constant.numeric.ip-address.v4.terraform
350+
351+
ipv6-at-start:
352+
- match: \G{{ipv6}}
353+
scope: meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
354+
355+
ipv6-square-bracket-at-start:
356+
- match: \G(\[){{ipv6}}(\])
357+
scope: meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
358+
captures:
359+
1: punctuation.definition.constant.begin.terraform
360+
2: punctuation.definition.constant.end.terraform
361+
362+
ip-addresses-at-start:
363+
- include: ipv6-at-start
364+
- include: ipv4-at-start
365+
366+
ipv4-with-cidr-at-start:
367+
- match: \G({{ipv4}})(?:(/)({{zero_to_32}}))?\b
368+
captures:
369+
1: meta.number.integer.other.terraform constant.numeric.ip-address.v4.terraform
370+
2: punctuation.separator.sequence.terraform
371+
3: constant.other.range.terraform
372+
373+
ipv6-with-cidr-at-start:
374+
- match: \G({{ipv6}})(?:(/)({{zero_to_128}})\b)?
375+
captures:
376+
1: meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
377+
2: punctuation.separator.sequence.terraform
378+
3: constant.other.range.terraform
379+
380+
ip-addresses-with-cidr-at-start:
381+
- include: ipv6-with-cidr-at-start
382+
- include: ipv4-with-cidr-at-start
383+
322384
# String Interpolation: ("${" | "${~") Expression ("}" | "~}"
323385
#
324386
# https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#templates

tests/syntax_test_scope.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,62 @@
256256
# ^^^^^^^^^^ constant.character.escape.terraform
257257
# ^ punctuation.definition.string.end.terraform
258258

259+
260+
/////
261+
// Matches IPs and CIDR
262+
/////
263+
264+
"1.2.3.4"
265+
# ^^^^^^^^^ meta.string.terraform string.quoted.double.terraform
266+
# ^ punctuation.definition.string.begin.terraform
267+
# ^^^^^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v4.terraform
268+
# ^ punctuation.definition.string.end.terraform
269+
270+
"1.2.3.4/32"
271+
# ^^^^^^^^^^^^ meta.string.terraform string.quoted.double.terraform
272+
# ^ punctuation.definition.string.begin.terraform
273+
# ^^^^^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v4.terraform
274+
# ^ punctuation.separator.sequence.terraform
275+
# ^^ constant.other.range.terraform
276+
# ^ punctuation.definition.string.end.terraform
277+
278+
"1.2.3.4/33"
279+
# ^^^^^^^^^^^^ meta.string.terraform string.quoted.double.terraform
280+
# ^ punctuation.definition.string.begin.terraform
281+
# ^^^^^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v4.terraform
282+
# ^^^ - constant - punctuation
283+
# ^ punctuation.definition.string.end.terraform
284+
285+
"256.2.3.4"
286+
# ^^^^^^^^^^^ - constant
287+
288+
"::1"
289+
# ^^^^^ meta.string.terraform string.quoted.double.terraform
290+
# ^ punctuation.definition.string.begin.terraform
291+
# ^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
292+
# ^ punctuation.definition.string.end.terraform
293+
294+
"1:2:3:4:5:6:7:8"
295+
# ^^^^^^^^^^^^^^^^^ meta.string.terraform string.quoted.double.terraform
296+
# ^ punctuation.definition.string.begin.terraform
297+
# ^^^^^^^^^^^^^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
298+
# ^ punctuation.definition.string.end.terraform
299+
300+
"1:2:3:4:5:6:7:8/128"
301+
# ^^^^^^^^^^^^^^^^^^^^^ meta.string.terraform string.quoted.double.terraform
302+
# ^ punctuation.definition.string.begin.terraform
303+
# ^^^^^^^^^^^^^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
304+
# ^ punctuation.separator.sequence.terraform
305+
# ^^^ constant.other.range.terraform
306+
# ^ punctuation.definition.string.end.terraform
307+
308+
"1:2:3:4:5:6:7:8/129"
309+
# ^^^^^^^^^^^^^^^^^^^^^ meta.string.terraform string.quoted.double.terraform
310+
# ^ punctuation.definition.string.begin.terraform
311+
# ^^^^^^^^^^^^^^^ meta.number.integer.other.terraform constant.numeric.ip-address.v6.terraform
312+
# ^^^^ - constant - punctuation
313+
# ^ punctuation.definition.string.end.terraform
314+
259315
/////////////////////////////////////////////////////////////////////
260316
// Identifiers
261317
/////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)