Files
eiffel-org/documentation/current/eiffelstudio/eiffelstudio-guided-tour/handling-syntax-and-validity-errors.wiki
jfiat 13b85fb9c9 Author:admin
Date:2008-09-18T14:32:43.000000Z


git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@13 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
2008-09-18 14:32:43 +00:00

70 lines
8.7 KiB
Plaintext

[[Property:title|12 HANDLING SYNTAX AND VALIDITY ERRORS]]
[[Property:link_title|Handling Syntax and Validity Errors]]
[[Property:weight|-3]]
[[Property:uuid|c2be8123-b793-f8ce-e082-d4fdacc6bbe6]]
[[Help reading the EiffelStudio Guided Tour|Help reading this manual]]
So far we have tried to make sure that everything went smoothly. But in actual software development you may encounter error situations, and it is useful to know what can happen then.
<div>
==Levels of language description==
Let's remind ourselves first of how the language is specified. The book <span> [http://www.eiffel.com/doc/ Eiffel: The Language] </span>, the language reference, carefully distinguishes between three levels of description: '''syntax''', '''validity''' and '''semantics'''. Their roles are clearly distinct:
* <span>Syntax</span> defines the structure of software texts. A typical syntax rule states that an assignment starts with a <code> Writable </code> entity, continues with the symbol <code> := </code>, and ends with an <code> Expression </code>. This is a purely structural specification, saying nothing for example about the types of the <code> Writable </code> and the <code> Expression </code>.
* <span>Validity</span>, applicable only to syntactically legal texts, defines required consistency conditions. A typical validity rule states that in an assignment the right-hand-side <code> Expression </code> must <span>conform</span> -- a property of its type, defined rigorously on the basis of inheritance -- to the left-hand-side <code> Writable </code>. Eiffel has about 75 validity rules; part of the language's originality is that these rules are of the " <span>if and only if</span> " form, not only telling you individual error cases ("this is valid <span>only if</span> <span>...</span> ") but also reassuring you that your text will in fact be valid <span>if</span> it satisfies the conditions listed exhaustively.
* Semantics, applicable only to valid texts, defines the software's expected run-time behavior. A typical semantic rule states that an assignment replaces the value of its left-hand-side <code> Writable </code> by the value of the right-hand-side <code> Expression </code> at the time the assignment is executed, with precise rules on the different possible cases involving references, objects and simple values.
You may make an error at any of these levels:
* Writing <code> = </code> instead of <code> := </code> for the assignment symbol is a syntax error.
* Writing <code> your_integer </code> <code> := </code> <code> your_real </code>, with the types suggested by the names, is a validity error.
* Calling a feature on a void target, violating a precondition, causing a division by zero, are semantic errors.
Syntax and validity errors will be detected by the compilation process. For semantic errors, you will rely on contract checking and on the debugging tools described later. Let's look now at examples of the first two cases.
</div><div>
==A syntax error==
To see what happens for a syntax error, replace the keyword <code> is </code> by <code> ist </code> in the first line of routine <code> display </code> of class <code> PARENT </code> (click the position immediately after the <code> s </code> and type a <code> t </code>.). Save the file by clicking the Save button or using CTRL- <code> S </code> and then compile the system.
<div> [[Image:index-75]] [[general/guided_tour/studio/index-75.png|(link)]] </div>
To correct the error, just bring the mouse back to its location, remove the spurious <code> t </code>, and click Save again; also click Compile to make sure that the project is recompiled up-to-date.
You may wonder why the syntax error messages are not a little more verbose than just <code> Syntax error </code>. The reason is merely that Eiffel's syntax, being simple and regular, does not require sophisticated error messages; syntax errors usually result from trivial oversights. If you make a syntax error and the reason is not immediately clear, check the syntax summary in the appendix of <span> [http://www.eiffel.com/doc/ Eiffel: The Language] </span>.
</div><div>
==A validity error==
A validity error is a violation of one of the validity constraints given in <span> [http://www.eiffel.com/doc/ Eiffel: The Language] </span>. Every such constraint is identified by a four-letter code of the form <code> V </code> <code> XXX </code> (the first letter is always <code> V </code>).
A validity error will produce a precise error message, which includes the validity code. Although short, the error message is usually sufficient to find out what the error is. If not, you can get the complete rule, straight from the book.
To see this mechanism at work, let us introduce a validity error. There is in fact one ready for you in class <code> TESTROOT </code>. Target a Development Window to this class; at the end of its text, just before the final <code> end </code>, you will find the following comment line:
<code>
-- inv: INVALID;</code>
If uncommented, this is a declaration of a feature of type <code> INVALID </code>. A class called <code> INVALID </code> indeed exists in file <code> invalid.e </code> of the root cluster, but it contains a validity error. To see what it is, remove the initial double-dash <code> -- </code> in the above line from class <code> TESTROOT </code> so that it is not a comment any more.
<div> [[Image:index-76]] [[general/guided_tour/studio/index-76.png|(link)]] </div>
Click <code> Save </code>, then <code> Compile </code>. Compilation starts but after a few degrees it stops with an error message that appears in the bottom Context Tool (you may have to do some resizing to see it in its entirety):
<div> [[Image:index-77]] [[general/guided_tour/studio/index-77.png|(link)]] </div>
As the error message indicates, you have (shame on you) violated the validity rule <code> VUAR </code>, which requires the number and types of actual arguments in a routine call to match the number and types of formal arguments declared in the routine.
One of the interesting properties of the error message is that everything in color is '''clickable''' : class name, feature name, but also the error code. This means that you can start a Pick-and-Drop on any of these elements to find out more.
For example, to see the exact context of the error, pick-and-drop the name of the affected feature, <code> display </code> -- appearing on the fifth non-blank line, after <code> Feature: </code> -- and pick-and-drop it to the top Text window. (As you remember this means: right-click on it and release; move the mouse to the text window, without clicking any button; right-click again. During the move the cursor shows a cross, the symbol for features.) This displays the erroneous feature:
<div> [[Image:index-78]] [[general/guided_tour/studio/index-78.png|(link)]] </div>
Note on this display a special property of Pick-and-Drop when its source is a feature name appearing in a validity error message: the instruction that causes the error is highlighted.
In the error message in the Context Tool, the error code itself, <code> VUAR </code>, is also clickable. Assuming the message was not sufficient to understand the error, you can use it to start a Pick-and-Drop. Do this now, by picking that code and starting to move the mouse, but not dropping yet:
<div> [[Image:index-79]] [[general/guided_tour/studio/index-79.png|(link)]] </div>
The icon shape for such information elements is a question mark <code> ? </code>. If it is not on a droppable target, as in the bottom Context Tool, that icon will be crossed. In principle the place to drop is the Explanation hole in the Project toolbar, the only one that remains highlighted during the Move step of Pick-and-Drop:
<div> [[Image:index-80]] [[general/guided_tour/studio/index-80.png|(link)]] </div>
As is often the case when dropping into a specific hole, you don't need to shoot straight; dropping the pebble anywhere in the Editing Tool has the same effect as dropping it into the Explanation hole:
<div> [[Image:index-81]] [[general/guided_tour/studio/index-81.png|(link)]] </div>
The result is to display the complete text of the violated rule, straight from the pages of <span> [http://www.eiffel.com/doc/ Eiffel: The Language] </span>.
The rule has several clauses, numbered. Since the error message showed the error code as <code> VUAR(1) </code>, the violated clause is the first; this convention of showing the clause number in parentheses applies to all multi-clause validity constraints.
To correct the error the easiest is to go back to class <code> TESTROOT </code> and reinstate the comment symbol <code> -- </code> (two consecutive dashes) on the erroneous line. Save and compile to continue with a valid system.
</div>
[[11 RECOMPILING AND EDITING|<< Previous]] [[13 DEBUGGING AND RUN-TIME MONITORING|Next >>]]