@@ -88,9 +88,8 @@ function _next(context: JsonParserContext) {
8888
8989
9090/**
91- * Read a token
92- * @param context
93- * @param valid
91+ * Read a single character from the input. If a `valid` string is passed, validate that the
92+ * character is included in the valid string.
9493 * @private
9594 */
9695function _token ( context : JsonParserContext , valid : string ) : string ;
@@ -112,6 +111,12 @@ function _token(context: JsonParserContext, valid?: string): string | undefined
112111}
113112
114113
114+ /**
115+ * Read the exponent part of a number. The exponent part is looser for JSON than the number
116+ * part. `str` is the string of the number itself found so far, and start the position
117+ * where the full number started. Returns the node found.
118+ * @private
119+ */
115120function _readExpNumber ( context : JsonParserContext ,
116121 start : Position ,
117122 str : string ,
@@ -149,6 +154,10 @@ function _readExpNumber(context: JsonParserContext,
149154}
150155
151156
157+ /**
158+ * Read a number from the context.
159+ * @private
160+ */
152161function _readNumber ( context : JsonParserContext , comments = _readBlanks ( context ) ) : JsonAstNumber {
153162 let str = '' ;
154163 let dotted = false ;
@@ -197,6 +206,11 @@ function _readNumber(context: JsonParserContext, comments = _readBlanks(context)
197206}
198207
199208
209+ /**
210+ * Read a string from the context. Takes the comments of the string or read the blanks before the
211+ * string.
212+ * @private
213+ */
200214function _readString ( context : JsonParserContext , comments = _readBlanks ( context ) ) : JsonAstString {
201215 const start = context . position ;
202216
@@ -261,6 +275,10 @@ function _readString(context: JsonParserContext, comments = _readBlanks(context)
261275}
262276
263277
278+ /**
279+ * Read the constant `true` from the context.
280+ * @private
281+ */
264282function _readTrue ( context : JsonParserContext ,
265283 comments = _readBlanks ( context ) ) : JsonAstConstantTrue {
266284 const start = context . position ;
@@ -281,6 +299,10 @@ function _readTrue(context: JsonParserContext,
281299}
282300
283301
302+ /**
303+ * Read the constant `false` from the context.
304+ * @private
305+ */
284306function _readFalse ( context : JsonParserContext ,
285307 comments = _readBlanks ( context ) ) : JsonAstConstantFalse {
286308 const start = context . position ;
@@ -302,6 +324,10 @@ function _readFalse(context: JsonParserContext,
302324}
303325
304326
327+ /**
328+ * Read the constant `null` from the context.
329+ * @private
330+ */
305331function _readNull ( context : JsonParserContext ,
306332 comments = _readBlanks ( context ) ) : JsonAstConstantNull {
307333 const start = context . position ;
@@ -323,6 +349,10 @@ function _readNull(context: JsonParserContext,
323349}
324350
325351
352+ /**
353+ * Read an array of JSON values from the context.
354+ * @private
355+ */
326356function _readArray ( context : JsonParserContext , comments = _readBlanks ( context ) ) : JsonAstArray {
327357 const start = context . position ;
328358
@@ -359,6 +389,11 @@ function _readArray(context: JsonParserContext, comments = _readBlanks(context))
359389}
360390
361391
392+ /**
393+ * Read an identifier from the context. An identifier is a valid JavaScript identifier, and this
394+ * function is only used in Loose mode.
395+ * @private
396+ */
362397function _readIdentifier ( context : JsonParserContext ,
363398 comments = _readBlanks ( context ) ) : JsonAstIdentifier {
364399 const start = context . position ;
@@ -401,6 +436,11 @@ function _readIdentifier(context: JsonParserContext,
401436}
402437
403438
439+ /**
440+ * Read a property from the context. A property is a string or (in Loose mode only) a number or
441+ * an identifier, followed by a colon `:`.
442+ * @private
443+ */
404444function _readProperty ( context : JsonParserContext ,
405445 comments = _readBlanks ( context ) ) : JsonAstKeyValue {
406446 const start = context . position ;
@@ -434,6 +474,10 @@ function _readProperty(context: JsonParserContext,
434474}
435475
436476
477+ /**
478+ * Read an object of properties -> JSON values from the context.
479+ * @private
480+ */
437481function _readObject ( context : JsonParserContext ,
438482 comments = _readBlanks ( context ) ) : JsonAstObject {
439483 const start = context . position ;
@@ -470,6 +514,11 @@ function _readObject(context: JsonParserContext,
470514}
471515
472516
517+ /**
518+ * Remove any blank character or comments (in Loose mode) from the context, returning an array
519+ * of comments if any are found.
520+ * @private
521+ */
473522function _readBlanks ( context : JsonParserContext ) : ( JsonAstComment | JsonAstMultilineComment ) [ ] {
474523 if ( ( context . mode & JsonParseMode . CommentsAllowed ) != 0 ) {
475524 const comments : ( JsonAstComment | JsonAstMultilineComment ) [ ] = [ ] ;
@@ -542,6 +591,10 @@ function _readBlanks(context: JsonParserContext): (JsonAstComment | JsonAstMulti
542591}
543592
544593
594+ /**
595+ * Read a JSON value from the context, which can be any form of JSON value.
596+ * @private
597+ */
545598function _readValue ( context : JsonParserContext ) : JsonAstNode {
546599 let result : JsonAstNode ;
547600
@@ -599,17 +652,28 @@ function _readValue(context: JsonParserContext): JsonAstNode {
599652}
600653
601654
655+ /**
656+ * The Parse mode used for parsing the JSON string.
657+ */
602658export enum JsonParseMode {
603- Strict = 0 ,
604- CommentsAllowed = 1 << 0 ,
605- SingleQuotesAllowed = 1 << 1 ,
606- IdentifierKeyNamesAllowed = 1 << 2 ,
659+ Strict = 0 , // Standard JSON.
660+ CommentsAllowed = 1 << 0 , // Allows comments, both single or multi lines.
661+ SingleQuotesAllowed = 1 << 1 , // Allow single quoted strings.
662+ IdentifierKeyNamesAllowed = 1 << 2 , // Allow identifiers as objectp properties.
607663
608664 Default = Strict ,
609- Loose = CommentsAllowed | SingleQuotesAllowed | IdentifierKeyNamesAllowed ,
665+ Loose = CommentsAllowed | SingleQuotesAllowed | IdentifierKeyNamesAllowed ,
610666}
611667
612668
669+ /**
670+ * Parse the JSON string and return its AST. The AST may be losing data (end comments are
671+ * discarded for example, and space characters are not represented in the AST), but all values
672+ * will have a single node in the AST (a 1-to-1 mapping).
673+ * @param input The string to use.
674+ * @param mode The mode to parse the input with. {@see JsonParseMode}.
675+ * @returns {JsonAstNode } The root node of the value of the AST.
676+ */
613677export function parseJsonAst ( input : string , mode = JsonParseMode . Default ) : JsonAstNode {
614678 if ( mode == JsonParseMode . Default ) {
615679 mode = JsonParseMode . Strict ;
@@ -633,6 +697,13 @@ export function parseJsonAst(input: string, mode = JsonParseMode.Default): JsonA
633697 return ast ;
634698}
635699
700+
701+ /**
702+ * Parse a JSON string into its value. This discards the AST and only returns the value itself.
703+ * @param input The string to parse.
704+ * @param mode The mode to parse the input with. {@see JsonParseMode}.
705+ * @returns {JsonValue } The value represented by the JSON string.
706+ */
636707export function parseJson ( input : string , mode = JsonParseMode . Default ) : JsonValue {
637708 // Try parsing for the fastest path available, if error, uses our own parser for better errors.
638709 if ( mode == JsonParseMode . Strict ) {
0 commit comments