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
  
/* vim:syntax=ragel 
 */ 
 
%%{ 
    machine JSON5_number; 
    alphtype int; 
    include JSON5D "json5_defaults.rl"; 
    # we could be much less specific here.. but i guess its ok to ensure the  
    # format, not correctness in the sense of sscanf  
    #  
    action break { 
      fpc--; 
      fbreak; 
    } 
    getkey ((int)INDEX_PCHARP(str, fpc)); 
 
    end = [\]},:/]|myspace; 
    exp = [eE] >{d = 1;}. [+\-]? . digit+ . (end >break)?; 
    hex = ('0' . [xX]  >{h = 1;}) . xdigit+ . (end > break)?; 
    float = '.' >{d = 1;} . digit* . (end >break | exp)?; 
    symbolic_numbers = number_identifiers >{s = 1;} ; 
    main := ('+' | '-') ? . ((symbolic_numbers . (end > break)?) | ((hex) . (end > break)?) | (((digit*) . (end >break | float | exp)?) | float));  
 
}%% 
 
static ptrdiff_t _parse_JSON5_number(PCHARP str, ptrdiff_t p, ptrdiff_t pe, struct parser_state *state) { 
    ptrdiff_t i = p; 
    ptrdiff_t eof = pe; 
    int cs; 
    int d = 0, h = 0, s = 0; 
 
    %% write data; 
 
    %% write init; 
    %% write exec; 
    if (cs >= JSON5_number_first_final) { 
        if (!(state->flags&JSON5_VALIDATE)) { 
            if (s == 1) { 
               s = (int)INDEX_PCHARP(str, i); 
               if(s == '+') s = 0, i++;  
               else if(s == '-') s = -1, i++; 
               // -symbol 
               if(s == -1) { 
                  s = (int)INDEX_PCHARP(str, i); 
                  if(s == 'I') { 
                    push_float(-MAKE_INF()); 
                  } else if(s == 'N') { 
                    push_float(MAKE_NAN()); /* note sign on -NaN has no meaning */ 
                  } else { /* should never get here */ 
                    state->flags |= JSON5_ERROR;  
                    return p; 
                  } 
               } else { 
                 /* if we had a + sign, look at the next digit, otherwise use the value. */ 
                 if(s == 0)  
                   s = (int)INDEX_PCHARP(str, i); 
                    
                 if(s == 'I') { 
                    push_float(MAKE_INF()); 
                 } else if(s == 'N') { 
                   push_float(MAKE_NAN()); /* note sign on -NaN has no meaning */ 
                 } else { /* should never get here */ 
                   state->flags |= JSON5_ERROR;  
                   return p; 
                 } 
              } 
            } else if (h == 1) { 
              // TODO handle errors better, handle possible bignums and possible better approach. 
              push_string(make_shared_binary_pcharp(ADD_PCHARP(str, i), p-i)); 
              push_text("%x"); 
              f_sscanf(2); 
              if((PIKE_TYPEOF(Pike_sp[-1]) != PIKE_T_ARRAY) || (Pike_sp[-1].u.array->size != 1)) { 
                state->flags |= JSON5_ERROR; 
                return p; 
              } 
              else { 
                 push_int(ITEM(Pike_sp[-1].u.array)[0].u.integer); 
                 stack_swap(); 
                 pop_stack(); 
              } 
            } else if (d == 1) { 
                push_float((FLOAT_TYPE)STRTOD_PCHARP(ADD_PCHARP(str, i), NULL)); 
            } else { 
                pcharp_to_svalue_inumber(Pike_sp++, ADD_PCHARP(str, i), NULL, 10, p - i); 
            } 
        } 
 
        return p; 
    } 
    state->flags |= JSON5_ERROR; 
    return p; 
}