File: parser.h

package info (click to toggle)
ruby-oj 3.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ansic: 19,402; ruby: 11,451; makefile: 17
file content (101 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download
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
// Copyright (c) 2021 Peter Ohler, All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#ifndef OJ_PARSER_H
#define OJ_PARSER_H

#include <ruby.h>
#include <stdbool.h>

#include "buf.h"

#define TOP_FUN 0
#define ARRAY_FUN 1
#define OBJECT_FUN 2

typedef uint8_t byte;

typedef enum {
    OJ_NONE    = '\0',
    OJ_NULL    = 'n',
    OJ_TRUE    = 't',
    OJ_FALSE   = 'f',
    OJ_INT     = 'i',
    OJ_DECIMAL = 'd',
    OJ_BIG     = 'b',  // indicates parser buf is used
    OJ_STRING  = 's',
    OJ_OBJECT  = 'o',
    OJ_ARRAY   = 'a',
} ojType;

typedef struct _num {
    long double dub;
    int64_t     fixnum;  // holds all digits
    uint32_t    len;
    int16_t     div;     // 10^div
    int16_t     exp;
    uint8_t     shift;   // shift of fixnum to get decimal
    bool        neg;
    bool        exp_neg;
    // for numbers as strings, reuse buf
} *Num;

struct _ojParser;

typedef struct _funcs {
    void (*add_null)(struct _ojParser *p);
    void (*add_true)(struct _ojParser *p);
    void (*add_false)(struct _ojParser *p);
    void (*add_int)(struct _ojParser *p);
    void (*add_float)(struct _ojParser *p);
    void (*add_big)(struct _ojParser *p);
    void (*add_str)(struct _ojParser *p);
    void (*open_array)(struct _ojParser *p);
    void (*close_array)(struct _ojParser *p);
    void (*open_object)(struct _ojParser *p);
    void (*close_object)(struct _ojParser *p);
} *Funcs;

typedef struct _ojParser {
    const char   *map;
    const char   *next_map;
    int           depth;
    unsigned char stack[1024];

    // value data
    struct _num num;
    struct _buf key;
    struct _buf buf;

    struct _funcs funcs[3];  // indexed by XXX_FUN defines

    void (*start)(struct _ojParser *p);
    VALUE (*option)(struct _ojParser *p, const char *key, VALUE value);
    VALUE (*result)(struct _ojParser *p);
    void (*free)(struct _ojParser *p);
    void (*mark)(struct _ojParser *p);

    void *ctx;
    VALUE reader;

    char     token[8];
    long     line;
    long     cur;  // only set before call to a function
    long     col;
    int      ri;
    uint32_t ucode;
    ojType   type;  // valType
    bool     just_one;
} *ojParser;

// Create a new parser without setting the delegate. The parser is
// wrapped. The parser is (ojParser)DATA_PTR(value) where value is the return
// from this function. A delegate must be added before the parser can be
// used. Optionally oj_parser_set_options can be called if the options are not
// set directly.
extern VALUE oj_parser_new();

// Set set the options from a hash (ropts).
extern void oj_parser_set_option(ojParser p, VALUE ropts);

#endif /* OJ_PARSER_H */