Skip to content

Commit 811d3d7

Browse files
committed
read line comments in blocks
1 parent 7db3e2f commit 811d3d7

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

prolog/metta_lang/metta_parser.pl

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,16 +1044,16 @@
10441044
; true
10451045
; nb_current('$file_src_depth', 0)),
10461046

1047-
cont_sexpr_once(EndChar, Stream, Subr), !,
1047+
with_depth_plus_one(cont_sexpr(EndChar, Stream, Subr)), !,
10481048
Item = exec(Subr).
10491049

10501050
% allow in mettalog the special ` ,(<eval this>) ` form in intepreter
1051-
cont_sexpr_from_char(EndChar, Stream, ',', Item) :-
1051+
cont_sexpr_from_char(EndChar, Stream, ',', Item) :- fail, % some cases users have written ( A,(B))
10521052
peek_char(Stream, Next),
10531053
Next == '(',
10541054
%\+ paren_pair_functor(_, Next, _),
10551055
%Next \== ' ',
1056-
cont_sexpr_once(EndChar, Stream, Subr), !,
1056+
with_depth_plus_one(cont_sexpr(EndChar, Stream, Subr)), !,
10571057
Item = exec(Subr).
10581058

10591059
% If '(', read an S-expression list.
@@ -1082,7 +1082,7 @@
10821082

10831083
% If '#' followed by '(', read SExpr as Prolog Expression
10841084
cont_sexpr_from_char(EndChar, Stream, '#', Item) :- peek_char(Stream, '('),
1085-
cont_sexpr_once(EndChar, Stream, Subr),
1085+
with_depth_plus_one(cont_sexpr(EndChar, Stream, Subr)),
10861086
univ_maybe_var(Item, Subr).
10871087

10881088
% Unexpected end character
@@ -1330,11 +1330,25 @@
13301330
% read_char(Stream, ';'), % Skip the ';' character.
13311331
read_line_char(Stream, line_char(Line1, Col)),
13321332
%succ(Col0, Col1),
1333-
read_line_to_string(Stream, Comment),
1334-
atom_length(Comment,Len), EndCol is Col + Len,
1335-
Range = range(line_char(Line1, Col), line_char(Line1, EndCol)),
1333+
read_line_to_string_maybe_more(Stream, Comment),
1334+
%atom_length(Comment,Len), EndCol is Col + Len,
1335+
read_line_char(Stream, line_char(Line2, EndCol)),
1336+
Range = range(line_char(Line1, Col), line_char(Line2, EndCol)),
13361337
push_item_range('$COMMENT'(Comment, Line1, Col), Range).
13371338

1339+
1340+
read_line_to_string_maybe_more(Stream, Comment):-
1341+
read_line_to_string(Stream, CommentStart),
1342+
peek_string(Stream, 5, LookAhead),
1343+
(start_line_comment(LookAhead) ->
1344+
(read_line_to_string_maybe_more(Stream, CommentCont), atomics_to_string([CommentStart,"\n",CommentCont],Comment)) ; CommentStart = Comment).
1345+
1346+
start_line_comment(Var):- var(Var), !, fail.
1347+
start_line_comment(end_of_file):- !, fail.
1348+
start_line_comment(String):- string(String), string_chars(String,Chars),!,start_line_comment(Chars).
1349+
start_line_comment(String):- atom(String), atom_chars(String,Chars),!,start_line_comment(Chars).
1350+
start_line_comment([C|Chars]):- C==';' -> true ; (C\=='\n',C\=='\r',is_like_space(C),!,start_line_comment(Chars)).
1351+
13381352
%! read_position(+Stream:stream, -Line:integer, -Col:integer, -CharPos:integer) is det.
13391353
%
13401354
% Reads the current line, column, and character position from the input stream.
@@ -1458,16 +1472,22 @@
14581472
% @arg List The list read from the stream.
14591473
% @arg EndChar that denotes the end of the list.
14601474
read_list(EndChar, Stream, List):-
1461-
nb_current('$file_src_depth', LvL),
1462-
flag('$file_src_ordinal',Ordinal,Ordinal+1),
1463-
succ(LvL,LvLNext),
1475+
with_depth_plus_one((
14641476
read_position(Stream, Line, Col, CharPos, _),
1465-
setup_call_cleanup(
1466-
nb_setval('$file_src_depth', LvLNext),
1467-
catch(read_list_cont(EndChar, Stream, List),
1477+
catch(read_list_cont(EndChar, Stream, List),
14681478
stream_error(Where,Why),
1469-
throw(stream_error(Line:Col:CharPos-Where,Why))),
1470-
nb_setval('$file_src_depth', LvL)).
1479+
throw(stream_error(Line:Col:CharPos-Where,Why))))).
1480+
1481+
with_depth_plus_one(Goal):-
1482+
nb_current('$file_src_depth', LvL),
1483+
flag('$file_src_ordinal',Ordinal,Ordinal+1),
1484+
succ(LvL,LvLNext),
1485+
setup_call_cleanup(
1486+
nb_setval('$file_src_depth', LvLNext),
1487+
Goal,
1488+
nb_setval('$file_src_depth', LvL)).
1489+
1490+
14711491

14721492
read_list_cont(EndChar, Stream, List) :-
14731493
skip_spaces(Stream), % Skip any leading spaces before reading.

0 commit comments

Comments
 (0)