about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2022-01-02 17:52:05 +0300
committerAleksey Kladov <aleksey.kladov@gmail.com>2022-01-02 17:52:05 +0300
commitfa049d94d1ec4b55c7d76b65fa2587e84101ce5b (patch)
tree4f30df37f64d6a4ce9fd9c2fcb2329733589c109
parent640cc27ff07e36d45dd2259b4a37ce797df0559e (diff)
downloadrust-fa049d94d1ec4b55c7d76b65fa2587e84101ce5b.tar.gz
rust-fa049d94d1ec4b55c7d76b65fa2587e84101ce5b.zip
add top-level tests for expressions
-rw-r--r--crates/parser/src/grammar.rs13
-rw-r--r--crates/parser/src/lib.rs2
-rw-r--r--crates/parser/src/tests/top_entries.rs40
3 files changed, 54 insertions, 1 deletions
diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs
index 6ffb4c191b5..c6c111c9a87 100644
--- a/crates/parser/src/grammar.rs
+++ b/crates/parser/src/grammar.rs
@@ -135,6 +135,19 @@ pub(crate) mod entry {
             }
             m.complete(p, ERROR);
         }
+
+        pub(crate) fn expr(p: &mut Parser) {
+            let m = p.start();
+            expressions::expr(p);
+            if p.at(EOF) {
+                m.abandon(p);
+                return;
+            }
+            while !p.at(EOF) {
+                p.bump_any();
+            }
+            m.complete(p, ERROR);
+        }
     }
 }
 
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index 72d529d6cfd..491a657a34d 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -123,8 +123,8 @@ impl TopEntryPoint {
             TopEntryPoint::MacroItems => grammar::entry::top::macro_items,
             TopEntryPoint::Pattern => grammar::entry::top::pattern,
             TopEntryPoint::Type => grammar::entry::top::type_,
+            TopEntryPoint::Expr => grammar::entry::top::expr,
             // FIXME
-            TopEntryPoint::Expr => grammar::entry::prefix::expr,
             TopEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
         };
         let mut p = parser::Parser::new(input);
diff --git a/crates/parser/src/tests/top_entries.rs b/crates/parser/src/tests/top_entries.rs
index dcf61b6aca5..0ba22cf2411 100644
--- a/crates/parser/src/tests/top_entries.rs
+++ b/crates/parser/src/tests/top_entries.rs
@@ -224,6 +224,46 @@ fn type_() {
     );
 }
 
+#[test]
+fn expr() {
+    check(
+        TopEntryPoint::Expr,
+        "2 + 2 == 5",
+        expect![[r#"
+        BIN_EXPR
+          BIN_EXPR
+            LITERAL
+              INT_NUMBER "2"
+            WHITESPACE " "
+            PLUS "+"
+            WHITESPACE " "
+            LITERAL
+              INT_NUMBER "2"
+          WHITESPACE " "
+          EQ2 "=="
+          WHITESPACE " "
+          LITERAL
+            INT_NUMBER "5"
+    "#]],
+    );
+    check(
+        TopEntryPoint::Expr,
+        "let _ = 0;",
+        expect![[r#"
+        ERROR
+          LET_KW "let"
+          WHITESPACE " "
+          UNDERSCORE "_"
+          WHITESPACE " "
+          EQ "="
+          WHITESPACE " "
+          INT_NUMBER "0"
+          SEMICOLON ";"
+        error 0: expected expression
+    "#]],
+    );
+}
+
 #[track_caller]
 fn check(entry: TopEntryPoint, input: &str, expect: expect_test::Expect) {
     let (parsed, _errors) = super::parse(entry, input);