コード例 #1
0
ファイル: lexer.cpp プロジェクト: gianantonio71/amber
TokenType token_type(const string &str)
{
  if (is_valid_operator(str))
    return operator_symbol;

  if (is_valid_operator_as_function(str))
    return op_function;

  if (is_valid_identifier(str))
    return plain_identifier;

  if (is_valid_symbol(str))
    return symbol;

  if (is_valid_label(str))
    return label;

  if (is_valid_number(str))
    return number;

  if (is_valid_type_name(str))
    return type_id;

  if (is_valid_type_var(str))
    return type_var;

  if (is_valid_builtin(str))
    return builtin;

  if (is_valid_string_lit(str))
    return string_lit;
    
  return invalid;
}
コード例 #2
0
int
validate (token_t *token_array)
{
  int i = 0;
  int res = 1;
  int in_subshell = 0;
  int token_array_len = 0;
  while (!(token_array[i]->line_num == -1)) {
    token_array_len++;
    i++;
  }

  i = 0;
  while (i < token_array_len && !(token_array[i]->line_num == -1)) 
  {
    char *operand = token_array[i]->word;

    // if (!is_char_validated(operand)) syntax_error(token_array[i]->line_num);
    if (!is_valid_operator(operand) && !is_valid_word(operand)) syntax_error(token_array[i]->line_num);

    if (operand[0] == '(') 
    {
      in_subshell = 1;
      if (i + 1 >= token_array_len) {
        res = 0;
        syntax_error(token_array_len);
      } 
      else 
      {
        if (token_array[i + 1]->word[0] != ')') 
        {
          token_t *sub_token_array = checked_malloc(sizeof (token_t) * 128);
          tokenize (token_array[i+1]->word, sub_token_array, token_array[i+1]->line_num);

          // int j = 0;
          // printf("subshell\n" );
          // while (sub_token_array[j]->line_num != -1) {
          //   printf ("token %d\n  word:'%s' line_number:%d\n", j, sub_token_array[j]->word, sub_token_array[j]->line_num);
          //   j++;
          // }

          if (validate (sub_token_array))
          {
            i++;
          }
        }
      }
      i++;
      continue;
    } 

    if (operand[0] == ')')
    {
      if (i - 1 < 0) {
        res = 0;
        syntax_error(0);
      } 
      else 
      {
        if (!in_subshell) 
        {
          res = 0;
          // error_number = (i - 1 >= 0) ? token_array[i - 1]->line_num: token_array[i]->line_num;
          syntax_error(token_array[i]->line_num);
        }
        else
        {
          in_subshell = 0;
        }
      }
      i++;
      continue;
    }

    if (!strcmp(operand, ";") || !strcmp(operand, "||") || !strcmp(operand, "|") || !strcmp(operand, "&&")) 
    {
      if (i - 1 < 0) 
      {
        res = 0;
        syntax_error(token_array[i]->line_num);
      }
      // else 
      // {
      //  if 
      // }
      if (!strcmp(operand, ";")) {
        if (i < token_array_len - 1 && (!strcmp(token_array[i + 1]->word, "&&") 
          || !strcmp(token_array[i + 1]->word, "||") || !strcmp(token_array[i + 1]->word, "|") 
          || token_array[i + 1]->word[0] == '>' || token_array[i + 1]->word[0] == '<' || 
          !strcmp(token_array[i+1]->word, ";")))
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }

        if (i >= token_array_len - 1) 
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }

        if (i - 1 >= 0)
        {
          char *tmp = token_array[i - 1]->word;
          int j = strlen(tmp) - 1;
          while (j >= 0)
          {
            if (tmp[j] == ' ' || tmp[j] == '\t') {j--; continue;}
            if (tmp[j] == '>' || tmp[j] == '<')
            {
              res = 0;
              syntax_error(token_array[i - 1]->line_num);
            }
            j--;
          }
        }
      }
      else 
      {
        if (i < token_array_len - 1 && (!strcmp(token_array[i + 1]->word, "&&") 
          || !strcmp(token_array[i + 1]->word, "||") || !strcmp(token_array[i + 1]->word, "|") 
          ))
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        } 
        if (i >= token_array_len - 1) 
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }
      }
      
    }

    if (!strcmp(operand, "<") || !strcmp(operand, ">"))
    {
      if (i == token_array_len - 1 || i == 0) 
      {
        res = 0;
        syntax_error(token_array[i]->line_num);
      } 
      else 
      {
        if (!strcmp(token_array[i+1]->word, ">") || !strcmp(token_array[i+1]->word, "<"))
        {
          res = 0;
          syntax_error(token_array[i]->line_num);
        }
      }
    }
    

    i++;  
  }

  return res;
}