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