Skip to content

Commit

Permalink
Removed unneeded parse from jsonParser
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney authored Feb 7, 2018
1 parent c2d86d0 commit b84fba0
Showing 1 changed file with 0 additions and 260 deletions.
260 changes: 0 additions & 260 deletions src/languageService/parser/jsonParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,263 +1073,3 @@ export class JSONDocument {
return validationResult.problems;
}
}

export function parse(text: string, config?: JSONDocumentConfig): JSONDocument {

let problems: IProblem[] = [];
let scanner = Json.createScanner(text, false);

let disallowComments = config && config.disallowComments;

function _scanNext(): Json.SyntaxKind {
while (true) {
let token = scanner.scan();
switch (token) {
case Json.SyntaxKind.LineCommentTrivia:
case Json.SyntaxKind.BlockCommentTrivia:
if (disallowComments) {
_error(localize('InvalidCommentTokem', 'Comments are not allowed.'), ErrorCode.CommentsNotAllowed);
}
break;
case Json.SyntaxKind.Trivia:
case Json.SyntaxKind.LineBreakTrivia:
break;
default:
return token;
}
}
}

function _accept(token: Json.SyntaxKind): boolean {
if (scanner.getToken() === token) {
_scanNext();
return true;
}
return false;
}

function _error<T extends ASTNode>(message: string, code: ErrorCode, node: T = null, skipUntilAfter: Json.SyntaxKind[] = [], skipUntil: Json.SyntaxKind[] = []): T {
if (problems.length === 0 || problems[0].location.start !== scanner.getTokenOffset()) {
// ignore multiple errors on the same offset
let start = scanner.getTokenOffset();
let end = scanner.getTokenOffset() + scanner.getTokenLength();
if (start === end && start > 0) {
start--;
while (start > 0 && /\s/.test(text.charAt(start))) {
start--;
}
end = start + 1;
}
problems.push({ message, location: { start, end }, code, severity: ProblemSeverity.Error });
}

if (node) {
_finalize(node, false);
}
if (skipUntilAfter.length + skipUntil.length > 0) {
let token = scanner.getToken();
while (token !== Json.SyntaxKind.EOF) {
if (skipUntilAfter.indexOf(token) !== -1) {
_scanNext();
break;
} else if (skipUntil.indexOf(token) !== -1) {
break;
}
token = _scanNext();
}
}
return node;
}

function _checkScanError(): boolean {
switch (scanner.getTokenError()) {
case Json.ScanError.InvalidUnicode:
_error(localize('InvalidUnicode', 'Invalid unicode sequence in string.'), ErrorCode.InvalidUnicode);
return true;
case Json.ScanError.InvalidEscapeCharacter:
_error(localize('InvalidEscapeCharacter', 'Invalid escape character in string.'), ErrorCode.InvalidEscapeCharacter);
return true;
case Json.ScanError.UnexpectedEndOfNumber:
_error(localize('UnexpectedEndOfNumber', 'Unexpected end of number.'), ErrorCode.UnexpectedEndOfNumber);
return true;
case Json.ScanError.UnexpectedEndOfComment:
_error(localize('UnexpectedEndOfComment', 'Unexpected end of comment.'), ErrorCode.UnexpectedEndOfComment);
return true;
case Json.ScanError.UnexpectedEndOfString:
_error(localize('UnexpectedEndOfString', 'Unexpected end of string.'), ErrorCode.UnexpectedEndOfString);
return true;
case Json.ScanError.InvalidCharacter:
_error(localize('InvalidCharacter', 'Invalid characters in string. Control characters must be escaped.'), ErrorCode.InvalidCharacter);
return true;
}
return false;
}

function _finalize<T extends ASTNode>(node: T, scanNext: boolean): T {
node.end = scanner.getTokenOffset() + scanner.getTokenLength();

if (scanNext) {
_scanNext();
}

return node;
}

function _parseArray(parent: ASTNode, name: Json.Segment): ArrayASTNode {
if (scanner.getToken() !== Json.SyntaxKind.OpenBracketToken) {
return null;
}
let node = new ArrayASTNode(parent, name, scanner.getTokenOffset());
_scanNext(); // consume OpenBracketToken

let count = 0;
if (node.addItem(_parseValue(node, count++))) {
while (_accept(Json.SyntaxKind.CommaToken)) {
if (!node.addItem(_parseValue(node, count++))) {
_error(localize('ValueExpected', 'Value expected'), ErrorCode.ValueExpected);
// report error, but continue
}
}
}

if (scanner.getToken() !== Json.SyntaxKind.CloseBracketToken) {
return _error(localize('ExpectedCloseBracket', 'Expected comma or closing bracket'), ErrorCode.CommaOrCloseBacketExpected, node);
}

return _finalize(node, true);
}

function _parseProperty(parent: ObjectASTNode, keysSeen: any): PropertyASTNode {

let key = _parseString(null, null, true);
if (!key) {
if (scanner.getToken() === Json.SyntaxKind.Unknown) {
// give a more helpful error message
let value = scanner.getTokenValue();
if (value.match(/^['\w]/)) {
_error(localize('DoubleQuotesExpected', 'Property keys must be doublequoted'), ErrorCode.Undefined);
}
}
return null;
}
let node = new PropertyASTNode(parent, key);

if (keysSeen[key.value]) {
problems.push({ location: { start: node.key.start, end: node.key.end }, message: localize('DuplicateKeyWarning', "Duplicate object key"), code: ErrorCode.Undefined, severity: ProblemSeverity.Warning });
}
keysSeen[key.value] = true;

if (scanner.getToken() === Json.SyntaxKind.ColonToken) {
node.colonOffset = scanner.getTokenOffset();
_scanNext(); // consume ColonToken
} else {
_error(localize('ColonExpected', 'Colon expected'), ErrorCode.ColonExpected);
}



if (!node.setValue(_parseValue(node, key.value))) {
return _error(localize('ValueExpected', 'Value expected'), ErrorCode.ValueExpected, node, [], [Json.SyntaxKind.CloseBraceToken, Json.SyntaxKind.CommaToken]);
}
node.end = node.value.end;
return node;
}

function _parseObject(parent: ASTNode, name: Json.Segment): ObjectASTNode {
if (scanner.getToken() !== Json.SyntaxKind.OpenBraceToken) {
return null;
}
let node = new ObjectASTNode(parent, name, scanner.getTokenOffset());
let keysSeen: any = Object.create(null);
_scanNext(); // consume OpenBraceToken
let needsComma = false;

while (scanner.getToken() !== Json.SyntaxKind.CloseBraceToken && scanner.getToken() !== Json.SyntaxKind.EOF) {
if (scanner.getToken() === Json.SyntaxKind.CommaToken) {
if (!needsComma) {
_error(localize('PropertyExpected', 'Property expected'), ErrorCode.PropertyExpected);
}
_scanNext(); // consume comma
} else if (needsComma) {
_error(localize('ExpectedComma', 'Expected comma'), ErrorCode.CommaExpected, node);
}
if (!node.addProperty(_parseProperty(node, keysSeen))) {
_error(localize('PropertyExpected', 'Property expected'), ErrorCode.PropertyExpected, null, [], [Json.SyntaxKind.CloseBraceToken, Json.SyntaxKind.CommaToken]);
}
needsComma = true;
}

if (scanner.getToken() !== Json.SyntaxKind.CloseBraceToken) {
return _error(localize('ExpectedCloseBrace', 'Expected comma or closing brace'), ErrorCode.CommaOrCloseBraceExpected, node);
}
return _finalize(node, true);
}

function _parseString(parent: ASTNode, name: Json.Segment, isKey: boolean): StringASTNode {
if (scanner.getToken() !== Json.SyntaxKind.StringLiteral) {
return null;
}

let node = new StringASTNode(parent, name, isKey, scanner.getTokenOffset());
node.value = scanner.getTokenValue();

_checkScanError();

return _finalize(node, true);
}

function _parseNumber(parent: ASTNode, name: Json.Segment): NumberASTNode {
if (scanner.getToken() !== Json.SyntaxKind.NumericLiteral) {
return null;
}

let node = new NumberASTNode(parent, name, scanner.getTokenOffset());
if (!_checkScanError()) {
let tokenValue = scanner.getTokenValue();
try {
let numberValue = JSON.parse(tokenValue);
if (typeof numberValue !== 'number') {
return _error(localize('InvalidNumberFormat', 'Invalid number format.'), ErrorCode.Undefined, node);
}
node.value = numberValue;
} catch (e) {
return _error(localize('InvalidNumberFormat', 'Invalid number format.'), ErrorCode.Undefined, node);
}
node.isInteger = tokenValue.indexOf('.') === -1;
}
return _finalize(node, true);
}

function _parseLiteral(parent: ASTNode, name: Json.Segment): ASTNode {
let node: ASTNode;
switch (scanner.getToken()) {
case Json.SyntaxKind.NullKeyword:
node = new NullASTNode(parent, name, scanner.getTokenOffset());
break;
case Json.SyntaxKind.TrueKeyword:
node = new BooleanASTNode(parent, name, true, scanner.getTokenOffset());
break;
case Json.SyntaxKind.FalseKeyword:
node = new BooleanASTNode(parent, name, false, scanner.getTokenOffset());
break;

default:
return null;
}
return _finalize(node, true);
}

function _parseValue(parent: ASTNode, name: Json.Segment): ASTNode {
return _parseArray(parent, name) || _parseObject(parent, name) || _parseString(parent, name, false) || _parseNumber(parent, name) || _parseLiteral(parent, name);
}

_scanNext();

let _root = _parseValue(null, null);
if (!_root) {
_error(localize('Invalid symbol', 'Expected a JSON object, array or literal.'), ErrorCode.Undefined);
} else if (scanner.getToken() !== Json.SyntaxKind.EOF) {
_error(localize('End of file expected', 'End of file expected.'), ErrorCode.Undefined);
}
return new JSONDocument(_root, problems);
}

0 comments on commit b84fba0

Please sign in to comment.