Exemplo n.º 1
0
static atom_p parse_symbol(parsebuf_p pb, char terminator, int print_mode)
{
  char * symbol_start;
  char * symbol_stop;
  int symbol_length;
  uint_t symbol_address;
  static char symbol_buffer[128];
  DEBUG();

  if (parse_literal(pb, &symbol_start, &symbol_stop, terminator))
    return NULL;
  if (symbol_start[0] != '@' ||
      !(('a' <= symbol_start[1] && symbol_start[1] <= 'z')
      || ('A' <= symbol_start[1] && symbol_start[1] <= 'Z')
      || ('0' <= symbol_start[1] && symbol_start[1] <= '9')
      || symbol_start[1] == '_')) return NULL;
  symbol_length = symbol_stop - (++symbol_start);
  if (symbol_length > sizeof(symbol_buffer) - 1)
    return NULL;
  strncpy(symbol_buffer, symbol_start, symbol_length);
  symbol_buffer[symbol_length] = '\0';
  symbol_address = splinter_get_symbol(symbol_buffer);
  if (!symbol_address)
    return NULL;
  DEBUG("parsed symbol atom [%s] -> [%lx]", symbol_buffer, symbol_address);
  return atom_alloc_plain(print_mode ? operator_hex : operator_value, symbol_address);
}
Exemplo n.º 2
0
static atom_p parse_variable(parsebuf_p pb, char terminator, int print_mode)
{
  char * var_start;
  char * var_stop;
  char * var_value;
  int var_length;
  static char var_buffer[128];
  DEBUG();

  if (parse_literal(pb, &var_start, &var_stop, terminator))
    return NULL;
  if (var_start[0] != '$' ||
      !(('a' <= var_start[1] && var_start[1] <= 'z')
      || ('A' <= var_start[1] && var_start[1] <= 'Z')
      || ('0' <= var_start[1] && var_start[1] <= '9')
      || var_start[1] == '_')) return NULL;
  var_length = var_stop - (++var_start);
  if (var_length > sizeof(var_buffer) - 1)
    return NULL;
  strncpy(var_buffer, var_start, var_length);
  var_buffer[var_length] = '\0';
  var_value = splinter_find_variable(var_buffer);
  if (!var_value)
    return NULL;
  var_length = strlen(var_value);
  DEBUG("parsed variable atom [%s] -> [%s]", var_buffer, var_value);
  return atom_alloc_string(print_mode ? operator_string : operator_value, var_value, var_value + var_length);
}
Exemplo n.º 3
0
/*
 * Name:    parse_macro
 * Purpose: separate literals from keys in a macro definition
 * Date:    June 5, 1992
 * Passed:  macro_key:  key that we are a assigning a macro to
 *          residue:    pointer to macro defs
 * Notes:   for each token in macro def, find out if it's a literal or a
 *             function key.
 *          a literal begins with a ".  to put a " in a macro def, precede
 *             a " with a ".
 */
void parse_macro( int macro_key, char *residue )
{
int  rc;
char literal[1042];
char *l;
int  key_no;

   /*
    * reset any previous macro def.
    */
   initialize_macro( macro_key );
   while (residue != NULL) {
      /*
       * skip over any leading spaces.
       */
      while (*residue == ' ')
         ++residue;

      /*
       * done if we hit a comment
       */
      if (*residue == ';')
         residue = NULL;

      /*
       * check for a literal.
       */
      else if (*residue == '\"') {
         rc = parse_literal( macro_key, residue, literal, &residue );
         if (rc == OK) {
            l = literal;
            while (*l != '\0'  &&  rc == OK) {
               rc = record_keys( macro_key, *l );
               ++l;
            }
         } else {
            printf( "==> %s", line_in );
            printf( "Literal not recognized: line %u  : literal  %s\n", line_no, literal );
         }

      /*
       * check for a function key.
       */
      } else {
         residue = parse_token( residue, literal );
         key_no = search( literal, valid_keys, AVAIL_KEYS );
         if (key_no != ERROR)
            record_keys( macro_key, key_no+256 );
         else {
            printf( "==> %s", line_in );
            printf( "Unrecognized key: line %u  : key %s\n", line_no, literal );
         }
      }
   }
   check_macro( macro_key );
}
Exemplo n.º 4
0
void parse_operand(char *string, EOperandInfo *operand, int index, EShaderInstruction instruction) {
	int operidx = 0;
	char original_string[128];
	memset(&original_string,0, sizeof(original_string));
	bool negate = false;
	if(string[0] == '-') {
		string++;
		negate = true;
	}
	strcpy(original_string, string);
	char *num = find_first_nonalpha(string);
	char reg[32];
	memset(&reg,0,sizeof(reg));
	if(num != NULL) { //"vec1"
		char *accessor = strchr(num, '.');
		if(accessor != NULL) { //"vec0.rgba"
			*(accessor) = 0;
			operidx = atoi(num);
			accessor++;
			char *non_alpha = find_first_nonalpha(string);
			if(non_alpha) {
				*non_alpha = 0;
			}
			EShaderRegister reg = find_register_by_name(string);
			operand->registers[index] = reg;
			operand->accessors[index] = parse_accessors(reg, accessor);
			operand->indexes[index] = operidx;
		} else { //"vec0"
			int regnum = atoi(num);
			operand->indexes[index] = regnum;
			operand->accessors[index] = 0;//EVectorFlags_Red|EVectorFlags_Blue|EVectorFlags_Green|EVectorFlags_Alpha;
			*num = 0;
			EShaderRegister reg = find_register_by_name(string);
			operand->registers[index] = reg;
		}
	}
	if(negate) {
		operand->accessors[index] |= EVectorFlags_Negate;
	}
	operand->registers[index] = find_register_by_name(string);
	if(instruction >= EShaderInstruction_BranchBegin && instruction <= EShaderInstruction_BranchEnd) {
		operand->jump_index = find_jump_index_by_name(original_string);
		//printf("read branch\n");
		return;
	}
	if(operand->registers[index] == -1) { //literal value
		if(original_string[0] == '#' || original_string[0] == ';') { //skip marker definition
		} else {
			parse_literal(operand, index, original_string);
			//printf("some literal: %s\n",original_string);
		}		
	}
}
Exemplo n.º 5
0
void parse()
{
    ncomments = nchars = 0;
    p = line;
    line[0] = 0;

    for (next_char(); c > 0; next_char()) {
        while (c == ' ' || c == '\n') next_char();

        if (c == '/') parse_comment();
        else if (c == '"') parse_string();
        else parse_literal();
    }
}
Exemplo n.º 6
0
node_p tree_copy(const node_cp& source, CallFactory factory)
{
    // Simply parse the sexpr.  This isn't the most efficient but is
    // extremely simple.
    size_t i = 0;
    string sexpr = source->to_s();
    if (sexpr.empty()) {
        return node_p();
    }
    if (sexpr[0] == '(') {
        return parse_call(sexpr, i, factory);
    }
    return parse_literal(sexpr, i);
}
Exemplo n.º 7
0
    std::pair< bool, bool > parser::parse(const char* begin, const char* end)
    {
        assert( !state_.empty() );

        for ( ; begin != end && !state_.empty(); )
        {
            switch ( main_state( state_.top() ) )
            {
            case idle_parsing:
                begin = eat_white_space(begin, end);

                if ( begin != end )
                {
                    state_.top() = parse_idle(*begin);
                }
                break;
            case start_number_parsing:
                begin = parse_number(begin, end);
                break;
            case start_array_parsing:
                begin = parse_array(begin, end);
                break;
            case start_object_parsing:
                begin = parse_object(begin, end);
                break;
            case start_string_parsing:
                begin = parse_string(begin, end);
                break;
            case start_true_parsing:
            case start_false_parsing:
            case start_null_parsing:
                begin = parse_literal(begin, end);
                break;
            default:
                assert(!"should not happen");
            }
        }

        // consume trailing whitespaces
        if ( state_.empty() )
            begin = eat_white_space(begin, end);

        return std::make_pair( begin == end, state_.empty() );
    }
Exemplo n.º 8
0
static struct __operator * __find_closest_token(parsebuf_p pb, char terminator)
{
  char * start, * stop;
  struct __operator *closest_operator = NULL;
  struct __operator *current_operator = operators;
  int closest_distance = -1, current_distance;
  parsebuf_t buf;
  DEBUG();

  __parsebuf_snapshot(pb, &buf);

  if(parse_literal(pb, &start, &stop, terminator))
    return NULL;

  for(; current_operator->token; current_operator++)
  {
    if((current_distance = __strings_distance(start, stop, current_operator->token)) == 0) {
      // A perfect match, return it
      DEBUG("found perfect match for [%s]", current_operator->token);
      closest_distance = current_distance;
      closest_operator = current_operator;
      break;
    }
    if(current_distance > 0) {
      if(closest_distance < 0 || current_distance > closest_distance) {
        closest_distance = current_distance;
        closest_operator = current_operator;
      }
    }
  }

  if (closest_distance > 0 && closest_distance < 3) {
    splinter_error_return(NULL, "%s ambiguous keyword @ %d:%d - %d:%d", buf.n, buf.l, buf.c, pb->l, pb->c);
  }

  if(closest_operator) {
    DEBUG("parsed keyword [%s] distance = %d", closest_operator->token, closest_distance);
    return closest_operator;
  }

  splinter_error_return(NULL, "%s invalid keyword @ %d:%d - %d:%d", buf.n, buf.l, buf.c, pb->l, pb->c);
  return NULL;
}
Exemplo n.º 9
0
Arquivo: parse.c Projeto: jkdewar/rook
/*----------------------------------------------------------------------*/
static ast_expression_t *parse_value(parse_state_t *p) {
    token_t *token = peek_token(p);

    if (token == NULL) {
    } else if (token->type == TK_INT_LITERAL ||
               token->type == TK_FLOAT_LITERAL ||
               token->type == TK_DOUBLE_LITERAL ||
               token->type == TK_STRING_LITERAL) {
        return parse_literal(p);
    } else if (token->type == TK_IDENTIFIER) {
        next_token(p);
        if (test_token(peek_token(p), TK_LBRACKET)) {
            rewind_token(p);
            return parse_function_call(p);
        } else {
            rewind_token(p);
            return parse_variable(p);
        }
    }
    error(p, "value expected");
    return NULL; /* unreachable */
}
Exemplo n.º 10
0
Arquivo: peg.c Projeto: ctelfer/catlib
static int parse_primary(struct peg_grammar_parser *pgp, struct peg_cursor *pc,
			 int *prip)
{
	struct peg_grammar *peg = pgp->peg;
	int pri;
	int rv;
	int match = -1;
	struct peg_cursor npc = *pc;
	int prefix = PEG_ATTR_NONE;
	int suffix = PEG_ATTR_NONE;
	int action = PEG_ACT_NONE;
	struct raw r = { 0, NULL };
	struct peg_node *pn;

	if ( string_match(pgp, "&", &npc) )
		prefix = PEG_ATTR_AND;
	else if ( string_match(pgp, "!", &npc) )
		prefix = PEG_ATTR_NOT;

	if ( (rv = parse_id_and_not_arrow(pgp, &npc, &match)) != 0 ) {
		if ( rv < 0 )
			goto err;
	} else if ( (rv = parse_paren_expr(pgp, &npc, &match)) != 0 ) {
		if ( rv < 0 )
			goto err;
	} else if ( (rv = parse_literal(pgp, &npc, &match)) != 0 ) {
		if ( rv < 0 )
			goto err;
	} else if ( (rv = parse_class(pgp, &npc, &match)) != 0 ) {
		if ( rv < 0 )
			goto err;
	} else {
		if ( prefix == PEG_ATTR_NONE )
			return 0;
		pgp->err = PEG_ERR_BAD_PRIMARY;
		pgp->eloc = *pc;
		return -1;
	}

	pri = peg_node_new(peg, PEG_PRIMARY, pc->line);
	if ( pri < 0 ) {
		pgp->err = PEG_ERR_NOMEM;
		goto err;
	}

	if ( string_match(pgp, "?", &npc) )
		suffix = PEG_ATTR_QUESTION;
	else if ( string_match(pgp, "*", &npc) )
		suffix = PEG_ATTR_STAR;
	else if ( string_match(pgp, "+", &npc) )
		suffix = PEG_ATTR_PLUS;
	else
		suffix = PEG_ATTR_NONE;

	rv = parse_code(pgp, &npc, &r);
	if ( rv < 0 )
		goto err;
	if ( rv > 0 ) {
		action = PEG_ACT_CODE;
	} else {
		rv = parse_action_label(pgp, &npc, &r);
		if ( rv < 0 )
			goto err;
		if ( rv > 0 )
			action = PEG_ACT_LABEL;
	}

	pn = NODE(peg, pri);
	pn->pn_next = -1;
	pn->pp_match = match;
	pn->pp_prefix = prefix;
	pn->pp_suffix = suffix;
	pn->pp_action = action;
	pn->pn_action_cb = NULL;
	pn->pp_code = r;

	*pc = npc;
	*prip = pri;
	return 1;

err:
	peg_node_free(peg, match);
	return -1;
}
Exemplo n.º 11
0
static ZZJSON *parse_null(ZZJSON_CONFIG *config) {
    return parse_literal(config, (char *)"null", ZZJSON_NULL);
}
Exemplo n.º 12
0
static ZZJSON *parse_false(ZZJSON_CONFIG *config) {
    return parse_literal(config, (char *)"false", ZZJSON_FALSE);
}
Exemplo n.º 13
0
static ZZJSON *parse_true(ZZJSON_CONFIG *config) {
    return parse_literal(config, (char *)"true", ZZJSON_TRUE);
}
Exemplo n.º 14
0
/**
 * Constructs the expression tree.
 */
void Filter::parse(const char* exp, int32_t len, Node *node, bool debug)
{
    if (debug)
    {
        std::cerr << "parsing \"";
        for (int32_t i=0; i<len; ++i)
            std::cerr << exp[i] ;
        std::cerr << "\" " << len << "\n";
    }

    //******************************
    //trim white spaces and brackets
    //******************************
    while (*exp==' ') ++exp;
    while (exp[len-1]==' ') --len;
    trim_brackets(exp, len);

    //this is a literal
    if (is_literal(exp, len))
    {
        //will not recurse any further
        return parse_literal(exp, len, node, debug);
    }
    //this is guaranteed to be decomposed unless there is an error in the expression
    else
    {
        const char* p = exp; //points to end of first part
        const char* q = exp; //points to start of second part
        const char* r = exp; //for iteration

        int32_t type = INT_MAX;

        while(r-exp!=len)
        {
            fwd_to_closing_bracket(r, len);

            int32_t oplen = -1;
            int32_t ctype = peek_op(r, len, oplen, debug);

            if(ctype!=-1)
            {
                if (ctype<type)
                {
                    if (debug) std::cerr<< "\tupdating type\n";
                    type = ctype;
                    p = r-1;
                    q = r+oplen;
                }

                r += oplen-1;
            }

            ++r;
        }

        if (type==-1)
        {
            kstring_t s = {0,0,0};
            kputsn(exp, len, &s);
            fprintf(stderr, "[%s:%d %s] expression not correct %s\n", __FILE__, __LINE__, __FUNCTION__, s.s);
            if (s.m) free(s.s);
            exit(1);
        }

        node->type = type;
        
        node->left = new Node();
        parse(exp, p-exp+1, node->left, debug);

        node->right = new Node();
        parse(q, len-(q-exp), node->right, debug);
    }
}