AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages
Ada FAQ


Join >
Articles >
Ada FAQ >
Getting Started >
Home >
Books & Tutorials >
Source Treasury >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Back
Ada Utilities - SEMI
Utilities for Ada Programming

--- procedure SEMI
--
--  Count all semi-colons in the input file and send this
--  count to the output file.

with TEXT_IO; use TEXT_IO;

procedure SEMI -- Count all semicolons.

is

    package INT_IO is new INTEGER_IO (NATURAL);

    CHAR: CHARACTER;
    LINE: STRING (1 .. 250);

    LAST: NATURAL;

    COUNTER: NATURAL := 0;

    QUOTE, DQUOTE, MINUS : BOOLEAN;

begin

    while not END_OF_FILE loop

        GET_LINE (LINE, LAST);

        if LAST >= LINE'FIRST then

            QUOTE := FALSE;
            DQUOTE := FALSE;
            MINUS := FALSE;

            for I in 1 .. LAST loop
                CHAR := LINE (I);
                case CHAR is
                    when '-' =>
                        if not DQUOTE and not QUOTE then
                            if MINUS then
                                -- double MINUS, i.e., comment; exit loop
                                exit;
                            else
                                -- We've seen a minus.
                                MINUS := TRUE;
                            end if;
                        end if;
                    when '"' =>
                        if not QUOTE then
                            -- not within a quote
                            DQUOTE := not DQUOTE;
                        end if;
                        MINUS := FALSE;
                    when ''' =>
                        if not DQUOTE then
                            -- not within a double quote
                            QUOTE := not QUOTE;
                        end if;
                        MINUS := FALSE;
                    when ';' =>
                        if not DQUOTE and not QUOTE then
                            -- not within quotes of any kind, count it.
                            COUNTER := COUNTER + 1;
                        end if;
                        MINUS := FALSE;
                    when OTHERS =>
                        MINUS := FALSE;
		end case;
            end loop;

        end if;

    end loop;

    PUT ("There are");
    INT_IO.PUT (COUNTER, WIDTH => 10);
    PUT_LINE (" semi-colons in the input stream.");

end SEMI;


(c) 1998-2004 All Rights Reserved David Botton