-- GHDL Run Time (GRT) - 'image subprograms. -- Copyright (C) 2002 - 2014 Tristan Gingold -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation, either version 2 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -- -- As a special exception, if other files instantiate generics from this -- unit, or you link this unit with other files to produce an executable, -- this unit does not by itself cause the resulting executable to be -- covered by the GNU General Public License. This exception does not -- however invalidate any other reasons why the executable file might be -- covered by the GNU Public License. with Interfaces; with Grt.Fcvt; package body Grt.To_Strings is generic type Ntype is range <>; --Max_Len : Natural; procedure Gen_To_String (Str : out String; First : out Natural; N : Ntype); procedure Gen_To_String (Str : out String; First : out Natural; N : Ntype) is subtype R_Type is String (1 .. Str'Length); S : R_Type renames Str; P : Natural := S'Last; V : Ntype; begin if N > 0 then V := -N; else V := N; end if; loop S (P) := Character'Val (48 - (V rem 10)); V := V / 10; exit when V = 0; P := P - 1; end loop; if N < 0 then P := P - 1; S (P) := '-'; end if; First := P; end Gen_To_String; procedure To_String_I32 is new Gen_To_String (Ntype => Ghdl_I32); procedure To_String (Str : out String; First : out Natural; N : Ghdl_I32) renames To_String_I32; procedure To_String_I64 is new Gen_To_String (Ntype => Ghdl_I64); procedure To_String (Str : out String; First : out Natural; N : Ghdl_I64) renames To_String_I64; procedure To_String (Str : out String; Last : out Natural; N : Ghdl_F64) is begin Grt.Fcvt.Format_Image (Str, Last, Interfaces.IEEE_Float_64 (N)); end To_String; procedure To_String (Str : out String; Last : out Natural; N : Ghdl_F64; Nbr_Digits : Ghdl_I32) is begin Grt.Fcvt.Format_Digits (Str, Last, Interfaces.IEEE_Float_64 (N), Natural (Nbr_Digits)); end To_String; procedure To_String (Str : out String_Real_Format; Last : out Natural; N : Ghdl_F64; Format : Ghdl_C_String) is procedure Snprintf_Fmtf (Str : in out String; Len : Natural; Format : Ghdl_C_String; V : Ghdl_F64); pragma Import (C, Snprintf_Fmtf, "__ghdl_snprintf_fmtf"); begin -- FIXME: check format ('%', f/g/e/a) Snprintf_Fmtf (Str, Str'Length, Format, N); Last := strlen (To_Ghdl_C_String (Str'Address)); end To_String; procedure To_String (Str : out String_Time_Unit; First : out Natural; Value : Ghdl_I64; Unit : Ghdl_I64) is V, U : Ghdl_I64; D : Natural; P : Natural := Str'Last; Has_Digits : Boolean; begin -- Always work on negative values. if Value > 0 then V := -Value; else V := Value; end if; Has_Digits := False; U := Unit; loop if U = 1 then if Has_Digits then Str (P) := '.'; P := P - 1; else Has_Digits := True; end if; end if; D := Natural (-(V rem 10)); if D /= 0 or else Has_Digits then Str (P) := Character'Val (48 + D); P := P - 1; Has_Digits := True; end if; U := U / 10; V := V / 10; exit when V = 0 and then U = 0; end loop; if not Has_Digits then Str (P) := '0'; else P := P + 1; end if; if Value < 0 then P := P - 1; Str (P) := '-'; end if; First := P; end To_String; end Grt.To_Strings; href='#n47'>47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/**
Copyright (c) 2014 Alex Tsui

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef PARSE_HELPER_H
#define PARSE_HELPER_H
#include <string>
#include <vector>
#include <list>
#include <memory>
#include "ParseMessage.h"

struct ParseListener;

/**
Helps chunk lines of Python code into compilable statements.
*/
class ParseHelper
{
public:
    struct Indent
    {
        std::string Token;
        Indent( );
        Indent( const std::string& indent );
    };

    /**
    Handle different states of parsing. Subclasses override
    process(const std::string&) to handle different types of multiline
    statements.
    */
    struct ParseState
    {
        ParseHelper& parent;
        ParseState( ParseHelper& parent_ );
        virtual ~ParseState( );

        /**
        Processes a single line of user input.

        Subclasses should return false if further processing should be done. In
        this case, the command buffer should be topped by the next statement to
        parse.

        \return whether processing of the line is done.
        */
        virtual bool process(const std::string& str) = 0;
    };

    struct ContinuationParseState : public ParseState
    {
        using ParseState::parent;
        ContinuationParseState( ParseHelper& parent_ );
        virtual bool process( const std::string& str);
    };

    /**
    Handle parsing a multiline indented block. Example of such a block:

        for i in range(10):
            print i
            print i*i

    */
    struct BlockParseState : public ParseState
    {
        Indent indent;

        BlockParseState( ParseHelper& parent );
        BlockParseState( ParseHelper& parent, const std::string& indent_ );

        // return whether processing is finished
        virtual bool process(const std::string& str);

        // return if there was an error
        bool initializeIndent(const std::string& str);
    };
    friend struct BlockParseState;

    struct BracketParseState : public ParseState
    {
        static const std::string OpeningBrackets;
        static const std::string ClosingBrackets;

        std::list<char> brackets;
        std::list<std::string> m_buffer;

        /**
        Return whether open brackets remain unclosed in \a str.
        */
        static bool HasOpenBrackets(const std::string& str);
        static bool LoadBrackets(const std::string& str,
            std::list<char>* stack);

        BracketParseState( ParseHelper& parent, const std::string& firstLine );

        virtual bool process(const std::string& str);
    };

protected:
    // TODO: Create a ContinuationParseState to handle this
    bool inContinuation;
    std::vector< ParseListener* > listeners;
    std::vector< std::shared_ptr< ParseState > > stateStack;
    std::vector< std::string > commandBuffer;

public:
    static bool PeekIndent( const std::string& str, Indent* indent );

public:
    ParseHelper( );

public:
    void process( const std::string& str );

    bool buffered( ) const;

    /**
    Generate a parse event from the current command buffer.
    */
    void flush( );

    /**
    Reset the state of the helper.
    */
    void reset( );

    bool isInContinuation( ) const;

    void subscribe( ParseListener* listener );
    void unsubscribeAll( );
    void broadcast( const ParseMessage& msg );

}; // class ParseHelper

inline bool operator== ( const ParseHelper::Indent& a, const ParseHelper::Indent& b )
{
    return a.Token == b.Token;
}

inline bool operator!= ( const ParseHelper::Indent& a, const ParseHelper::Indent& b )
{
    return a.Token != b.Token;
}

#ifndef NDEBUG
void print(const ParseHelper::Indent& indent);
#endif

#endif // PARSE_HELPER_H