Commit 35b6ca25 authored by yangguo's avatar yangguo Committed by Commit bot

[json parser] add position to error message.

R=jochen@chromium.org, verwaest@chromium.org
BUG=chromium:585724
LOG=N

Review URL: https://codereview.chromium.org/1681513002

Cr-Commit-Position: refs/heads/master@{#33864}
parent d3604cdb
......@@ -1130,6 +1130,7 @@ source_set("v8_base") {
"src/isolate-inl.h",
"src/isolate.cc",
"src/isolate.h",
"src/json-parser.h",
"src/json-stringifier.h",
"src/key-accumulator.h",
"src/key-accumulator.cc",
......@@ -1165,7 +1166,6 @@ source_set("v8_base") {
"src/parsing/expression-classifier.h",
"src/parsing/func-name-inferrer.cc",
"src/parsing/func-name-inferrer.h",
"src/parsing/json-parser.h",
"src/parsing/parameter-initializer-rewriter.cc",
"src/parsing/parameter-initializer-rewriter.h",
"src/parsing/parser-base.h",
......
......@@ -38,8 +38,8 @@
#include "src/global-handles.h"
#include "src/icu_util.h"
#include "src/isolate-inl.h"
#include "src/json-parser.h"
#include "src/messages.h"
#include "src/parsing/json-parser.h"
#include "src/parsing/parser.h"
#include "src/parsing/scanner-character-streams.h"
#include "src/pending-compilation-error-handler.h"
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_PARSING_JSON_PARSER_H_
#define V8_PARSING_JSON_PARSER_H_
#ifndef V8_JSON_PARSER_H_
#define V8_JSON_PARSER_H_
#include "src/char-predicates.h"
#include "src/conversions.h"
......@@ -217,11 +217,12 @@ MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() {
// Parse failed. Current character is the unexpected token.
Factory* factory = this->factory();
MessageTemplate::Template message;
Handle<String> argument;
Handle<Object> arg1 = Handle<Smi>(Smi::FromInt(position_), isolate());
Handle<Object> arg2;
switch (c0_) {
case kEndOfString:
message = MessageTemplate::kUnexpectedEOS;
message = MessageTemplate::kJsonParseUnexpectedEOS;
break;
case '-':
case '0':
......@@ -234,14 +235,15 @@ MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() {
case '7':
case '8':
case '9':
message = MessageTemplate::kUnexpectedTokenNumber;
message = MessageTemplate::kJsonParseUnexpectedTokenNumber;
break;
case '"':
message = MessageTemplate::kUnexpectedTokenString;
message = MessageTemplate::kJsonParseUnexpectedTokenString;
break;
default:
message = MessageTemplate::kUnexpectedToken;
argument = factory->LookupSingleCharacterStringFromCode(c0_);
message = MessageTemplate::kJsonParseUnexpectedToken;
arg2 = arg1;
arg1 = factory->LookupSingleCharacterStringFromCode(c0_);
break;
}
......@@ -250,7 +252,7 @@ MaybeHandle<Object> JsonParser<seq_one_byte>::ParseJson() {
// separated source file.
isolate()->debug()->OnCompileError(script);
MessageLocation location(script, position_, position_ + 1);
Handle<Object> error = factory->NewSyntaxError(message, argument);
Handle<Object> error = factory->NewSyntaxError(message, arg1, arg2);
return isolate()->template Throw<Object>(error, &location);
}
return result;
......@@ -839,4 +841,4 @@ Handle<String> JsonParser<seq_one_byte>::ScanJsonString() {
} // namespace internal
} // namespace v8
#endif // V8_PARSING_JSON_PARSER_H_
#endif // V8_JSON_PARSER_H_
......@@ -406,6 +406,10 @@ class CallSite {
T(InvalidLhsInPrefixOp, \
"Invalid left-hand side expression in prefix operation") \
T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \
T(JsonParseUnexpectedEOS, "Unexpected end of JSON input") \
T(JsonParseUnexpectedToken, "Unexpected token % in JSON at position %") \
T(JsonParseUnexpectedTokenNumber, "Unexpected number in JSON at position %") \
T(JsonParseUnexpectedTokenString, "Unexpected string in JSON at position %") \
T(LabelRedeclaration, "Label '%' has already been declared") \
T(MalformedArrowFunParamList, "Malformed arrow function parameter list") \
T(MalformedRegExp, "Invalid regular expression: /%/: %") \
......
......@@ -7,9 +7,9 @@
#include "src/arguments.h"
#include "src/char-predicates-inl.h"
#include "src/isolate-inl.h"
#include "src/json-parser.h"
#include "src/json-stringifier.h"
#include "src/objects-inl.h"
#include "src/parsing/json-parser.h"
namespace v8 {
namespace internal {
......
......@@ -345,36 +345,35 @@ test(function() {
eval("/a/x.test(\"a\");");
}, "Invalid regular expression flags", SyntaxError);
// kMalformedRegExp
test(function() {
/(/.test("a");
}, "Invalid regular expression: /(/: Unterminated group", SyntaxError);
// kParenthesisInArgString
test(function() {
new Function(")", "");
}, "Function arg string contains parenthesis", SyntaxError);
// kUnexpectedEOS
//kJsonParseUnexpectedEOS
test(function() {
JSON.parse("{")
}, "Unexpected end of input", SyntaxError);
}, "Unexpected end of JSON input", SyntaxError);
// kUnexpectedToken
// kJsonParseUnexpectedTokenAt
test(function() {
JSON.parse("/")
}, "Unexpected token /", SyntaxError);
}, "Unexpected token / in JSON at position 0", SyntaxError);
// kUnexpectedTokenNumber
// kJsonParseUnexpectedTokenNumberAt
test(function() {
JSON.parse("{ 1")
}, "Unexpected number", SyntaxError);
}, "Unexpected number in JSON at position 2", SyntaxError);
// kUnexpectedTokenString
// kJsonParseUnexpectedTokenStringAt
test(function() {
JSON.parse('"""')
}, "Unexpected string", SyntaxError);
}, "Unexpected string in JSON at position 2", SyntaxError);
// kMalformedRegExp
test(function() {
/(/.test("a");
}, "Invalid regular expression: /(/: Unterminated group", SyntaxError);
// kParenthesisInArgString
test(function() {
new Function(")", "");
}, "Function arg string contains parenthesis", SyntaxError);
// === ReferenceError ===
......
......@@ -952,6 +952,7 @@
'../../src/isolate-inl.h',
'../../src/isolate.cc',
'../../src/isolate.h',
'../../src/json-parser.h',
'../../src/json-stringifier.h',
'../../src/key-accumulator.h',
'../../src/key-accumulator.cc',
......@@ -989,7 +990,6 @@
'../../src/parsing/expression-classifier.h',
'../../src/parsing/func-name-inferrer.cc',
'../../src/parsing/func-name-inferrer.h',
'../../src/parsing/json-parser.h',
'../../src/parsing/parameter-initializer-rewriter.cc',
'../../src/parsing/parameter-initializer-rewriter.h',
'../../src/parsing/parser-base.h',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment