Skip to content

Commit 1f010d5

Browse files
authored
Regular Expression fixes in class set and comment rules (#8)
* RegExp: fixed class_set, character_range and comment rules. * Tests: new checks for class_set, character_range and comment.
1 parent 89b3bad commit 1f010d5

File tree

2 files changed

+87
-28
lines changed

2 files changed

+87
-28
lines changed

Regular Expressions (Elixir).sublime-syntax

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ contexts:
2525
- include: subroutine_call
2626
- include: back_reference
2727
- include: assertion
28+
- include: comment
2829
- include: escape_sequence
2930
- include: class_set
3031
- include: inline_option
@@ -33,7 +34,6 @@ contexts:
3334
- include: operator
3435
- include: quantifier
3536
- include: dot_meta_char
36-
- include: comment
3737
- include: literal
3838

3939
quoted_sequence:
@@ -367,16 +367,11 @@ contexts:
367367
- match: \[:.*?:\]
368368
scope: invalid.illegal.posix-class.regexp.elixir
369369
set: unexpected_quantifier_pop
370-
- match: (\[)(\])
371-
scope: meta.set.regexp.elixir keyword.control.set.regexp.elixir invalid.illegal.set.regexp.elixir
372-
captures:
373-
1: punctuation.definition.set.begin.regexp.elixir
374-
2: punctuation.definition.set.end.regexp.elixir
375-
set: unexpected_quantifier_pop
376-
- match: (\[)(\^?)
370+
- match: (\[)(\^?)(]?)
377371
captures:
378372
1: keyword.control.set.regexp.elixir punctuation.definition.set.begin.regexp.elixir
379373
2: keyword.operator.negation.regexp.elixir
374+
3: meta.literal.regexp.elixir
380375
push:
381376
- meta_scope: meta.set.regexp.elixir
382377
- include: character_range
@@ -388,9 +383,9 @@ contexts:
388383
- include: literal
389384

390385
character_range:
391-
- match: (?=(\\[dsw]|\\x\h\h|\\\d+|\\c\p{ascii}|\\.|.)-\g<1>)
386+
- match: (?=(\\x\h\h?|\\x{\h+}|\\[0-7]{1,3}|\o{[0-7]+}|\\c\p{ascii}|\\.|[^\\])-(?!])\g<1>)
392387
push:
393-
- match: (?>(\\[dsw])|(\\x\h\h|\\\d+|\\c\p{ascii}|\\.|.))
388+
- match: (\\[dsw])|(\\x\h\h?|\\x{\h+}|\\[0-7]{1,3}|\o{[0-7]+}|\\c\p{ascii}|\\.|[^\\])
394389
scope: meta.character-range.regexp.elixir
395390
captures:
396391
1: invalid.illegal.range.regexp.elixir
@@ -400,7 +395,7 @@ contexts:
400395
scope: keyword.operator.range.regexp.elixir
401396
set:
402397
- meta_scope: meta.character-range.regexp.elixir
403-
- match: (?>(\\[dsw])|(\\x\h\h|\\\d+|\\c\p{ascii}|\\.|.))
398+
- match: (\\[dsw])|(\\x\h\h?|\\x{\h+}|\\[0-7]{1,3}|\o{[0-7]+}|\\c\p{ascii}|\\.|[^\\])
404399
captures:
405400
1: invalid.illegal.range.regexp.elixir
406401
2: constant.other.range.regexp.elixir
@@ -412,6 +407,9 @@ contexts:
412407
scope: invalid.illegal.escape-sequence.regexp.elixir
413408
- match: \\b
414409
scope: constant.character.escape.backspace.regexp.elixir
410+
# Inside a class set \x, \xh and \xhh are valid sequences.
411+
- match: \\x\h?\h?
412+
scope: constant.character.escape.hex.regexp.elixir
415413
- include: common_escape_sequence
416414

417415
# E.g: [:alpha:] or [:^alpha:]
@@ -465,7 +463,13 @@ contexts:
465463
scope: keyword.other.any.regexp.elixir
466464

467465
comment:
468-
- match: (?<!\\\s)(?<=\s)#
466+
- match: '(?=(\\{2})*\\ #)'
467+
push:
468+
- include: escape_sequence
469+
- match: '#'
470+
scope: meta.literal.regexp.elixir
471+
pop: true
472+
- match: (?<=\s)#
469473
push:
470474
- meta_scope: comment.line.number-sign.regexp.elixir
471475
- match: \n

syntax_test_regexp.ex

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#// ^^ invalid.illegal.general-category.regexp.elixir
6565
#// ^^ invalid.illegal.general-category.regexp.elixir
6666
#// ^^^ invalid.illegal.general-category.regexp.elixir
67-
#//^^^^^^^^^ constant.other.escape-sequence.general-category.regexp.elixir
67+
#//^^^^^^^^^^^^^ constant.other.escape-sequence.general-category.regexp.elixir
6868
~r"\K\R\X\?\*\+\.\x00\00\07\o\o{}\o{84}\o{0}\cX\cä\a\e\f\n\r\t"
6969
#//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant.character.escape
7070

@@ -73,19 +73,61 @@
7373
#//^^^^^^^^^^^^^^ keyword.control.anchor
7474

7575
# Class sets:
76-
# TODO:
77-
~r"[.+*?{1}()|^$][\N\b]"
78-
#// ^^ constant.character.escape.backspace.regexp.elixir
79-
#// ^^ invalid.illegal.escape-sequence.regexp.elixir
80-
#// ^^^^^^^^^^^^ meta.literal.regexp.elixir
81-
#//^^^^^^^^^^^^^^ meta.set.regexp.elixir
82-
~r"[[:>:]][]"
83-
#// ^^ invalid.illegal.set.regexp.elixir
76+
~r"[.+*?{1}()[|^$][\N\b\x\xf\xff\A\B\g\G\k\K\R\X\z\Z]"
77+
#// ^^^^^^^^^^^^^^^^^^^^ constant.character.escape.regexp.elixir
78+
#// ^^^^^^^^^ constant.character.escape.hex.regexp.elixir
79+
#// ^^ constant.character.escape.backspace.regexp.elixir
80+
#// ^^ invalid.illegal.escape-sequence.regexp.elixir
81+
#// ^^^^^^^^^^^^^ meta.literal.regexp.elixir
82+
#//^^^^^^^^^^^^^^^ meta.set.regexp.elixir
83+
~r"[[:>:]]"
8484
#// ^^^^^ invalid.deprecated.word-boundary.regexp.elixir
85+
~r"[a]][]][^]][^a]"
86+
#// ^ meta.literal.regexp.elixir
87+
#// ^ punctuation.definition.set.end.regexp.elixir
88+
#// ^ meta.literal.regexp.elixir
89+
#// ^ keyword.operator.negation.regexp.elixir
90+
#// ^ punctuation.definition.set.end.regexp.elixir
91+
#// ^ meta.literal.regexp.elixir
92+
#// ^ meta.literal.regexp.elixir
93+
#// ^ meta.literal.regexp.elixir
8594
~r"[
86-
#// ^ meta.set.regexp.elixir
95+
#// ^ meta.set.regexp.elixir meta.literal.regexp.elixir
8796
]"
8897

98+
~r"[a-b-c] [-a-z-] [\d-\s \w-\\] [\x0-\x1] [\x00-\xff] [\000-\007] [\0-\18]"
99+
#// ^ meta.literal.regexp.elixir
100+
#// ^^^^ constant.other.range.regexp.elixir
101+
#// ^ keyword.operator.range.regexp.elixir
102+
#// ^^^^ constant.other.range.regexp.elixir
103+
#// ^^^^ constant.other.range.regexp.elixir
104+
#// ^^^^ constant.other.range.regexp.elixir
105+
#// ^^^ constant.other.range.regexp.elixir
106+
#// ^^^ constant.other.range.regexp.elixir
107+
#// ^^ constant.other.range.regexp.elixir
108+
#// ^^ invalid.illegal.range.regexp.elixir
109+
#// ^^ invalid.illegal.range.regexp.elixir
110+
#// ^^ invalid.illegal.range.regexp.elixir
111+
#// ^ meta.literal.regexp.elixir
112+
#// ^ keyword.operator.range.regexp.elixir
113+
#// ^ meta.literal.regexp.elixir
114+
#// ^^ meta.literal.regexp.elixir
115+
~r"[-] [--] [---] [\--\-] [[--] [--[] [[-[] [[-] [[-\]] [\[-\]]"
116+
#// ^^ constant.other.range.regexp.elixir
117+
#// ^^ constant.other.range.regexp.elixir
118+
#// ^^ meta.literal.regexp.elixir
119+
#// ^ constant.other.range.regexp.elixir
120+
#// ^ constant.other.range.regexp.elixir
121+
#// ^ constant.other.range.regexp.elixir
122+
#// ^ constant.other.range.regexp.elixir
123+
#// ^ constant.other.range.regexp.elixir
124+
#// ^^ constant.other.range.regexp.elixir
125+
#// ^^ constant.other.range.regexp.elixir
126+
#// ^ keyword.operator.range.regexp.elixir
127+
#// ^ punctuation.definition.set.end.regexp.elixir
128+
#// ^^ meta.literal.regexp.elixir
129+
#// ^ meta.literal.regexp.elixir
130+
89131
# Inline options:
90132
~r"(*NO_START_OPT)(*UTF)(*UTF8)(*UCP)(*CRLF)(*CR)(*LF)(*ANYCRLF)(*ANY)(*BSR_ANYCRLF)(*BSR_UNICODE)(*LIMIT_MATCH=)(*LIMIT_RECURSION=)(*ANY)"
91133
#//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ keyword.control.flag.regexp.elixir
@@ -206,18 +248,31 @@
206248

207249
# Comments:
208250
~r"(?#comment block)"
209-
#//^^^^^^^^^^ comment.block.group.regexp.elixir
251+
#//^^^^^^^^^^^^^^^^^ comment.block.group.regexp.elixir
210252
~r"# # comment line"
211253
#// ^ comment.line.number-sign.regexp.elixir
212254
#//^^ meta.literal.regexp.elixir
213255
~r"[ # not a comment inside a set]"
214256
#// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.literal.regexp.elixir
215-
~r" # a comment"
216-
#// ^^^^^^^^^^^ comment.line.number-sign.regexp.elixir
217-
#//^ meta.literal.regexp.elixir
218-
~r"\ # escape preceding space to prevent matching as a comment"
219-
#// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.literal.regexp.elixir
257+
~r"\ # \\\ # # a comment"
258+
#// ^^^^^^^^^^^ comment.line.number-sign.regexp.elixir
259+
#// ^^ meta.literal.regexp.elixir
260+
#// ^^^^ constant.character.escape.regexp.elixir
261+
#// ^^ meta.literal.regexp.elixir
220262
#//^^ constant.character.escape.regexp.elixir
263+
~r"\ # escape preceding space to prevent matching as a comment
264+
#// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.literal.regexp.elixir
265+
#//^^ constant.character.escape.regexp.elixir
266+
(back to regex...)
267+
#// ^^^ keyword.other.any.regexp.elixir
268+
"
269+
~r"\\ # match as comment with an even number of preceding backslashes
270+
#// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.number-sign.regexp.elixir
271+
#//^^ constant.character.escape.regexp.elixir
272+
(back to regex...)
273+
#// ^^^ keyword.other.any.regexp.elixir
274+
"
275+
221276
~r"no preceding space#not a comment"
222277
#//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.literal.regexp.elixir
223278
~R[ #{123}] ~r[ #{123}]

0 commit comments

Comments
 (0)