Пример #1
0
    static void dump_ex(const typename char_state_machine::dfa &dfa_,
        ostream &stream_)
    {
        const std::size_t states_ = dfa_._states.size();
        const id_type bol_index_ = dfa_._bol_index;
        typename dfa_state::id_type_string_token_map::const_iterator iter_;
        typename dfa_state::id_type_string_token_map::const_iterator end_;

        for (std::size_t i_ = 0; i_ < states_; ++i_)
        {
            const dfa_state &state_ = dfa_._states[i_];

            state(stream_);
            stream_ << i_ << std::endl;

            if (state_._end_state)
            {
                end_state(stream_);

                if (state_._push_pop_dfa == dfa_state::push_dfa)
                {
                    push(stream_);
                    stream_ << state_._push_dfa;
                }
                else if (state_._push_pop_dfa == dfa_state::pop_dfa)
                {
                    pop(stream_);
                }

                id(stream_);
                stream_ << static_cast<std::size_t>(state_._id);
                user_id(stream_);
                stream_ << static_cast<std::size_t>(state_._user_id);
                dfa(stream_);
                stream_ << static_cast<std::size_t>(state_._next_dfa);
                stream_ << std::endl;
            }

            if (i_ == 0 && bol_index_ != char_state_machine::npos())
            {
                bol(stream_);
                stream_ << static_cast<std::size_t>(bol_index_) << std::endl;
            }

            if (state_._eol_index != char_state_machine::npos())
            {
                eol(stream_);
                stream_ << static_cast<std::size_t>(state_._eol_index) <<
                    std::endl;
            }

            iter_ = state_._transitions.begin();
            end_ = state_._transitions.end();

            for (; iter_ != end_; ++iter_)
            {
                string_token token_ = iter_->second;

                open_bracket(stream_);

                if (!iter_->second.any() && iter_->second.negatable())
                {
                    token_.negate();
                    negated(stream_);
                }

                string chars_;
                typename string_token::range_vector::const_iterator
                    ranges_iter_ = token_._ranges.begin();
                typename string_token::range_vector::const_iterator
                    ranges_end_ = token_._ranges.end();

                for (; ranges_iter_ != ranges_end_; ++ranges_iter_)
                {
                    if (ranges_iter_->first == '-' ||
                        ranges_iter_->first == '^' ||
                        ranges_iter_->first == ']')
                    {
                        stream_ << '\\';
                    }

                    chars_ = string_token::escape_char
                        (ranges_iter_->first);

                    if (ranges_iter_->first != ranges_iter_->second)
                    {
                        if (ranges_iter_->first + 1 < ranges_iter_->second)
                        {
                            chars_ += '-';
                        }

                        if (ranges_iter_->second == '-' ||
                            ranges_iter_->second == '^' ||
                            ranges_iter_->second == ']')
                        {
                            stream_ << '\\';
                        }

                        chars_ += string_token::escape_char
                            (ranges_iter_->second);
                    }

                    stream_ << chars_;
                }

                close_bracket(stream_);
                stream_ << static_cast<std::size_t>(iter_->first) <<
                    std::endl;
            }

            stream_ << std::endl;
        }
    }
Пример #2
0
    // Internal function actually performing the work of dumping the
    // state machine in DOT.
    static void dump_ex (
        id_type dfa_id_,
        const typename char_state_machine::dfa &dfa_,
        rules &rules_,
        ostream &stream_)
    {
        const std::size_t states_ = dfa_._states.size ();
        typename dfa_state::id_type_string_token_map::const_iterator iter_;
        typename dfa_state::id_type_string_token_map::const_iterator end_;

        stream_ << std::endl;

        for (std::size_t i_ = 0; i_ < states_; ++i_)
        {
            const dfa_state &state_ = dfa_._states[i_];

            const string name = node_name(dfa_id_, i_);
            if (i_ == 0)
            {
                stream_ << "    " << name << " [shape = doublecircle, xlabel=\""
                        << rules_.state(dfa_id_) << "\"];" << std::endl;
            }
            else if (state_._end_state)
            {
                stream_ << "    " << name << " [shape = doublecircle, xlabel=\"id ="
                        << static_cast<std::size_t>(state_._id) << "\"];" << std::endl;
            }
            else {
                stream_ << "    " << name << " [shape = circle];" << std::endl;
            }
        }

        stream_ << std::endl;

        for (std::size_t i_ = 0; i_ < states_; ++i_)
        {
            const dfa_state &state_ = dfa_._states[i_];

            iter_ = state_._transitions.begin ();
            end_ = state_._transitions.end ();

            const string src_name = node_name(dfa_id_, i_);

            for (; iter_ != end_; ++iter_)
            {
                const string dst_name = node_name(dfa_id_, iter_->first);
                stream_ << "    " << src_name << " -> " << dst_name << " [label = \"";

                string_token token_ = iter_->second;

                open_bracket (stream_);

                if (!iter_->second.any () && iter_->second.negatable ())
                {
                    token_.negate ();
                    negated (stream_);
                }

                string chars_;
                typename string_token::range_vector::const_iterator
                    ranges_iter_ = token_._ranges.begin ();
                typename string_token::range_vector::const_iterator
                    ranges_end_ = token_._ranges.end ();

                for (; ranges_iter_ != ranges_end_; ++ranges_iter_)
                {
                    if (ranges_iter_->first == '^' ||
                        ranges_iter_->first == ']')
                    {
                        stream_ << "\\\\";
                    }

                    chars_ = double_escape_char(ranges_iter_->first);

                    if (ranges_iter_->first != ranges_iter_->second)
                    {
                        if (ranges_iter_->first + 1 < ranges_iter_->second)
                        {
                            chars_ += '-';
                        }

                        if (ranges_iter_->second == '^' ||
                            ranges_iter_->second == ']')
                        {
                            stream_ << "\\\\";
                        }

                        chars_ += double_escape_char(ranges_iter_->second);
                    }

                    stream_ << chars_;
                }

                close_bracket (stream_);
                stream_ << "\"];" << std::endl;
            }

            if (state_._end_state) {
                const string dst_name = node_name(state_._next_dfa, 0);
                stream_ << "    " << src_name << " -> " << dst_name
                        << " [style = \"dashed\"];" << std::endl;
            }
        }
    }
Пример #3
0
    static void dump (const basic_state_machine<CharT> &state_machine_, ostream &stream_)
    {
        typename basic_state_machine<CharT>::iterator iter_ =
            state_machine_.begin ();
        typename basic_state_machine<CharT>::iterator end_ =
            state_machine_.end ();

        for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
            dfa_ < dfas_; ++dfa_)
        {
            const std::size_t states_ = iter_->states;

            for (std::size_t i_ = 0; i_ < states_; ++i_)
            {
                state (stream_);
                stream_ << i_ << std::endl;

                if (iter_->end_state)
                {
                    end_state (stream_);
                    stream_ << iter_->id;
                    dfa (stream_);
                    stream_ << iter_->goto_dfa;
                    stream_ << std::endl;
                }

                if (iter_->bol_index != npos)
                {
                    bol (stream_);
                    stream_ << iter_->bol_index << std::endl;
                }

                if (iter_->eol_index != npos)
                {
                    eol (stream_);
                    stream_ << iter_->eol_index << std::endl;
                }

                const std::size_t transitions_ = iter_->transitions;

                if (transitions_ == 0)
                {
                    ++iter_;
                }

                for (std::size_t t_ = 0; t_ < transitions_; ++t_)
                {
                    std::size_t goto_state_ = iter_->goto_state;

                    if (iter_->token.any ())
                    {
                        any (stream_);
                    }
                    else
                    {
                        open_bracket (stream_);

                        if (iter_->token._negated)
                        {
                            negated (stream_);
                        }

                        string charset_;
                        CharT c_ = 0;

                        escape_control_chars (iter_->token._charset,
                            charset_);
                        c_ = *charset_.c_str ();

                        if (!iter_->token._negated &&
                            (c_ == '^' || c_ == ']'))
                        {
                            stream_ << '\\';
                        }

                        stream_ << charset_;
                        close_bracket (stream_);
                    }

                    stream_ << goto_state_ << std::endl;
                    ++iter_;
                }

                stream_ << std::endl;
            }
        }
    }
Пример #4
0
/**
 * Evaluate expression. bForDsp determines which registers and symbols
 * are interpreted. Sets given value and parsing offset.
 * Return error string or NULL for success.
 */
const char* Eval_Expression(const char *in, Uint32 *out, int *erroff, bool bForDsp)
{
	/* in	 : expression to evaluate				*/
	/* out	 : final parsed value					*/
	/* value : current parsed value					*/
	/* mark	 : current character in expression			*/
	/* valid : expression validation flag, set when number parsed	*/
	/* end	 : 'expression end' flag				*/
	/* offset: character offset in expression			*/

	long long value;
	int dummy, offset = 0;
	char mark;
	
	/* Uses global variables:	*/

	par.idx = 0;			/* parenthesis stack pointer	*/
	par.opx[0] = par.vax[0] = 0;	/* additional stack pointers	*/
	op.idx = val.idx = -1;

	id.error = NULL;
	id.valid = false;		/* value validation		*/
	value = 0;

	/* parsing loop, repeated until expression ends */
	do {
		mark = in[offset];
		switch(mark) {
		case '\0':
			break;
		case ' ':
		case '\t':
			offset ++;		/* jump over white space */
			break;
		case '~':			/* prefixes */
			unary(mark);
			offset ++;
			break;
		case '>':			/* operators  */
		case '<':
			offset ++;
			/* check that it's '>>' or '<<' */
			if (in[offset] != mark)
			{
				id.error = CLAC_GEN_ERR;
				break;
			}
			operation (value, mark);
			offset ++;
			break;
		case '|':
		case '&':
		case '^':
		case '+':
		case '-':
		case '*':
		case '/':
			operation (value, mark);
			offset ++;
			break;
		case '(':
			open_bracket ();
			offset ++;
			break;
		case ')':
			value = close_bracket (value);
			offset ++;
			break;
		default:
			/* register/symbol/number value needed? */
			if (id.valid == false) {
				Uint32 tmp;
				int consumed;
				consumed = getValue(&(in[offset]), &tmp, &dummy, bForDsp);
				/* number parsed? */
				if (consumed) {
					offset += consumed;
					id.valid = true;
					value = tmp;
					break;
				}
			}
			id.error = CLAC_GEN_ERR;
		}

	/* until exit or error message					*/
	} while(mark && !id.error);

        /* result of evaluation 					*/
        if (val.idx >= 0)
		*out = val.buf[val.idx];

	/* something to return?						*/
	if (!id.error) {
		if (id.valid) {

			/* evaluate rest of the expression		*/
			operation (value, LOWEST_PREDECENCE);
			if (par.idx)			/* mismatched	*/
				id.error = CLAC_PAR_ERR;
			else				/* result out	*/
				*out = val.buf[val.idx];

		} else {
			if ((val.idx < 0) && (op.idx < 0)) {
				id.error = CLAC_EXP_ERR;
			} else			/* trailing operators	*/
				id.error = CLAC_GEN_ERR;
		}
	}

	*erroff = offset;
	if (id.error) {
		*out = 0;
		return id.error;
	}
	return NULL;
}