Skip to content
This repository has been archived by the owner on Jul 26, 2018. It is now read-only.

Parsing case-insensitive languages #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MSVC/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Debug
Release
*.iobj
*.ipdb
*.opendb
*.db
test
68 changes: 61 additions & 7 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static int yyl(void)
return ++prev;
}

static int toLowerCase(int c) { return c >= 'A' && c <= 'Z' ? c | 0x20 : c; }
static int toUpperCase(int c) { return c >= 'a' && c <= 'z' ? c & ~0x20 : c; }
static void charClassSet (unsigned char bits[], int c) { bits[c >> 3] |= (1 << (c & 7)); }
static void charClassClear(unsigned char bits[], int c) { bits[c >> 3] &= ~(1 << (c & 7)); }

Expand All @@ -54,6 +56,20 @@ static int readChar(unsigned char **cp)
c= oct;
goto done;
}
else if (c == 'x')
{
unsigned char hex = 0;
c = toUpperCase(*cclass++);
for (i = 1; i >= 0; i--) {
if (!(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'F'))
break;
hex = (hex * 16) + (c - (c < 'A' ? '0' : 'A' - 10));
c = toUpperCase(*cclass++);
}
cclass--;
c = hex;
goto done;
}

switch (c)
{
Expand Down Expand Up @@ -121,7 +137,7 @@ static char *yyqq(char* s) {
return dst;
}

static char *makeCharClass(unsigned char *cclass)
static char *makeCharClass(unsigned char *cclass, int flags)
{
unsigned char bits[32];
setter set;
Expand All @@ -145,13 +161,28 @@ static char *makeCharClass(unsigned char *cclass)
if ('-' == c && *cclass && prev >= 0)
{
for (c= readChar(&cclass); prev <= c; ++prev)
set(bits, prev);
{
if (IgnoreCase & flags)
{
set(bits, toLowerCase(prev));
set(bits, toUpperCase(prev));
}
else
set(bits, prev);
}
prev= -1;
}
else
{
set(bits, prev= c);
}
{
prev = c;
if (IgnoreCase & flags)
{
set(bits, toLowerCase(prev));
set(bits, toUpperCase(prev));
}
else
set(bits, prev);
}
}

ptr= string;
Expand Down Expand Up @@ -203,7 +234,11 @@ static void Node_compile_c_ko(Node *node, int ko)
case String:
{
int len= strlen(node->string.value);
if (1 == len)
if (IgnoreCase & node->string.flags)
{
fprintf(output, " if (!yymatchNoCaseString(G, \"%s\")) goto l%d;\n", node->string.value, ko);
}
else if (1 == len)
{
if ('\'' == node->string.value[0])
fprintf(output, " if (!yymatchChar(G, '\\'')) goto l%d;\n", ko);
Expand All @@ -219,7 +254,7 @@ static void Node_compile_c_ko(Node *node, int ko)
break;

case Class:
fprintf(output, " if (!yymatchClass(G, (const unsigned char *)\"%s\", \"%s\")) goto l%d;\n", makeCharClass(node->cclass.value), yyqq((char*)node->cclass.value), ko);
fprintf(output, " if (!yymatchClass(G, (const unsigned char *)\"%s\", \"%s\")) goto l%d;\n", makeCharClass(node->cclass.value, node->cclass.flags), yyqq((char*)node->cclass.value), ko);
break;

case Action:
Expand Down Expand Up @@ -610,6 +645,25 @@ YY_LOCAL(int) yymatchString(GREG *G, const char *s)\n\
return 1;\n\
}\n\
\n\
YY_LOCAL(int) yymatchNoCaseString(GREG *G, const char *s)\n\
{\n\
#define YY_TOUPPER(ch) ((ch) >= 'a' && (ch) <= 'z' ? (ch) & 0xffdf : (ch))\n\
int yysav= G->pos;\n\
while (*s)\n\
{\n\
if (G->pos >= G->limit && !yyrefill(G)) return 0;\n\
if (YY_TOUPPER(G->buf[G->pos]) != YY_TOUPPER(*s))\n\
{\n\
G->pos= yysav;\n\
return 0;\n\
}\n\
++s;\n\
++G->pos;\n\
}\n\
return 1;\n\
#undef YY_TOUPPER\n\
}\n\
\n\
YY_LOCAL(int) yymatchClass(GREG *G, const unsigned char *bits, const char *cclass)\n\
{\n\
int c;\n\
Expand Down
Loading