Editing an Ada Program
Editing an Ada Program
This chapter first guides you through the process of writing a simple
Ada unit, then introduces the minimal concepts you should understand
before beginning to experiment on your own. Ada-ASSURED should already
be installed. If not, and you must install it yourself, refer to the
Ada-ASSURED Installation Guide, which is a separate volume.
The following is an introduction to editing with Ada-ASSURED. For a more
thorough explanation of editing features, see Chapter Editor
Overview.
Invoke Ada-ASSURED by entering the command aa from the command line of a shell window; on Microsoft® Windows® you may also invoke it from the Start menu by selecting Start/Programs/Ada-ASSURED/Ada-ASSURED. [Note: Depending on the installation, aa invokes either the Ada 83 version or the Ada 95 version of Ada-ASSURED. The show-copyright command (Help/Copyright... menu item [X] or Help/About... menu item [MS]) can be used to confirm which version you are running. The default version can be overridden by command line flag aa -language 83 or aa -language 95.] At any stage, you can quit the editor by invoking exit from the File menu. If you encounter difficulties getting started, consult Appendix Installation Checklist, or a system administrator.
When the editor starts up, you will see a window that is split into several sections. The section below the title bar, which contains the names of pull-down menus, is the menu bar. The large middle section, which contains the text <compilation_item>, is where you construct the program; it is known as the object pane. The section below this, which contains the label Command, is known as the command pane. At the bottom of the window, there is a status pane for displaying error messages and context information. For now, these are the only parts of the window you will be using.
Writing an Ada Unit
In this section, you enter two versions of a simple program that
outputs the message ``Hello world''. This introduction is
sufficiently brief that if something goes awry, then rather than attempting
to recover, you should probably just quit the editor and try again.
Position the mouse within the Ada-ASSURED window and type the following line of text exactly as shown below.
If you need to take a new line to continue typing, do not use Enter. Instead, use Control+M to start a new line, or Control+J to start a new line and indent.
with text_io; use text_io; procedure demo is begin put("Hello world"); end;
Now strike Enter. Congratulations. You have successfully used Ada-ASSURED to create a simple Ada unit. The absence of any complaint from Ada-ASSURED indicates that the code is syntactically correct. As you can see, the program has been prettyprinted and standardized automatically:
--~-----------------------------------------------------Several aspects of the display are noteworthy:
with Text_IO;
use Text_IO;
procedure Demo is
begin -- Demo
Put("Hello world");
end Demo;
--~-----------------------------------------------------
Ada-ASSURED provides many facilities to aid in the rapid and accurate creation of Ada programs. Although it may be used as a basic text editor, the full power of the tool comes through when the more advanced features are used. In this section, you delete the program just entered, and then reenter it using some of those features.
To delete the program,
Basic Concepts
To use Ada-ASSURED effectively and become conversant with the terminology
of
language-sensitive editing, we recommend that you study this
section, which introduces the basic concepts. To illustrate points,
we will occasionally ask you to carry out some action in the editor
using the unit you just created.
The Structural Selection
As you followed the above instructions, you will no doubt have noticed
the underlining. This is known as the structural selection. Just
as the insertion cursor is the focus for textual operations, the
structural selection is the focus for structural operations. The unit
you just created is not just textit has an underlying syntactic
structure, and what you see on the screen is the textual
representation of that underlying structure. The structure is a
hierarchyfor example the string "Hello world" is a
substructure of the Put statement, which itself is a
substructure of the procedure.
The following diagram shows the structure of the example unit. (The lines of dashes are omitted here for simplicity). The outlining indicates the hierarchy of well-formed phrases and subphrases.
The structural selection indicates the current position in the hierarchy. When you click on a character, the structural selection is reset to underline the smallest phrase in the hierarchy containing that character. For example, if you click on a character in the procedure name Demo, the structural selection is set to just that word. However, if you click on the keyword use, then the entire line is selected because the word Text_IO is a substructure of the use clause.
If you move the insertion cursor using the cursor motion keys (Up Arrow, Left Arrow, etc.), then when the cursor comes to rest, the structural selection is set in the same manner as described above.
Try clicking on various parts of the object, and moving the insertion cursor with the cursor keys to see how the structural selection moves.
The syntactic category of the structural selection, which is known as the context, is shown on the right side of the status pane. For example, if you move the structural selection to the unit name Text_IO, you will see that the context is expanded_name. If you click on the text of the comment -- Example one, you will see that the context is comment_text. Because Ada-ASSURED is language-sensitive, it knows what syntactic categories are valid in what contexts, and it can prevent you from putting things where they do not belong. For example, it will not allow you to put a statement where a declaration is expected.
Text, Parsing, and Syntax Errors
As you constructed the example unit, you typed in text,
and that text was analyzed and prettyprinted. The same
basic process is used for all textual editing operations.
When you type a character within already analyzed code, you text-capture the structural selection. That is, you turn the structure into text. When you then try to move the structural selection away, the text you just typed is parsed. The parsing process turns the text back into structure. Of course it can do this only if the text is syntactically correct. If it is not, Ada-ASSURED issues a syntax-error message.
To illustrate this, let's turn the text "Hello world" into "Hello beautiful world". Click where you need to insert the string ``beautiful'', and type the text. When you typed the first character, you text-captured the string. You will notice that the font or color of the string changed to indicate this. Now click elsewhere in the object, say in the comment. When you move away, the text is parsed and, because it is syntactically correct, it is prettyprinted.
To illustrate syntax errors, let's change the procedure name Demo to Demo-it. It is, of course, illegal to have a dash in an identifier. Move the insertion cursor to the end of the name Demo, and type -it. Now try to move away by clicking elsewhere. As you try to move away, Ada-ASSURED attempts to parse what you typed. It discovered a syntax error, so it moved the cursor back to the position where it found the error. Notice also that the editor makes some error sound and that a syntax error message is printed on the left side of the status pane. The status message is erased as soon as you perform another operation in the editor.
Now change the dash to an underscore and move the cursor away again. Note that all is again well with the procedure.
Optionals
The procedure you created is a simple one. For example, it does not
have any formal parameters or declarations, which are both examples of
optional parts of the unit. It is easy to add optional parts at
any stage. Just as in any text editor, you can simply set the insertion
cursor where the optional element is desired and type it.
Alternatively, you can first cause a placeholder to appear for the
optional part, and then make an insertion. If you decide you do not
want the optional placeholder, just move away and it automatically
disappears.
For example, say you wish to add some formal parameters to the procedure. Move the structural selection to procedure name Demo_It, and strike Enter (twice if you are using Ada 95 version of Ada-ASSURED)[Note: Striking the Enter key does not insert a linefeed. Instead, it moves the structural selection while inserting placeholders for optional elements.]. Placeholder syntax for a formal parameter appears. Now, say you decide you no longer want the formal parameter. Simply click elsewhere in the object, and you will see that the placeholder syntax disappears.
Repeatedly striking the Enter key moves the structural selection through the object. When it reaches the end of the object, it wraps round to the beginning. Try doing this to see the optionals first materialize, then disappear. Also note that the context influences which buttons appear in the Transforms menu.
Each file is edited in a mode that determines the applicable language sensitivity. The default mode illustrated above provides active, immediate Ada sensitivity. In contrast, mode TEXT provides generic text editing and only passive, on-demand Ada language sensitivity. Because Ada syntax checking, formatting, and style enforcement are not effective in TEXT mode unless explicitly invoked, TEXT mode can be used for editing non-Ada text files. This section illustrates TEXT mode using the familiar ``Hello World'' program.
with text_io; use text_io;
procedure demo is begin put("Hello world"); end;
Note that in the TEXT syntactic mode, Enter just inserts a newline.
Delete the goto statement, and reinvoke format. Note that the violation message is gone.