3
3
4
4
#include " slang-json-diagnostics.h"
5
5
6
+ #include " ../core/slang-string-escape-util.h"
7
+
6
8
/*
7
9
https://www.json.org/json-en.html
8
10
*/
@@ -11,21 +13,26 @@ namespace Slang {
11
13
12
14
SlangResult JSONParser::_parseObject ()
13
15
{
14
- SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::LBrace));
15
-
16
- m_listener->startObject ();
16
+ {
17
+ const SourceLoc loc = m_lexer->peekLoc ();
18
+ SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::LBrace));
19
+ m_listener->startObject (loc);
20
+ }
17
21
18
- if (m_lexer->advanceIf (JSONTokenType::RBrace))
19
22
{
20
- m_listener->endObject ();
21
- return SLANG_OK;
23
+ const SourceLoc loc = m_lexer->peekLoc ();
24
+ if (m_lexer->advanceIf (JSONTokenType::RBrace))
25
+ {
26
+ m_listener->endObject (loc);
27
+ return SLANG_OK;
28
+ }
22
29
}
23
30
24
31
while (true )
25
32
{
26
33
JSONToken keyToken;
27
34
SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::StringLiteral, keyToken));
28
- m_listener->addLexemeKey (m_lexer->getLexeme (keyToken));
35
+ m_listener->addKey (m_lexer->getLexeme (keyToken), keyToken. loc );
29
36
30
37
SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::Colon));
31
38
@@ -38,21 +45,29 @@ SlangResult JSONParser::_parseObject()
38
45
break ;
39
46
}
40
47
41
- SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::RBrace));
42
- m_listener->endObject ();
48
+ {
49
+ const SourceLoc loc = m_lexer->peekLoc ();
50
+ SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::RBrace));
51
+ m_listener->endObject (loc);
52
+ }
43
53
return SLANG_OK;
44
54
}
45
55
46
56
SlangResult JSONParser::_parseArray ()
47
57
{
48
- SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::LBracket));
49
-
50
- m_listener->startArray ();
58
+ {
59
+ const SourceLoc loc = m_lexer->peekLoc ();
60
+ SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::LBracket));
61
+ m_listener->startArray (loc);
62
+ }
51
63
52
- if (m_lexer->advanceIf (JSONTokenType::RBracket))
53
64
{
54
- m_listener->endArray ();
55
- return SLANG_OK;
65
+ const SourceLoc loc = m_lexer->peekLoc ();
66
+ if (m_lexer->advanceIf (JSONTokenType::RBracket))
67
+ {
68
+ m_listener->endArray (loc);
69
+ return SLANG_OK;
70
+ }
56
71
}
57
72
58
73
while (true )
@@ -65,8 +80,11 @@ SlangResult JSONParser::_parseArray()
65
80
break ;
66
81
}
67
82
68
- SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::RBracket));
69
- m_listener->endArray ();
83
+ {
84
+ const SourceLoc loc = m_lexer->peekLoc ();
85
+ SLANG_RETURN_ON_FAIL (m_lexer->expect (JSONTokenType::RBracket));
86
+ m_listener->endArray (loc);
87
+ }
70
88
return SLANG_OK;
71
89
}
72
90
@@ -81,7 +99,8 @@ SlangResult JSONParser::_parseValue()
81
99
case JSONTokenType::FloatLiteral:
82
100
case JSONTokenType::StringLiteral:
83
101
{
84
- m_listener->addLexemeValue (m_lexer->peekType (), m_lexer->peekLexeme ());
102
+ const JSONToken& tok = m_lexer->peekToken ();
103
+ m_listener->addLexemeValue (tok.type , m_lexer->peekLexeme (), tok.loc );
85
104
m_lexer->advance ();
86
105
return SLANG_OK;
87
106
}
@@ -256,8 +275,9 @@ void JSONWriter::_maybeEmitFieldComma()
256
275
}
257
276
}
258
277
259
- void JSONWriter::startObject ()
278
+ void JSONWriter::startObject (SourceLoc loc )
260
279
{
280
+ SLANG_UNUSED (loc);
261
281
SLANG_ASSERT (m_state.canEmitValue ());
262
282
263
283
_maybeEmitComma ();
@@ -276,8 +296,9 @@ void JSONWriter::startObject()
276
296
m_state.m_flags = 0 ;
277
297
}
278
298
279
- void JSONWriter::endObject ()
299
+ void JSONWriter::endObject (SourceLoc loc )
280
300
{
301
+ SLANG_UNUSED (loc);
281
302
SLANG_ASSERT (m_state.m_kind == State::Kind::Object);
282
303
283
304
_handleFormat (Location::BeforeCloseObject);
@@ -289,8 +310,9 @@ void JSONWriter::endObject()
289
310
m_stack.removeLast ();
290
311
}
291
312
292
- void JSONWriter::startArray ()
313
+ void JSONWriter::startArray (SourceLoc loc )
293
314
{
315
+ SLANG_UNUSED (loc);
294
316
SLANG_ASSERT (m_state.canEmitValue ());
295
317
296
318
_maybeEmitComma ();
@@ -309,8 +331,9 @@ void JSONWriter::startArray()
309
331
m_state.m_flags = 0 ;
310
332
}
311
333
312
- void JSONWriter::endArray ()
334
+ void JSONWriter::endArray (SourceLoc loc )
313
335
{
336
+ SLANG_UNUSED (loc);
314
337
SLANG_ASSERT (m_state.m_kind == State::Kind::Array);
315
338
316
339
_handleFormat (Location::BeforeCloseArray);
@@ -322,8 +345,9 @@ void JSONWriter::endArray()
322
345
m_stack.removeLast ();
323
346
}
324
347
325
- void JSONWriter::addLexemeKey (const UnownedStringSlice& key)
348
+ void JSONWriter::addKey (const UnownedStringSlice& key, SourceLoc loc )
326
349
{
350
+ SLANG_UNUSED (loc);
327
351
SLANG_ASSERT (m_state.m_kind == State::Kind::Object && (m_state.m_flags & State::Flag::HasKey) == 0 );
328
352
329
353
_maybeEmitFieldComma ();
@@ -339,12 +363,27 @@ void JSONWriter::addLexemeKey(const UnownedStringSlice& key)
339
363
m_state.m_flags &= ~State::Flag::HasPrevious;
340
364
}
341
365
342
- void JSONWriter::addLexemeValue (JSONTokenType type, const UnownedStringSlice& value )
366
+ void JSONWriter::_preValue (SourceLoc loc )
343
367
{
368
+ SLANG_UNUSED (loc);
344
369
SLANG_ASSERT (m_state.canEmitValue ());
345
370
346
371
_maybeEmitComma ();
347
372
_maybeEmitIndent ();
373
+ }
374
+
375
+ void JSONWriter::_postValue ()
376
+ {
377
+ // We have a previous
378
+ m_state.m_flags |= State::Flag::HasPrevious;
379
+ // We don't have a key
380
+ m_state.m_flags &= ~State::Flag::HasKey;
381
+ }
382
+
383
+
384
+ void JSONWriter::addLexemeValue (JSONTokenType type, const UnownedStringSlice& value, SourceLoc loc)
385
+ {
386
+ _preValue (loc);
348
387
349
388
switch (type)
350
389
{
@@ -375,10 +414,45 @@ void JSONWriter::addLexemeValue(JSONTokenType type, const UnownedStringSlice& va
375
414
SLANG_ASSERT (!" Can only emit values" );
376
415
}
377
416
}
378
- // We have a previous
379
- m_state.m_flags |= State::Flag::HasPrevious;
380
- // We don't have a key
381
- m_state.m_flags &= ~State::Flag::HasKey;
417
+
418
+ _postValue ();
419
+ }
420
+
421
+ void JSONWriter::addIntegerValue (int64_t value, SourceLoc loc)
422
+ {
423
+ _preValue (loc);
424
+ m_builder << value;
425
+ _postValue ();
426
+ }
427
+
428
+ void JSONWriter::addFloatValue (double value, SourceLoc loc)
429
+ {
430
+ _preValue (loc);
431
+ m_builder << value;
432
+ _postValue ();
433
+ }
434
+
435
+ void JSONWriter::addBoolValue (bool inValue, SourceLoc loc)
436
+ {
437
+ _preValue (loc);
438
+ const UnownedStringSlice slice = inValue ? UnownedStringSlice::fromLiteral (" true" ) : UnownedStringSlice::fromLiteral (" false" );
439
+ m_builder << slice;
440
+ _postValue ();
441
+ }
442
+
443
+ void JSONWriter::addNullValue (SourceLoc loc)
444
+ {
445
+ _preValue (loc);
446
+ m_builder << UnownedStringSlice::fromLiteral (" null" );
447
+ _postValue ();
448
+ }
449
+
450
+ void JSONWriter::addStringValue (const UnownedStringSlice& slice, SourceLoc loc)
451
+ {
452
+ _preValue (loc);
453
+ StringEscapeHandler* handler = StringEscapeUtil::getHandler (StringEscapeUtil::Style ::JSON);
454
+ StringEscapeUtil::appendQuoted (handler, slice, m_builder);
455
+ _postValue ();
382
456
}
383
457
384
458
} // namespace Slang
0 commit comments