Debian Codegraph @

lezer-lr/src/constants.ts

raw
1 // This file defines some constants that are needed both in this
2 // package and in lezer-generator, so that the generator code can
3 // access them without them being part of lezer's public interface.
4
5 // Parse actions are represented as numbers, in order to cheaply and
6 // simply pass them around. The numbers are treated as bitfields
7 // holding different pieces of information.
8 //
9 // When storing actions in 16-bit number arrays, they are split in the
10 // middle, with the first element holding the first 16 bits, and the
11 // second the rest.
12 //
13 // The value 0 (which is not a valid action because no shift goes to
14 // state 0, the start state), is often used to denote the absence of a
15 // valid action.
16 export const enum Action {
17 // Distinguishes between shift (off) and reduce (on) actions.
18 ReduceFlag = 1 << 16,
19 // The first 16 bits hold the target state's id for shift actions,
20 // and the reduced term id for reduce actions.
21 ValueMask = 2**16 - 1,
22 // In reduce actions, all bits beyond 18 hold the reduction's depth
23 // (the amount of stack frames it reduces).
24 ReduceDepthShift = 19,
25 // This is set for reduce actions that reduce two instances of a
26 // repeat term to the term (but _not_ for the reductions that match
27 // the repeated content).
28 RepeatFlag = 1 << 17,
29 // Goto actions are a special kind of shift that don't actually
30 // shift the current token, just add a stack frame. This is used for
31 // non-simple skipped expressions, to enter the skip rule when the
32 // appropriate token is seen (because the arbitrary state from which
33 // such a rule may start doesn't have the correct goto entries).
34 GotoFlag = 1 << 17,
35 // Both shifts and reduces can have a stay flag set. For shift, it
36 // means that the current token must be shifted but the state should
37 // stay the same (used for single-token skip expression). For
38 // reduce, it means that, instead of consulting the goto table to
39 // determine which state to go to, the state already on the stack
40 // must be returned to (used at the end of non-simple skip
41 // expressions).
42 StayFlag = 1 << 18
43 }
44
45 // Each parser state has a `flags` field.
46 export const enum StateFlag {
47 // Set if this state is part of a skip expression (which means nodes
48 // produced by it should be moved out of any node reduced directly
49 // after them).
50 Skipped = 1,
51 // Indicates whether this is an accepting state.
52 Accepting = 2
53 }
54
55 // The lowest bit of the values stored in `parser.specializations`
56 // indicate whether this specialization replaced the original token
57 // (`Specialize`) or adds a second interpretation while also leaving
58 // the first (`Extend`).
59 export const enum Specialize {
60 Specialize = 0,
61 Extend = 1
62 }
63
64 // Terms are 16-bit numbers
65 export const enum Term {
66 // The value of the error term is hard coded, the others are
67 // allocated per grammar.
68 Err = 0
69 }
70
71 export const enum Seq {
72 // Used as end marker for most of the sequences stored in uint16
73 // arrays
74 End = 0xffff,
75 Done = 0,
76 Next = 1,
77 Other = 2,
78 }
79
80 // Memory layout of parse states
81 export const enum ParseState {
82 // Offsets into the record of the individual fields
83 Flags = 0,
84 Actions = 1,
85 Skip = 2,
86 TokenizerMask = 3,
87 DefaultReduce = 4,
88 ForcedReduce = 5,
89 // Total size of a state record
90 Size = 6
91 }
92
93 export const enum Encode {
94 BigValCode = 126,
95 BigVal = 0xffff,
96 Start = 32,
97 Gap1 = 34, // '"'
98 Gap2 = 92, // '\\;
99 Base = 46 // (126 - 32 - 2) / 2
100 }
101
102 export const enum File {
103 Version = 14
104 }