Skip to content

Commit

Permalink
imported peg-0.1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
gpakosz committed Jan 25, 2024
1 parent c8ec83c commit 2d2f6c3
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 22 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2023-03-06 Ian Piumarta <com -dot- gmail -at- piumarta (backwards)>

* src/version.h (PEG_LEVEL): Version 0.1.19.
* src/compile.c: Allow re-entrant parsing.
Macro YYPARSEFROM_R names the function yyparsefrom_r().
yyDone takes second argument, the yythunkpos at which actions start.
yyparsefrom_r saves and restores thunkpos and val state during recursive parse.
yyparsefrom resets thunkpos, val before recursive parse.

2016-07-22 Ian Piumarta <com -dot- gmail -at- piumarta (backwards)>

* src/version.h (PEG_LEVEL): Version 0.1.18.
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.19** ([zip](../../archive/0.1.19.zip), [tar.gz](../../archive/0.1.19.tar.gz)) &mdash; 2019-03-06
Experimental support for re-entrant parsing through `yyparsefrom_r()`.
* **0.1.18** ([zip](../../archive/0.1.18.zip), [tar.gz](../../archive/0.1.18.tar.gz)) &mdash; 2016-07-22
Add `-P` option to disable `#line` directives.
Emit `#line` directives for all actions.
Expand Down
39 changes: 39 additions & 0 deletions leg-mode.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;;; leg-mode.el -- minor mode for editing leg parsers
;;;
;;; THIS SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
;;;
;;; Last edited: 2016-07-26 14:14:47 by piumarta on nas.local

(defvar-local leg-mode-active? nil)
(defvar-local leg-mode-timer nil)
(defvar-local leg-mode-check? nil)

(defun leg-mode-changed ()
(when leg-mode-timer (cancel-timer leg-mode-timer))
(setq leg-mode-timer (run-with-idle-timer 0.2 t 'leg-mode-check))
(add-hook 'post-command-hook 'leg-mode-enable-check nil t))

(defun leg-mode-enable-check ()
(setq leg-mode-check? t))

(defun leg-mode-check ()
(when (and leg-mode-check? (not (region-active-p)))
(setq leg-mode-check? nil)
(let (( active (save-excursion (setq active (or (search-backward "%}" nil t) -1))))
(inactive (save-excursion (setq inactive (or (search-backward "%%" nil t) -1))))
(active? (> active inactive)))
(unless (eq active? leg-mode-active?)
(funcall (if active? 'fundamental-mode 'c-mode))
(leg-mode-changed)
(and font-lock-mode (not active?) (font-lock-fontify-buffer))
(setq leg-mode-active? active?)))))

(defun leg-mode ()
"Minor mode for editing leg parsers."
(interactive)
(funcall 'c-mode)
(unless (assq 'leg-mode-timer minor-mode-alist)
(setq minor-mode-alist (cons '(leg-mode-timer " LEG") minor-mode-alist)))
(leg-mode-changed))

(provide 'leg-mode)
33 changes: 24 additions & 9 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: 2016-07-22 09:43:05 by piumarta on zora.local
* Last edited: 2023-11-19 09:20:46 by piumarta on zora
*/

#include <stdio.h>
Expand Down Expand Up @@ -432,6 +432,9 @@ static char *preamble= "\
#ifndef YYPARSE\n\
#define YYPARSE yyparse\n\
#endif\n\
#ifndef YYPARSEFROM_R\n\
#define YYPARSEFROM_R yyparsefrom_r\n\
#endif\n\
#ifndef YYPARSEFROM\n\
#define YYPARSEFROM yyparsefrom\n\
#endif\n\
Expand Down Expand Up @@ -619,10 +622,10 @@ YY_LOCAL(int) yyText(yycontext *yy, int begin, int end)\n\
return yyleng;\n\
}\n\
\n\
YY_LOCAL(void) yyDone(yycontext *yy)\n\
YY_LOCAL(void) yyDone(yycontext *yy, int yythunkpos)\n\
{\n\
int pos;\n\
for (pos= 0; pos < yy->__thunkpos; ++pos)\n\
for (pos= yythunkpos; pos < yy->__thunkpos; ++pos)\n\
{\n\
yythunk *thunk= &yy->__thunks[pos];\n\
int yyleng= thunk->end ? yyText(yy, thunk->begin, thunk->end) : thunk->begin;\n\
Expand Down Expand Up @@ -652,7 +655,7 @@ YY_LOCAL(int) yyAccept(yycontext *yy, int tp0)\n\
}\n\
else\n\
{\n\
yyDone(yy);\n\
yyDone(yy, 0);\n\
yyCommit(yy);\n\
}\n\
return 1;\n\
Expand Down Expand Up @@ -684,9 +687,10 @@ static char *footer= "\n\
\n\
typedef int (*yyrule)(yycontext *yy);\n\
\n\
YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)\n\
YY_PARSE(int) YYPARSEFROM_R(YY_CTX_PARAM_ yyrule yystart)\n\
{\n\
int yyok;\n\
int yyok, yythunkpos;\n\
YYSTYPE *yyval;\n\
if (!yyctx->__buflen)\n\
{\n\
yyctx->__buflen= YY_BUFFER_SIZE;\n\
Expand All @@ -696,14 +700,25 @@ YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)\n\
yyctx->__thunkslen= YY_STACK_SIZE;\n\
yyctx->__thunks= (yythunk *)YY_MALLOC(yyctx, sizeof(yythunk) * yyctx->__thunkslen);\n\
yyctx->__valslen= YY_STACK_SIZE;\n\
yyctx->__vals= (YYSTYPE *)YY_MALLOC(yyctx, sizeof(YYSTYPE) * yyctx->__valslen);\n\
yyctx->__val= yyctx->__vals= (YYSTYPE *)YY_MALLOC(yyctx, sizeof(YYSTYPE) * yyctx->__valslen);\n\
yyctx->__begin= yyctx->__end= yyctx->__pos= yyctx->__limit= yyctx->__thunkpos= 0;\n\
}\n\
yyctx->__begin= yyctx->__end= yyctx->__pos;\n\
yythunkpos= yyctx->__thunkpos;\n\
yyval= yyctx->__val;\n\
yyok= yystart(yyctx);\n\
if (yyok) yyDone(yyctx, yythunkpos);\n\
yyctx->__thunkpos= yythunkpos;\n\
yyctx->__val= yyval;\n\
return yyok;\n\
}\n\
\n\
YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)\n\
{\n\
int yyok;\n\
yyctx->__thunkpos= 0;\n\
yyctx->__val= yyctx->__vals;\n\
yyok= yystart(yyctx);\n\
if (yyok) yyDone(yyctx);\n\
yyok= YYPARSEFROM_R(YY_CTX_ARG_ yystart);\n\
yyCommit(yyctx);\n\
return yyok;\n\
}\n\
Expand Down
33 changes: 24 additions & 9 deletions src/leg.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* A recursive-descent parser generated by peg 0.1.18 */
/* A recursive-descent parser generated by peg 0.1.19 */

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -76,6 +76,9 @@
#ifndef YYPARSE
#define YYPARSE yyparse
#endif
#ifndef YYPARSEFROM_R
#define YYPARSEFROM_R yyparsefrom_r
#endif
#ifndef YYPARSEFROM
#define YYPARSEFROM yyparsefrom
#endif
Expand Down Expand Up @@ -263,10 +266,10 @@ YY_LOCAL(int) yyText(yycontext *yy, int begin, int end)
return yyleng;
}

YY_LOCAL(void) yyDone(yycontext *yy)
YY_LOCAL(void) yyDone(yycontext *yy, int yythunkpos)
{
int pos;
for (pos= 0; pos < yy->__thunkpos; ++pos)
for (pos= yythunkpos; pos < yy->__thunkpos; ++pos)
{
yythunk *thunk= &yy->__thunks[pos];
int yyleng= thunk->end ? yyText(yy, thunk->begin, thunk->end) : thunk->begin;
Expand Down Expand Up @@ -296,7 +299,7 @@ YY_LOCAL(int) yyAccept(yycontext *yy, int tp0)
}
else
{
yyDone(yy);
yyDone(yy, 0);
yyCommit(yy);
}
return 1;
Expand Down Expand Up @@ -1352,9 +1355,10 @@ YY_RULE(int) yy_grammar(yycontext *yy)

typedef int (*yyrule)(yycontext *yy);

YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)
YY_PARSE(int) YYPARSEFROM_R(YY_CTX_PARAM_ yyrule yystart)
{
int yyok;
int yyok, yythunkpos;
YYSTYPE *yyval;
if (!yyctx->__buflen)
{
yyctx->__buflen= YY_BUFFER_SIZE;
Expand All @@ -1364,14 +1368,25 @@ YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)
yyctx->__thunkslen= YY_STACK_SIZE;
yyctx->__thunks= (yythunk *)YY_MALLOC(yyctx, sizeof(yythunk) * yyctx->__thunkslen);
yyctx->__valslen= YY_STACK_SIZE;
yyctx->__vals= (YYSTYPE *)YY_MALLOC(yyctx, sizeof(YYSTYPE) * yyctx->__valslen);
yyctx->__val= yyctx->__vals= (YYSTYPE *)YY_MALLOC(yyctx, sizeof(YYSTYPE) * yyctx->__valslen);
yyctx->__begin= yyctx->__end= yyctx->__pos= yyctx->__limit= yyctx->__thunkpos= 0;
}
yyctx->__begin= yyctx->__end= yyctx->__pos;
yythunkpos= yyctx->__thunkpos;
yyval= yyctx->__val;
yyok= yystart(yyctx);
if (yyok) yyDone(yyctx, yythunkpos);
yyctx->__thunkpos= yythunkpos;
yyctx->__val= yyval;
return yyok;
}

YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)
{
int yyok;
yyctx->__thunkpos= 0;
yyctx->__val= yyctx->__vals;
yyok= yystart(yyctx);
if (yyok) yyDone(yyctx);
yyok= YYPARSEFROM_R(YY_CTX_ARG_ yystart);
yyCommit(yyctx);
return yyok;
}
Expand Down
6 changes: 3 additions & 3 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: 2016-07-22 09:45:53 by piumarta on zora.local
# Last edited: 2023-10-29 12:52:56 by piumarta on zora

%{
# include "tree.h"
Expand Down Expand Up @@ -155,7 +155,7 @@ TILDE= '~' -
RPERCENT= '%}' -

-= (space | comment)*
space= ' ' | '\t' | end-of-line
space= ' ' | '\t' | '\f' | end-of-line
comment= '#' (!end-of-line .)* end-of-line
end-of-line= ( '\r\n' | '\n' | '\r' ) { ++lineNumber }
end-of-file= !.
Expand Down Expand Up @@ -311,7 +311,7 @@ int main(int argc, char **argv)

if (trailer) {
if (!nolinesFlag)
fprintf(output, "#line %i \"%s\"\n", trailerLine, fileName, trailer);
fprintf(output, "#line %i \"%s\"\n", trailerLine, fileName);
fprintf(output, "%s\n", trailer);
}

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 18
#define PEG_LEVEL 19

0 comments on commit 2d2f6c3

Please sign in to comment.