summaryrefslogtreecommitdiffstats
path: root/llparse-builder/src/code/creator.ts
blob: 98f9296facedb6555be583314f1cfb72b28cc978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import * as code from './';

/**
 * API for creating external callbacks and intrinsic operations.
 */
export class Creator {
  // Callbacks to external C functions

  /**
   * Create an external callback that **has no** `value` argument.
   *
   * This callback can be used in all `Invoke` nodes except those that are
   * targets of `.select()` method.
   *
   * C signature of callback must be:
   *
   * ```c
   * int name(llparse_t* state, const char* p, const char* endp)
   * ```
   *
   * Where `llparse_t` is parser state's type name.
   *
   * @param name External function name.
   */
  public match(name: string): code.Match {
    return new code.Match(name);
  }

  /**
   * Create an external callback that **has** `value` argument.
   *
   * This callback can be used only in `Invoke` nodes that are targets of
   * `.select()` method.
   *
   * C signature of callback must be:
   *
   * ```c
   * int name(llparse_t* state, const char* p, const char* endp, int value)
   * ```
   *
   * Where `llparse_t` is parser state's type name.
   *
   * @param name External function name.
   */
  public value(name: string): code.Value {
    return new code.Value(name);
  }

  /**
   * Create an external span callback.
   *
   * This callback can be used only in `Span` constructor.
   *
   * C signature of callback must be:
   *
   * ```c
   * int name(llparse_t* state, const char* p, const char* endp)
   * ```
   *
   * NOTE: non-zero return value is treated as resumable error.
   *
   * @param name External function name.
   */
  public span(name: string): code.Span {
    return new code.Span(name);
  }

  // Helpers

  /**
   * Intrinsic operation. Stores `value` from `.select()` node into the state's
   * property with the name specified by `field`, returns zero.
   *
   *   state[field] = value;
   *   return 0;
   *
   * @param field  Property name
   */
  public store(field: string): code.Store {
    return new code.Store(field);
  }

  /**
   * Intrinsic operation. Loads and returns state's property with the name
   * specified by `field`.
   *
   * The value of the property is either truncated or zero-extended to fit into
   * 32-bit unsigned integer.
   *
   *   return state[field];
   *
   * @param field  Property name.
   */
  public load(field: string): code.Load {
    return new code.Load(field);
  }

  /**
   * Intrinsic operation. Takes `value` from `.select()`, state's property
   * with the name `field` and does:
   *
   *   field = state[field];
   *   field *= options.base;
   *   field += value;
   *   state[field] = field;
   *   return 0;  // or 1 on overflow
   *
   * Return values are:
   *
   * - 0 - success
   * - 1 - overflow
   *
   * @param field    Property name
   * @param options  See `code.MulAdd` documentation.
   */
  public mulAdd(field: string, options: code.IMulAddOptions): code.MulAdd {
    return new code.MulAdd(field, options);
  }

  /**
   * Intrinsic operation. Puts `value` integer into the state's property with
   * the name specified by `field`.
   *
   *   state[field] = value;
   *   return 0;
   *
   * @param field Property name
   * @param value Integer value to be stored into the property.
   */
  public update(field: string, value: number): code.Update {
    return new code.Update(field, value);
  }

  /**
   * Intrinsic operation. Returns 1 if the integer `value` is equal to the
   * state's property with the name specified by `field`.
   *
   *   return state[field] === value ? 1 : 0;
   *
   * @param field Property name
   * @param value Integer value to be checked against.
   */
  public isEqual(field: string, value: number): code.IsEqual {
    return new code.IsEqual(field, value);
  }

  /**
   * Intrinsic operation.
   *
   *   state[field] &= value
   *   return 0;
   *
   * @param field Property name
   * @param value Integer value
   */
  public and(field: string, value: number): code.And {
    return new code.And(field, value);
  }

  /**
   * Intrinsic operation.
   *
   *   state[field] |= value
   *   return 0;
   *
   * @param field Property name
   * @param value Integer value
   */
  public or(field: string, value: number): code.Or {
    return new code.Or(field, value);
  }

  /**
   * Intrinsic operation.
   *
   *   return (state[field] & value) == value ? 1 : 0;
   *
   * @param field Property name
   * @param value Integer value
   */
  public test(field: string, value: number): code.Test {
    return new code.Test(field, value);
  }
}