Files
mercury/library/stack.m
Zoltan Somogyi ca7385d2c7 Generate better diagnostics for parentheses mismatches.
When you have an unclosed (, [ or { in a clause, the diagnostic
you got did not tell you

- where the unclosed parenthesis was,
- which kind of parenthesis it was.

Fix this by including both pieces of information in the diagnostic.

Likewise, print more useful info for mixed-up parentheses,
such as [(]).

library/mercury_term_parser.m:
    When consuming a (, [ or { token, push it and its context on a stack.
    When consuming a ), ] or } token, pop off the top item from this stack,
    and generate a diagnostic if the close token does not match it.
    The one exception from this pushing and pulling is for code that
    handles the case where the open is followed immediately by
    the matching close, such as when parsing [] or {}.

    Print the contents of the stack also when getting to either
    the end of the term, or the end of the input, with a nonempty stack.

    Maintaining this stack has a small performance cost, but I expect
    it to be negligible, especially compared to the usefulness
    of the new detail in diagnostics,

    Completely rework the error handling parts of this module.
    The main changes are the following.

    First, the old code used to include *part* of the intended message
    in the pr_error structures it created, with a "Syntax error: "
    prefix being added later. Since this makes it hard to ensure
    that the error messages follow the rules of English, change this
    to generate each error message all at once.

    Second, the old code included the list of the remaining tokens
    in each pr_error structure. This was overkill, because the only part
    of this list that was used was the id and the context of the
    first token in the list. Apart from being inelegant, the main flaw
    of this approach was that in the case of premature end-of-file
    errors, the only token list available was token_nil, which
    of course contains neither a token nor its context. The old code
    compensated for it later by using the context of the *first* token
    of the whole term being parsed, which is ... less than useful.
    (The missing token is trivially replaced by "end-of-file".)

    The new code replaces the token list with the context, if it
    is available; if it is not, then later we compute the context
    of the last token in the whole token list. The new code
    does not return the token itself; instead, it includes
    its string version in the generated error message where appropriate.

    Third, as mentioned above, we now include info about unbalanced
    (), [] and {} pairs in diagnostics, as extra sentences.
    (These extra sentences are preceded by \n characters;
    see the change to parse_module.m below.)

    Fifth, to make the above possible without adding unnecesary
    complications, the diagnostic texts this module generates
    now always include the period at the ends of sentences:
    they are not added by the compiler.

    Fourth, we now consistently use "Syntax error at token abc:
    expected def, fgh, or xyz" phraseology.

library/mercury_term_lexer.m:
    Stop requiring the customers of this module to handle

    - integer_dot tokens, which are needed only by, and are
      an implementation detail of, the get_* family of predicates, and

    - eof tokens, which the lexer also never returns, converting each one
      into the end of its token list instead.

    The fact that the lexer never returned integer_dot tokens was
    documented, but the fact that it never returned eof tokens was not.

    The reason for this change was simply that I did not want to write
    two pieces of code to handle the out-of-input case in each affected
    spot in the parser: once for an eof token, and once for token_nil.

library/stack.m:
    Add a utility function needed by new code in mercury_term_parser.m.

compiler/parse_module.m:
    Stop adding a period at the ends of error messages generated by
    mercury_term_parser.m; mercury_term_parser.m now adds those itself.
    Do post-process those messages by turning any \n characters in them
    into nl format_pieces.

NEWS.md:
    Announce the change in mercury_term_lexer.m, and the
    new function in stack.m.

library/io.text_read.m:
    Unrelated bug fix, for which I discovered the need while
    working on the other library files: add a missing foreign import.

tests/invalid_nodepend/unbalanced.{m,err_exp}:
    A new test case to check the updated diagnostics.

tests/invalid_nodepend/Mmakefile:
    Enable the new test case.

tests/hard_coded/parse_number_from_string.exp:
tests/invalid_nodepend/impl_def_literal_syntax.err_exp:
tests/invalid_nodepend/invalid_binary_literal.err_exp:
tests/invalid_nodepend/invalid_float_literal.err_exp:
tests/invalid_nodepend/invalid_hex_literal.err_exp:
tests/invalid_nodepend/invalid_octal_literal.err_exp:
tests/invalid_nodepend/null_char.err_exp:
tests/invalid_nodepend/typeclass_test_1.err_exp:
tests/invalid_nodepend/unicode_1.err_exp:
tests/invalid_nodepend/unicode_2.err_exp:
tests/invalid_purity/purity_nonsense_2.err_exp:
    Expect the updated diagnostics.
2025-07-30 01:37:28 +02:00

175 lines
4.6 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 1994-1995, 1997-1999, 2005-2006, 2011-2012 The University of Melbourne.
% Copyright (C) 2014-2016, 2018, 2025 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%---------------------------------------------------------------------------%
%
% File: stack.m.
% Main author: fjh.
% Stability: high.
%
% This file contains a stack abstract data type.
% The concrete representation of each stack is a list.
%
%---------------------------------------------------------------------------%
:- module stack.
:- interface.
:- import_module list.
%---------------------------------------------------------------------------%
:- type stack(T).
% init = Stack:
% init(Stack):
%
% True if-and-only-if Stack is an empty stack.
%
:- func init = stack(T).
:- pred init(stack(T)::out) is det.
% is_empty(Stack):
%
% True if-and-only-if Stack is an empty stack.
%
:- pred is_empty(stack(T)::in) is semidet.
% is_full(Stack):
%
% This is intended to be true if-and-only-if Stack is a stack
% whose capacity is exhausted. This implementation allows
% arbitrary-sized stacks, so is_full always fails.
%
:- pred is_full(stack(T)::in) is semidet.
% push(Stack0, Elem) = Stack:
% push(Elem, Stack0, Stack):
%
% True if-and-only-if Stack is the stack which results from pushing Elem
% onto the top of Stack0.
%
:- func push(stack(T), T) = stack(T).
:- pred push(T::in, stack(T)::in, stack(T)::out) is det.
% push_list(Stack0, Elems) = Stack:
% push_list(Elems, Stack0, Stack):
%
% True if-and-only-if Stack is the stack which results from
% pushing the elements of the list Elems onto the top of Stack0.
%
:- func push_list(stack(T), list(T)) = stack(T).
:- pred push_list(list(T)::in, stack(T)::in, stack(T)::out) is det.
% top(Stack, Elem):
%
% True if-and-only-if Stack is a non-empty stack whose top element is Elem.
%
:- pred top(stack(T)::in, T::out) is semidet.
% det_top is like top except that it will call error/1 rather than
% failing if given an empty stack.
%
:- func det_top(stack(T)) = T.
:- pred det_top(stack(T)::in, T::out) is det.
% pop(Elem, Stack0, Stack):
%
% True if-and-only-if Stack0 is a non-empty stack whose top element
% is Elem, and Stack the stack which results from popping Elem off Stack0.
%
:- pred pop(T::out, stack(T)::in, stack(T)::out) is semidet.
% det_pop is like pop except that it will call error/1 rather than
% failing if given an empty stack.
%
:- pred det_pop(T::out, stack(T)::in, stack(T)::out) is det.
% depth(Stack) = Depth:
% depth(Stack, Depth):
%
% True if-and-only-if Stack is a stack containing Depth elements.
%
:- func depth(stack(T)) = int.
:- pred depth(stack(T)::in, int::out) is det.
:- func to_list(stack(T)) = list(T).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module require.
%---------------------------------------------------------------------------%
:- type stack(T)
---> stack(list(T)).
init = Stack :-
stack.init(Stack).
init(stack([])).
is_empty(stack([])).
is_full(_) :-
semidet_fail.
push(!.Stack, X) = !:Stack :-
stack.push(X, !Stack).
push(Elem, !Stack) :-
!.Stack = stack(Elems),
!:Stack = stack([Elem | Elems]).
push_list(!.Stack, Xs) = !:Stack :-
stack.push_list(Xs, !Stack).
push_list([], !Stack).
push_list([Elem | Elems], !Stack) :-
stack.push(Elem, !Stack),
stack.push_list(Elems, !Stack).
top(stack([Elem | _]), Elem).
det_top(Stack) = X :-
stack.det_top(Stack, X).
det_top(Stack, Elem) :-
(
Stack = stack([Elem | _])
;
Stack = stack([]),
unexpected($pred, "top of empty stack")
).
pop(Elem, !Stack) :-
!.Stack = stack([Elem | Elems]),
!:Stack = stack(Elems).
det_pop( Elem, !Stack) :-
(
!.Stack = stack([Elem | Elems]),
!:Stack = stack(Elems)
;
!.Stack = stack([]),
unexpected($pred, "pop from empty stack")
).
depth(Stack) = Depth :-
stack.depth(Stack, Depth).
depth(Stack, Depth) :-
Stack = stack(Elems),
list.length(Elems, Depth).
to_list(stack(Elems)) = Elems.
%---------------------------------------------------------------------------%
:- end_module stack.
%---------------------------------------------------------------------------%