robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
2.3 KiB79 linesraw
1
2#if !defined(lpvm_h)
3#define lpvm_h
4
5#include "lpcap.h"
6
7
8/*
9** About Character sets in instructions: a set is a bit map with an
10** initial offset, in bits, and a size, in number of instructions.
11** aux1 has the default value for the bits outsize that range.
12*/
13
14
15/* Virtual Machine's instructions */
16typedef enum Opcode {
17  IAny, /* if no char, fail */
18  IChar,  /* if char != aux1, fail */
19  ISet,  /* if char not in set, fail */
20  ITestAny,  /* in no char, jump to 'offset' */
21  ITestChar,  /* if char != aux1, jump to 'offset' */
22  ITestSet,  /* if char not in set, jump to 'offset' */
23  ISpan,  /* read a span of chars in set */
24  IUTFR,  /* if codepoint not in range [offset, utf_to], fail */
25  IBehind,  /* walk back 'aux1' characters (fail if not possible) */
26  IRet,  /* return from a rule */
27  IEnd,  /* end of pattern */
28  IChoice,  /* stack a choice; next fail will jump to 'offset' */
29  IJmp,  /* jump to 'offset' */
30  ICall,  /* call rule at 'offset' */
31  IOpenCall,  /* call rule number 'key' (must be closed to a ICall) */
32  ICommit,  /* pop choice and jump to 'offset' */
33  IPartialCommit,  /* update top choice to current position and jump */
34  IBackCommit,  /* backtrack like "fail" but jump to its own 'offset' */
35  IFailTwice,  /* pop one choice and then fail */
36  IFail,  /* go back to saved state on choice and jump to saved offset */
37  IGiveup,  /* internal use */
38  IFullCapture,  /* complete capture of last 'off' chars */
39  IOpenCapture,  /* start a capture */
40  ICloseCapture,
41  ICloseRunTime,
42  IEmpty  /* to fill empty slots left by optimizations */
43} Opcode;
44
45
46/*
47** All array of instructions has a 'codesize' as its first element
48** and is referred by a pointer to its second element, which is the
49** first actual opcode.
50*/
51typedef union Instruction {
52  struct Inst {
53    byte code;
54    byte aux1;
55    union {
56      short key;
57      struct {
58        byte offset;
59        byte size;
60      } set;
61    } aux2;
62  } i;
63  int offset;
64  uint codesize;
65  byte buff[1];
66} Instruction;
67
68
69/* extract 24-bit value from an instruction */
70#define utf_to(inst)	(((inst)->i.aux2.key << 8) | (inst)->i.aux1)
71
72
73int charinset (const Instruction *i, const byte *buff, uint c);
74const char *match (lua_State *L, const char *o, const char *s, const char *e,
75                   Instruction *op, Capture *capture, int ptop);
76
77
78#endif
79