Skip to content

Commit

Permalink
Improved metta_parser.pl:
Browse files Browse the repository at this point in the history
- Introduced skip_chars for more flexible character skipping during comment parsing.
- Replaced get_string with skip_chars in block comment processing for enhanced compatibility with nested comments.
  • Loading branch information
TeamSPoon committed Jan 4, 2025
1 parent 1b13878 commit 36e21f1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions prolog/metta_lang/metta_parser.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,11 @@
; true % Otherwise, no block comment, continue processing.
).


skip_chars(_, N):- N<1,!.
skip_chars(Stream, N):- Nm1 is N -1, get_char(Stream,_), !, skip_chars(Stream, Nm1).


%! read_block_comment(+Stream:stream) is det.
%
% Reads a block comment (including nested block comments) from the stream
Expand All @@ -1317,7 +1322,8 @@
% @arg Stream The input stream from which to read the block comment.
read_block_comment(Stream) :-
read_line_char(Stream, StartRange), % Capture the start position.
get_string(Stream, 2, _), % Skip the '/*' characters.
%get_string(Stream, 2, _), % Skip the '/*' characters.
skip_chars(Stream, 2),
read_nested_block_comment(Stream, 1, Chars), % Read the block comment, supporting nested ones.
string_chars(Comment, Chars),
read_line_char(Stream, EndRange), %capture the end pos
Expand All @@ -1339,15 +1345,15 @@
read_nested_block_comment(Stream, Level, Acc, Comment) :-
peek_string(Stream, 2, LookAhead),
( LookAhead = "*/" ->
( get_string(Stream, 2, _), % Consume the '*/'.
( skip_chars(Stream, 2), % Consume the '*/'.
NewLevel is Level - 1, % Decrease the nesting level.
( NewLevel = 0 ->
reverse(Acc, Comment) % If outermost comment is closed, return the accumulated comment.
; read_nested_block_comment(Stream, NewLevel, ['*', '/' | Acc], Comment) % Continue, append '*/'.
)
)
; LookAhead = "/*" ->
( get_string(Stream, 2, _), % Consume the '/*'.
( skip_chars(Stream, 2), % Consume the '/*'.
NewLevel is Level + 1, % Increase the nesting level.
read_nested_block_comment(Stream, NewLevel, ['/', '*' | Acc], Comment) % Continue, append '/*'.
)
Expand Down

0 comments on commit 36e21f1

Please sign in to comment.