]> git.mdlowis.com Git - proto/libregexp.git/commitdiff
minor refactoring
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 19 Dec 2018 14:12:49 +0000 (09:12 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 19 Dec 2018 14:12:49 +0000 (09:12 -0500)
regcomp.c
regexp9.h

index 077ef00456ad9d3fb8c0bc1f416ee8689039e2b7..426a14669425e0ca4e343abd7c27836a7a6c054d 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -13,12 +13,16 @@ typedef struct Node {
     Reinst* last;
 } Node;
 
-/* max rune ranges per character class is nelem(classp->spans)/2 */
-#define NCCRUNE    nelem(classp->spans)
+#define NSTACK 20
 
-static char* SrcExpr; /* pointer to next character in source expression */
+static char* SrcExpr;       /* pointer to next character in source expression */
+static int CurrRune;        /* last lex'd rune */
+static int CurrSubExpr;     /* id of current subexpression */
+static bool LastWasOperand; /* Last token was operand */
+static jmp_buf LandingPad;  /* longjmp() Landing pad for fatal errors */
+static int NParens;         /* number of left parentheses seen so far */
+static int NClass;          /* number of character classes seen so far */
 
-#define NSTACK 20
 static Node  OperandStack[NSTACK];  /* Stack of operands */
 static Node* OperandPtr;            /* Pointer to the top of the operand stack */
 static int   OperatorStack[NSTACK]; /* Stack of operators */
@@ -26,15 +30,8 @@ static int*  OperatorPtr;           /* Pointer to the top of the operator stack
 static int   SubExprStack[NSTACK];  /* parallel to OperatorStack */
 static int*  SubExprPtr;            /* Pointer to the top of the sub-expression stack */
 
-static int   CurrSubExpr;           /* id of current subexpression */
-static bool  LastWasOperand;        /* Last token was operand */
-static jmp_buf LandingPad;
-static int NParens;
-static int NClass;
-
 static Reclass* classp;
 static Reinst* freep;
-static int yyrune;        /* last lex'd rune */
 static Reclass* yyclassp; /* last lex'd class */
 
 /* predeclared crap */
@@ -77,14 +74,14 @@ static int NextRune(int* p_rune) {
 }
 
 static int GetToken(int literal, int dot_type) {
-    int quoted = NextRune(&yyrune);
+    int quoted = NextRune(&CurrRune);
     if (literal || quoted) {
-        if (yyrune == 0)
+        if (CurrRune == 0)
             return END;
         return RUNE;
     }
 
-    switch (yyrune) {
+    switch (CurrRune) {
         case 0:   return END;
         case '*': return STAR;
         case '?': return QUEST;
@@ -138,7 +135,7 @@ static int PopOperator(void) {
 /******************************************************************************/
 
 static int BuildClass(void) {
-    int r[NCCRUNE];
+    int r[NCLASSSPANS];
     int *p, *ep, *np;
     int rune;
     int quoted;
@@ -161,7 +158,7 @@ static int BuildClass(void) {
     }
 
     /* parse class into a set of spans */
-    while (ep < &r[NCCRUNE-1]) {
+    while (ep < &r[NCLASSSPANS-1]) {
         if (rune == 0) {
             CompileError("malformed '[]'");
             return 0;
@@ -185,7 +182,7 @@ static int BuildClass(void) {
         }
         quoted = NextRune(&rune);
     }
-    if (ep >= &r[NCCRUNE-1]) {
+    if (ep >= &r[NCLASSSPANS-1]) {
         CompileError("char class too large; increase Reclass.spans size");
         return 0;
     }
@@ -240,7 +237,7 @@ static void Operand(int t) {
     if (t == CCLASS || t == NCCLASS)
         i->r.cp = yyclassp;
     if (t == RUNE)
-        i->r.r = yyrune;
+        i->r.r = CurrRune;
 
     PushOperand(i, i);
     LastWasOperand = true;
index 375fa5363333edd5c550aa7bf8b4e1ec2ee7b3be..82eb4ae9570fe869703b7cfb255308eb44eef032 100644 (file)
--- a/regexp9.h
+++ b/regexp9.h
@@ -3,6 +3,11 @@
 
 #include "utf.h"
 
+enum {
+    NCLASSES    = 16, /* maximum number of character classes in a regex */
+    NCLASSSPANS = 64, /* maximum number of spans in a character class */
+};
+
 /* Sub expression matches */
 typedef struct {
     union {
@@ -18,27 +23,25 @@ typedef struct {
 /* character class, each pair of rune's defines a range */
 typedef struct {
     int* end;
-    int spans[64];
+    int spans[NCLASSSPANS];
 } Reclass;
 
 /* Machine instructions */
 typedef struct Reinst Reinst;
 struct Reinst {
-    int type;
+    int type; /* type of instruction */
     union {
         Reclass* cp;   /* class pointer */
         int r;         /* character */
         int subid;     /* sub-expression id for RBRA and LBRA */
         Reinst* right; /* right child of OR */
-    }r;
+    } r;
     union { /* regexp relies on these two being in the same union */
         Reinst* left; /* left child of OR */
         Reinst* next; /* next instruction for CAT & LBRA */
-    }l;
+    } l;
 };
 
-#define NCLASSES 16
-
 /* Reprogram definition */
 typedef struct {
     Reinst* startinst;       /* start pc */