diff --git a/src/parser/event_parser/grammar.rs b/src/parser/event_parser/grammar.rs index e1f717714a..f26cbc4f66 100644 --- a/src/parser/event_parser/grammar.rs +++ b/src/parser/event_parser/grammar.rs @@ -6,7 +6,7 @@ use syntax_kinds::*; pub fn file(p: &mut Parser) { node(p, FILE, |p| { - shebang(p); + p.optional(SHEBANG); inner_attributes(p); many(p, |p| { skip_to_first( @@ -17,11 +17,6 @@ pub fn file(p: &mut Parser) { }) } - -fn shebang(_: &mut Parser) { - //TODO -} - fn inner_attributes(_: &mut Parser) { //TODO } @@ -136,4 +131,10 @@ impl<'p> Parser<'p> { false } } + + pub(crate) fn optional(&mut self, kind: SyntaxKind) { + if self.current_is(kind) { + self.bump(); + } + } } \ No newline at end of file diff --git a/tests/data/parser/err/0002_duplicate_shebang.rs b/tests/data/parser/err/0002_duplicate_shebang.rs new file mode 100644 index 0000000000..48a3a3980b --- /dev/null +++ b/tests/data/parser/err/0002_duplicate_shebang.rs @@ -0,0 +1,2 @@ +#!/use/bin/env rusti +#!/use/bin/env rusti diff --git a/tests/data/parser/err/0002_duplicate_shebang.txt b/tests/data/parser/err/0002_duplicate_shebang.txt new file mode 100644 index 0000000000..e7cf7187d0 --- /dev/null +++ b/tests/data/parser/err/0002_duplicate_shebang.txt @@ -0,0 +1,7 @@ +FILE@[0; 42) + SHEBANG@[0; 20) + ERROR@[20; 42) + err: `expected item` + WHITESPACE@[20; 21) + SHEBANG@[21; 41) + WHITESPACE@[41; 42) diff --git a/tests/data/parser/ok/0004_file_shebang.rs b/tests/data/parser/ok/0004_file_shebang.rs new file mode 100644 index 0000000000..53dc9e6173 --- /dev/null +++ b/tests/data/parser/ok/0004_file_shebang.rs @@ -0,0 +1 @@ +#!/use/bin/env rusti \ No newline at end of file diff --git a/tests/data/parser/ok/0004_file_shebang.txt b/tests/data/parser/ok/0004_file_shebang.txt new file mode 100644 index 0000000000..33055cf3d2 --- /dev/null +++ b/tests/data/parser/ok/0004_file_shebang.txt @@ -0,0 +1,2 @@ +FILE@[0; 20) + SHEBANG@[0; 20) diff --git a/tests/lexer.rs b/tests/lexer.rs index 6c7531596e..20840f4560 100644 --- a/tests/lexer.rs +++ b/tests/lexer.rs @@ -2,30 +2,20 @@ extern crate file; extern crate libsyntax2; extern crate testutils; -use std::path::{Path}; use std::fmt::Write; use libsyntax2::{Token, tokenize}; -use testutils::{assert_equal_text, collect_tests}; +use testutils::dir_tests; #[test] fn lexer_tests() { - for test_case in collect_tests(&["lexer"]) { - lexer_test_case(&test_case); - } -} - -fn lexer_test_case(path: &Path) { - let actual = { - let text = file::get_text(path).unwrap(); - let tokens = tokenize(&text); - dump_tokens(&tokens, &text) - }; - let path = path.with_extension("txt"); - let expected = file::get_text(&path).unwrap(); - let expected = expected.as_str(); - let actual = actual.as_str(); - assert_equal_text(expected, actual, &path) + dir_tests( + &["lexer"], + |text| { + let tokens = tokenize(text); + dump_tokens(&tokens, text) + } + ) } fn dump_tokens(tokens: &[Token], text: &str) -> String { diff --git a/tests/parser.rs b/tests/parser.rs index 518852bb2f..02cef938d3 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -2,33 +2,21 @@ extern crate file; extern crate libsyntax2; extern crate testutils; -use std::path::{Path}; use std::fmt::Write; use libsyntax2::{tokenize, parse, Node, File}; -use testutils::{collect_tests, assert_equal_text}; +use testutils::dir_tests; #[test] fn parser_tests() { - for test_case in collect_tests(&["parser/ok", "parser/err"]) { - parser_test_case(&test_case); - } -} - -fn parser_test_case(path: &Path) { - let actual = { - let text = file::get_text(path).unwrap(); - let tokens = tokenize(&text); - let file = parse(text, &tokens); - dump_tree(&file) - }; - let expected_path = path.with_extension("txt"); - let expected = file::get_text(&expected_path).expect( - &format!("Can't read {}", expected_path.display()) - ); - let expected = expected.as_str(); - let actual = actual.as_str(); - assert_equal_text(expected, actual, &expected_path); + dir_tests( + &["parser/ok", "parser/err"], + |text| { + let tokens = tokenize(text); + let file = parse(text.to_string(), &tokens); + dump_tree(&file) + } + ) } fn dump_tree(file: &File) -> String { diff --git a/tests/testutils/src/lib.rs b/tests/testutils/src/lib.rs index 9fc85cc24c..b34517c5f7 100644 --- a/tests/testutils/src/lib.rs +++ b/tests/testutils/src/lib.rs @@ -6,7 +6,33 @@ use std::fs::read_dir; use difference::Changeset; -pub fn assert_equal_text( +pub fn dir_tests( + paths: &[&str], + f: F +) +where + F: Fn(&str) -> String +{ + for path in collect_tests(paths) { + let actual = { + let text = file::get_text(&path).unwrap(); + f(&text) + }; + let path = path.with_extension("txt"); + if !path.exists() { + println!("\nfile: {}", path.display()); + println!("No .txt file with expected result, creating..."); + file::put_text(&path, actual).unwrap(); + panic!("No expected result") + } + let expected = file::get_text(&path).unwrap(); + let expected = expected.as_str(); + let actual = actual.as_str(); + assert_equal_text(expected, actual, &path); + } +} + +fn assert_equal_text( expected: &str, actual: &str, path: &Path @@ -16,7 +42,7 @@ pub fn assert_equal_text( } } -pub fn collect_tests(paths: &[&str]) -> Vec { +fn collect_tests(paths: &[&str]) -> Vec { paths.iter().flat_map(|path| { let path = test_data_dir().join(path); test_from_dir(&path).into_iter()