summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/torque/earley-parser-unittest.cc
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2018-09-21 09:14:51 +0200
committerMichaël Zasso <targos@protonmail.com>2018-09-22 18:29:25 +0200
commit0e7ddbd3d7e9439c67573b854c49cf82c398ae82 (patch)
tree2afe372acde921cb57ddb3444ff00c5adef8848c /deps/v8/test/unittests/torque/earley-parser-unittest.cc
parent13245dc50da4cb7443c39ef6c68d419d5e6336d4 (diff)
downloadandroid-node-v8-0e7ddbd3d7e9439c67573b854c49cf82c398ae82.tar.gz
android-node-v8-0e7ddbd3d7e9439c67573b854c49cf82c398ae82.tar.bz2
android-node-v8-0e7ddbd3d7e9439c67573b854c49cf82c398ae82.zip
deps: update V8 to 7.0.276.20
PR-URL: https://github.com/nodejs/node/pull/22754 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'deps/v8/test/unittests/torque/earley-parser-unittest.cc')
-rw-r--r--deps/v8/test/unittests/torque/earley-parser-unittest.cc84
1 files changed, 84 insertions, 0 deletions
diff --git a/deps/v8/test/unittests/torque/earley-parser-unittest.cc b/deps/v8/test/unittests/torque/earley-parser-unittest.cc
new file mode 100644
index 0000000000..9718a404c9
--- /dev/null
+++ b/deps/v8/test/unittests/torque/earley-parser-unittest.cc
@@ -0,0 +1,84 @@
+// Copyright 2018 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/torque/earley-parser.h"
+#include "test/unittests/test-utils.h"
+
+namespace v8 {
+namespace internal {
+namespace torque {
+
+namespace {
+
+template <int op(int, int)>
+base::Optional<ParseResult> MakeBinop(ParseResultIterator* child_results) {
+ // Ideally, we would want to use int as a result type here instead of
+ // std::string. This is possible, but requires adding int to the list of
+ // supported ParseResult types in torque-parser.cc. To avoid changing that
+ // code, we use std::string here, which is already used in the Torque parser.
+ auto a = child_results->NextAs<std::string>();
+ auto b = child_results->NextAs<std::string>();
+ return ParseResult{std::to_string(op(std::stoi(a), std::stoi(b)))};
+}
+
+int plus(int a, int b) { return a + b; }
+int minus(int a, int b) { return a - b; }
+int mul(int a, int b) { return a * b; }
+
+} // namespace
+
+struct SimpleArithmeticGrammar : Grammar {
+ static bool MatchWhitespace(InputPosition* pos) {
+ while (MatchChar(std::isspace, pos)) {
+ }
+ return true;
+ }
+
+ static bool MatchInteger(InputPosition* pos) {
+ InputPosition current = *pos;
+ MatchString("-", &current);
+ if (MatchChar(std::isdigit, &current)) {
+ while (MatchChar(std::isdigit, &current)) {
+ }
+ *pos = current;
+ return true;
+ }
+ return false;
+ }
+
+ SimpleArithmeticGrammar() : Grammar(&sum_expression) {
+ SetWhitespace(MatchWhitespace);
+ }
+
+ Symbol integer = {Rule({Pattern(MatchInteger)}, YieldMatchedInput)};
+
+ Symbol atomic_expression = {Rule({&integer}),
+ Rule({Token("("), &sum_expression, Token(")")})};
+
+ Symbol mul_expression = {
+ Rule({&atomic_expression}),
+ Rule({&mul_expression, Token("*"), &atomic_expression}, MakeBinop<mul>)};
+
+ Symbol sum_expression = {
+ Rule({&mul_expression}),
+ Rule({&sum_expression, Token("+"), &mul_expression}, MakeBinop<plus>),
+ Rule({&sum_expression, Token("-"), &mul_expression}, MakeBinop<minus>)};
+};
+
+TEST(EarleyParser, SimpleArithmetic) {
+ SimpleArithmeticGrammar grammar;
+ SourceFileMap::Scope source_file_map;
+ CurrentSourceFile::Scope current_source_file{
+ SourceFileMap::AddSource("dummy_filename")};
+ std::string result1 =
+ grammar.Parse("-5 - 5 + (3 + 5) * 2")->Cast<std::string>();
+ ASSERT_EQ("6", result1);
+ std::string result2 = grammar.Parse("((-1 + (1) * 2 + 3 - 4 * 5 + -6 * 7))")
+ ->Cast<std::string>();
+ ASSERT_EQ("-58", result2);
+}
+
+} // namespace torque
+} // namespace internal
+} // namespace v8