TutorialEditing an Ada Program




Prev | Up | Next | Back | Forward | Online Documentation Home Page

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.

Startup

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.

Window Roadmap

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.

First Version

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:

--~-----------------------------------------------------

with Text_IO;
use  Text_IO;
procedure Demo is
begin -- Demo
    Put("Hello world");
end Demo;

--~-----------------------------------------------------
Several aspects of the display are noteworthy:

  1. The entire unit is underlined. The meaning of the underlining will be explained later. For now, just click the left mouse button on the message "Hello world" to underline a smaller phrase.

  2. The lines of dashes inserted before and after the unit are specified by the built-in Ada style guide. This feature can be disabled.

  3. Despite the fact that it was typed as demo, the first character of procedure name Demo is now capitalized, as specified by the Ada style guide. Automatic capitalization can be mixed case, upper case, lower case, or can be disabled.

  4. Procedure name Demo is replicated after begin and after end, as indicated by the Ada style guide.

  5. The last syllable of Text_IO is all capitalized, despite the fact that mixed-case capitalization is enabled. The style guide includes a customizable list of exceptions from the capitalization rules. As delivered, this list specifies that each occurrence of the syllable IO in an identifier is to be all capitalized.

Second Version

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,

  1. Click the left mouse button on the keyword procedure, which underlines the entire compilation unit.

  2. Delete the entire unit by choosing command cut-structure from the Edit menu.
If you did this correctly, the placeholder <compilation_item> should have reappeared. You are now ready to reenter the program:

  1. Select procedure-body from the Transforms menu. This inserts a template for a procedure body. Parts of the unit remaining to be filled in are indicated by placeholders <context_clause>, <identifier>, etc.

  2. Type the characters with text_io; and strike Enter. You will see that the text you typed is recognized and prettyprinted, and a new <context_clause> placeholder appears.

  3. Templates can be inserted by keystroke rather than mouse click. You only need to enter an unambiguous prefix of the template name. Strike the Tab key, which moves the keyboard focus to the text field in the command pane. Type the string ``use-c'', which is an unambiguous prefix of the template name use-clause, and strike Enter. You will see that the use-clause template has been inserted and keyboard focus has been returned to the object pane.

  4. Type the characters text_io and strike Enter. Note that the Enter key does not insert a linefeed. Rather, Enter checks the syntactic correctness of what you just typed, and advances to the next place where something can be inserted, in this case, the placeholder <expanded_name>.

  5. Click in the middle of the <identifier> placeholder. You will see that the placeholder <expanded_name>, which is no longer needed, automatically disappears.

  6. Type the characters demo for the procedure name. Then click the mouse in the middle of <basic_declarative_item>.

  7. Now enter a comment. Type the text -- Example one. Then click on <statement>.

  8. Type the text put("Hello world"); and then use the cursor keys to position the insertion cursor on a different line. You will see that clicking the cursor away from a phrase causes modified text to be analyzed and appropriately prettyprinted.

  9. Finally, save the unit to disk as an ASCII text file. Invoke save-as from the File menu, type the name of the file (say /tmp/demo.a [X] or C:\temp\demo.a [MS]), and strike Enter.

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 text—it has an underlying syntactic structure, and what you see on the screen is the textual representation of that underlying structure. The structure is a hierarchy—for 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.

Standard Text Editing

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.

  1. Invoke new from the File menu. In the dialog box, enter a name for the buffer (say t), choose syntactic category TEXT, and click on OK.

  2. Enter the ``Hello World'' program in an arbitrary format, say:
    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.

  3. Syntax can be checked by invoking command parse-buffer from the Text menu. Introduce a syntax error, say by deleting a semicolon. Then invoke parse-buffer. Note the error message that results in the status pane. Correct the error, and reinvoke parse-buffer. Note that the status pane now displays a message indicating successful parsing.

  4. Syntactically correct code can be formatted and style checked by invoking command format from the Text menu. Enter goto done; after the put statement. Then invoke format. Note that a violation message flagging the goto appears.

    Delete the goto statement, and reinvoke format. Note that the violation message is gone.


Forward