Exemplo n.º 1
0
dsk_boolean    dsk_json_parser_finish   (DskJsonParser *parser,
                                         DskError     **error)
{
  switch (parser->lex_state)
    {
    case JSON_LEX_STATE_INIT:
      return DSK_TRUE;

    case JSON_LEX_STATE_TRUE:
      if (parser->fixed_n_chars == 4)
        {
          if (!handle_token (parser, JSON_TOKEN_TRUE, error))
            return DSK_FALSE;
          break;
        }
      else
        goto bad_lex_state;
    case JSON_LEX_STATE_FALSE:
      if (parser->fixed_n_chars == 5)
        {
          if (!handle_token (parser, JSON_TOKEN_FALSE, error))
            return DSK_FALSE;
          break;
        }
      else
        goto bad_lex_state;
    case JSON_LEX_STATE_NULL:
      if (parser->fixed_n_chars == 4)
        {
          if (!handle_token (parser, JSON_TOKEN_NULL, error))
            return DSK_FALSE;
          break;
        }
      else
        goto bad_lex_state;
    case JSON_LEX_STATE_IN_DQ:
    case JSON_LEX_STATE_IN_DQ_BS:
      goto bad_lex_state;
    case JSON_LEX_STATE_IN_NUMBER:
      if (!handle_token (parser, JSON_TOKEN_NUMBER, error))
        return DSK_FALSE;
      break;
    }
  if (parser->parse_state != PARSE_INIT)
    {
      dsk_set_error (error, "unfinished %s",
                     parser->stack[0].type == STACK_NODE_OBJECT ? "object" : "array");
      return DSK_FALSE;
    }
  return DSK_TRUE;


bad_lex_state:
  dsk_set_error (error, "invalid lex state %s at end-of-file",
                 lex_state_names[parser->lex_state]);
  return DSK_FALSE;
}
Exemplo n.º 2
0
int uac_token_exchange(const sip_entity* to,const TokenType toketype_)
{
	eXosip_event_t *event;
	if(send_token(to,toketype_)<1)
	{
		printf("send_token error\n");
		return 0;
	}
	if(uac_waitfor(NULL,EXOSIP_MESSAGE_NEW,&event)<1)
	{
		printf("uac_waitfor error event->type:%d\n",event->type);
		return 0;
	}
	if(handle_token(event->request,toketype_)<1)
	{
		printf("handle_token error\n");
		return 0;
	}
	osip_message_t *g_answer = NULL;/*请求的确认型应答*/
	eXosip_lock();
	eXosip_message_build_answer(event->tid, 200, &g_answer);/*Build default Answer for request*/
	eXosip_message_send_answer(event->tid, 200, g_answer);/*按照规则回复200OK*/
	eXosip_unlock();
	eXosip_event_free (event);
	printf("uac_token_exchange-----finished\n");
	return 1;
	}
Exemplo n.º 3
0
bool
PhraseParser::parse() {
	skip_whitespace();
	discern_token();
	read_token();
	handle_token();
	if (TOK_EOF == m_token.type() || duct::CHAR_EOF == m_curchar) {
		return false;
	}
	return true;
}
Exemplo n.º 4
0
void scan_callback(long tok_id, char* tokentext, int tokentextlen){
  
  TokenRec NewTok;
  NewTok.id=tok_id;
  NewTok.text=std::string(tokentext,tokentextlen);
  TokenList.push_back(NewTok);
  //  std::cout << tokentext;
  if (handle_token()){
    std::cerr << "fatal error";
    exit (1);
  }
  if (tok_id==TOK_EXIT){
    printAllCleanStack();
    //std::cerr << "ready";
  }
}
Exemplo n.º 5
0
int	process( char * struct_name )
{
	int	status;
	int	c;
	int	l;
	char	occi_header_filename[512];
	int	holder;
	char	filter_filename[512];
	char	tn[512];
	char 	token[512];
	FILE * sh;
	sprintf(occi_header_filename,"%s.h",struct_name);
	sprintf(filter_filename, "%s_occi_filter.h", struct_name);
	sprintf(tn,"%s.c", struct_name);
	if (!( sh = fopen( occi_header_filename, "r" )))
		return( failure(40,"file not found",occi_header_filename) );
	else if (!( C.target = fopen( tn, "w" )))
	{
		fclose(sh);
		return( failure(46,"creating file",tn) );
	}
	else
	{
		holder = check_cool_cosacs( struct_name );
		file_header( C.target, tn, occi_header_filename, filter_filename);
		while ((c = remove_white_space( sh )))
		{
			if ( is_punctuation(c) )
			{
				if (!(status = handle_punctuation(sh,getch(sh))))
					continue;
				else	break;
			}
			else if (!( l = get_token(sh,token,512) ))
				break;
			else if (!( status = handle_token(sh,token) ))
				continue;
			else	break;
		}

		file_footer( C.target, tn );
		fclose(C.target);
		fclose(sh);
		C.gensql = holder;
	}		
	return( 0 );
}
Exemplo n.º 6
0
double RPNParser::evaluate(double x)
{
    size_t last_match_start = 0;
    size_t last_match_end = 0;
    while(last_match_end != std::string::npos)
    {
        last_match_end = m_expression.find(' ', last_match_start);
        std::string token = m_expression.substr(last_match_start, last_match_end - last_match_start);
        last_match_start = last_match_end + 1;
        // Evaluate it
        if( ! handle_token(token, x))
        {
            return nan("");
        }
    }
    return m_stack.top();
}
Exemplo n.º 7
0
int	schema( char * nptr )
{
	int	status;
	int	c;
	int	l;
	char	sn[512];
	char	tn[512];
	char 	token[512];
	FILE * sh;
	sprintf(sn,"%s.h",nptr);
	sprintf(tn,"%s.xsd",nptr);
	if (!( sh = fopen( sn, "r" )))
		return( failure(40,"file not found",sn) );
	else if (!( C.target = fopen( tn, "w" )))
	{
		fclose(sh);
		return( failure(46,"creating file",tn) );
	}
	else
	{
		schema_header( C.target, tn, sn );
		while ((c = remove_white_space( sh )))
		{
			if ( is_punctuation(c) )
			{
				if (!(status = handle_punctuation(sh,getch(sh))))
					continue;
				else	break;
			}
			else if (!( l = get_token(sh,token,512) ))
				break;
			else if (!( status = handle_token(sh,token) ))
				continue;
			else	break;
		}

		schema_footer( C.target, tn );
		fclose(C.target);
		fclose(sh);
	}		
	return( 0 );
}
Exemplo n.º 8
0
int parse( Parser * parser, FileBuf * buf, FILE * out )
{
	if( !parser || !out )
		return 0;

	Token cur_tk = new_tok();

	for(;;) {
		cur_tk = get_token( buf->head );
		if( cur_tk.type == NOTOKEN
		    || cur_tk.type == EOF_TOK )
		{
			break;
		}
		if( cur_tk.type == EOL_TOK ) {
			++parser->cur_line;
		}
		if( cur_tk.type == PREPROC
		    || cur_tk.type == MUL_COMMENT
		    || cur_tk.type == STR_LIT )
		{
			parser->cur_line += how_much_eols( &cur_tk );
		}
		
		if( !handle_token( &cur_tk, parser, out ) )
			return 0;
		if( cur_tk.type != EOL_TOK ) {
			parser->prev_tk = cur_tk;
		}
		buf->head = cur_tk.end;
#ifdef DEBUG_ON
		check_parser( parser );
#endif // DEBUG_ON
	}
	return 1;
}
Exemplo n.º 9
0
/* --- lexing --- */
dsk_boolean
dsk_json_parser_feed     (DskJsonParser *parser,
                          size_t         n_bytes,
                          const uint8_t *bytes,
                          DskError     **error)
{
  while (n_bytes > 0)
    {
      switch (parser->lex_state)
        {
        case JSON_LEX_STATE_INIT:
          while (n_bytes > 0 && dsk_ascii_isspace (*bytes))
            {
              if (*bytes == '\n')
                parser->line_no++;
              bytes++;
              n_bytes--;
            }
          if (n_bytes == 0)
            break;
          switch (*bytes)
            {
            case 't': case 'T':
              parser->lex_state = JSON_LEX_STATE_TRUE;
              parser->fixed_n_chars = 1;
              bytes++;
              n_bytes--;
              break;
            case 'f': case 'F':
              parser->lex_state = JSON_LEX_STATE_FALSE;
              parser->fixed_n_chars = 1;
              bytes++;
              n_bytes--;
              break;
            case 'n': case 'N':
              parser->lex_state = JSON_LEX_STATE_NULL;
              parser->fixed_n_chars = 1;
              bytes++;
              n_bytes--;
              break;
            case '"':
              parser->lex_state = JSON_LEX_STATE_IN_DQ;
              parser->str_len = 0;
              bytes++;
              n_bytes--;
              break;
            case '-': case '+':
            case '0': case '1': case '2': case '3': case '4': 
            case '5': case '6': case '7': case '8': case '9': 
              parser->lex_state = JSON_LEX_STATE_IN_NUMBER;
              parser->str_len = 0;
              append_to_string_buffer (parser, 1, bytes);
              bytes++;
              n_bytes--;
              break;

#define WRITE_CHAR_TOKEN_CASE(character, SHORTNAME) \
            case character: \
              if (!handle_token (parser, JSON_TOKEN_##SHORTNAME, error)) \
                return DSK_FALSE; \
              n_bytes--; \
              bytes++; \
              break
            WRITE_CHAR_TOKEN_CASE('{', LBRACE);
            WRITE_CHAR_TOKEN_CASE('}', RBRACE);
            WRITE_CHAR_TOKEN_CASE('[', LBRACKET);
            WRITE_CHAR_TOKEN_CASE(']', RBRACKET);
            WRITE_CHAR_TOKEN_CASE(',', COMMA);
            WRITE_CHAR_TOKEN_CASE(':', COLON);
#undef WRITE_CHAR_TOKEN_CASE

            case '\n':
              parser->line_no++;
              n_bytes--;
              bytes++;
              break;
            case '\t': case '\r': case ' ':
              n_bytes--;
              bytes++;
              break;
            default:
              dsk_set_error (error,
                             "unexpected character %s in json (line %u)",
                             dsk_ascii_byte_name (*bytes), parser->line_no);
              return DSK_FALSE;
            }
          break;

#define WRITE_FIXED_BAREWORD_CASE(SHORTNAME, lc, UC, length) \
        case JSON_LEX_STATE_##SHORTNAME: \
          if (parser->fixed_n_chars == length) \
            { \
              /* are we at end of string? */ \
              if (dsk_ascii_isalnum (*bytes)) \
                { \
                  dsk_set_error (error,  \
                                 "got %s after '%s' (line %u)", \
                                 dsk_ascii_byte_name (*bytes), lc, \
                                 parser->line_no); \
                  return DSK_FALSE; \
                } \
              else \
                { \
                  parser->lex_state = JSON_LEX_STATE_INIT; \
                  if (!handle_token (parser, JSON_TOKEN_##SHORTNAME, \
                                     error)) \
                    return DSK_FALSE; \
                } \
            } \
          else if (*bytes == lc[parser->fixed_n_chars] \
                || *bytes == UC[parser->fixed_n_chars]) \
            { \
              parser->fixed_n_chars += 1; \
              n_bytes--; \
              bytes++; \
            } \
          else \
            { \
              dsk_set_error (error, \
                           "unexpected character %s (parsing %s) (line %u)", \
                           dsk_ascii_byte_name (*bytes), UC, parser->line_no); \
              return DSK_FALSE; \
            } \
          break;
        WRITE_FIXED_BAREWORD_CASE(TRUE, "true", "TRUE", 4);
        WRITE_FIXED_BAREWORD_CASE(FALSE, "false", "FALSE", 5);
        WRITE_FIXED_BAREWORD_CASE(NULL, "null", "NULL", 4);
#undef WRITE_FIXED_BAREWORD_CASE

        case JSON_LEX_STATE_IN_DQ:
          if (*bytes == '"')
            {
              // TODO ASSERT utf16_surrogate == 0
              if (!handle_token (parser, JSON_TOKEN_STRING, error))
                return DSK_FALSE;
              bytes++;
              n_bytes--;
              parser->lex_state = JSON_LEX_STATE_INIT;
            }
          else if (*bytes == '\\')
            {
              n_bytes--;
              bytes++;
              parser->bs_sequence_len = 0;
              parser->lex_state = JSON_LEX_STATE_IN_DQ_BS;
            }
          else
            {
              // TODO ASSERT utf16_surrogate == 0
              unsigned i;
              if (*bytes == '\n')
                parser->line_no++;
              for (i = 1; i < n_bytes; i++)
                if (bytes[i] == '"' || bytes[i] == '\\')
                  break;
                else if (bytes[i] == '\n')
                  parser->line_no++;
              append_to_string_buffer (parser, i, bytes);
              n_bytes -= i;
              bytes += i;
            }
          break;
        case JSON_LEX_STATE_IN_DQ_BS:
          if (parser->bs_sequence_len == 0)
            {
              switch (*bytes)
                {
#define WRITE_BS_CHAR_CASE(bschar, cchar) \
                case bschar: \
                  /* TODO ASSERT utf16_surrogate == 0 */ \
                  append_char_to_string_buffer (parser, cchar); \
                  bytes++; \
                  n_bytes--; \
                  parser->lex_state = JSON_LEX_STATE_IN_DQ; \
                  break
                WRITE_BS_CHAR_CASE('b', '\b');
                WRITE_BS_CHAR_CASE('f', '\f');
                WRITE_BS_CHAR_CASE('n', '\n');
                WRITE_BS_CHAR_CASE('r', '\r');
                WRITE_BS_CHAR_CASE('t', '\t');
                WRITE_BS_CHAR_CASE('/', '/');
                WRITE_BS_CHAR_CASE('"', '"');
                WRITE_BS_CHAR_CASE('\\', '\\');
#undef WRITE_BS_CHAR_CASE
                case 'u':
                  parser->bs_sequence[parser->bs_sequence_len++] = *bytes++;
                  n_bytes--;
                  break;
                default:
                  dsk_set_error (error,
                               "invalid character %s after '\\' (line %u)",
                               dsk_ascii_byte_name (*bytes), parser->line_no);
                  return DSK_FALSE;
                }
            }
          else
            {
              /* must be \uxxxx (the only multi-character \ sequence) */
              if (!dsk_ascii_isxdigit (*bytes))
                {
                  dsk_set_error (error,
                               "expected 4 hex digits after \\u, got %s (line %u)",
                               dsk_ascii_byte_name (*bytes), parser->line_no);
                  return DSK_FALSE;
                }
              parser->bs_sequence[parser->bs_sequence_len++] = *bytes++;
              n_bytes--;
              if (parser->bs_sequence_len == 5)
                {
                  char utf8buf[8];
                  unsigned value;
                  parser->bs_sequence[5] = 0;
                  value = strtoul (parser->bs_sequence + 1, NULL, 16);
                  if (DSK_UTF16_LO_SURROGATE_START <= value
                   && value <= DSK_UTF16_LO_SURROGATE_END)
                    {
                      if (parser->utf16_surrogate == 0)
                        {
                          dsk_set_error (error,
                                       "low (second) half of surrogate pair was encountered without high-half, line %u",
                                       parser->line_no);
                          return DSK_FALSE;
                        }
                      uint32_t code = dsk_utf16_surrogate_pair_to_codepoint (parser->utf16_surrogate, value);
                      append_to_string_buffer (parser,
                                               dsk_utf8_encode_unichar (utf8buf, code),
                                               (const uint8_t *) utf8buf);
                      parser->utf16_surrogate = 0;
                    }
                  else if (DSK_UTF16_HI_SURROGATE_START <= value
                        && value <= DSK_UTF16_HI_SURROGATE_END)
                    {
                      if (parser->utf16_surrogate != 0)
                        {
                          dsk_set_error (error,
                                       "got two first-half surrogate pairs (UTF16 surrogate \\u%04u was followed by \\%04u), line %u",
                                       parser->utf16_surrogate, value, parser->line_no);
                          return DSK_FALSE;
                        }
                      parser->utf16_surrogate = value;
                    }
                  else
                    {
                      if (parser->utf16_surrogate != 0)
                        {
                          dsk_set_error (error,
                                       "second half of UTF16 surrogate \\u%04u was not preceded by utf16, line %u", 
                                       parser->utf16_surrogate, parser->line_no);
                          return DSK_FALSE;
                        }
                      append_to_string_buffer (parser,
                                               dsk_utf8_encode_unichar (utf8buf, value),
                                               (const uint8_t *) utf8buf);
                      parser->utf16_surrogate = 0;
                    }
                  parser->lex_state = JSON_LEX_STATE_IN_DQ;
                }
#if 0
              else
                {
                  dsk_set_error (error,
                               "internal error: expected 4 hex digits (line %u)",
                               parser->line_no);
                  return DSK_FALSE;
                }
#endif
            }
          break;
        case JSON_LEX_STATE_IN_NUMBER:
          if (dsk_ascii_isdigit (*bytes)
           || *bytes == '.'
           || *bytes == 'e'
           || *bytes == 'E'
           || *bytes == '+'
           || *bytes == '-')
            {
              append_to_string_buffer (parser, 1, bytes);
              bytes++;
              n_bytes--;
            }
          else
            {
              /* append the number token */
              if (!handle_token (parser, JSON_TOKEN_NUMBER, error))
                return DSK_FALSE;

              /* go back to init state (do not consume character) */
              parser->lex_state = JSON_LEX_STATE_INIT;
            }
          break;
        default:
          dsk_error ("unhandled lex state %u", parser->lex_state);
        }
    }
  return DSK_TRUE;
}