Files
mercury/tests/invalid/bad_foreign_enum.m
Julien Fischer 2985037e86 Improve error reporting for foreign_enum pragmas.
Have the parser, rather than the parse tree -> HLDS conversion, check for the
third argument of a foreign_enum pragma being an empty list.  (A enumeration
must define at least one constant in order to be an enumeration so the list
should never be non-empty.)

Parse each argument of a foreign_enum pragma separately and report all of the
errors (well, most of them -- see below) that occur in one go.

Minor improvements and fixes to the wording of parser error messages for
foreign_enum pragmas.

Improve test coverage of error messages for parsing pragmas.

compiler/parse_pragma.m:
    Parse each argument of a foreign_enum pragma separately and report
    all the errors that occur.  The handling of the third argument is
    not as good as it could be -- add an XXX comment about it.

    Check that the third argument of a foreign_enum pragma is not an
    empty list during parsing -- while we currently generate an error
    for this during parse tree -> HLDS conversion, that error is not
    entirely accurate and we may as well catch this error earlier in
    any case.

    Do not incorrectly refer to 'foreign_export_enum' pragmas in error
    messages about 'foreign_enum' pragmas.  Factor out the shared code
    that was the cause of this more thoroughly.

    Improve and fix parser error messages for foreign_enum pragmas.

    Fix spelling in the error message for a misformed pragma declaration.

    Fix a misspelled predicate name: s/unrecogized/unrecognized/

    Fix the wording and spelling in some comments.

tests/invalid/bad_foreign_enum.{m,err_exp}:
    Extend this test case to cover the above.

tests/invalid/foreign_enum_invalid.{m,err_exp}:
    Delete the test for an empty mapping list, that situation is now
    caught earlier.

tests/invalid/invalid_pragma.{m,err_exp}:
    Test the error message for an invalid pragma.

tests/invalid/unrecognized_pragma.{m,err_exp}:
    Test the error message for an unrecognized pragma.

tests/invalid/Mmakefile:
    Add the new test cases.
2016-05-19 23:30:21 +10:00

84 lines
1.8 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%---------------------------------------------------------------------------%
%
% Test for various syntax errors in 'foreign_enum' pragmas.
% This is also a regression test for a problem in rotd-2016-05-16 and before
% where errors occurring in 'foreign_enum' pragmas were incorrectly reported
% as occurring in 'foreign_export_enum' pragmas.
:- module bad_foreign_enum.
:- interface.
:- type fruit
---> orange
; lemon
; apple.
:- type more_fruit
---> pear
; grapefruit.
:- type vegetable
---> carrot
; potato
; turnip.
:- type meat
---> beef
; lamb
; pork.
:- type poultry
---> chicken
; duck.
:- type more_poultry
---> goose
; turkey.
:- implementation.
% Wrong number of arguments.
%
:- pragma foreign_enum("C", fruit/0, [], []).
% Invalid foreign language.
%
:- pragma foreign_enum("InvalidLanguage", more_fruit/0, []).
% Second arg is not name / arity.
%
:- pragma foreign_enum("C", vegetable,
[carrot - "1", potato - "2", turnip - "3"]).
% Check that multiple errors are reported and that the correct
% contexts are attached to them.
%
:- pragma foreign_enum(
"InvalidLanguage",
meat,
[]
).
% Check for invalid elements in the mapping list.
%
:- pragma foreign_enum("C", poultry/0, [
chicken,
duck - "2"
]).
% Ditto.
%
:- pragma foreign_enum("C", more_poultry/0, [
"goose" - "1",
turkey - "561"
]).
% XXX TODO: we should generate more specific error messages in cases
% like the following:
%:- pragma foreign_enum("C", more_poultry/0, [
% goose - "",
% turkey - 562
%]).