1
- #pragma once
2
1
#include "parser.h"
3
2
#include <stdio.h>
4
3
#include <stdlib.h>
@@ -18,6 +17,21 @@ void ignoreWhiteCharactor() {
18
17
}
19
18
}
20
19
20
+ int hexCharToInt (char c ) {
21
+ if ('0' <= c && c <= '9' ) {
22
+ return c - '0' ;
23
+ }
24
+ else if ('a' <= c && c <= 'f' ) {
25
+ return 10 + c - 'a' ;
26
+ }
27
+ else if ('A' <= c && c <= 'F' ) {
28
+ return 10 + c - 'A' ;
29
+ }
30
+ else {
31
+ fprintf (stderr , "Invalid hexadecimal character: %c\n" , c );
32
+ exit (1 );
33
+ }
34
+ }
21
35
22
36
struct JsonVal * parseValue () {
23
37
ignoreWhiteCharactor ();
@@ -47,31 +61,95 @@ struct JsonVal* parseValue() {
47
61
}
48
62
49
63
struct JsonVal * parseString () {
50
- char c ; struct JsonString * str = JsonString_New ();
64
+ char c ;
65
+ struct JsonString * str = JsonString_New ();
51
66
size_t pos = ftell (f ) / sizeof (char );
67
+
52
68
while ((c = fgetc (f )) && c != EOF && c != '"' && c != '\'' ) {
53
- JsonStringPushBackChar (c , str );
69
+ if (c == '\\' ) { // Check for escape character
70
+ c = fgetc (f ); // Read the next character after '\'
71
+ if (c == EOF ) {
72
+ fprintf (stderr , "Unexpected EOF after escape character.\tString value parse begin with %llu\n" , pos );
73
+ exit (1 );
74
+ }
75
+ // Process escaped character
76
+ switch (c ) {
77
+ case '"' :
78
+ case '\'' :
79
+ case '\\' :
80
+ // These characters are escaped as themselves
81
+ JsonStringPushBackChar (c , str );
82
+ break ;
83
+ case 'n' :
84
+ JsonStringPushBackChar ('\n' , str );
85
+ break ;
86
+ case 't' :
87
+ JsonStringPushBackChar ('\t' , str );
88
+ break ;
89
+ case 'u' : {
90
+ // Unicode escape sequence: \uXXXX
91
+ int unicodeValue = 0 ;
92
+ for (int i = 0 ; i < 4 ; ++ i ) {
93
+ c = fgetc (f );
94
+ if (c == EOF ) {
95
+ fprintf (stderr , "Unexpected EOF in Unicode escape sequence.\tString value parse begin with %llu\n" , pos );
96
+ exit (1 );
97
+ }
98
+ unicodeValue = unicodeValue * 16 + hexCharToInt (c );
99
+ }
100
+ // Assuming JsonStringPushBackUnicode is a function that appends a Unicode character to the string
101
+ JsonStringPushBackChar (unicodeValue , str );
102
+ break ;
103
+ }
104
+ case '0' :
105
+ // Null character
106
+ JsonStringPushBackChar ('\0' , str );
107
+ break ;
108
+ case 'x' : {
109
+ // Hexadecimal escape sequence: \xXX
110
+ int hexValue = 0 ;
111
+ for (int i = 0 ; i < 2 ; ++ i ) {
112
+ c = fgetc (f );
113
+ if (c == EOF ) {
114
+ fprintf (stderr , "Unexpected EOF in hexadecimal escape sequence.\tString value parse begin with %llu\n" , pos );
115
+ exit (1 );
116
+ }
117
+ hexValue = hexValue * 16 + hexCharToInt (c );
118
+ }
119
+ JsonStringPushBackChar ((char )hexValue , str );
120
+ break ;
121
+ }
122
+ // Add more cases for other escape sequences as needed
123
+ default :
124
+ fprintf (stderr , "Invalid escape sequence: \\%c\tString value parse begin with %llu\n" , c , pos );
125
+ exit (1 );
126
+ }
127
+ }
128
+ else {
129
+ JsonStringPushBackChar (c , str );
130
+ }
54
131
}
55
-
132
+
56
133
if (c != '"' && c != '\'' ) {
57
- fprintf (
58
- stderr ,
59
- "Excepted charactor \" or ', but got EOF.\tString value parse begin with %llu\n" ,
60
- pos
61
- ); // 写入报错到标准错误流
134
+ fprintf (stderr , "Expected character \" or ', but got EOF.\tString value parse begin with %llu\n" , pos );
62
135
exit (1 );
63
136
}
137
+
64
138
struct JsonVal * res = (struct JsonVal * )malloc (sizeof (struct JsonVal ));
65
139
if (res == NULL ) {
66
- // 内存分配失败 OOM (? )
67
- // 异常退出, OS进行内存回收
140
+ // Memory allocation failure (OOM )
141
+ // Handle the error and exit
68
142
exit (1 );
69
143
}
70
- res -> type = STRING ; res -> val = str ;
144
+
145
+ res -> type = STRING ;
146
+ res -> val = str ;
71
147
return res ;
72
148
}
73
149
74
150
151
+
152
+
75
153
struct JsonVal * parseNumber () {
76
154
char c ;
77
155
struct JsonString * str = JsonString_New ();
0 commit comments