aboutsummaryrefslogtreecommitdiff
path: root/lptypes.h
diff options
context:
space:
mode:
Diffstat (limited to 'lptypes.h')
-rw-r--r--lptypes.h145
1 files changed, 145 insertions, 0 deletions
diff --git a/lptypes.h b/lptypes.h
new file mode 100644
index 0000000..5226970
--- /dev/null
+++ b/lptypes.h
@@ -0,0 +1,145 @@
1/*
2** $Id: lptypes.h,v 1.17 2017/12/14 16:56:27 roberto Exp $
3** LPeg - PEG pattern matching for Lua
4** Copyright 2007-2017, Lua.org & PUC-Rio (see 'lpeg.html' for license)
5** written by Roberto Ierusalimschy
6*/
7
8#if !defined(lptypes_h)
9#define lptypes_h
10
11
12#include <assert.h>
13#include <limits.h>
14
15#include "lua.h"
16
17
18#define VERSION "1.0.1"
19
20
21#define PATTERN_T "lpeg-pattern"
22#define MAXSTACKIDX "lpeg-maxstack"
23
24
25/*
26** compatibility with Lua 5.1
27*/
28#if (LUA_VERSION_NUM == 501)
29
30#define lp_equal lua_equal
31
32#define lua_getuservalue lua_getfenv
33#define lua_setuservalue lua_setfenv
34
35#define lua_rawlen lua_objlen
36
37#define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f)
38#define luaL_newlib(L,f) luaL_register(L,"lpeg",f)
39
40#endif
41
42
43#if !defined(lp_equal)
44#define lp_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ)
45#endif
46
47
48/* default maximum size for call/backtrack stack */
49#if !defined(MAXBACK)
50#define MAXBACK 400
51#endif
52
53
54/* maximum number of rules in a grammar (limited by 'unsigned char') */
55#if !defined(MAXRULES)
56#define MAXRULES 250
57#endif
58
59
60
61/* initial size for capture's list */
62#define INITCAPSIZE 32
63
64
65/* index, on Lua stack, for subject */
66#define SUBJIDX 2
67
68/* number of fixed arguments to 'match' (before capture arguments) */
69#define FIXEDARGS 3
70
71/* index, on Lua stack, for capture list */
72#define caplistidx(ptop) ((ptop) + 2)
73
74/* index, on Lua stack, for pattern's ktable */
75#define ktableidx(ptop) ((ptop) + 3)
76
77/* index, on Lua stack, for backtracking stack */
78#define stackidx(ptop) ((ptop) + 4)
79
80
81
82typedef unsigned char byte;
83
84
85#define BITSPERCHAR 8
86
87#define CHARSETSIZE ((UCHAR_MAX/BITSPERCHAR) + 1)
88
89
90
91typedef struct Charset {
92 byte cs[CHARSETSIZE];
93} Charset;
94
95
96
97#define loopset(v,b) { int v; for (v = 0; v < CHARSETSIZE; v++) {b;} }
98
99/* access to charset */
100#define treebuffer(t) ((byte *)((t) + 1))
101
102/* number of slots needed for 'n' bytes */
103#define bytes2slots(n) (((n) - 1) / sizeof(TTree) + 1)
104
105/* set 'b' bit in charset 'cs' */
106#define setchar(cs,b) ((cs)[(b) >> 3] |= (1 << ((b) & 7)))
107
108
109/*
110** in capture instructions, 'kind' of capture and its offset are
111** packed in field 'aux', 4 bits for each
112*/
113#define getkind(op) ((op)->i.aux & 0xF)
114#define getoff(op) (((op)->i.aux >> 4) & 0xF)
115#define joinkindoff(k,o) ((k) | ((o) << 4))
116
117#define MAXOFF 0xF
118#define MAXAUX 0xFF
119
120
121/* maximum number of bytes to look behind */
122#define MAXBEHIND MAXAUX
123
124
125/* maximum size (in elements) for a pattern */
126#define MAXPATTSIZE (SHRT_MAX - 10)
127
128
129/* size (in elements) for an instruction plus extra l bytes */
130#define instsize(l) (((l) + sizeof(Instruction) - 1)/sizeof(Instruction) + 1)
131
132
133/* size (in elements) for a ISet instruction */
134#define CHARSETINSTSIZE instsize(CHARSETSIZE)
135
136/* size (in elements) for a IFunc instruction */
137#define funcinstsize(p) ((p)->i.aux + 2)
138
139
140
141#define testchar(st,c) (((int)(st)[((c) >> 3)] & (1 << ((c) & 7))))
142
143
144#endif
145