Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow no-whitespace and single-line comments (#66) #67

Merged
merged 1 commit into from Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 - '#');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it took me a bit to figure out why this is necessary.

it is because the (ascii_print -- space)+ [...] literal matcher used to match up to and including pound signs, and the comment matcher only had a chance to fire if the literal matcher was interrupted by a space.

having the declarations of what can "safely" be considered a literal spread out is a little confusing, but that was already the case before this PR. i might look into consolidating this a bit later.

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