Ejemplo n.º 1
0
char verifypattern(int index,char *string)
{
  int errorcode;
  char *errorstring;

  if(!is_valid_pattern(string,&errorcode))
  {
    switch(errorcode)
    {
      case PATTERN_ESC:
        errorstring="Literal escape at end of pattern";
        break;
      case PATTERN_RANGE:
        errorstring="Malformed range in [..] construct";
        break;
      case PATTERN_CLOSE:
        errorstring="No end bracket in [..] construct";
        break;
      case PATTERN_EMPTY:
        errorstring="[..] construct is empty";
        break;
    }
    printf("Syntax error (Arg#%u:%s). %s\n",index,string,errorstring);
  }
  return errorcode;
}
Ejemplo n.º 2
0
static int SprMatch(char *mask)
 {
   int error;

   (void)is_valid_pattern(mask,&error);
   switch(error) {
      case PATTERN_VALID:
	 break;
      case PATTERN_RANGE:
	 printf("    No End of Range in [..] Construct\n");
	 return -1;
      case PATTERN_CLOSE:
	 printf("    [..] Construct is Open\n");
	 return -1;
      case PATTERN_EMPTY:
	 printf("    [..] Construct is Empty\n");
	 return -1;
      default:
	 printf("    Internal Error in is_valid_pattern()\n");
	 return -1;
     }
  return 0;
 }
Ejemplo n.º 3
0
int AL_PROTO ALName::WildCardMatch( const char AL_DLL_FAR *pattern )
{
    int error;
    int result;
    char *p = new char[ strlen( pattern ) + 1 ];

    if ( !p )
        return 0;
    strcpy( p, pattern );
    switch ( mCase ) {
        case AL_UPPER : strupr( p ); break;
        case AL_LOWER : strlwr( p ); break;
        default       : break;
    }
    if ( !is_valid_pattern( p, &error ) )
        result = 0;
    else if ( matche( p, mszName ) == MATCH_VALID )
        result = 1;
    else
        result = 0;
    delete p;
    return result;
}
Ejemplo n.º 4
0
// ************************************************************************************************
// Procédure principale.
// ************************************************************************************************
int main(int argc,char *argv[])
{
 int touche=0;
 int erreurpattern=0;
 FILE *in;
 
 if(strcmp(argv[1],"-H")==0 or strcmp(argv[1],"/?")==0 or strcmp(argv[1],"?")==0 or strcmp(argv[1],"-HELP")==0 or argc<2)
 {
  printf("RegExp par Herve Thouzard Version 1.0 - 1997 °±² FREEWARE ²±°\n");
  printf("Utilisation : REGEXP expression_r‚guliŠre fichier\n");
  printf("Si vous ne pr‚cisez pas de fichier, le clavier sera pris comme entr‚e\npar d‚faut.\n");
  printf("Vous disposez des caractŠres suivants pour composer des expressions\n"
  "r‚guliŠres :\n\n"
  "   *       Recherchera de 0 … N caractŠres.\n"
  "   ?       Recherchera 1 et 1 seul caractŠre. N'importe lequel.\n"
  "   [azer]  Recherchera 'a' ou 'z' ou 'e' ou 'r'.\n"
  "   [!aze]  Recherchera tout sauf 'a', 'z' et 'e'.\n\n");
  printf("Les expressions entre crochets, peuvent ˆtre constitu‚s de s‚ries de lettres\n"
  "mais peuvent aussi contenir des indications de suites. Par exemple [a-m]\n"
  "indique au programme de choisir toutes les lettres allant de 'a' … 'm'\n"
  "(abcdefghijklm). On peut aussi faire [0-9A-Z] pour avoir :\n"
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.\n\n");
  touche=getch();
  if(touche==27 or touche==113 or touche==81) exit(1);
  printf("Quelques exemples d'expressions r‚guliŠres :\n\n"
  "       [t-w]??D*S     Rechercher tout ce qui commence par 't' ou 'u' ou 'v' ou\n"
  "                      'w', et qui contient aprŠs, deux lettres (peut importe\n"
  "                      lesquelles) qui est suivi par la lettre D, puis qui a un\n"
  "                      nombre quelconque de caractŠres. On pr‚cise enfin, que\n"
  "                      le mot doit se terminer par 'S'\n\n"
  "       [t-w]??D*[G-T] Rechercher tout ce qui commence par 't' ou 'u' ou 'v' ou\n"
  "                      'w', et qui contient aprŠs, deux lettres (peut importe\n"
  "                      lesquelles) qui est suivi par la lettre D, puis qui a un\n"
  "                      un nombre quelconque de caractŠres et qui se termine par\n"
  "                      les lettres 'g' ou 'h' ou 'i' ou 'j' .... jusqu'… 't'\n\n");
  touche=getch();
  if(touche==27 or touche==113 or touche==81) exit(1);
  printf("regexp W*              Rechercher tout ce qui commence par W et qui contient\n"
  "                       ensuite un nombre quelconque de lettres. Cela peut ˆtre\n"
  "                       tout simplement la lettre 'W'.\n\n"
  "       ???????         On demande de rechercher tous ce qui fait de 0 … 6\n"
  "                       caractŠres. WINDOWS en fait partie, ainsi que 'LETTRE'\n\n"
  "       windo[!a-r]     Rechercher tout ce qui commence par 'window' et qui ne\n"
  "                       se termine pas par les lettres allant de 'a' … 'r'.\n\n"
  "       w*d*s           Rechercher tout ce qui commence par 'w' et qui se\n"
  "                       termine    par 's' et qui contient entre les deux (peut\n"
  "                       importe o—) la lettre 'd'.\n\n"
  "       win[a-f]ows     Rechercher tout ce qui commence par 'win' puis qui\n"
  "                       contient soit 'a' soit 'b' soit 'c' .... jusqu'…\n"
  "                       'f' et qui se termine par 'ows'.\n\n"
  "       win[abcdef]ows  Mˆme exemple que pr‚c‚demment mais formul‚ autrement.\n\n"
  "       w*s             Recherche tout ce qui commence par 'w' et fini par 's'\n");
  printf("\n\nQuelques exemples d'utilisation :\n\n");
  printf("dir/b | regexp [!M]*    ==> Affiche tous les fichiers qui ne commencent\n");
  printf("                            pas par M.\n");
  printf("type autoexec.bat | regexp *SET*   ==> Affiche toutes les lignes du fichier\n");
  printf("                                       autoexec.bat qui contiennent un mot\n");
  printf("                                       contenant 'SET'\n");
  printf("on peut aussi faire regexp [!M]* autoexec.bat\n");
  return(1);
 }

 if(argc==2) // On prend le clavier comme entrée standard.
 {
  in=stdin; 
 }
 
 if(argc==3) // On a spécifié un fichier, donc, on le prend.
 {
  if((in=fopen(argv[2],"rt"))==NULL)  
  {
   fprintf(stderr,"Erreur, Impossible d'ouvrir le fichier \"%s\".\n",argv[2]);
   exit(2);
  }  
 }

 // On récupère l'expression régulière dans une variable de travail.
 // et on test la validité de l'expression.
 strcpy(expression,argv[1]);  
 if(!is_pattern(expression))
 {
  printf("%s n'est pas une expression r‚guliŠre.\n",expression);
  exit(3);
 }


 if(is_valid_pattern(expression,&erreurpattern)!=TRUE)
 {
  printf("D‚sol‚, mais votre expression r‚guliŠre n'est pas correcte !\n");
  switch(erreurpattern)
  {
   case MATCH_LITERAL:
    printf("\tProblŠme sur un litt‚ral\n");
    exit(4);
   case MATCH_RANGE:
    printf("\tProblŠme sur une construction du type [..]\n");
    exit(4);
   case MATCH_ABORT:
    printf("\tProblŠme sur la fin du texte de l'expression\n");
    exit(4);
   case MATCH_END:
    printf("\tProblŠme sur la fin de l'expression\n");
    exit(4);
   case MATCH_PATTERN:
    switch(erreurpattern) 
    {
     case PATTERN_ESC:
      printf("\tProblŠme avec une s‚quence d'‚chappement … la fin de l'expression\n");
      exit(4);
     case PATTERN_RANGE:
      printf("\tProblŠme, pas de fin de limite dans la construction [..]\n");
      exit(4);
     case PATTERN_CLOSE:
      printf("\tProblŠme, la construction est ouverte [..]\n");
      exit(4);
     case PATTERN_EMPTY:
      printf("\tProblŠme, la construction [..] est vide.\n");
      exit(4);
     default:
      printf("\tProblŠme interne avec la procedure is_valid_pattern()\n");
      exit(4);
    }
    break;
   default:
    printf("ProblŠme, erreur interne avec la procedure matche()\n");
    exit(4);   
  } // ********************************************************************************************
 }
// printf("Expression recherchee :%s:\n",expression);
 fgets(ligne,30000,in);
 while(!feof(in)) 
 {  
  if(match(expression,ligne)==TRUE) printf("%s",ligne);
  fgets(ligne,30000,in);
 }
 fclose(in);
 return(0);
}
Ejemplo n.º 5
0
Archivo: match.c Proyecto: mgist/ponyc
bool expr_case(pass_opt_t* opt, ast_t* ast)
{
  assert(opt != NULL);
  assert(ast_id(ast) == TK_CASE);
  AST_GET_CHILDREN(ast, pattern, guard, body);

  if((ast_id(pattern) == TK_NONE) && (ast_id(guard) == TK_NONE))
  {
    ast_error(ast, "can't have a case with no conditions, use an else clause");
    return false;
  }

  ast_t* cases = ast_parent(ast);
  ast_t* match = ast_parent(cases);
  ast_t* match_expr = ast_child(match);
  ast_t* match_type = ast_type(match_expr);

  if(is_control_type(match_type) || is_typecheck_error(match_type))
    return false;

  if(!infer_pattern_type(pattern, match_type, opt))
    return false;

  if(!is_valid_pattern(opt, pattern))
    return false;

  ast_t* operand_type = alias(match_type);
  ast_t* pattern_type = ast_type(pattern);
  bool ok = true;

  switch(is_matchtype(operand_type, pattern_type))
  {
    case MATCHTYPE_ACCEPT:
      break;

    case MATCHTYPE_REJECT:
      ast_error(pattern, "this pattern can never match");
      ast_error(match_type, "match type: %s", ast_print_type(operand_type));
      ast_error(pattern, "pattern type: %s", ast_print_type(pattern_type));
      ok = false;
      break;

    case MATCHTYPE_DENY:
      ast_error(pattern, "this capture violates capabilities");
      ast_error(match_type, "match type: %s", ast_print_type(operand_type));
      ast_error(pattern, "pattern type: %s", ast_print_type(pattern_type));
      ok = false;
      break;
  }

  if(ast_id(guard) != TK_NONE)
  {
    ast_t* guard_type = ast_type(guard);

    if(is_typecheck_error(guard_type))
    {
      ok = false;
    }
    else if(!is_bool(guard_type))
    {
      ast_error(guard, "guard must be a boolean expression");
      ok = false;
    }
  }

  ast_free_unattached(operand_type);
  ast_inheritflags(ast);
  return ok;
}
Ejemplo n.º 6
0
Archivo: match.c Proyecto: mgist/ponyc
static bool is_valid_pattern(pass_opt_t* opt, ast_t* pattern)
{
  if(ast_id(pattern) == TK_NONE)
  {
    ast_settype(pattern, ast_from(pattern, TK_DONTCARE));
    return true;
  }

  ast_t* pattern_type = ast_type(pattern);

  if(is_control_type(pattern_type))
  {
    ast_error(pattern, "not a matchable pattern");
    return false;
  }

  switch(ast_id(pattern))
  {
    case TK_VAR:
    case TK_LET:
    {
      // Disallow capturing tuples.
      AST_GET_CHILDREN(pattern, id, capture_type);

      if(ast_id(capture_type) == TK_TUPLETYPE)
      {
        ast_error(capture_type,
          "can't capture a tuple, change this into a tuple of capture "
          "expressions");

        return false;
      }

      // Set the pattern type to be the capture type.
      ast_settype(pattern, capture_type);
      return true;
    }

    case TK_TUPLE:
    {
      ast_t* pattern_child = ast_child(pattern);

      // Treat a one element tuple as a normal expression.
      if(ast_sibling(pattern_child) == NULL)
      {
        bool ok = is_valid_pattern(opt, pattern_child);
        ast_settype(pattern, ast_type(pattern_child));
        return ok;
      }

      // Check every element pairwise.
      ast_t* pattern_type = ast_from(pattern, TK_TUPLETYPE);
      bool ok = true;

      while(pattern_child != NULL)
      {
        if(!is_valid_pattern(opt, pattern_child))
          ok = false;

        ast_append(pattern_type, ast_type(pattern_child));
        pattern_child = ast_sibling(pattern_child);
      }

      ast_settype(pattern, pattern_type);
      return ok;
    }

    case TK_SEQ:
    {
      // Patterns cannot contain sequences.
      ast_t* child = ast_child(pattern);
      ast_t* next = ast_sibling(child);

      if(next != NULL)
      {
        ast_error(next, "expression in patterns cannot be sequences");
        return false;
      }

      bool ok = is_valid_pattern(opt, child);
      ast_settype(pattern, ast_type(child));
      return ok;
    }

    case TK_DONTCARE:
      // It's always ok not to care.
      return true;

    default:
    {
      // Structural equality, pattern.eq(match).
      ast_t* fun = lookup(opt, pattern, pattern_type, stringtab("eq"));

      if(fun == NULL)
      {
        ast_error(pattern,
          "this pattern element doesn't support structural equality");

        return false;
      }

      if(ast_id(fun) != TK_FUN)
      {
        ast_error(pattern, "eq is not a function on this pattern element");
        ast_error(fun, "definition of eq is here");
        ast_free_unattached(fun);
        return false;
      }

      AST_GET_CHILDREN(fun, cap, id, typeparams, params, result, partial);
      bool ok = true;

      if(ast_id(typeparams) != TK_NONE)
      {
        ast_error(pattern, "polymorphic eq not supported in pattern matching");
        ok = false;
      }

      if(!is_bool(result))
      {
        ast_error(pattern, "eq must return Bool when pattern matching");
        ok = false;
      }

      if(ast_id(partial) != TK_NONE)
      {
        ast_error(pattern, "eq cannot be partial when pattern matching");
        ok = false;
      }

      ast_t* param = ast_child(params);

      if(param == NULL || ast_sibling(param) != NULL)
      {
        ast_error(pattern,
          "eq must take a single argument when pattern matching");

        ok = false;
      } else {
        AST_GET_CHILDREN(param, param_id, param_type);
        ast_settype(pattern, param_type);
      }

      ast_free_unattached(fun);
      return ok;
    }
  }

  assert(0);
  return false;
}
Ejemplo n.º 7
0
static bool is_valid_pattern(pass_opt_t* opt, ast_t* pattern)
{
  if(ast_id(pattern) == TK_NONE)
  {
    ast_settype(pattern, ast_from(pattern, TK_DONTCARE));
    return true;
  }

  ast_t* pattern_type = ast_type(pattern);

  if(is_control_type(pattern_type))
  {
    ast_error(opt->check.errors, pattern, "not a matchable pattern");
    return false;
  }

  switch(ast_id(pattern))
  {
    case TK_MATCH_CAPTURE:
      // Captures are always OK.
      return true;

    case TK_TUPLE:
    {
      ast_t* pattern_child = ast_child(pattern);

      // Treat a one element tuple as a normal expression.
      if(ast_sibling(pattern_child) == NULL)
      {
        bool ok = is_valid_pattern(opt, pattern_child);
        ast_settype(pattern, ast_type(pattern_child));
        return ok;
      }

      // Check every element pairwise.
      ast_t* pattern_type = ast_from(pattern, TK_TUPLETYPE);
      bool ok = true;

      while(pattern_child != NULL)
      {
        if(!is_valid_pattern(opt, pattern_child))
          ok = false;

        ast_append(pattern_type, ast_type(pattern_child));
        pattern_child = ast_sibling(pattern_child);
      }

      ast_settype(pattern, pattern_type);
      return ok;
    }

    case TK_SEQ:
      if(ast_childcount(pattern) == 1)  // This may be a just a capture.
        return is_valid_pattern(opt, ast_child(pattern));

      // Treat this like other nodes.
      break;

    case TK_DONTCARE:
      // It's always ok not to care.
      return true;

    default:
      break;
  }

  // Structural equality, pattern.eq(match).
  ast_t* fun = lookup(opt, pattern, pattern_type, stringtab("eq"));

  if(fun == NULL)
  {
    ast_error(opt->check.errors, pattern,
      "this pattern element doesn't support structural equality");

    return false;
  }

  if(ast_id(fun) != TK_FUN)
  {
    ast_error(opt->check.errors, pattern,
      "eq is not a function on this pattern element");
    ast_error_continue(opt->check.errors, fun,
      "definition of eq is here");
    ast_free_unattached(fun);
    return false;
  }

  AST_GET_CHILDREN(fun, cap, id, typeparams, params, result, partial);
  bool ok = true;

  if(ast_id(typeparams) != TK_NONE)
  {
    ast_error(opt->check.errors, pattern,
      "polymorphic eq not supported in pattern matching");
    ok = false;
  }

  if(!is_bool(result))
  {
    ast_error(opt->check.errors, pattern,
      "eq must return Bool when pattern matching");
    ok = false;
  }

  if(ast_id(partial) != TK_NONE)
  {
    ast_error(opt->check.errors, pattern,
      "eq cannot be partial when pattern matching");
    ok = false;
  }

  ast_t* r_type = set_cap_and_ephemeral(pattern_type, ast_id(cap), TK_NONE);
  ast_t* a_type = alias(pattern_type);
  errorframe_t info = NULL;

  if(!is_subtype(a_type, r_type, &info, opt))
  {
    errorframe_t frame = NULL;
    ast_error_frame(&frame, pattern, "eq cannot be called on this pattern");
    errorframe_append(&frame, &info);
    errorframe_report(&frame, opt->check.errors);
    ok = false;
  }

  ast_t* param = ast_child(params);

  if(param == NULL || ast_sibling(param) != NULL)
  {
    ast_error(opt->check.errors, pattern,
      "eq must take a single argument when pattern matching");

    ok = false;
  } else {
    AST_GET_CHILDREN(param, param_id, param_type);
    ast_settype(pattern, param_type);
  }

  ast_free_unattached(r_type);
  ast_free_unattached(a_type);
  ast_free_unattached(fun);
  return ok;
}