Skip to content

Commit

Permalink
Merge pull request #67 from owst/allow_no_whitespace_comments
Browse files Browse the repository at this point in the history
Allow no-whitespace and single-line comments (#66)
  • Loading branch information
jaynetics committed Sep 12, 2020
2 parents 35d25d3 + 6085eb5 commit 58a00d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/regexp_parser/scanner/scanner.rl
Expand Up @@ -21,7 +21,7 @@
set_close = ']';
brackets = set_open | set_close;

comment = ('#' . [^\n]* . '\n');
comment = ('#' . [^\n]* . '\n'?);

class_name_posix = 'alnum' | 'alpha' | 'blank' |
'cntrl' | 'digit' | 'graph' |
Expand Down Expand Up @@ -120,7 +120,7 @@

literal_delimiters = ']' | '}';

ascii_print = ((0x20..0x7e) - meta_char);
ascii_print = ((0x20..0x7e) - meta_char - '#');
ascii_nonprint = (0x01..0x1f | 0x7f);

utf8_2_byte = (0xc2..0xdf 0x80..0xbf);
Expand Down
29 changes: 25 additions & 4 deletions spec/parser/free_space_spec.rb
Expand Up @@ -24,13 +24,34 @@
expect(root.first.text).to eq 'a b c d'
end

specify('parse single-line free space comments without spaces') do
regexp = /a#b/x

root = RP.parse(regexp)
expect(root.length).to eq 2

expect(root[0]).to be_instance_of(Literal)
expect(root[1]).to be_instance_of(Comment)
end

specify('parse single-line free space comments with spaces') do
regexp = /a # b/x

root = RP.parse(regexp)
expect(root.length).to eq 3

expect(root[0]).to be_instance_of(Literal)
expect(root[1]).to be_instance_of(WhiteSpace)
expect(root[2]).to be_instance_of(Comment)
end

specify('parse free space comments') do
regexp = /
a ? # One letter
b {2,5} # Another one
[c-g] + # A set
(h|i|j) | # A group
klm *
klm#nospace before or after comment hash
nop +
/x

Expand All @@ -51,11 +72,11 @@

alt_2 = alt.alternatives.last
expect(alt_2).to be_instance_of(Alternative)
expect(alt_2.length).to eq 7
expect(alt_2.length).to eq 8

[0, 2, 4, 6].each { |i| expect(alt_2[i].class).to eq WhiteSpace }
[0, 2, 5, 7].each { |i| expect(alt_2[i].class).to eq WhiteSpace }

expect(alt_2[1]).to be_instance_of(Comment)
[1, 4].each { |i| expect(alt_2[i]).to be_instance_of(Comment) }
end

specify('parse free space nested comments') do
Expand Down

0 comments on commit 58a00d6

Please sign in to comment.