Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 7, 2019
1 parent 3818f8b commit 6f0f2fc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
11 changes: 9 additions & 2 deletions src/libsyntax/parse/mod.rs
Expand Up @@ -6,6 +6,7 @@ use crate::source_map::{SourceMap, FilePathMapping};
use crate::feature_gate::UnstableFeatures;
use crate::parse::parser::Parser;
use crate::symbol::Symbol;
use crate::syntax::parse::parser::emit_unclosed_delims;
use crate::tokenstream::{TokenStream, TokenTree};
use crate::diagnostics::plugin::ErrorMap;
use crate::print::pprust::token_to_string;
Expand Down Expand Up @@ -141,8 +142,14 @@ pub fn parse_stream_from_source_str(
source: String,
sess: &ParseSess,
override_span: Option<Span>,
) -> (TokenStream, Vec<lexer::UnmatchedBrace>) {
source_file_to_stream(sess, sess.source_map().new_source_file(name, source), override_span)
) -> TokenStream {
let (stream, mut errors) = source_file_to_stream(
sess,
sess.source_map().new_source_file(name, source),
override_span,
);
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
stream
}

/// Creates a new parser from a source string.
Expand Down
6 changes: 2 additions & 4 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -261,10 +261,8 @@ pub struct Parser<'a> {

impl<'a> Drop for Parser<'a> {
fn drop(&mut self) {
if !self.unclosed_delims.is_empty() {
let diag = self.diagnostic();
emit_unclosed_delims(&mut self.unclosed_delims, diag);
}
let diag = self.diagnostic();
emit_unclosed_delims(&mut self.unclosed_delims, diag);
}
}

Expand Down
21 changes: 3 additions & 18 deletions src/libsyntax/parse/token.rs
Expand Up @@ -10,7 +10,6 @@ use crate::print::pprust;
use crate::ptr::P;
use crate::symbol::keywords;
use crate::syntax::parse::parse_stream_from_source_str;
use crate::syntax::parse::parser::emit_unclosed_delims;
use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};

use syntax_pos::symbol::{self, Symbol};
Expand Down Expand Up @@ -675,9 +674,7 @@ impl Nonterminal {
// FIXME(#43081): Avoid this pretty-print + reparse hack
let source = pprust::nonterminal_to_string(self);
let filename = FileName::macro_expansion_source_code(&source);
let (tokens_for_real, mut errors) =
parse_stream_from_source_str(filename, source, sess, Some(span));
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
let tokens_for_real = parse_stream_from_source_str(filename, source, sess, Some(span));

// During early phases of the compiler the AST could get modified
// directly (e.g., attributes added or removed) and the internal cache
Expand Down Expand Up @@ -740,13 +737,7 @@ fn prepend_attrs(sess: &ParseSess,
let source = pprust::attr_to_string(attr);
let macro_filename = FileName::macro_expansion_source_code(&source);
if attr.is_sugared_doc {
let (stream, mut errors) = parse_stream_from_source_str(
macro_filename,
source,
sess,
Some(span),
);
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span));
builder.push(stream);
continue
}
Expand All @@ -763,13 +754,7 @@ fn prepend_attrs(sess: &ParseSess,
// ... and for more complicated paths, fall back to a reparse hack that
// should eventually be removed.
} else {
let (stream, mut errors) = parse_stream_from_source_str(
macro_filename,
source,
sess,
Some(span),
);
emit_unclosed_delims(&mut errors, &sess.span_diagnostic);
let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span));
brackets.push(stream);
}

Expand Down
7 changes: 2 additions & 5 deletions src/libsyntax_ext/proc_macro_server.rs
Expand Up @@ -12,7 +12,6 @@ use syntax::ast;
use syntax::ext::base::ExtCtxt;
use syntax::parse::lexer::comments;
use syntax::parse::{self, token, ParseSess};
use syntax::parse::parser::emit_unclosed_delims;
use syntax::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
use syntax_pos::hygiene::{SyntaxContext, Transparency};
use syntax_pos::symbol::{keywords, Symbol};
Expand Down Expand Up @@ -410,14 +409,12 @@ impl server::TokenStream for Rustc<'_> {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
let (tokens, mut errors) = parse::parse_stream_from_source_str(
parse::parse_stream_from_source_str(
FileName::proc_macro_source_code(src.clone()),
src.to_string(),
self.sess,
Some(self.call_site),
);
emit_unclosed_delims(&mut errors, &self.sess.span_diagnostic);
tokens
)
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
Expand Down

0 comments on commit 6f0f2fc

Please sign in to comment.