Skip to content

Commit

Permalink
implment comment rule
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani committed Mar 28, 2024
1 parent 6f2761c commit d883dde
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/rupkl/parser/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,36 @@ class Parser
WS_PATTERN = '[ \t\f\r\n;]'

define_parser do
rule(:line_comment) do
str('//') >> match('[^\n]').repeat >> (nl | eof)
end

rule(:block_comment) do
str('/*') >> (block_comment | (str('*/').absent? >> any)).repeat >> str('*/')
end

rule(:comment) do
line_comment | block_comment
end

rule(:nl) do
match('\n')
end

rule(:eof) do
any.absent?
end

rule(:ws) do
match(WS_PATTERN).repeat(1).ignore
(match(WS_PATTERN) | comment).repeat(1).ignore
end

rule(:ws?) do
match(WS_PATTERN).repeat.ignore
(match(WS_PATTERN) | comment).repeat.ignore
end

rule(:pure_ws?) do
match('[ \t\f]').repeat.ignore
(match('[ \t\f]') | comment).repeat.ignore
end

private
Expand Down
37 changes: 37 additions & 0 deletions spec/rupkl/parser/comment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

RSpec.describe RuPkl::Parser do
let(:parser) do
RuPkl::Parser.new
end

describe 'comment' do
it 'should be ignored' do
pkl = <<~'PKL'.chomp
x = 10 // end-of-line comment
y = 20 /*
multi
line
/* nested */
comment
*/ z = 30
/// documentation comment
a = 40
b = 50 // single-line comment running until EOF rather than newline
PKL

expect(parser)
.to parse_string(pkl, :pkl_module)
.as(pkl_module do |m|
m.property :x, 10
m.property :y, 20
m.property :z, 30
m.property :a, 40
m.property :b, 50
end)
end
end
end

0 comments on commit d883dde

Please sign in to comment.