Skip to content

Commit

Permalink
feat(lex): add snapshot recording and appling features
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Mar 21, 2024
1 parent eb1ca6e commit 470851e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/Melon Developer Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,12 @@ Their definitions can be found in melon/include/mln_types.h.
MLN_LEX_EMANYMACRODEF
MLN_LEX_EINVMACRO

p) mln_lex_off_t mln_lex_snapshot_record(mln_lex_t *lex);
Snapshotted the current input stream position.

q) void mln_lex_snapshot_apply(mln_lex_t *lex, mln_lex_off_t off);
Apply the snapshot to the current input stream, restoring the read offset to the snapshot position.

10) Configuration
As shown below, this is the architecture of configuration.
------- ------- ------- -------
Expand Down
24 changes: 24 additions & 0 deletions docs/book/cn/lex.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,30 @@ mln_lex_init_with_hooks(PREFIX_NAME,lex_ptr,attr_ptr)
#### mln_lex_snapshot_record
```c
mln_lex_off_t mln_lex_snapshot_record(mln_lex_t *lex);
```

描述:记录当前输入流的位置信息。

返回值:文件内偏移或内存地址



#### mln_lex_snapshot_apply

```c
void mln_lex_snapshot_apply(mln_lex_t *lex, mln_lex_off_t off);
```
描述:恢复当前输入流的读取偏移到快照的位置。注意,这个函数应用时应确保当前输入流是`mln_lex_snapshot_record`调用时的输入流,函数中会进行一定的检查,但并不能确保万无一失。
返回值:无
### 示例
```c
Expand Down
24 changes: 24 additions & 0 deletions docs/book/en/lex.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,30 @@ Return value: There is no return value, but it should be checked whether `lex_pt
#### mln_lex_snapshot_record
```c
mln_lex_off_t mln_lex_snapshot_record(mln_lex_t *lex);
```

Description: Records the position information of the current input stream.

Return value: File offset or memory address



#### mln_lex_snapshot_apply

```c
void mln_lex_snapshot_apply(mln_lex_t *lex, mln_lex_off_t off);
```
Description: Restores the read offset of the current input stream to the position of the snapshot. Note that this function should ensure that the current input stream is the one when `mln_lex_snapshot_record` was called. The function performs some checks, but does not guarantee complete safety.
Return value: None.
### Example
```c
Expand Down
7 changes: 7 additions & 0 deletions include/mln_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ struct mln_lex_attr {
mln_string_t *data;
};

typedef union {
mln_u8ptr_t soff;
off_t foff;
} mln_lex_off_t;

typedef struct {
mln_u32_t type;
int fd;
Expand Down Expand Up @@ -207,6 +212,8 @@ extern int mln_lex_condition_test(mln_lex_t *lex) __NONNULL1(1);
extern mln_lex_input_t *
mln_lex_input_new(mln_lex_t *lex, mln_u32_t type, mln_string_t *data, int *err, mln_u64_t line) __NONNULL3(1,3,4);
extern void mln_lex_input_free(void *in);
extern mln_lex_off_t mln_lex_snapshot_record(mln_lex_t *lex);
extern void mln_lex_snapshot_apply(mln_lex_t *lex, mln_lex_off_t off);
#define mln_lex_get_pool(lex) ((lex)->pool)
#define mln_lex_result_clean(lex) ((lex)->result_pos = (lex)->result_buf)
#define mln_lex_get_cur_filename(lex) \
Expand Down
36 changes: 36 additions & 0 deletions src/mln_lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,39 @@ MLN_FUNC(, int, mln_lex_condition_test, (mln_lex_t *lex), (lex), {
return reverse? 0: 1;
})

MLN_FUNC(, mln_lex_off_t, mln_lex_snapshot_record, (mln_lex_t *lex), (lex), {
mln_lex_off_t ret;
mln_lex_input_t *input = lex->cur;

if (input == NULL && (input = mln_stack_top(lex->stack)) == NULL) {
ret.soff = NULL;
return ret;
}

if (input->type == M_INPUT_T_BUF) {
ret.soff = input->pos;
} else {
ret.foff = lseek(input->fd, 0, SEEK_CUR) - (input->buf_len - (input->pos - input->buf));
}

return ret;
})

MLN_FUNC_VOID(, void, mln_lex_snapshot_apply, (mln_lex_t *lex, mln_lex_off_t off), (lex, off), {
mln_lex_input_t *input = lex->cur;

if (input == NULL && (input = mln_stack_top(lex->stack)) == NULL) {
return;
}
if (input->type == M_INPUT_T_BUF) {
if (off.soff < input->buf || off.soff >= input->buf + input->buf_len) return;

input->pos = off.soff;
} else {
off_t foff = lseek(input->fd, 0, SEEK_CUR);
if (off.foff >= foff || off.foff < foff - input->buf_len) return;
lseek(input->fd, off.foff, SEEK_SET);
input->pos = input->buf + input->buf_len;
}
})

0 comments on commit 470851e

Please sign in to comment.