Skip to content

Commit

Permalink
imported peg-0.1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
gpakosz committed Jul 8, 2016
1 parent 2acf83a commit dc057c4
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 130 deletions.
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2016-06-25 piumarta <com -dot- gmail -at- piumarta (backwards)>

* src/version.h: 0.1.16

* src/tree.[ch], src/compile.c, src/leg.leg: Add @-actions.

* src/peg.1: Explain @-actions.

* src/peg/peg-c, src/leg.c: Regenerate C source.

2013-12-18 piumarta <com -dot- gmail -at- piumarta (backwards)>

* src/version.h: 0.1.15
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ distribute them any way you like.

## Version history

* **0.1.16** ([zip](../../archive/0.1.16.zip), [tar.gz](../../archive/0.1.16.tar.gz)) &mdash; 2016-06-25
Add `@{...}` actions that are performed during matching.
* **0.1.15** ([zip](../../archive/0.1.15.zip), [tar.gz](../../archive/0.1.15.tar.gz)) &mdash; 2013-12-17
Calls to `YY_FREE` fixed (thanks to Andrew Dunham).
* **0.1.14** ([zip](../../archive/0.1.14.zip), [tar.gz](../../archive/0.1.14.tar.gz)) &mdash; 2013-12-01
Expand Down
14 changes: 12 additions & 2 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
*
* Last edited: 2013-12-18 10:09:42 by piumarta on linux32
* Last edited: 2016-02-19 11:08:58 by piumarta on zora
*/

#include <stdio.h>
Expand Down Expand Up @@ -186,11 +186,20 @@ static void Node_compile_c_ko(Node *node, int ko)
fprintf(output, " yyDo(yy, yy%s, yy->__begin, yy->__end);", node->action.name);
break;

case Inline:
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, "%s;\n", node->inLine.text);
fprintf(output, "#undef yytext\n");
fprintf(output, "#undef yyleng\n");
break;

case Predicate:
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, "if (!(%s)) goto l%d;\n", node->predicate.text, ko);
fprintf(output, "#undef yytext\n");
fprintf(output, "#undef yyleng\n");
fprintf(output, " }");
Expand Down Expand Up @@ -755,6 +764,7 @@ int consumesInput(Node *node)
case String: return strlen(node->string.value) > 0;
case Class: return 1;
case Action: return 0;
case Inline: return 0;
case Predicate: return 0;
case Error: return consumesInput(node->error.element);

Expand Down
260 changes: 142 additions & 118 deletions src/leg.c

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/leg.leg
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
# THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
#
# Last edited: 2013-08-16 00:14:11 by piumarta on emilia
# Last edited: 2016-02-19 11:04:51 by piumarta on zora

%{
# include "tree.h"
Expand Down Expand Up @@ -82,7 +82,8 @@ sequence= error (error { Node *f= pop(); push(Sequence_append(pop(), f));
error= prefix (TILDE action { push(makeError(pop(), yytext)); }
)?

prefix= AND action { push(makePredicate(yytext)); }
prefix= AT action { push(makeInline(yytext)); }
| AND action { push(makePredicate(yytext)); }
| AND suffix { push(makePeekFor(pop())); }
| NOT suffix { push(makePeekNot(pop())); }
| suffix
Expand Down Expand Up @@ -132,6 +133,7 @@ SEMICOLON= ';' -
BAR= '|' -
AND= '&' -
NOT= '!' -
AT= '@' -
QUESTION= '?' -
STAR= '*' -
PLUS= '+' -
Expand Down
20 changes: 17 additions & 3 deletions src/peg.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Copyright (c) 2007 by Ian Piumarta
.\" Copyright (c) 2007,2016 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-09-09 14:58:44 by piumarta on emilia
.\" Last edited: 2016-06-25 12:38:49 by piumarta on gentoo64.piumarta.com
.\"
.TH PEG 1 "September 2013" "Version 0.1"
.SH NAME
Expand Down Expand Up @@ -447,7 +447,7 @@ The 'assignment' operator replaces the left arrow operator '<\-'.
.TP
.B rule\-name
Hyphens can appear as letters in the names of rules. Each hyphen is
converted into an underscore in the generated C source code. A single
converted into an underscore in the generated C source code. A
single hyphen '\-' is a legal rule name.
.nf

Expand Down Expand Up @@ -487,6 +487,20 @@ in
.I leg
(with the final semicolon being optional, as described next).
.TP
.IB @{\ action\ }
Actions prefixed with an 'at' symbol will be performed during parsing,
at the time they are encountered while matching the input text with a
rule.
Because of back-tracking in the PEG parsing algorithm, actions
prefixed with '@' might be performed multiple times for the same input
text.
(The usual behviour of actions is that they are saved up until
matching is complete, and then those that are part of the
final derivation are performed in left-to-right order.)
The variable
.I yytext
is available within these actions.
.TP
.IB exp \ ~ \ {\ action\ }
A postfix operator
.BI ~ {\ action\ }
Expand Down
2 changes: 1 addition & 1 deletion src/peg.peg-c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* A recursive-descent parser generated by peg 0.1.15 */
/* A recursive-descent parser generated by peg 0.1.16 */

#include <stdio.h>
#include <stdlib.h>
Expand Down
9 changes: 8 additions & 1 deletion src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
*
* Last edited: 2013-07-20 12:47:35 by piumarta on margaux1
* Last edited: 2016-02-19 11:21:30 by piumarta on zora
*/

#include <stdio.h>
Expand Down Expand Up @@ -160,6 +160,13 @@ Node *makeAction(char *text)
return node;
}

Node *makeInline(char *text)
{
Node *node= newNode(Inline);
node->inLine.text= strdup(text);
return node;
}

Node *makePredicate(char *text)
{
Node *node= newNode(Predicate);
Expand Down
7 changes: 5 additions & 2 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
*
* Last edited: 2012-05-15 22:37:53 by piumarta on emilia
* Last edited: 2016-02-19 11:06:20 by piumarta on zora
*/

#include <stdio.h>

enum { Unknown= 0, Rule, Variable, Name, Dot, Character, String, Class, Action, Predicate, Error, Alternate, Sequence, PeekFor, PeekNot, Query, Star, Plus };
enum { Unknown= 0, Rule, Variable, Name, Dot, Character, String, Class, Action, Inline, Predicate, Error, Alternate, Sequence, PeekFor, PeekNot, Query, Star, Plus };

enum {
RuleUsed = 1<<0,
Expand All @@ -35,6 +35,7 @@ struct Character { int type; Node *next; char *value; };
struct String { int type; Node *next; char *value; };
struct Class { int type; Node *next; unsigned char *value; };
struct Action { int type; Node *next; char *text; Node *list; char *name; Node *rule; };
struct Inline { int type; Node *next; char *text; };
struct Predicate { int type; Node *next; char *text; };
struct Error { int type; Node *next; Node *element; char *text; };
struct Alternate { int type; Node *next; Node *first; Node *last; };
Expand All @@ -57,6 +58,7 @@ union Node
struct String string;
struct Class cclass;
struct Action action;
struct Inline inLine;
struct Predicate predicate;
struct Error error;
struct Alternate alternate;
Expand Down Expand Up @@ -89,6 +91,7 @@ extern Node *makeCharacter(char *text);
extern Node *makeString(char *text);
extern Node *makeClass(char *text);
extern Node *makeAction(char *text);
extern Node *makeInline(char *text);
extern Node *makePredicate(char *text);
extern Node *makeError(Node *e, char *text);
extern Node *makeAlternate(Node *e);
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define PEG_MAJOR 0
#define PEG_MINOR 1
#define PEG_LEVEL 15
#define PEG_LEVEL 16

0 comments on commit dc057c4

Please sign in to comment.