(text: string, visitor: JSONVisitor, options: ParseOptions = ParseOptions.DEFAULT)
| 1067 | * Parses the given text and invokes the visitor functions for each object, array and literal reached. |
| 1068 | */ |
| 1069 | export function visit(text: string, visitor: JSONVisitor, options: ParseOptions = ParseOptions.DEFAULT): any { |
| 1070 | |
| 1071 | const _scanner = createScanner(text, false); |
| 1072 | |
| 1073 | function toNoArgVisit(visitFunction?: (offset: number, length: number) => void): () => void { |
| 1074 | return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength()) : () => true; |
| 1075 | } |
| 1076 | function toOneArgVisit<T>(visitFunction?: (arg: T, offset: number, length: number) => void): (arg: T) => void { |
| 1077 | return visitFunction ? (arg: T) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength()) : () => true; |
| 1078 | } |
| 1079 | |
| 1080 | const onObjectBegin = toNoArgVisit(visitor.onObjectBegin), |
| 1081 | onObjectProperty = toOneArgVisit(visitor.onObjectProperty), |
| 1082 | onObjectEnd = toNoArgVisit(visitor.onObjectEnd), |
| 1083 | onArrayBegin = toNoArgVisit(visitor.onArrayBegin), |
| 1084 | onArrayEnd = toNoArgVisit(visitor.onArrayEnd), |
| 1085 | onLiteralValue = toOneArgVisit(visitor.onLiteralValue), |
| 1086 | onSeparator = toOneArgVisit(visitor.onSeparator), |
| 1087 | onComment = toNoArgVisit(visitor.onComment), |
| 1088 | onError = toOneArgVisit(visitor.onError); |
| 1089 | |
| 1090 | const disallowComments = options && options.disallowComments; |
| 1091 | const allowTrailingComma = options && options.allowTrailingComma; |
| 1092 | function scanNext(): SyntaxKind { |
| 1093 | while (true) { |
| 1094 | const token = _scanner.scan(); |
| 1095 | switch (_scanner.getTokenError()) { |
| 1096 | case ScanError.InvalidUnicode: |
| 1097 | handleError(ParseErrorCode.InvalidUnicode); |
| 1098 | break; |
| 1099 | case ScanError.InvalidEscapeCharacter: |
| 1100 | handleError(ParseErrorCode.InvalidEscapeCharacter); |
| 1101 | break; |
| 1102 | case ScanError.UnexpectedEndOfNumber: |
| 1103 | handleError(ParseErrorCode.UnexpectedEndOfNumber); |
| 1104 | break; |
| 1105 | case ScanError.UnexpectedEndOfComment: |
| 1106 | if (!disallowComments) { |
| 1107 | handleError(ParseErrorCode.UnexpectedEndOfComment); |
| 1108 | } |
| 1109 | break; |
| 1110 | case ScanError.UnexpectedEndOfString: |
| 1111 | handleError(ParseErrorCode.UnexpectedEndOfString); |
| 1112 | break; |
| 1113 | case ScanError.InvalidCharacter: |
| 1114 | handleError(ParseErrorCode.InvalidCharacter); |
| 1115 | break; |
| 1116 | } |
| 1117 | switch (token) { |
| 1118 | case SyntaxKind.LineCommentTrivia: |
| 1119 | case SyntaxKind.BlockCommentTrivia: |
| 1120 | if (disallowComments) { |
| 1121 | handleError(ParseErrorCode.InvalidCommentToken); |
| 1122 | } else { |
| 1123 | onComment(); |
| 1124 | } |
| 1125 | break; |
| 1126 | case SyntaxKind.Unknown: |
no test coverage detected
searching dependent graphs…