Ejemplo n.º 1
0
int		fill_tree(t_shell *shell, char *command_line)
{
  t_tree	*up;
  char		**commands;
  int		i;

  if ((commands = my_str_to_wordtab(command_line, " \t")) == NULL)
    return (nothing_more(shell));
  i = -1;
  while (commands != NULL && commands[++i] != NULL)
    {
      if ((is_token(shell->tokens, commands[i]) >= 0) &&
	  ((shell->tree->right = malloc(sizeof(t_tree))) != NULL))
	{
	  shell->tree->token = is_token(shell->tokens, commands[i]);
	  if ((shell->tree->left = get_left(commands, i)) == NULL)
	    return (-1);
	  up = shell->tree;
	  shell->tree = shell->tree->right;
	  shell->tree->up = up;
	  return (fill_tree(shell, new_command(commands, i)));
	}
    }
  return (last_command(shell, commands, i));
}
bool expression_parser::is_name(any_regular_t& result) {
    if (!is_token(at_k))
        return false;

    if (!is_token(keyword_k, result) && !is_token(identifier_k, result))
        throw_exception("identifier or keyword required.");

    return true;
}
Ejemplo n.º 3
0
const token* parser::eat_token(token_type type, const char* value)
{
  if (is_token(type) == false) throw bad_token(this->state_get(), type);
  if (is_token(type, value) == false) throw bad_token_value(this->state_get(), type, value);

  const token* last = this->state_get().get();

  this->state_incr();
  return last;
}
Ejemplo n.º 4
0
//  sheet_specifier = [lead_comment] "sheet" identifier "{" { qualified_cell_decl } "}" [trail_comment].
bool adam_parser::is_sheet_specifier(name_t& name)
{
/* REVISIT (sparent) : Top level block is ignored. */

    is_token(lead_comment_k);
    if (!is_keyword(sheet_k)) return false;
    if(!is_identifier(name))
        throw_exception("sheet name identifier expected");
    require_token(open_brace_k);
    while (is_qualified_cell_decl()) { }
    require_token(close_brace_k);
    is_token(trail_comment_k);
    return true;
}
//  equality_expression = relational_expression { ("==" | "!=") relational_expression }.
bool expression_parser::is_equality_expression(array_t& expression_stack) {
    if (!is_relational_expression(expression_stack))
        return false;

    bool is_equal = false;

    while ((is_equal = is_token(equal_k)) || is_token(not_equal_k)) {
        if (!is_relational_expression(expression_stack))
            throw_exception("Primary required.");

        expression_stack.push_back(is_equal ? any_regular_t(equal_k) : any_regular_t(not_equal_k));
    }

    return true;
}
Ejemplo n.º 6
0
int ini_read_bool_section(FFILE f, const char *p_section, const char *p_template, int dflt)
{
  char line[MAX_TOKEN_LEN];
  
  if (!ini_read_string_section(f, p_section, p_template, line, MAX_TOKEN_LEN, NULL))
    return (dflt);
  else {
    if (is_token(line, TOKEN_FALSE1) || is_token(line, TOKEN_FALSE2))
      return (FALSE);
    else if (is_token(line, TOKEN_TRUE1) || is_token(line, TOKEN_TRUE2))
      return (TRUE);
    else
      return (dflt);
  }
}
Ejemplo n.º 7
0
int ini_read_bool(FFILE f, const char *p_template, int dflt)
{
    char line[MAX_TOKEN_LEN];

    ini_read_string(f, p_template, line, MAX_TOKEN_LEN, "");
    if (line[0] == '\0')
        return (dflt);
    else {
        if (is_token(line, TOKEN_FALSE1) || is_token(line, TOKEN_FALSE2))
            return (FALSE);
        else if (is_token(line, TOKEN_TRUE1) || is_token(line, TOKEN_TRUE2))
            return (TRUE);
        else
            return (dflt);
    }
}
Ejemplo n.º 8
0
static int get_message_start_pos(char *buff, size_t bufflen) {
	/*FIXME still to optimize and better test, specially REQUEST PATH and error path*/
	int i;
	int res=0;
	int status_code;
	char method[17]={0};
	char saved_char1;
	char sip_version[10]={0};
	int saved_char1_index;

	for(i=0; i<(int)bufflen-12;i++) { /*9=strlen( SIP/2.0\r\n)*/
		switch (buff[i]) { /*to avoid this character to be ignored by scanf*/
			case '\r':
			case '\n':
			case ' ' :
			case '\t':
				continue;
			default:
				break;
		}
		saved_char1_index=bufflen-1;
		saved_char1=buff[saved_char1_index]; /*make sure buff is null terminated*/
		buff[saved_char1_index]='\0';
		res=sscanf(buff+i,"SIP/2.0 %d ",&status_code);
		if (res!=1) res=sscanf(buff+i,"HTTP/1.%*i %d ",&status_code); /*might be HTTP ?*/
		if (res!=1) {
			res= sscanf(buff+i,"%16s %*s %9s\r\n",method,sip_version)==2
					&& is_token(method,sizeof(method))
					&& (strcmp("SIP/2.0",sip_version)==0 || strncmp("HTTP/1.",sip_version,strlen("HTTP/1."))==0);
		}
		buff[saved_char1_index]=saved_char1;
		if (res==1) return i;
	}
	return -1;
}
Ejemplo n.º 9
0
char *ini_read_string_section(FFILE f, const char *p_section,
                              const char *p_template, char *p_out,
                              int max_len, const char *p_default)
{
  char line[MAX_TOKEN_LEN];
  char section[MAX_TOKEN_LEN];
  bool section_found = FALSE;

  fseek(f, 0, SEEK_SET);
  while (fgets(line, MAX_TOKEN_LEN, f)) {
    if(!section_found) {
      section_found = (bool)read_section(line, section, MAX_TOKEN_LEN);
      if(section_found) {
        section_found = !strncasecmp(p_section, section, MAX_TOKEN_LEN);
      }
      if(section_found)
        continue;
    }
    
    if(section_found && is_section(line)) {
      // we hit next section - so it's not found
      break;
    }
  
    if(section_found) {
      int len = is_token(line, p_template);
      char *p_rest;
      if (len && (p_rest = ini_skip_separator(line + len))) {
        return (ini_read_param(p_rest, p_out, max_len));
      }
    }
  }

  return p_default ? (strcpy(p_out, p_default)) : NULL;
}
Ejemplo n.º 10
0
void		lexer(char *line, t_lex *lst)
{
	int		param[3];

	param[2] = 0;
	while (*line)
	{
		no_space(&line);
		if (!*line)
			break ;
		if ((is_token(line, param)))
		{
			add_token(&lst, param);
			line = line + param[1];
		}
		else if ((is_word(line, param)))
		{
			add_word(&lst, param, line);
			line = line + param[1];
		}
		else
		{
			add_word(&lst, NULL, line);
			line++;
		}
	}
	parser(&lst);
}
Ejemplo n.º 11
0
static char *parse_token(http_parser_t *parser)
{
    char *old_ptr, *start, *token;

    old_ptr = parser->parse_ptr;
    start = old_ptr;
    do {
        if (old_ptr - parser->data >= parser->len)
            return NULL;

        if (is_token(*old_ptr))
            old_ptr++;
        else
            break;
    } while (1);

    parser->parse_ptr = old_ptr;

    token = malloc(old_ptr - start + 1);
    ASSERT(token != NULL);

    strncpy(token, start, old_ptr - start);
    token[old_ptr - start] = '\0';

    return token;
}
Ejemplo n.º 12
0
/* Consumes all characters up to and including the next Brainfuck token and
   returns the token, or EOF if there are no more tokens. */
static int get_token(FILE* source)
{
	int character;
	while ((character = getc(source)) != EOF)
		if (is_token(character)) break;
	return character;
}
Ejemplo n.º 13
0
//  define_expression       = "<==" expression.
bool adam_parser::is_define_expression(line_position_t& position, array_t& expression)
{
    if (!is_token(is_k)) return false;
    
    position = next_position();
    require_expression(expression);
    return true;
}
// examples might be ".OUT('GN1')" or ".OUT(.LABEL *1)"?
// I think I have a typo or three:
// OUTPUT = ('.OUT' '('
// \$ OUT1 ')' / '.LABEL' .OUT('LB') OUT1) .OUT('OUT') .,
void output() {
  istoken(TOKEN_OUT);
  istoken(TOKEN_OPEN_PAREN);
  is_token(TOKEN_LABEL);
  emit(OP_LB);
  out1();
  emit(OP_OUT);
}
Ejemplo n.º 15
0
bool binspector_parser_t::is_offset(adobe::array_t& offset_expression)
{
    if (!is_token(adobe::at_k))
        return false;

    require_expression(offset_expression);

    return true;
}
Ejemplo n.º 16
0
bool is_illegal(char c)
{
  /* This function makes sure that the shell contains tokens that fall
    within the allowable subset. This doesn't apply to comments*/
    
  if((isalnum((unsigned char)c) == 0) && (!is_special(c)) && (c != ';')
    && (!is_token(c)) && (isspace((unsigned char)c) == 0))
    return true;
  return false;
}
bool expression_parser::is_array(array_t& expression_stack) {
    if (!is_token(open_bracket_k))
        return false;

    if (!is_argument_list(expression_stack))
        push_back(expression_stack, adobe::array_t());

    require_token(close_bracket_k);

    return true;
}
bool expression_parser::is_postfix_expression(array_t& expression_stack) {
    if (!is_primary_expression(expression_stack))
        return false;

    while (true) {
        if (is_token(open_bracket_k)) {
            require_expression(expression_stack);
            require_token(close_bracket_k);
        } else if (is_token(dot_k)) {
            any_regular_t result;
            require_token(identifier_k, result);
            expression_stack.push_back(result);
        } else
            break;

        expression_stack.push_back(any_regular_t(index_k));
    }

    return true;
}
Ejemplo n.º 19
0
bool shade::formatter::is_end_token(const std::string& code)
{
  int size(code.size());

  if (size < 3)
    return false;

  if (code[1] != '/')
    return false;

  return is_token(code);
}
Ejemplo n.º 20
0
bool binspector_parser_t::is_enum_entry_list()
{
    if (!is_token(adobe::open_bracket_k))
        return false;

    if (!is_enum_list_item_set())
        throw_exception("Enumerate list must have at least one option");

    require_token(adobe::close_bracket_k);

    return true;
}
//  variable_or_function = identifier ["(" [argument_expression_list] ")"].
bool expression_parser::is_variable_or_function(array_t& expression_stack) {
    any_regular_t result;

    if (!is_token(identifier_k, result))
        return false;

    if (is_token(open_parenthesis_k)) {
        // If there are no parameters then set the parameters to an empty array.
        if (!is_argument_expression_list(expression_stack))
            expression_stack.push_back(any_regular_t(adobe::array_t()));

        require_token(close_parenthesis_k);
        expression_stack.push_back(result);
        expression_stack.push_back(any_regular_t(function_k));
    } else {
        expression_stack.push_back(result);
        expression_stack.push_back(any_regular_t(variable_k));
    }

    return true;
}
bool expression_parser::is_primary_expression(array_t& expression_stack) {
    any_regular_t result; // empty result used if is_keyword(empty_k)

    if (is_name(result) || is_token(number_k, result) || is_boolean(result) ||
        is_token(string_k, result) || is_keyword(empty_k)) {
        expression_stack.push_back(std::move(result));
        return true;
    } else if (is_array(expression_stack))
        return true;
    else if (is_dictionary(expression_stack))
        return true;
    else if (is_variable_or_function(expression_stack))
        return true;
    else if (is_token(open_parenthesis_k)) {
        require_expression(expression_stack);
        require_token(close_parenthesis_k);
        return true;
    }

    return false;
}
Ejemplo n.º 23
0
void infix_to_postfix(int len)
{
	int i;
	char ch;

	for (i = 0; i < len; i++) {
			if (is_token(expr[i]) == OPERAND)
					printf("%c", expr[i]);
			else if ((is_token(expr[i]) == OPERATOR)) {
							while (stack_pointer >= 0 && is_token(stack_buffer[stack_pointer]) != LEFTPAREN &&
									compare_precedence(expr[i], stack_buffer[stack_pointer]) == -1)
									printf("%c", stack_buffer[stack_pointer--]);
							stack_buffer[++stack_pointer] = expr[i];
			}
			else if (is_token(expr[i]) == LEFTPAREN)
					stack_buffer[++stack_pointer] = expr[i];
			else if (is_token(expr[i]) == RIGHTPAREN) {
					while (stack_pointer >= 0 && stack_buffer[stack_pointer] != '(')
							printf("%c", stack_buffer[stack_pointer--]);
					stack_pointer--;
			} 
	}

	while (stack_pointer >= 0) {
		if (is_token(stack_buffer[stack_pointer]) != LEFTPAREN)
				printf("%c", stack_buffer[stack_pointer--]);
		else
			stack_pointer--;
	}

	printf("\n");
}
Ejemplo n.º 24
0
bool ini_find_string_section(FFILE f, const char *p_section,
                             const char *p_template,
                             long *p_file_start, long *p_file_end)
{
  char line[MAX_TOKEN_LEN];
  char section[MAX_TOKEN_LEN];
  bool section_found = FALSE;
  long file_pos;
  long file_last_line_start = 0;
  long file_last_line_end = 0;

  file_pos = 0;
  fseek(f, 0, SEEK_SET);
  while (fgets(line, MAX_TOKEN_LEN, f)) {
    if(!section_found) {
      section_found = (bool)read_section(line, section, MAX_TOKEN_LEN);
      if(section_found) {
        section_found = !strncasecmp(p_section, section, MAX_TOKEN_LEN);
      }
      if(section_found) {
        file_pos = ftell(f);
        continue;
      }
    }
    
    // Cache last non-section line from recent section
    if(section_found && !is_section(line) && !is_empty(line)) {
      file_last_line_start = file_pos;
      file_last_line_end = ftell(f);
    }
  
    if(section_found && is_section(line)) {
      // we hit next section - so it's not found,
      // create a new entry
      *p_file_start = file_last_line_start;
      *p_file_end = file_last_line_end;
      return(TRUE);
    }
  
    // Replace this line
    if(section_found && is_token(line, p_template)) {
      *p_file_start = file_pos;
      *p_file_end = ftell(f);
      return(TRUE);
    }
    
    file_pos = ftell(f);
  }

  // no section -> create a new one
  return(FALSE);
}
Ejemplo n.º 25
0
bool binspector_parser_t::is_enum_list_item_set()
{
    if (!is_enum_list_item())
        return false;

    while (is_token(adobe::comma_k))
    {
            if (!is_enum_list_item())
            throw_exception("Expected an enumerate list item after the comma");
    }

    return true;
}
// interaction_list        = [ lead_comment_decl ] interaction [ ";" ] [ interaction_list ].
bool adam_test_parser::is_interaction_list() {
    // REVISIT (mmarcus) : fix up grammar here
    std::string comment;
    bool result(true);
    while (result) {
        while (is_lead_comment(comment))
            ;
        result = is_interaction();
        while (is_lead_comment(comment) || is_token(semicolon_k))
            ;
    }
    return result;
}
Ejemplo n.º 27
0
float ini_read_float(FFILE f, const char *p_template, float dflt)
{
    char line[MAX_TOKEN_LEN];

    fseek(f, SEEK_SET, 0);
    while (fgets(line, MAX_TOKEN_LEN, f)) {
        int len = is_token(line, p_template);
        char *p_rest;
        if (len && (p_rest = ini_skip_separator(line + len))) {
            return (atof(ini_remove_end_of_line(p_rest)));
        }
    }
    return (dflt);
}
Ejemplo n.º 28
0
void		define_type_loop(t_liste *tmp, char **path)
{
  if (is_builtin(tmp->data) == 0)
    tmp->type = BUILTIN;
  else if (is_token(tmp->data) == 0)
    tmp->type = TOKEN;
  else if (is_separator(tmp->data) == 0)
    tmp->type = SEPARATOR;
  else if (is_cmd(tmp->data, path) == 0 || tmp->prev == NULL
    || tmp->prev->type == TOKEN)
    tmp->type = CMD;
  else
    tmp->type = OTHER;
}
Ejemplo n.º 29
0
void binspector_parser_t::require_scope_content(adobe::name_t scope_name)
{
    temp_assign_and_call<adobe::name_t> tmp(current_struct_m, scope_name, set_structure_proc_m);

    if (is_token(adobe::open_brace_k))
    {
        is_statement_set();

        require_token(adobe::close_brace_k);
    }
    else if (!is_scope_or_statement())
    {
        throw_exception("Expected scope or statement");
    }
}
Ejemplo n.º 30
0
bool binspector_parser_t::is_enum_entry_map()
{
    if (!is_token(adobe::open_brace_k))
        return false;

    if (!is_enum_map_item_set())
        throw_exception("Enumerate map must have at least one option");

    // must be last in the enumerate construct!
    is_enum_map_default();

    require_token(adobe::close_brace_k);

    return true;
}