From 0b77eec922441e3252803f3fbf20fb5ca0b8c6ef Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 9 Dec 2018 19:19:23 +0000 Subject: Add a test to ensure that we can parse each file Note that this has a non-spurious failure in ra_analysis/src/mock_analysis --- crates/ra_syntax/tests/test.rs | 45 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 67acc9020..7f385f86f 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -6,7 +6,7 @@ extern crate walkdir; use std::{ fmt::Write, fs, - path::{Path, PathBuf}, + path::{Path, PathBuf, Component}, }; use ra_syntax::{ @@ -37,6 +37,45 @@ fn parser_fuzz_tests() { } } +/// Test that Rust-analyzer can parse and validate the rust-analyser +/// TODO: Use this as a benchmark +#[test] +fn self_hosting_parsing() { + let empty_vec = vec![]; + let dir = project_dir(); + let mut count = 0u32; + for entry in walkdir::WalkDir::new(dir) + .into_iter() + .filter_entry(|entry| { + !entry + .path() + .components() + // TODO: this more neatly + .any(|component| { + // Get all files which are not in the crates/ra_syntax/tests/data folder + (component == Component::Normal(std::ffi::OsStr::new("data")) + // or the .git folder + || component == Component::Normal(std::ffi::OsStr::new(".git"))) + }) + }) + .map(|e| e.unwrap()) + .filter(|entry| { + // Get all `.rs ` files + !entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs"))) + }) + { + count += 1; + let text = read_text(entry.path()); + let node = SourceFileNode::parse(&text); + let errors = node.errors(); + assert_eq!( + errors, empty_vec, + "There should be no errors in the file {:?}", + entry + ); + } + panic!("{}", count) +} /// Read file and normalize newlines. /// /// `rustc` seems to always normalize `\r\n` newlines to `\n`: @@ -49,7 +88,9 @@ fn parser_fuzz_tests() { /// /// so this should always be correct. fn read_text(path: &Path) -> String { - fs::read_to_string(path).unwrap().replace("\r\n", "\n") + fs::read_to_string(path) + .expect(&format!("File at {:?} should be valid", path)) + .replace("\r\n", "\n") } pub fn dir_tests(paths: &[&str], f: F) -- cgit v1.2.3 From 0bd9d87e87c6435d439de3f682b83bc74d784de3 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Mon, 17 Dec 2018 22:33:40 +0000 Subject: Reload package-lock.json --- editors/code/package-lock.json | 150 +++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 72 deletions(-) diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 51c72f019..5e3d64eef 100644 --- a/editors/code/package-lock.json +++ b/editors/code/package-lock.json @@ -17,9 +17,9 @@ "dev": true }, "ajv": { - "version": "6.6.1", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.6.1.tgz", - "integrity": "sha512-ZoJjft5B+EJBjUyu9C9Hc0OZyPZSSlOF+plzouTrg6UlA8f+e/n8NIgBFG/9tppJtpPWfthHakK7juJdNDODww==", + "version": "6.6.2", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.6.2.tgz", + "integrity": "sha512-FBHEW6Jf5TB9MGBgUUA9XHkTbjXYfAUjY43ACMfmdMRHniyoMHjHjzD50OK8LGDWQwp4rWEsIq5kEqq7rvIM1g==", "dev": true, "requires": { "fast-deep-equal": "2.0.1", @@ -193,12 +193,6 @@ "strip-ansi": "3.0.1", "supports-color": "2.0.0" } - }, - "supports-color": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", - "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", - "dev": true } } }, @@ -298,12 +292,6 @@ "color-convert": "1.9.3" } }, - "has-flag": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", - "dev": true - }, "supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", @@ -409,9 +397,9 @@ } }, "commander": { - "version": "2.18.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.18.0.tgz", - "integrity": "sha512-6CYPa+JP2ftfRU2qkDK+UTVeQYosOg/2GbcjIcKPHfinyOLPVGXu/ovN86RP49Re5ndJK1N0kuiidFFuepc4ZQ==", + "version": "2.19.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", + "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==", "dev": true }, "concat-map": { @@ -437,7 +425,7 @@ }, "css-select": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz", "integrity": "sha1-KzoRBTnFNV8c2NMUYj6HCxIeyFg=", "dev": true, "requires": { @@ -473,7 +461,7 @@ }, "deep-assign": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/deep-assign/-/deep-assign-1.0.0.tgz", + "resolved": "http://registry.npmjs.org/deep-assign/-/deep-assign-1.0.0.tgz", "integrity": "sha1-sJJ0O+hCfcYh6gBnzex+cN0Z83s=", "dev": true, "requires": { @@ -502,9 +490,9 @@ "dev": true }, "diff": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz", - "integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww==", + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", "dev": true }, "dom-serializer": { @@ -519,7 +507,7 @@ "dependencies": { "domelementtype": { "version": "1.1.3", - "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz", + "resolved": "http://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz", "integrity": "sha1-vSh3PiZCiBrsUVRJJCmcXNgiGFs=", "dev": true } @@ -928,13 +916,13 @@ }, "string_decoder": { "version": "0.10.31", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", "dev": true }, "through2": { "version": "0.6.5", - "resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz", + "resolved": "http://registry.npmjs.org/through2/-/through2-0.6.5.tgz", "integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=", "dev": true, "requires": { @@ -1078,7 +1066,7 @@ "integrity": "sha512-sNvOCzEQNr/qrvJgc3UG/kD4QtlHycrzwS+6mfTrrSq97BvaYcPZZI1ZSqGSPR73Cxn4LKTD4PttRwfU7jWq5g==", "dev": true, "requires": { - "ajv": "6.6.1", + "ajv": "6.6.2", "har-schema": "2.0.0" } }, @@ -1101,9 +1089,9 @@ } }, "has-flag": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz", - "integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", "dev": true }, "has-symbols": { @@ -1129,7 +1117,7 @@ "domutils": "1.5.1", "entities": "1.1.2", "inherits": "2.0.3", - "readable-stream": "3.0.6" + "readable-stream": "3.1.0" } }, "http-signature": { @@ -1160,9 +1148,9 @@ "dev": true }, "is": { - "version": "3.2.1", - "resolved": "https://registry.npmjs.org/is/-/is-3.2.1.tgz", - "integrity": "sha1-0Kwq1V63sL7JJqUmb2xmKqqD3KU=", + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/is/-/is-3.3.0.tgz", + "integrity": "sha512-nW24QBoPcFGGHJGUwnfpI7Yc5CdqWNdsyHQszVE/z2pKHXzh7FZ5GWhJqSyaQ9wMkQnsTx+kAI8bHlCX4tKdbg==", "dev": true }, "is-absolute": { @@ -1331,7 +1319,7 @@ }, "kind-of": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/kind-of/-/kind-of-1.1.0.tgz", "integrity": "sha1-FAo9LUGjbS78+pN3tiwk+ElaXEQ=", "dev": true }, @@ -1488,6 +1476,12 @@ "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==", "dev": true }, + "diff": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz", + "integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww==", + "dev": true + }, "glob": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", @@ -1501,6 +1495,21 @@ "once": "1.4.0", "path-is-absolute": "1.0.1" } + }, + "has-flag": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz", + "integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=", + "dev": true + }, + "supports-color": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz", + "integrity": "sha512-rKC3+DyXWgK0ZLKwmRsrkyHVZAjNkfzeehuFWdGGcqGDTZFH73+RH6S/RDAAxl9GusSjZSUWYLmT9N5pzXFOXQ==", + "dev": true, + "requires": { + "has-flag": "2.0.0" + } } } }, @@ -1535,7 +1544,7 @@ "dev": true, "requires": { "has": "1.0.3", - "is": "3.2.1" + "is": "3.3.0" } }, "normalize-path": { @@ -1635,13 +1644,13 @@ }, "os-homedir": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", + "resolved": "http://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=", "dev": true }, "os-tmpdir": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "resolved": "http://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=", "dev": true }, @@ -1657,11 +1666,11 @@ }, "parse-semver": { "version": "1.1.1", - "resolved": "https://registry.npmjs.org/parse-semver/-/parse-semver-1.1.1.tgz", + "resolved": "http://registry.npmjs.org/parse-semver/-/parse-semver-1.1.1.tgz", "integrity": "sha1-mkr9bfBj3Egm+T+6SpnPIj9mbLg=", "dev": true, "requires": { - "semver": "5.5.1" + "semver": "5.6.0" } }, "parse5": { @@ -1681,7 +1690,7 @@ }, "path-is-absolute": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, @@ -1738,9 +1747,9 @@ "dev": true }, "psl": { - "version": "1.1.29", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.29.tgz", - "integrity": "sha512-AeUmQ0oLN02flVHXWh9sSJF7mcdFq0ppid/JkErufc3hGIV/AMa8Fo9VgDo/cT2jFdOWoFvHp90qqBH54W+gjQ==", + "version": "1.1.31", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.1.31.tgz", + "integrity": "sha512-/6pt4+C+T+wZUieKR620OpzN/LlnNKuWjy1iFLQ/UG35JqHlR/89MP1d96dUfkf6Dne3TuLQzOYEYshJ+Hx8mw==", "dev": true }, "pump": { @@ -1807,9 +1816,9 @@ } }, "readable-stream": { - "version": "3.0.6", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.0.6.tgz", - "integrity": "sha512-9E1oLoOWfhSXHGv6QlwXJim7uNzd9EVlWK+21tCU9Ju/kR0/p2AZYPz4qSchgO8PlLIH4FpZYfzwS+rEksZjIg==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.1.0.tgz", + "integrity": "sha512-vpydAvIJvPODZNagCPuHG87O9JNPtvFEtjHHRVwNVsVVRBqemvPJkc2SYbxJsiZXawJdtZNmkmnsPuE3IgsG0A==", "dev": true, "requires": { "inherits": "2.0.3", @@ -1885,9 +1894,9 @@ "dev": true }, "resolve": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", - "integrity": "sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.9.0.tgz", + "integrity": "sha512-TZNye00tI67lwYvzxCxHGjwTNlUV70io54/Ed4j6PscB8xVfuBJpRenI/o6dVk0cY0PYTY27AgCoGGxRnYuItQ==", "dev": true, "requires": { "path-parse": "1.0.6" @@ -1924,9 +1933,9 @@ "dev": true }, "semver": { - "version": "5.5.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.1.tgz", - "integrity": "sha512-PqpAxfrEhlSUWge8dwIp4tZnQ25DIOthpiaHNIthsjEFQD6EvqUKUDM7L8O2rShkFccYo1VjJR0coWfNkCubRw==" + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", + "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==" }, "source-map": { "version": "0.6.1", @@ -1955,7 +1964,7 @@ }, "sprintf-js": { "version": "1.0.3", - "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "resolved": "http://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "dev": true }, @@ -2057,17 +2066,14 @@ } }, "supports-color": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz", - "integrity": "sha512-rKC3+DyXWgK0ZLKwmRsrkyHVZAjNkfzeehuFWdGGcqGDTZFH73+RH6S/RDAAxl9GusSjZSUWYLmT9N5pzXFOXQ==", - "dev": true, - "requires": { - "has-flag": "2.0.0" - } + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", + "dev": true }, "tar": { "version": "2.2.1", - "resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz", + "resolved": "http://registry.npmjs.org/tar/-/tar-2.2.1.tgz", "integrity": "sha1-jk0qJWwOIYXGsYrWlK7JaLg8sdE=", "dev": true, "requires": { @@ -2162,7 +2168,7 @@ "integrity": "sha512-Q5srk/4vDM54WJsJio3XNn6K2sCG+CQ8G5Wz6bZhRZoAe/+TxjWB/GlFAnYEbkYVlON9FMk/fE3h2RLpPXo4lQ==", "dev": true, "requires": { - "psl": "1.1.29", + "psl": "1.1.31", "punycode": "1.4.1" }, "dependencies": { @@ -2189,13 +2195,13 @@ "babel-code-frame": "6.26.0", "builtin-modules": "1.1.1", "chalk": "2.4.1", - "commander": "2.18.0", - "diff": "3.3.1", + "commander": "2.19.0", + "diff": "3.5.0", "glob": "7.1.3", "js-yaml": "3.12.0", "minimatch": "3.0.4", - "resolve": "1.8.1", - "semver": "5.5.1", + "resolve": "1.9.0", + "semver": "5.6.0", "tslib": "1.9.3", "tsutils": "2.29.0" } @@ -2248,7 +2254,7 @@ "dependencies": { "underscore": { "version": "1.8.3", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", + "resolved": "http://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=", "dev": true } @@ -2489,7 +2495,7 @@ "dev": true, "requires": { "cheerio": "1.0.0-rc.2", - "commander": "2.18.0", + "commander": "2.19.0", "denodeify": "1.2.1", "glob": "7.1.3", "lodash": "4.17.11", @@ -2499,7 +2505,7 @@ "osenv": "0.1.5", "parse-semver": "1.1.1", "read": "1.0.7", - "semver": "5.5.1", + "semver": "5.6.0", "tmp": "0.0.29", "url-join": "1.1.0", "vso-node-api": "6.1.2-preview", @@ -2522,7 +2528,7 @@ "gulp-vinyl-zip": "2.1.2", "mocha": "4.1.0", "request": "2.88.0", - "semver": "5.5.1", + "semver": "5.6.0", "source-map-support": "0.5.9", "url-parse": "1.4.4", "vinyl-fs": "3.0.3", @@ -2539,7 +2545,7 @@ "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-5.2.1.tgz", "integrity": "sha512-7jrS/9WnV0ruqPamN1nE7qCxn0phkH5LjSgSp9h6qoJGoeAKzwKz/PF6M+iGA/aklx4GLZg1prddhEPQtuXI1Q==", "requires": { - "semver": "5.5.1", + "semver": "5.6.0", "vscode-languageserver-protocol": "3.14.1" } }, -- cgit v1.2.3 From 20bbe0127cc6bfac3ced0c7ed1de4f0526f3bbed Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Mon, 17 Dec 2018 22:34:18 +0000 Subject: Fix parsing of inclusive ranges (#214) I'm not certain that this is correct, so extra eyes would be good --- crates/ra_syntax/src/grammar/expressions.rs | 4 +- crates/ra_syntax/src/grammar/patterns.rs | 8 +- .../tests/data/parser/inline/0094_range_pat.rs | 6 +- .../tests/data/parser/inline/0094_range_pat.txt | 86 +++++++++++++++------- .../tests/data/parser/ok/0029_range_forms.rs | 5 ++ .../tests/data/parser/ok/0029_range_forms.txt | 86 ++++++++++++++++++++-- 6 files changed, 158 insertions(+), 37 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index a9449c7bf..bca32f707 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -143,7 +143,7 @@ fn current_op(p: &Parser) -> (u8, Op) { let bp = match p.current() { EQ => 1, - DOTDOT => 2, + DOTDOT | DOTDOTEQ => 2, EQEQ | NEQ | L_ANGLE | R_ANGLE => 5, PIPE => 6, CARET => 7, @@ -173,7 +173,7 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { }; loop { - let is_range = p.current() == DOTDOT; + let is_range = p.current() == DOTDOT || p.current() == DOTDOTEQ; let (op_bp, op) = current_op(p); if op_bp < bp { break; diff --git a/crates/ra_syntax/src/grammar/patterns.rs b/crates/ra_syntax/src/grammar/patterns.rs index 10fa0e0be..64cdf0b1b 100644 --- a/crates/ra_syntax/src/grammar/patterns.rs +++ b/crates/ra_syntax/src/grammar/patterns.rs @@ -14,9 +14,13 @@ pub(super) fn pattern_r(p: &mut Parser, recovery_set: TokenSet) { if let Some(lhs) = atom_pat(p, recovery_set) { // test range_pat // fn main() { - // match 92 { 0 ... 100 => () } + // match 92 { + // 0 ... 100 => (), + // 101 ..= 200 => (), + // 200 .. 301=> (), + // } // } - if p.at(DOTDOTDOT) { + if p.at(DOTDOTDOT) || p.at(DOTDOTEQ) || p.at(DOTDOT) { let m = lhs.precede(p); p.bump(); atom_pat(p, recovery_set); diff --git a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs index 657467e75..3bca7bf5d 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs +++ b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs @@ -1,3 +1,7 @@ fn main() { - match 92 { 0 ... 100 => () } + match 92 { + 0 ... 100 => (), + 101 ..= 200 => (), + 200 .. 301=> (), + } } diff --git a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt index 12ccc1314..7eb0fcdf4 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt +++ b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt @@ -1,5 +1,5 @@ -SOURCE_FILE@[0; 47) - FN_DEF@[0; 46) +SOURCE_FILE@[0; 113) + FN_DEF@[0; 112) FN_KW@[0; 2) WHITESPACE@[2; 3) NAME@[3; 7) @@ -8,35 +8,69 @@ SOURCE_FILE@[0; 47) L_PAREN@[7; 8) R_PAREN@[8; 9) WHITESPACE@[9; 10) - BLOCK@[10; 46) + BLOCK@[10; 112) L_CURLY@[10; 11) WHITESPACE@[11; 16) - MATCH_EXPR@[16; 44) + MATCH_EXPR@[16; 110) MATCH_KW@[16; 21) WHITESPACE@[21; 22) LITERAL@[22; 24) INT_NUMBER@[22; 24) "92" WHITESPACE@[24; 25) - MATCH_ARM_LIST@[25; 44) + MATCH_ARM_LIST@[25; 110) L_CURLY@[25; 26) - WHITESPACE@[26; 27) - MATCH_ARM@[27; 42) - RANGE_PAT@[27; 36) - LITERAL@[27; 28) - INT_NUMBER@[27; 28) "0" - WHITESPACE@[28; 29) - DOTDOTDOT@[29; 32) - WHITESPACE@[32; 33) - LITERAL@[33; 36) - INT_NUMBER@[33; 36) "100" - WHITESPACE@[36; 37) - FAT_ARROW@[37; 39) - WHITESPACE@[39; 40) - TUPLE_EXPR@[40; 42) - L_PAREN@[40; 41) - R_PAREN@[41; 42) - WHITESPACE@[42; 43) - R_CURLY@[43; 44) - WHITESPACE@[44; 45) - R_CURLY@[45; 46) - WHITESPACE@[46; 47) + WHITESPACE@[26; 36) + MATCH_ARM@[36; 51) + RANGE_PAT@[36; 45) + LITERAL@[36; 37) + INT_NUMBER@[36; 37) "0" + WHITESPACE@[37; 38) + DOTDOTDOT@[38; 41) + WHITESPACE@[41; 42) + LITERAL@[42; 45) + INT_NUMBER@[42; 45) "100" + WHITESPACE@[45; 46) + FAT_ARROW@[46; 48) + WHITESPACE@[48; 49) + TUPLE_EXPR@[49; 51) + L_PAREN@[49; 50) + R_PAREN@[50; 51) + COMMA@[51; 52) + WHITESPACE@[52; 61) + MATCH_ARM@[61; 78) + RANGE_PAT@[61; 72) + LITERAL@[61; 64) + INT_NUMBER@[61; 64) "101" + WHITESPACE@[64; 65) + DOTDOTEQ@[65; 68) + WHITESPACE@[68; 69) + LITERAL@[69; 72) + INT_NUMBER@[69; 72) "200" + WHITESPACE@[72; 73) + FAT_ARROW@[73; 75) + WHITESPACE@[75; 76) + TUPLE_EXPR@[76; 78) + L_PAREN@[76; 77) + R_PAREN@[77; 78) + COMMA@[78; 79) + WHITESPACE@[79; 88) + MATCH_ARM@[88; 103) + RANGE_PAT@[88; 98) + LITERAL@[88; 91) + INT_NUMBER@[88; 91) "200" + WHITESPACE@[91; 92) + DOTDOT@[92; 94) + WHITESPACE@[94; 95) + LITERAL@[95; 98) + INT_NUMBER@[95; 98) "301" + FAT_ARROW@[98; 100) + WHITESPACE@[100; 101) + TUPLE_EXPR@[101; 103) + L_PAREN@[101; 102) + R_PAREN@[102; 103) + COMMA@[103; 104) + WHITESPACE@[104; 109) + R_CURLY@[109; 110) + WHITESPACE@[110; 111) + R_CURLY@[111; 112) + WHITESPACE@[112; 113) diff --git a/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.rs b/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.rs index 03f4ae7b2..f9ff444d4 100644 --- a/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.rs +++ b/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.rs @@ -3,4 +3,9 @@ fn foo() { ..z = 2; x = false..1 == 1; let x = 1..; + + ..=1 + 1; + ..=z = 2; + x = false..=1 == 1; + let x = 1..; } diff --git a/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.txt b/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.txt index 2e7703c21..e3706bfbd 100644 --- a/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.txt +++ b/crates/ra_syntax/tests/data/parser/ok/0029_range_forms.txt @@ -1,5 +1,5 @@ -SOURCE_FILE@[0; 79) - FN_DEF@[0; 78) +SOURCE_FILE@[0; 153) + FN_DEF@[0; 152) FN_KW@[0; 2) WHITESPACE@[2; 3) NAME@[3; 6) @@ -8,7 +8,7 @@ SOURCE_FILE@[0; 79) L_PAREN@[6; 7) R_PAREN@[7; 8) WHITESPACE@[8; 9) - BLOCK@[9; 78) + BLOCK@[9; 152) L_CURLY@[9; 10) WHITESPACE@[10; 15) EXPR_STMT@[15; 23) @@ -78,6 +78,80 @@ SOURCE_FILE@[0; 79) INT_NUMBER@[72; 73) "1" DOTDOT@[73; 75) SEMI@[75; 76) - WHITESPACE@[76; 77) - R_CURLY@[77; 78) - WHITESPACE@[78; 79) + WHITESPACE@[76; 86) + err: `expected expression` + EXPR_STMT@[86; 89) + ERROR@[86; 89) + DOTDOTEQ@[86; 89) + err: `expected SEMI` + EXPR_STMT@[89; 95) + BIN_EXPR@[89; 94) + LITERAL@[89; 90) + INT_NUMBER@[89; 90) "1" + WHITESPACE@[90; 91) + PLUS@[91; 92) + WHITESPACE@[92; 93) + LITERAL@[93; 94) + INT_NUMBER@[93; 94) "1" + SEMI@[94; 95) + WHITESPACE@[95; 100) + err: `expected expression` + EXPR_STMT@[100; 103) + ERROR@[100; 103) + DOTDOTEQ@[100; 103) + err: `expected SEMI` + EXPR_STMT@[103; 109) + BIN_EXPR@[103; 108) + PATH_EXPR@[103; 104) + PATH@[103; 104) + PATH_SEGMENT@[103; 104) + NAME_REF@[103; 104) + IDENT@[103; 104) "z" + WHITESPACE@[104; 105) + EQ@[105; 106) + WHITESPACE@[106; 107) + LITERAL@[107; 108) + INT_NUMBER@[107; 108) "2" + SEMI@[108; 109) + WHITESPACE@[109; 114) + EXPR_STMT@[114; 133) + BIN_EXPR@[114; 132) + PATH_EXPR@[114; 115) + PATH@[114; 115) + PATH_SEGMENT@[114; 115) + NAME_REF@[114; 115) + IDENT@[114; 115) "x" + WHITESPACE@[115; 116) + EQ@[116; 117) + WHITESPACE@[117; 118) + RANGE_EXPR@[118; 132) + LITERAL@[118; 123) + FALSE_KW@[118; 123) + DOTDOTEQ@[123; 126) + BIN_EXPR@[126; 132) + LITERAL@[126; 127) + INT_NUMBER@[126; 127) "1" + WHITESPACE@[127; 128) + EQEQ@[128; 130) + WHITESPACE@[130; 131) + LITERAL@[131; 132) + INT_NUMBER@[131; 132) "1" + SEMI@[132; 133) + WHITESPACE@[133; 138) + LET_STMT@[138; 150) + LET_KW@[138; 141) + WHITESPACE@[141; 142) + BIND_PAT@[142; 143) + NAME@[142; 143) + IDENT@[142; 143) "x" + WHITESPACE@[143; 144) + EQ@[144; 145) + WHITESPACE@[145; 146) + RANGE_EXPR@[146; 149) + LITERAL@[146; 147) + INT_NUMBER@[146; 147) "1" + DOTDOT@[147; 149) + SEMI@[149; 150) + WHITESPACE@[150; 151) + R_CURLY@[151; 152) + WHITESPACE@[152; 153) -- cgit v1.2.3 From 012537bd6cdf2348dc7bc4ec89c8ecbb9578f5b3 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 18 Dec 2018 21:13:55 +0000 Subject: Fix at_ts doc comment --- crates/ra_syntax/src/grammar/expressions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index bca32f707..512823ddf 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -416,7 +416,7 @@ fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { m.complete(p, STRUCT_LIT) } EXCL => { - items::macro_call_after_excl(p); + items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) m.complete(p, MACRO_CALL) } _ => m.complete(p, PATH_EXPR), -- cgit v1.2.3 From 29bf389034db9a0de4ec2dbd7492c1f75caf4a60 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 18 Dec 2018 21:15:14 +0000 Subject: Actually fix at_ts doc comment (committed wrong file :P) --- crates/ra_syntax/src/parser_api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_syntax/src/parser_api.rs b/crates/ra_syntax/src/parser_api.rs index c37c30e34..02421def1 100644 --- a/crates/ra_syntax/src/parser_api.rs +++ b/crates/ra_syntax/src/parser_api.rs @@ -36,7 +36,7 @@ impl<'t> Parser<'t> { self.current() == kind } - /// Checks if the current token is `kind`. + /// Checks if the current token is in `kinds`. pub(crate) fn at_ts(&self, kinds: TokenSet) -> bool { kinds.contains(self.current()) } -- cgit v1.2.3 From 97e70bf50f15007f5782f7f96d19da342b7c9505 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Tue, 18 Dec 2018 22:59:34 +0000 Subject: Possibly fix #225 --- crates/ra_syntax/src/grammar/expressions.rs | 45 ++++++++++++++++-------- crates/ra_syntax/src/grammar/expressions/atom.rs | 16 +++++---- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 512823ddf..043f41b98 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,18 +158,18 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - Some(lhs) => { + (Some(lhs), blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && is_block(lhs.kind()) { + if r.prefer_stmt && (is_block(lhs.kind()) || blocklike == Some(BlockLike::Block)) { return BlockLike::Block; } lhs } - None => return BlockLike::NotBlock, + (None, _) => return BlockLike::NotBlock, }; loop { @@ -213,7 +213,7 @@ const LHS_FIRST: TokenSet = token_set_union![ atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> Option { +fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { let m; let kind = match p.current() { // test ref_expr @@ -246,19 +246,33 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option { if p.at_ts(EXPR_FIRST) { expr_bp(p, r, 2); } - return Some(m.complete(p, RANGE_EXPR)); + return (Some(m.complete(p, RANGE_EXPR)), None); } _ => { - let lhs = atom::atom_expr(p, r)?; - return Some(postfix_expr(p, r, lhs)); + let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + + if let Some(lhs_marker) = lhs_marker { + return ( + Some(postfix_expr(p, r, lhs_marker, macro_block_like)), + macro_block_like, + ); + } else { + return (None, None); + } } }; expr_bp(p, r, 255); - Some(m.complete(p, kind)) + (Some(m.complete(p, kind)), None) } -fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { - let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); +fn postfix_expr( + p: &mut Parser, + r: Restrictions, + mut lhs: CompletedMarker, + macro_block_like: Option, +) -> CompletedMarker { + let mut allow_calls = + !r.prefer_stmt || !is_block(lhs.kind()) || macro_block_like != Some(BlockLike::Block); loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity @@ -406,21 +420,22 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { +fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - match p.current() { + let res = match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { - items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - m.complete(p, MACRO_CALL) + let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) + return (m.complete(p, MACRO_CALL), Some(block_like)); } _ => m.complete(p, PATH_EXPR), - } + }; + (res, None) } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 04087fd60..e86a33090 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,12 +61,16 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option { +pub(super) fn atom_expr( + p: &mut Parser, + r: Restrictions, +) -> (Option, Option) { if let Some(m) = literal(p) { - return Some(m); + return (Some(m), None); } if paths::is_path_start(p) || p.at(L_ANGLE) { - return Some(path_expr(p, r)); + let path_expr = path_expr(p, r); + return (Some(path_expr.0), path_expr.1); } let la = p.nth(1); let done = match p.current() { @@ -94,7 +98,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option Option break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return None; + return (None, None); } }; - Some(done) + (Some(done), None) } // test tuple_expr -- cgit v1.2.3 From 7a8560ba382af4b3955b14757518f748e0d67709 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 18:12:19 +0000 Subject: Fix expression parsing by bailing out upon a macro block being found TODO: Fix this when the block like macro is in expression position E.g. `test(test!{})` currently parses --- crates/ra_syntax/src/grammar/expressions.rs | 26 ++++++++++++-------------- crates/ra_syntax/tests/test.rs | 3 --- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 043f41b98..9d75bfb90 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,13 +158,14 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), blocklike) => { + (Some(lhs), macro_blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || blocklike == Some(BlockLike::Block)) { + if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) + { return BlockLike::Block; } lhs @@ -251,11 +252,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + if macro_block_like == Some(BlockLike::Block) { + return (lhs_marker, macro_block_like); + } if let Some(lhs_marker) = lhs_marker { - return ( - Some(postfix_expr(p, r, lhs_marker, macro_block_like)), - macro_block_like, - ); + return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); } else { return (None, None); } @@ -265,14 +266,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option, -) -> CompletedMarker { - let mut allow_calls = - !r.prefer_stmt || !is_block(lhs.kind()) || macro_block_like != Some(BlockLike::Block); +fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { + // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple + // E.g. `while true {break}();` is parsed as + // `while true {break}; ();` + let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 7f385f86f..a07855768 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -43,7 +43,6 @@ fn parser_fuzz_tests() { fn self_hosting_parsing() { let empty_vec = vec![]; let dir = project_dir(); - let mut count = 0u32; for entry in walkdir::WalkDir::new(dir) .into_iter() .filter_entry(|entry| { @@ -64,7 +63,6 @@ fn self_hosting_parsing() { !entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs"))) }) { - count += 1; let text = read_text(entry.path()); let node = SourceFileNode::parse(&text); let errors = node.errors(); @@ -74,7 +72,6 @@ fn self_hosting_parsing() { entry ); } - panic!("{}", count) } /// Read file and normalize newlines. /// -- cgit v1.2.3 From cd8e33fb7e60d78809da5e77c968ef30433c2317 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 18:33:36 +0000 Subject: Revert to f6f7c5 --- crates/ra_syntax/src/grammar/expressions.rs | 39 ++++++++---------------- crates/ra_syntax/src/grammar/expressions/atom.rs | 16 ++++------ crates/ra_syntax/tests/test.rs | 3 ++ 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 9d75bfb90..512823ddf 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,19 +158,18 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), macro_blocklike) => { + Some(lhs) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) - { + if r.prefer_stmt && is_block(lhs.kind()) { return BlockLike::Block; } lhs } - (None, _) => return BlockLike::NotBlock, + None => return BlockLike::NotBlock, }; loop { @@ -214,7 +213,7 @@ const LHS_FIRST: TokenSet = token_set_union![ atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { +fn lhs(p: &mut Parser, r: Restrictions) -> Option { let m; let kind = match p.current() { // test ref_expr @@ -247,29 +246,18 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { - let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); - - if macro_block_like == Some(BlockLike::Block) { - return (lhs_marker, macro_block_like); - } - if let Some(lhs_marker) = lhs_marker { - return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); - } else { - return (None, None); - } + let lhs = atom::atom_expr(p, r)?; + return Some(postfix_expr(p, r, lhs)); } }; expr_bp(p, r, 255); - (Some(m.complete(p, kind)), None) + Some(m.complete(p, kind)) } fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { - // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple - // E.g. `while true {break}();` is parsed as - // `while true {break}; ();` let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { @@ -418,22 +406,21 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { +fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - let res = match p.current() { + match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { - let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - return (m.complete(p, MACRO_CALL), Some(block_like)); + items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) + m.complete(p, MACRO_CALL) } _ => m.complete(p, PATH_EXPR), - }; - (res, None) + } } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index e86a33090..04087fd60 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,16 +61,12 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr( - p: &mut Parser, - r: Restrictions, -) -> (Option, Option) { +pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option { if let Some(m) = literal(p) { - return (Some(m), None); + return Some(m); } if paths::is_path_start(p) || p.at(L_ANGLE) { - let path_expr = path_expr(p, r); - return (Some(path_expr.0), path_expr.1); + return Some(path_expr(p, r)); } let la = p.nth(1); let done = match p.current() { @@ -98,7 +94,7 @@ pub(super) fn atom_expr( // } p.error("expected a loop"); m.complete(p, ERROR); - return (None, None); + return None; } } } @@ -115,10 +111,10 @@ pub(super) fn atom_expr( BREAK_KW => break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return (None, None); + return None; } }; - (Some(done), None) + Some(done) } // test tuple_expr diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index a07855768..7f385f86f 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -43,6 +43,7 @@ fn parser_fuzz_tests() { fn self_hosting_parsing() { let empty_vec = vec![]; let dir = project_dir(); + let mut count = 0u32; for entry in walkdir::WalkDir::new(dir) .into_iter() .filter_entry(|entry| { @@ -63,6 +64,7 @@ fn self_hosting_parsing() { !entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs"))) }) { + count += 1; let text = read_text(entry.path()); let node = SourceFileNode::parse(&text); let errors = node.errors(); @@ -72,6 +74,7 @@ fn self_hosting_parsing() { entry ); } + panic!("{}", count) } /// Read file and normalize newlines. /// -- cgit v1.2.3 From 4dce66ad319cbcbfae5f9e941b80c28cf546590c Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 19:35:14 +0000 Subject: Fix handling of structs in match arms --- crates/ra_syntax/src/grammar/expressions/atom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 04087fd60..452e91485 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -362,7 +362,7 @@ fn match_arm(p: &mut Parser) -> BlockLike { patterns::pattern(p); } if p.eat(IF_KW) { - expr_no_struct(p); + expr(p); } p.expect(FAT_ARROW); let ret = expr_stmt(p); -- cgit v1.2.3 From def0a95d357f334b7329c3ee9e7da3c30563321c Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 20:02:37 +0000 Subject: Revert "Revert to f6f7c5" This approach is correct, but it needs an addition to Restrictions too This reverts commit ad00d0c8a5f64142e6636e8b048204c8f8982f4a. --- crates/ra_syntax/src/grammar/expressions.rs | 39 ++++++++++++++++-------- crates/ra_syntax/src/grammar/expressions/atom.rs | 16 ++++++---- crates/ra_syntax/tests/test.rs | 3 -- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 512823ddf..9d75bfb90 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,18 +158,19 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - Some(lhs) => { + (Some(lhs), macro_blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && is_block(lhs.kind()) { + if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) + { return BlockLike::Block; } lhs } - None => return BlockLike::NotBlock, + (None, _) => return BlockLike::NotBlock, }; loop { @@ -213,7 +214,7 @@ const LHS_FIRST: TokenSet = token_set_union![ atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> Option { +fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { let m; let kind = match p.current() { // test ref_expr @@ -246,18 +247,29 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option { if p.at_ts(EXPR_FIRST) { expr_bp(p, r, 2); } - return Some(m.complete(p, RANGE_EXPR)); + return (Some(m.complete(p, RANGE_EXPR)), None); } _ => { - let lhs = atom::atom_expr(p, r)?; - return Some(postfix_expr(p, r, lhs)); + let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + + if macro_block_like == Some(BlockLike::Block) { + return (lhs_marker, macro_block_like); + } + if let Some(lhs_marker) = lhs_marker { + return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); + } else { + return (None, None); + } } }; expr_bp(p, r, 255); - Some(m.complete(p, kind)) + (Some(m.complete(p, kind)), None) } fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { + // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple + // E.g. `while true {break}();` is parsed as + // `while true {break}; ();` let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { @@ -406,21 +418,22 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { +fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - match p.current() { + let res = match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); m.complete(p, STRUCT_LIT) } EXCL => { - items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - m.complete(p, MACRO_CALL) + let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) + return (m.complete(p, MACRO_CALL), Some(block_like)); } _ => m.complete(p, PATH_EXPR), - } + }; + (res, None) } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 452e91485..471f398f5 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,12 +61,16 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option { +pub(super) fn atom_expr( + p: &mut Parser, + r: Restrictions, +) -> (Option, Option) { if let Some(m) = literal(p) { - return Some(m); + return (Some(m), None); } if paths::is_path_start(p) || p.at(L_ANGLE) { - return Some(path_expr(p, r)); + let path_expr = path_expr(p, r); + return (Some(path_expr.0), path_expr.1); } let la = p.nth(1); let done = match p.current() { @@ -94,7 +98,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option Option break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return None; + return (None, None); } }; - Some(done) + (Some(done), None) } // test tuple_expr diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 7f385f86f..a07855768 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -43,7 +43,6 @@ fn parser_fuzz_tests() { fn self_hosting_parsing() { let empty_vec = vec![]; let dir = project_dir(); - let mut count = 0u32; for entry in walkdir::WalkDir::new(dir) .into_iter() .filter_entry(|entry| { @@ -64,7 +63,6 @@ fn self_hosting_parsing() { !entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs"))) }) { - count += 1; let text = read_text(entry.path()); let node = SourceFileNode::parse(&text); let errors = node.errors(); @@ -74,7 +72,6 @@ fn self_hosting_parsing() { entry ); } - panic!("{}", count) } /// Read file and normalize newlines. /// -- cgit v1.2.3 From db677414304bec41a5eae57eea4eb0b546619415 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 20:55:24 +0000 Subject: Move is_block to lower in the call tree --- crates/ra_syntax/src/grammar/expressions.rs | 77 ++++++------- crates/ra_syntax/src/grammar/expressions/atom.rs | 20 ++-- .../tests/data/parser/inline/0094_range_pat.rs | 2 +- .../tests/data/parser/inline/0094_range_pat.txt | 120 ++++++++++----------- 4 files changed, 104 insertions(+), 115 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 9d75bfb90..5f5a3077d 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -64,6 +64,16 @@ pub(crate) fn block(p: &mut Parser) { if p.at(R_CURLY) { m.abandon(p); } else { + // test no_semi_after_block + // fn foo() { + // if true {} + // loop {} + // match () {} + // while true {} + // for _ in () {} + // {} + // {} + // } if is_blocklike { p.eat(SEMI); } else { @@ -158,19 +168,18 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), macro_blocklike) => { + Some((lhs, macro_blocklike)) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) - { + if r.prefer_stmt && macro_blocklike.is_block() { return BlockLike::Block; } lhs } - (None, _) => return BlockLike::NotBlock, + None => return BlockLike::NotBlock, }; loop { @@ -192,29 +201,12 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { BlockLike::NotBlock } -// test no_semi_after_block -// fn foo() { -// if true {} -// loop {} -// match () {} -// while true {} -// for _ in () {} -// {} -// {} -// } -fn is_block(kind: SyntaxKind) -> bool { - match kind { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true, - _ => false, - } -} - const LHS_FIRST: TokenSet = token_set_union![ token_set![AMP, STAR, EXCL, DOTDOT, MINUS], atom::ATOM_EXPR_FIRST, ]; -fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option) { +fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { let m; let kind = match p.current() { // test ref_expr @@ -247,30 +239,28 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { - let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); - - if macro_block_like == Some(BlockLike::Block) { - return (lhs_marker, macro_block_like); - } - if let Some(lhs_marker) = lhs_marker { - return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); - } else { - return (None, None); - } + let (lhs, blocklike) = atom::atom_expr(p, r)?; + return Some(( + postfix_expr(p, lhs, !(r.prefer_stmt && blocklike.is_block())), + blocklike, + )); } }; expr_bp(p, r, 255); - (Some(m.complete(p, kind)), None) + Some((m.complete(p, kind), BlockLike::NotBlock)) } -fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { +fn postfix_expr( + p: &mut Parser, + mut lhs: CompletedMarker, // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple // E.g. `while true {break}();` is parsed as // `while true {break}; ();` - let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); + mut allow_calls: bool, +) -> CompletedMarker { loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity @@ -418,22 +408,21 @@ fn arg_list(p: &mut Parser) { // let _ = ::a::; // let _ = format!(); // } -fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, Option) { +fn path_expr(p: &mut Parser, r: Restrictions) -> (CompletedMarker, BlockLike) { assert!(paths::is_path_start(p) || p.at(L_ANGLE)); let m = p.start(); paths::expr_path(p); - let res = match p.current() { + match p.current() { L_CURLY if !r.forbid_structs => { named_field_list(p); - m.complete(p, STRUCT_LIT) + (m.complete(p, STRUCT_LIT), BlockLike::Block) } EXCL => { - let block_like = items::macro_call_after_excl(p); // TODO: Use return type (BlockLike) - return (m.complete(p, MACRO_CALL), Some(block_like)); + let block_like = items::macro_call_after_excl(p); + return (m.complete(p, MACRO_CALL), block_like); } - _ => m.complete(p, PATH_EXPR), - }; - (res, None) + _ => (m.complete(p, PATH_EXPR), BlockLike::NotBlock), + } } // test struct_lit diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 471f398f5..a976799e7 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -61,16 +61,12 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet = token_set_union![ const EXPR_RECOVERY_SET: TokenSet = token_set![LET_KW]; -pub(super) fn atom_expr( - p: &mut Parser, - r: Restrictions, -) -> (Option, Option) { +pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> { if let Some(m) = literal(p) { - return (Some(m), None); + return Some((m, BlockLike::NotBlock)); } if paths::is_path_start(p) || p.at(L_ANGLE) { - let path_expr = path_expr(p, r); - return (Some(path_expr.0), path_expr.1); + return Some(path_expr(p, r)); } let la = p.nth(1); let done = match p.current() { @@ -98,7 +94,7 @@ pub(super) fn atom_expr( // } p.error("expected a loop"); m.complete(p, ERROR); - return (None, None); + return None; } } } @@ -115,10 +111,14 @@ pub(super) fn atom_expr( BREAK_KW => break_expr(p), _ => { p.err_recover("expected expression", EXPR_RECOVERY_SET); - return (None, None); + return None; } }; - (Some(done), None) + let blocklike = match done.kind() { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block, + _ => BlockLike::NotBlock, + }; + Some((done, blocklike)) } // test tuple_expr diff --git a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs index 3bca7bf5d..c12ab6fce 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs +++ b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.rs @@ -1,5 +1,5 @@ fn main() { - match 92 { + match 92 { 0 ... 100 => (), 101 ..= 200 => (), 200 .. 301=> (), diff --git a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt index 7eb0fcdf4..d47f38903 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt +++ b/crates/ra_syntax/tests/data/parser/inline/0094_range_pat.txt @@ -1,5 +1,5 @@ -SOURCE_FILE@[0; 113) - FN_DEF@[0; 112) +SOURCE_FILE@[0; 112) + FN_DEF@[0; 111) FN_KW@[0; 2) WHITESPACE@[2; 3) NAME@[3; 7) @@ -8,69 +8,69 @@ SOURCE_FILE@[0; 113) L_PAREN@[7; 8) R_PAREN@[8; 9) WHITESPACE@[9; 10) - BLOCK@[10; 112) + BLOCK@[10; 111) L_CURLY@[10; 11) WHITESPACE@[11; 16) - MATCH_EXPR@[16; 110) + MATCH_EXPR@[16; 109) MATCH_KW@[16; 21) WHITESPACE@[21; 22) LITERAL@[22; 24) INT_NUMBER@[22; 24) "92" WHITESPACE@[24; 25) - MATCH_ARM_LIST@[25; 110) + MATCH_ARM_LIST@[25; 109) L_CURLY@[25; 26) - WHITESPACE@[26; 36) - MATCH_ARM@[36; 51) - RANGE_PAT@[36; 45) - LITERAL@[36; 37) - INT_NUMBER@[36; 37) "0" - WHITESPACE@[37; 38) - DOTDOTDOT@[38; 41) - WHITESPACE@[41; 42) - LITERAL@[42; 45) - INT_NUMBER@[42; 45) "100" - WHITESPACE@[45; 46) - FAT_ARROW@[46; 48) - WHITESPACE@[48; 49) - TUPLE_EXPR@[49; 51) - L_PAREN@[49; 50) - R_PAREN@[50; 51) - COMMA@[51; 52) - WHITESPACE@[52; 61) - MATCH_ARM@[61; 78) - RANGE_PAT@[61; 72) - LITERAL@[61; 64) - INT_NUMBER@[61; 64) "101" - WHITESPACE@[64; 65) - DOTDOTEQ@[65; 68) - WHITESPACE@[68; 69) - LITERAL@[69; 72) - INT_NUMBER@[69; 72) "200" - WHITESPACE@[72; 73) - FAT_ARROW@[73; 75) - WHITESPACE@[75; 76) - TUPLE_EXPR@[76; 78) - L_PAREN@[76; 77) - R_PAREN@[77; 78) - COMMA@[78; 79) - WHITESPACE@[79; 88) - MATCH_ARM@[88; 103) - RANGE_PAT@[88; 98) - LITERAL@[88; 91) - INT_NUMBER@[88; 91) "200" - WHITESPACE@[91; 92) - DOTDOT@[92; 94) - WHITESPACE@[94; 95) - LITERAL@[95; 98) - INT_NUMBER@[95; 98) "301" - FAT_ARROW@[98; 100) - WHITESPACE@[100; 101) - TUPLE_EXPR@[101; 103) - L_PAREN@[101; 102) - R_PAREN@[102; 103) - COMMA@[103; 104) - WHITESPACE@[104; 109) - R_CURLY@[109; 110) - WHITESPACE@[110; 111) - R_CURLY@[111; 112) - WHITESPACE@[112; 113) + WHITESPACE@[26; 35) + MATCH_ARM@[35; 50) + RANGE_PAT@[35; 44) + LITERAL@[35; 36) + INT_NUMBER@[35; 36) "0" + WHITESPACE@[36; 37) + DOTDOTDOT@[37; 40) + WHITESPACE@[40; 41) + LITERAL@[41; 44) + INT_NUMBER@[41; 44) "100" + WHITESPACE@[44; 45) + FAT_ARROW@[45; 47) + WHITESPACE@[47; 48) + TUPLE_EXPR@[48; 50) + L_PAREN@[48; 49) + R_PAREN@[49; 50) + COMMA@[50; 51) + WHITESPACE@[51; 60) + MATCH_ARM@[60; 77) + RANGE_PAT@[60; 71) + LITERAL@[60; 63) + INT_NUMBER@[60; 63) "101" + WHITESPACE@[63; 64) + DOTDOTEQ@[64; 67) + WHITESPACE@[67; 68) + LITERAL@[68; 71) + INT_NUMBER@[68; 71) "200" + WHITESPACE@[71; 72) + FAT_ARROW@[72; 74) + WHITESPACE@[74; 75) + TUPLE_EXPR@[75; 77) + L_PAREN@[75; 76) + R_PAREN@[76; 77) + COMMA@[77; 78) + WHITESPACE@[78; 87) + MATCH_ARM@[87; 102) + RANGE_PAT@[87; 97) + LITERAL@[87; 90) + INT_NUMBER@[87; 90) "200" + WHITESPACE@[90; 91) + DOTDOT@[91; 93) + WHITESPACE@[93; 94) + LITERAL@[94; 97) + INT_NUMBER@[94; 97) "301" + FAT_ARROW@[97; 99) + WHITESPACE@[99; 100) + TUPLE_EXPR@[100; 102) + L_PAREN@[100; 101) + R_PAREN@[101; 102) + COMMA@[102; 103) + WHITESPACE@[103; 108) + R_CURLY@[108; 109) + WHITESPACE@[109; 110) + R_CURLY@[110; 111) + WHITESPACE@[111; 112) -- cgit v1.2.3 From a3b842fb8b7b5503b1c4fc49355edd4f2fe0d28d Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 21:19:32 +0000 Subject: Add tests and only traverse in the crates directory --- crates/ra_syntax/src/grammar/expressions.rs | 8 +- crates/ra_syntax/src/grammar/expressions/atom.rs | 1 + .../tests/data/parser/inline/0069_match_arm.rs | 1 + .../tests/data/parser/inline/0069_match_arm.txt | 194 +++++++++++++-------- .../data/parser/inline/0086_no_semi_after_block.rs | 4 + .../parser/inline/0086_no_semi_after_block.txt | 56 ++++-- crates/ra_syntax/tests/test.rs | 15 +- 7 files changed, 185 insertions(+), 94 deletions(-) diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 5f5a3077d..4f8c46ab3 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -73,6 +73,10 @@ pub(crate) fn block(p: &mut Parser) { // for _ in () {} // {} // {} + // macro_rules! test { + // () => {} + // } + // test!{} // } if is_blocklike { p.eat(SEMI); @@ -168,13 +172,13 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - Some((lhs, macro_blocklike)) => { + Some((lhs, blocklike)) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && macro_blocklike.is_block() { + if r.prefer_stmt && blocklike.is_block() { return BlockLike::Block; } lhs diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index a976799e7..cd7d62aff 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -353,6 +353,7 @@ pub(crate) fn match_arm_list(p: &mut Parser) { // fn foo() { // match () { // _ => (), +// _ if Test>{field: 0} => (), // X | Y if Z => (), // | X | Y if Z => (), // | X => (), diff --git a/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.rs b/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.rs index b23a12500..3380fa4ca 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.rs +++ b/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.rs @@ -1,6 +1,7 @@ fn foo() { match () { _ => (), + _ if Test>{field: 0} => (), X | Y if Z => (), | X | Y if Z => (), | X => (), diff --git a/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.txt b/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.txt index 044faec04..e5647765c 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.txt +++ b/crates/ra_syntax/tests/data/parser/inline/0069_match_arm.txt @@ -1,5 +1,5 @@ -SOURCE_FILE@[0; 125) - FN_DEF@[0; 124) +SOURCE_FILE@[0; 161) + FN_DEF@[0; 160) FN_KW@[0; 2) WHITESPACE@[2; 3) NAME@[3; 6) @@ -8,18 +8,18 @@ SOURCE_FILE@[0; 125) L_PAREN@[6; 7) R_PAREN@[7; 8) WHITESPACE@[8; 9) - BLOCK@[9; 124) + BLOCK@[9; 160) L_CURLY@[9; 10) WHITESPACE@[10; 15) - EXPR_STMT@[15; 122) - MATCH_EXPR@[15; 121) + EXPR_STMT@[15; 158) + MATCH_EXPR@[15; 157) MATCH_KW@[15; 20) WHITESPACE@[20; 21) TUPLE_EXPR@[21; 23) L_PAREN@[21; 22) R_PAREN@[22; 23) WHITESPACE@[23; 24) - MATCH_ARM_LIST@[24; 121) + MATCH_ARM_LIST@[24; 157) L_CURLY@[24; 25) WHITESPACE@[25; 34) MATCH_ARM@[34; 41) @@ -33,76 +33,116 @@ SOURCE_FILE@[0; 125) R_PAREN@[40; 41) COMMA@[41; 42) WHITESPACE@[42; 51) - MATCH_ARM@[51; 67) - BIND_PAT@[51; 52) - NAME@[51; 52) - IDENT@[51; 52) "X" + MATCH_ARM@[51; 77) + PLACEHOLDER_PAT@[51; 52) + UNDERSCORE@[51; 52) WHITESPACE@[52; 53) - PIPE@[53; 54) - WHITESPACE@[54; 55) - BIND_PAT@[55; 56) - NAME@[55; 56) - IDENT@[55; 56) "Y" - WHITESPACE@[56; 57) - IF_KW@[57; 59) - WHITESPACE@[59; 60) - PATH_EXPR@[60; 61) - PATH@[60; 61) - PATH_SEGMENT@[60; 61) - NAME_REF@[60; 61) - IDENT@[60; 61) "Z" - WHITESPACE@[61; 62) - FAT_ARROW@[62; 64) - WHITESPACE@[64; 65) - TUPLE_EXPR@[65; 67) - L_PAREN@[65; 66) - R_PAREN@[66; 67) - COMMA@[67; 68) - WHITESPACE@[68; 77) - MATCH_ARM@[77; 95) - PIPE@[77; 78) - WHITESPACE@[78; 79) - BIND_PAT@[79; 80) - NAME@[79; 80) - IDENT@[79; 80) "X" - WHITESPACE@[80; 81) - PIPE@[81; 82) - WHITESPACE@[82; 83) - BIND_PAT@[83; 84) - NAME@[83; 84) - IDENT@[83; 84) "Y" - WHITESPACE@[84; 85) - IF_KW@[85; 87) - WHITESPACE@[87; 88) - PATH_EXPR@[88; 89) - PATH@[88; 89) - PATH_SEGMENT@[88; 89) - NAME_REF@[88; 89) - IDENT@[88; 89) "Z" - WHITESPACE@[89; 90) - FAT_ARROW@[90; 92) + IF_KW@[53; 55) + WHITESPACE@[55; 56) + BIN_EXPR@[56; 71) + PATH_EXPR@[56; 60) + PATH@[56; 60) + PATH_SEGMENT@[56; 60) + NAME_REF@[56; 60) + IDENT@[56; 60) "Test" + R_ANGLE@[60; 61) + BLOCK_EXPR@[61; 71) + BLOCK@[61; 71) + L_CURLY@[61; 62) + EXPR_STMT@[62; 67) + PATH_EXPR@[62; 67) + PATH@[62; 67) + PATH_SEGMENT@[62; 67) + NAME_REF@[62; 67) + IDENT@[62; 67) "field" + err: `expected SEMI` + err: `expected expression` + EXPR_STMT@[67; 68) + ERROR@[67; 68) + COLON@[67; 68) + err: `expected SEMI` + WHITESPACE@[68; 69) + LITERAL@[69; 70) + INT_NUMBER@[69; 70) "0" + R_CURLY@[70; 71) + WHITESPACE@[71; 72) + FAT_ARROW@[72; 74) + WHITESPACE@[74; 75) + TUPLE_EXPR@[75; 77) + L_PAREN@[75; 76) + R_PAREN@[76; 77) + COMMA@[77; 78) + WHITESPACE@[78; 87) + MATCH_ARM@[87; 103) + BIND_PAT@[87; 88) + NAME@[87; 88) + IDENT@[87; 88) "X" + WHITESPACE@[88; 89) + PIPE@[89; 90) + WHITESPACE@[90; 91) + BIND_PAT@[91; 92) + NAME@[91; 92) + IDENT@[91; 92) "Y" WHITESPACE@[92; 93) - TUPLE_EXPR@[93; 95) - L_PAREN@[93; 94) - R_PAREN@[94; 95) - COMMA@[95; 96) - WHITESPACE@[96; 105) - MATCH_ARM@[105; 114) - PIPE@[105; 106) - WHITESPACE@[106; 107) - BIND_PAT@[107; 108) - NAME@[107; 108) - IDENT@[107; 108) "X" - WHITESPACE@[108; 109) - FAT_ARROW@[109; 111) - WHITESPACE@[111; 112) - TUPLE_EXPR@[112; 114) - L_PAREN@[112; 113) - R_PAREN@[113; 114) - COMMA@[114; 115) - WHITESPACE@[115; 120) - R_CURLY@[120; 121) - SEMI@[121; 122) - WHITESPACE@[122; 123) - R_CURLY@[123; 124) - WHITESPACE@[124; 125) + IF_KW@[93; 95) + WHITESPACE@[95; 96) + PATH_EXPR@[96; 97) + PATH@[96; 97) + PATH_SEGMENT@[96; 97) + NAME_REF@[96; 97) + IDENT@[96; 97) "Z" + WHITESPACE@[97; 98) + FAT_ARROW@[98; 100) + WHITESPACE@[100; 101) + TUPLE_EXPR@[101; 103) + L_PAREN@[101; 102) + R_PAREN@[102; 103) + COMMA@[103; 104) + WHITESPACE@[104; 113) + MATCH_ARM@[113; 131) + PIPE@[113; 114) + WHITESPACE@[114; 115) + BIND_PAT@[115; 116) + NAME@[115; 116) + IDENT@[115; 116) "X" + WHITESPACE@[116; 117) + PIPE@[117; 118) + WHITESPACE@[118; 119) + BIND_PAT@[119; 120) + NAME@[119; 120) + IDENT@[119; 120) "Y" + WHITESPACE@[120; 121) + IF_KW@[121; 123) + WHITESPACE@[123; 124) + PATH_EXPR@[124; 125) + PATH@[124; 125) + PATH_SEGMENT@[124; 125) + NAME_REF@[124; 125) + IDENT@[124; 125) "Z" + WHITESPACE@[125; 126) + FAT_ARROW@[126; 128) + WHITESPACE@[128; 129) + TUPLE_EXPR@[129; 131) + L_PAREN@[129; 130) + R_PAREN@[130; 131) + COMMA@[131; 132) + WHITESPACE@[132; 141) + MATCH_ARM@[141; 150) + PIPE@[141; 142) + WHITESPACE@[142; 143) + BIND_PAT@[143; 144) + NAME@[143; 144) + IDENT@[143; 144) "X" + WHITESPACE@[144; 145) + FAT_ARROW@[145; 147) + WHITESPACE@[147; 148) + TUPLE_EXPR@[148; 150) + L_PAREN@[148; 149) + R_PAREN@[149; 150) + COMMA@[150; 151) + WHITESPACE@[151; 156) + R_CURLY@[156; 157) + SEMI@[157; 158) + WHITESPACE@[158; 159) + R_CURLY@[159; 160) + WHITESPACE@[160; 161) diff --git a/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.rs b/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.rs index d769da43d..4919665cb 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.rs +++ b/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.rs @@ -6,4 +6,8 @@ fn foo() { for _ in () {} {} {} + macro_rules! test { + () => {} + } + test!{} } diff --git a/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.txt b/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.txt index 69f0a48ce..63b230091 100644 --- a/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.txt +++ b/crates/ra_syntax/tests/data/parser/inline/0086_no_semi_after_block.txt @@ -1,5 +1,5 @@ -SOURCE_FILE@[0; 107) - FN_DEF@[0; 106) +SOURCE_FILE@[0; 167) + FN_DEF@[0; 166) FN_KW@[0; 2) WHITESPACE@[2; 3) NAME@[3; 6) @@ -8,7 +8,7 @@ SOURCE_FILE@[0; 107) L_PAREN@[6; 7) R_PAREN@[7; 8) WHITESPACE@[8; 9) - BLOCK@[9; 106) + BLOCK@[9; 166) L_CURLY@[9; 10) WHITESPACE@[10; 15) EXPR_STMT@[15; 25) @@ -78,10 +78,46 @@ SOURCE_FILE@[0; 107) L_CURLY@[95; 96) R_CURLY@[96; 97) WHITESPACE@[97; 102) - BLOCK_EXPR@[102; 104) - BLOCK@[102; 104) - L_CURLY@[102; 103) - R_CURLY@[103; 104) - WHITESPACE@[104; 105) - R_CURLY@[105; 106) - WHITESPACE@[106; 107) + EXPR_STMT@[102; 104) + BLOCK_EXPR@[102; 104) + BLOCK@[102; 104) + L_CURLY@[102; 103) + R_CURLY@[103; 104) + WHITESPACE@[104; 109) + EXPR_STMT@[109; 152) + MACRO_CALL@[109; 152) + PATH@[109; 120) + PATH_SEGMENT@[109; 120) + NAME_REF@[109; 120) + IDENT@[109; 120) "macro_rules" + EXCL@[120; 121) + WHITESPACE@[121; 122) + IDENT@[122; 126) "test" + WHITESPACE@[126; 127) + TOKEN_TREE@[127; 152) + L_CURLY@[127; 128) + WHITESPACE@[128; 138) + TOKEN_TREE@[138; 140) + L_PAREN@[138; 139) + R_PAREN@[139; 140) + WHITESPACE@[140; 141) + FAT_ARROW@[141; 143) + WHITESPACE@[143; 144) + TOKEN_TREE@[144; 146) + L_CURLY@[144; 145) + R_CURLY@[145; 146) + WHITESPACE@[146; 151) + R_CURLY@[151; 152) + WHITESPACE@[152; 157) + MACRO_CALL@[157; 164) + PATH@[157; 161) + PATH_SEGMENT@[157; 161) + NAME_REF@[157; 161) + IDENT@[157; 161) "test" + EXCL@[161; 162) + TOKEN_TREE@[162; 164) + L_CURLY@[162; 163) + R_CURLY@[163; 164) + WHITESPACE@[164; 165) + R_CURLY@[165; 166) + WHITESPACE@[166; 167) diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index a07855768..c17b6ffa6 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -41,8 +41,10 @@ fn parser_fuzz_tests() { /// TODO: Use this as a benchmark #[test] fn self_hosting_parsing() { + use std::ffi::OsStr; let empty_vec = vec![]; - let dir = project_dir(); + let dir = project_dir().join("crates"); + let mut count = 0; for entry in walkdir::WalkDir::new(dir) .into_iter() .filter_entry(|entry| { @@ -52,17 +54,16 @@ fn self_hosting_parsing() { // TODO: this more neatly .any(|component| { // Get all files which are not in the crates/ra_syntax/tests/data folder - (component == Component::Normal(std::ffi::OsStr::new("data")) - // or the .git folder - || component == Component::Normal(std::ffi::OsStr::new(".git"))) + component == Component::Normal(OsStr::new("data")) }) }) .map(|e| e.unwrap()) .filter(|entry| { // Get all `.rs ` files - !entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs"))) + !entry.path().is_dir() && (entry.path().extension() == Some(OsStr::new("rs"))) }) { + count += 1; let text = read_text(entry.path()); let node = SourceFileNode::parse(&text); let errors = node.errors(); @@ -72,6 +73,10 @@ fn self_hosting_parsing() { entry ); } + assert!( + count > 30, + "self_hosting_parsing found too few files - is it running in the right directory?" + ) } /// Read file and normalize newlines. /// -- cgit v1.2.3