Home Libraries Author Links

matchtst.c

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the SysToMath C Libraries package (LibStmC).
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Tom Michaelis, SysToMath.
00018  * Portions created by the Initial Developer are Copyright (C) 1989-2006
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /******************************************************************************
00038  *                       First Release 1989-03-17
00039  ******************************************************************************/
00040 
00041 
00078 const char *Copyright = "(C) 1989-2006 Tom Michaelis, SysToMath";
00079 const char *Version   = "1.12-r341";
00080 
00081 #include <stdio.h>
00082 #include <string.h>
00083 #include <stdlib.h>
00084 #include <stm/dvec.h>
00085 #include <stm/match.h>
00086 
00087 
00133 static void defsreact (StmReCtrl reCtrl);
00134 
00135 
00142 static int incLineNumber (StmMtcFctCmd cmd, StmQreMtc qreMtc);
00143 
00144 
00151 static int out (StmMtcFctCmd cmd, StmQreMtc qreMtc);
00152 
00153 
00159 static int printIdent (StmMtcFctCmd cmd, StmReMtc reMtc);
00160 
00161 
00168 static char *mkCtrl (char *str);
00169 
00170 
00174 typedef struct MatchTstEnv MatchTstEnv;
00175 
00176 
00180 struct MatchTstEnv
00181 {
00182     int lnr;   
00183 };
00184 
00185 
00206 int main (int argc, char **argv);
00207 
00208 
00212 int main (int argc, char **argv)
00213 {
00214     char *prog = argv [0];
00215     char *str = NULL;                  /* String to match.           */
00216     char *reStr = NULL;                /* Regular expression string. */
00217     int ret;
00218     MatchTstEnv env;
00219     StmReCtrl reCtrl = NULL;
00220     StmRe re = NULL;
00221     jmp_buf envbuf;
00222     if (! (ret = setjmp (envbuf)))
00223     {
00224         reCtrl = stmReCtrlCreate (stderr, &envbuf, NULL, NULL);
00225         defsreact (reCtrl);
00226 
00227         /* Begin of the execution loop. */
00228         while (StmTrue)
00229         {
00230             /*
00231              * Input of the string to match.  It can contain newline, tab,
00232              * carriage return and form feed charcters as \\n, \\t, \\r and
00233              * \\f, respectively.  A single "q" causes the loop to be left.
00234              */
00235             printf ("String: ");
00236             str = stmFgetDstr (stdin, str, StmFgdStripNl, StmFgdNew);
00237             if (strcmp (str, "q") == 0) break;
00238             str = mkCtrl (str);
00239 
00240             /*
00241              * Input of the regular expression string to match.  It can contain
00242              * newline, tab, carriage return and form feed charcters as \\n,
00243              * \\t, \\r and \\f, respectively.
00244              */
00245             printf ("Re:     ");
00246             reStr = stmFgetDstr (stdin, reStr, StmFgdStripNl, StmFgdNew);
00247             reStr = mkCtrl (reStr);
00248 
00249             /* Generation of the named regular expression \b test. */
00250             re = stmReCreate (reCtrl, "test", reStr, NULL);
00251 
00252             /*
00253              * Matching operation by means of the function stmReMatch().
00254              * The matching result is printed. On error an according error
00255              * message is printed.
00256              */
00257             env.lnr = 1;
00258             printf ("success = %s\n",
00259                     stmReMatch (re, str, &env, NULL) ? "true" : "false");
00260 
00261             /*
00262              * Output of the line number counter implemented by the action
00263              * \b inclnr.
00264              */
00265             printf ("Last Line Number = %d\n\n", env.lnr);
00266 
00267             stmReDestroy (re);
00268             re = NULL;
00269         }
00270     }
00271     else
00272     {
00273         fprintf (stderr, "%s: %s\n", prog, stmReCtrlGetErrorMsg (reCtrl));
00274     }
00275 
00276     /* Program end. */
00277     stmReDestroy (re);
00278     stmReCtrlDestroy (reCtrl);
00279     stmDvecFree (str);
00280     stmDvecFree (reStr);
00281     return ret;
00282 }
00283 
00284 
00285 static void defsreact (StmReCtrl reCtrl)
00286 {
00287     stmReCreate (reCtrl,
00288                  "ident",
00289                  "[_a-zA-Z][_0-9a-zA-Z]*",
00290                  printIdent);
00291     stmReCreate (reCtrl,
00292                  "nl",
00293                  "\n+:inclnr:",
00294                  NULL);
00295     stmReCreate (reCtrl,
00296                  "blank",
00297                  "[ \t]+",
00298                  NULL);
00299     stmReCreate (reCtrl,
00300                  "ws",
00301                  "(&blank&||&comm&||&nl&)*",
00302                  NULL);
00303     stmReCreate (reCtrl,
00304                  "comm",
00305                  "%/%*([^*\n]*(%**)([^/\n]|&nl&))*([^*\n]|&nl&)*%*+%/",
00306                  NULL);
00307     stmReCreate (reCtrl,
00308                  "subtext",
00309                  "&xy&:out:&ident&:out:",
00310                  NULL);
00311     stmReCreate (reCtrl,
00312                  "text",
00313                  "&subtext&:out:",
00314                  NULL);
00315     stmReCreate (reCtrl,
00316                  "xy",
00317                  "x|y",
00318                  NULL);
00319     stmReCreate (reCtrl,
00320                  "e",
00321                  "[^.]|&e&%.",
00322                  NULL);
00323     stmReCreate (reCtrl,
00324                  "a",
00325                  "[^.]&a&|%.",
00326                  NULL);
00327     stmQreActDef (reCtrl, "inclnr", incLineNumber);
00328     stmQreActDef (reCtrl, "out", out);
00329     return;
00330 }
00331 
00332 
00333 static int incLineNumber (StmMtcFctCmd cmd, StmQreMtc qreMtc)
00334 {
00335     if (cmd.action)
00336     {
00337         MatchTstEnv *env = (MatchTstEnv *) stmQreMtcEnv (qreMtc);
00338         env -> lnr += stmQreMtcSimpleReMtcCount (qreMtc);
00339         printf ("incLineNumber: lnr = %d\n", env -> lnr);
00340     }
00341     return 0;
00342 }
00343 
00344 
00345 static int out (StmMtcFctCmd cmd, StmQreMtc qreMtc)
00346 {
00347     if (cmd.action)
00348     {
00349         printf ("out: len = %d, q = %d, str = {%.*s}\n",
00350                 stmQreMtcLen (qreMtc), stmQreMtcSimpleReMtcCount (qreMtc),
00351                 stmQreMtcLen (qreMtc), stmQreMtcStr (qreMtc));
00352     }
00353     return 0;
00354 }
00355 
00356 
00357 static int printIdent (StmMtcFctCmd cmd, StmReMtc reMtc)
00358 {
00359     if (cmd.action)
00360     {
00361         printf ("printIdent: %.*s\n",
00362                 stmReMtcLen (reMtc), stmReMtcStr (reMtc));
00363     }
00364     return 0;
00365 }
00366 
00367 
00368 static char *mkCtrl (char *str)
00369 {
00370     int i;
00371     for (i = 0; str [i]; i ++)
00372     {
00373         if (str [i] == '\\')
00374         {
00375             char ch;
00376             switch (str [i + 1])
00377             {
00378             case 'n' :
00379                 ch = '\n';
00380                 break;
00381             case 't' :
00382                 ch = '\t';
00383                 break;
00384             case 'r' :
00385                 ch = '\r';
00386                 break;
00387             case 'f' :
00388                 ch = '\f';
00389                 break;
00390             default :
00391                 ch = str [i + 1];
00392                 break;
00393             }
00394             str [i] = ch;
00395             str = stmDstrDel (str, i + 1, 1);
00396         }
00397     }
00398     return str;
00399 }

© Copyright Tom Michaelis 2002-2007

Distributed under the SysToMath Software License (See the accompanying file license.txt or a copy at www.SysToMath.com).