1 /*
2   Copyright 2002-2004 John Plevyak, All Rights Reserved
3 */
4 module ddparser.dparse_tables;
5 
6 import ddparser.dparse;
7 import ddparser.parse;
8 import std.conv;
9 import core.stdc.string;
10 
11 enum SCANNER_BLOCKS_POW2=		2;
12 enum SCANNER_BLOCKS	=		(1 << SCANNER_BLOCKS_POW2);
13 enum SCANNER_BLOCK_SHIFT		=(8 - SCANNER_BLOCKS_POW2);
14 enum SCANNER_BLOCK_MASK		=((1 << SCANNER_BLOCK_SHIFT) - 1);
15 enum SCANNER_BLOCK_SIZE		=(256 / SCANNER_BLOCKS);
16 
17 struct D_ShiftTable;
18 
19 D_ParseNode* D_PN(T)(void* _x, T _o)
20 {
21     return (cast(D_ParseNode*)(cast(char*)(_x) + _o));
22 }
23 
24 struct d_loc_t {
25   const(char) *s, pathname, ws;
26   int col, line;
27 }
28 
29 alias D_WhiteSpaceFn = void delegate(D_Parser *p, 
30         d_loc_t *loc, void **p_globals);
31 alias D_ScanCode = int function(d_loc_t *loc, ushort *symbol, 
32         int *term_priority, ubyte *op_assoc, int *op_priority);
33 alias D_ReductionCode = int function(
34         void *new_ps, void **children, int n_children, int pn_offset,
35         D_Parser *parser);
36 
37 struct D_Reduction {
38     ushort	nelements;
39     ushort	symbol;
40     D_ReductionCode	speculative_code;
41     D_ReductionCode	final_code;
42     ushort	op_assoc;
43     ushort	rule_assoc;
44     int 			op_priority;
45     int 			rule_priority;
46     int			action_index;
47     D_ReductionCode[] pass_code;
48 }
49 
50 struct D_RightEpsilonHint {
51     ushort	depth;
52     ushort	preceeding_state;
53     D_Reduction		*reduction;
54 }
55 
56 struct D_ErrorRecoveryHint {
57     ushort	depth;
58     ushort	symbol;
59     string str;
60 }
61 
62 struct D_Shift {
63   ushort	symbol;
64   ubyte		shift_kind;
65   ubyte		op_assoc;
66   int			op_priority;
67   int			term_priority;
68   int			action_index;
69   D_ReductionCode	speculative_code;
70 }
71 
72 struct SB_(T)
73 {
74     D_Shift*[] shift;
75     T*[SCANNER_BLOCKS] scanner_block;
76 }
77 
78 alias SB_uint8 = SB_!ubyte;
79 alias SB_uint16 = SB_!ushort;
80 alias SB_uint32 = SB_!uint;
81 
82 static assert(SB_uint8.sizeof == SB_uint16.sizeof && SB_uint16.sizeof == SB_uint32.sizeof);
83 
84 struct SB_trans(T)
85 {
86     T*[SCANNER_BLOCKS] scanner_block;
87 }
88 
89 alias SB_trans_uint8 = SB_trans!ubyte;
90 alias SB_trans_uint16 = SB_trans!ushort;
91 alias SB_trans_uint32 = SB_trans!uint;
92 
93 static assert(SB_trans_uint8.sizeof == SB_trans_uint16.sizeof && SB_trans_uint16.sizeof == SB_trans_uint32.sizeof);
94 
95 enum D_SCAN_ALL=	0;
96 enum D_SCAN_LONGEST	=1;
97 enum D_SCAN_MIXED	=2;
98 enum D_SCAN_TRAILING	=3;
99 enum D_SCAN_RESERVED	=4;
100 enum D_SCAN_DEFAULT	=D_SCAN_ALL;
101 
102 struct D_State {
103     ubyte[]			goto_valid;
104     D_Reduction*[]  reductions;
105     D_RightEpsilonHint[] right_epsilon_hints;
106     D_ErrorRecoveryHint[] error_recovery_hints;
107     SB_uint32[]		scanner_table;
108     SB_trans_uint32[]	transition_table;
109     D_Shift*[][]     accepts_diff;
110     D_ScanCode			scanner_code;
111     int				reduces_to;
112     int				goto_table_offset;
113     bool			shifts;
114     ubyte			scanner_size;
115     bool			accept;
116     ubyte			scan_kind;
117 }
118 
119 enum D_SymbolKind
120 {
121     D_SYMBOL_NTERM = 1,
122     D_SYMBOL_INTERNAL,
123     D_SYMBOL_EBNF,
124     D_SYMBOL_STRING,
125     D_SYMBOL_REGEX,
126     D_SYMBOL_CODE,
127     D_SYMBOL_TOKEN
128 }
129 
130 struct D_Symbol {
131   string name;
132   int			start_symbol;
133   D_SymbolKind		kind;
134 }
135 
136 enum D_PASS_PRE_ORDER	=0x0001;
137 enum D_PASS_POST_ORDER	=0x0002;
138 enum D_PASS_MANUAL		=0x0004;
139 enum D_PASS_FOR_ALL		=0x0008;
140 enum D_PASS_FOR_UNDEFINED	=0x0010;
141 struct D_Pass {
142     string name;
143     uint    kind;
144     uint	index;
145 }
146 
147 struct D_ParserTables {
148     D_State[]   states;
149     ushort[]    goto_table;
150     uint		whitespace_state;
151     D_Symbol[]  symbols;
152     D_WhiteSpaceFn	default_white_space;
153     D_Pass[]    passes;
154     uint		save_parse_tree;
155 }
156