Skip to content

Commit dc057c4

Browse files
committed
imported peg-0.1.16
1 parent 2acf83a commit dc057c4

File tree

10 files changed

+202
-130
lines changed

10 files changed

+202
-130
lines changed

ChangeLog

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2016-06-25 piumarta <com -dot- gmail -at- piumarta (backwards)>
2+
3+
* src/version.h: 0.1.16
4+
5+
* src/tree.[ch], src/compile.c, src/leg.leg: Add @-actions.
6+
7+
* src/peg.1: Explain @-actions.
8+
9+
* src/peg/peg-c, src/leg.c: Regenerate C source.
10+
111
2013-12-18 piumarta <com -dot- gmail -at- piumarta (backwards)>
212

313
* src/version.h: 0.1.15

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ distribute them any way you like.
2626

2727
## Version history
2828

29+
* **0.1.16** ([zip](../../archive/0.1.16.zip), [tar.gz](../../archive/0.1.16.tar.gz)) &mdash; 2016-06-25
30+
Add `@{...}` actions that are performed during matching.
2931
* **0.1.15** ([zip](../../archive/0.1.15.zip), [tar.gz](../../archive/0.1.15.tar.gz)) &mdash; 2013-12-17
3032
Calls to `YY_FREE` fixed (thanks to Andrew Dunham).
3133
* **0.1.14** ([zip](../../archive/0.1.14.zip), [tar.gz](../../archive/0.1.14.tar.gz)) &mdash; 2013-12-01

src/compile.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
1515
*
16-
* Last edited: 2013-12-18 10:09:42 by piumarta on linux32
16+
* Last edited: 2016-02-19 11:08:58 by piumarta on zora
1717
*/
1818

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

189+
case Inline:
190+
fprintf(output, " yyText(yy, yy->__begin, yy->__end);\n");
191+
fprintf(output, "#define yytext yy->__text\n");
192+
fprintf(output, "#define yyleng yy->__textlen\n");
193+
fprintf(output, "%s;\n", node->inLine.text);
194+
fprintf(output, "#undef yytext\n");
195+
fprintf(output, "#undef yyleng\n");
196+
break;
197+
189198
case Predicate:
190199
fprintf(output, " yyText(yy, yy->__begin, yy->__end); {\n");
191200
fprintf(output, "#define yytext yy->__text\n");
192201
fprintf(output, "#define yyleng yy->__textlen\n");
193-
fprintf(output, "if (!(%s)) goto l%d;\n", node->action.text, ko);
202+
fprintf(output, "if (!(%s)) goto l%d;\n", node->predicate.text, ko);
194203
fprintf(output, "#undef yytext\n");
195204
fprintf(output, "#undef yyleng\n");
196205
fprintf(output, " }");
@@ -755,6 +764,7 @@ int consumesInput(Node *node)
755764
case String: return strlen(node->string.value) > 0;
756765
case Class: return 1;
757766
case Action: return 0;
767+
case Inline: return 0;
758768
case Predicate: return 0;
759769
case Error: return consumesInput(node->error.element);
760770

src/leg.c

+142-118
Large diffs are not rendered by default.

src/leg.leg

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
# THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
1717
#
18-
# Last edited: 2013-08-16 00:14:11 by piumarta on emilia
18+
# Last edited: 2016-02-19 11:04:51 by piumarta on zora
1919

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

85-
prefix= AND action { push(makePredicate(yytext)); }
85+
prefix= AT action { push(makeInline(yytext)); }
86+
| AND action { push(makePredicate(yytext)); }
8687
| AND suffix { push(makePeekFor(pop())); }
8788
| NOT suffix { push(makePeekNot(pop())); }
8889
| suffix
@@ -132,6 +133,7 @@ SEMICOLON= ';' -
132133
BAR= '|' -
133134
AND= '&' -
134135
NOT= '!' -
136+
AT= '@' -
135137
QUESTION= '?' -
136138
STAR= '*' -
137139
PLUS= '+' -

src/peg.1

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" Copyright (c) 2007 by Ian Piumarta
1+
.\" Copyright (c) 2007,2016 by Ian Piumarta
22
.\" All rights reserved.
33
.\"
44
.\" Permission is hereby granted, free of charge, to any person obtaining a
@@ -13,7 +13,7 @@
1313
.\"
1414
.\" THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
1515
.\"
16-
.\" Last edited: 2013-09-09 14:58:44 by piumarta on emilia
16+
.\" Last edited: 2016-06-25 12:38:49 by piumarta on gentoo64.piumarta.com
1717
.\"
1818
.TH PEG 1 "September 2013" "Version 0.1"
1919
.SH NAME
@@ -447,7 +447,7 @@ The 'assignment' operator replaces the left arrow operator '<\-'.
447447
.TP
448448
.B rule\-name
449449
Hyphens can appear as letters in the names of rules. Each hyphen is
450-
converted into an underscore in the generated C source code. A single
450+
converted into an underscore in the generated C source code. A
451451
single hyphen '\-' is a legal rule name.
452452
.nf
453453

@@ -487,6 +487,20 @@ in
487487
.I leg
488488
(with the final semicolon being optional, as described next).
489489
.TP
490+
.IB @{\ action\ }
491+
Actions prefixed with an 'at' symbol will be performed during parsing,
492+
at the time they are encountered while matching the input text with a
493+
rule.
494+
Because of back-tracking in the PEG parsing algorithm, actions
495+
prefixed with '@' might be performed multiple times for the same input
496+
text.
497+
(The usual behviour of actions is that they are saved up until
498+
matching is complete, and then those that are part of the
499+
final derivation are performed in left-to-right order.)
500+
The variable
501+
.I yytext
502+
is available within these actions.
503+
.TP
490504
.IB exp \ ~ \ {\ action\ }
491505
A postfix operator
492506
.BI ~ {\ action\ }

src/peg.peg-c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* A recursive-descent parser generated by peg 0.1.15 */
1+
/* A recursive-descent parser generated by peg 0.1.16 */
22

33
#include <stdio.h>
44
#include <stdlib.h>

src/tree.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
1515
*
16-
* Last edited: 2013-07-20 12:47:35 by piumarta on margaux1
16+
* Last edited: 2016-02-19 11:21:30 by piumarta on zora
1717
*/
1818

1919
#include <stdio.h>
@@ -160,6 +160,13 @@ Node *makeAction(char *text)
160160
return node;
161161
}
162162

163+
Node *makeInline(char *text)
164+
{
165+
Node *node= newNode(Inline);
166+
node->inLine.text= strdup(text);
167+
return node;
168+
}
169+
163170
Node *makePredicate(char *text)
164171
{
165172
Node *node= newNode(Predicate);

src/tree.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
*
1414
* THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
1515
*
16-
* Last edited: 2012-05-15 22:37:53 by piumarta on emilia
16+
* Last edited: 2016-02-19 11:06:20 by piumarta on zora
1717
*/
1818

1919
#include <stdio.h>
2020

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

2323
enum {
2424
RuleUsed = 1<<0,
@@ -35,6 +35,7 @@ struct Character { int type; Node *next; char *value; };
3535
struct String { int type; Node *next; char *value; };
3636
struct Class { int type; Node *next; unsigned char *value; };
3737
struct Action { int type; Node *next; char *text; Node *list; char *name; Node *rule; };
38+
struct Inline { int type; Node *next; char *text; };
3839
struct Predicate { int type; Node *next; char *text; };
3940
struct Error { int type; Node *next; Node *element; char *text; };
4041
struct Alternate { int type; Node *next; Node *first; Node *last; };
@@ -57,6 +58,7 @@ union Node
5758
struct String string;
5859
struct Class cclass;
5960
struct Action action;
61+
struct Inline inLine;
6062
struct Predicate predicate;
6163
struct Error error;
6264
struct Alternate alternate;
@@ -89,6 +91,7 @@ extern Node *makeCharacter(char *text);
8991
extern Node *makeString(char *text);
9092
extern Node *makeClass(char *text);
9193
extern Node *makeAction(char *text);
94+
extern Node *makeInline(char *text);
9295
extern Node *makePredicate(char *text);
9396
extern Node *makeError(Node *e, char *text);
9497
extern Node *makeAlternate(Node *e);

src/version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#define PEG_MAJOR 0
22
#define PEG_MINOR 1
3-
#define PEG_LEVEL 15
3+
#define PEG_LEVEL 16

0 commit comments

Comments
 (0)