Skip to content

Commit

Permalink
imported peg-0.1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
gpakosz committed Aug 18, 2013
1 parent 7938c9d commit 574853e
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 185 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ distribute them any way you like.

## Version history

* **0.1.13** ([zip](../../archive/0.1.13.zip), [tar.gz](../../archive/0.1.13.tar.gz)) — 2013-08-16
Predicate actions can refer to `yytext` (thanks to Grégory Pakosz).
Hexadecimal character escapes are supported by `leg` (thanks to Hugo Etchegoyen).
* **0.1.12** ([zip](../../archive/0.1.12.zip), [tar.gz](../../archive/0.1.12.tar.gz)) — 2013-07-20
Use BSD-licensed `getopt()` in Windows build.
Verbose mode handles Variable nodes.
Expand Down
30 changes: 25 additions & 5 deletions src/compile.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2007, 2012 by Ian Piumarta
/* Copyright (c) 2007--2013 by Ian Piumarta
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -13,7 +13,7 @@
*
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
*
* Last edited: 2013-06-06 12:24:20 by piumarta on ubuntu
* Last edited: 2013-08-16 00:58:47 by piumarta on emilia
*/

#include <stdio.h>
Expand All @@ -40,7 +40,16 @@ static void charClassClear(unsigned char bits[], int c) { bits[c >> 3] &= ~(1 <<

typedef void (*setter)(unsigned char bits[], int c);

static inline int oigit(int c) { return '0' <= c && c <= '7'; }
static inline int oigit(int c) { return ('0' <= c && c <= '7'); }
static inline int higit(int c) { return ('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f'); }

static inline int hexval(int c)
{
if ('0' <= c && c <= '9') return c - '0';
if ('A' <= c && c <= 'F') return 10 - 'A' + c;
if ('a' <= c && c <= 'f') return 10 - 'a' + c;
return 0;
}

static int cnext(unsigned char **ccp)
{
Expand All @@ -60,6 +69,11 @@ static int cnext(unsigned char **ccp)
case 'r': c= '\r'; break; /* cr */
case 't': c= '\t'; break; /* ht */
case 'v': c= '\v'; break; /* vt */
case 'x':
c= 0;
if (higit(*cclass)) c= (c << 4) + hexval(*cclass++);
if (higit(*cclass)) c= (c << 4) + hexval(*cclass++);
break;
default:
if (oigit(c))
{
Expand Down Expand Up @@ -159,7 +173,7 @@ static void Node_compile_c_ko(Node *node, int ko)
else
if (2 == len && '\\' == node->string.value[0])
fprintf(output, " if (!yymatchChar(yy, '%s')) goto l%d;", node->string.value, ko);
else
else
fprintf(output, " if (!yymatchString(yy, \"%s\")) goto l%d;", node->string.value, ko);
}
break;
Expand All @@ -173,7 +187,13 @@ static void Node_compile_c_ko(Node *node, int ko)
break;

case Predicate:
fprintf(output, " yyText(yy, yy->__begin, yy->__end); if (!(%s)) goto l%d;", node->action.text, ko);
fprintf(output, " yyText(yy, yy->__begin, yy->__end); {\n");
fprintf(output, "#define yytext yy->__text\n");
fprintf(output, "#define yyleng yy->__textlen\n");
fprintf(output, "if (!(%s)) goto l%d;\n", node->action.text, ko);
fprintf(output, "#undef yytext\n");
fprintf(output, "#undef yyleng\n");
fprintf(output, " }");
break;

case Error:
Expand Down
Loading

0 comments on commit 574853e

Please sign in to comment.