From d883dde8764f7a62faad932b9a704d9432fecd45 Mon Sep 17 00:00:00 2001 From: Taichi Ishitani Date: Fri, 29 Mar 2024 01:04:43 +0900 Subject: [PATCH] implment comment rule --- lib/rupkl/parser/misc.rb | 22 +++++++++++++++--- spec/rupkl/parser/comment_spec.rb | 37 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 spec/rupkl/parser/comment_spec.rb diff --git a/lib/rupkl/parser/misc.rb b/lib/rupkl/parser/misc.rb index a0b1821..aa2f510 100644 --- a/lib/rupkl/parser/misc.rb +++ b/lib/rupkl/parser/misc.rb @@ -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 diff --git a/spec/rupkl/parser/comment_spec.rb b/spec/rupkl/parser/comment_spec.rb new file mode 100644 index 0000000..a393feb --- /dev/null +++ b/spec/rupkl/parser/comment_spec.rb @@ -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