mirror of
https://github.com/RustPython/Parser.git
synced 2025-09-01 08:07:50 +00:00
Implement suggestion from clippy.
This commit is contained in:
parent
de2e496e1f
commit
5dc63b9e8e
1 changed files with 14 additions and 10 deletions
|
@ -13,7 +13,7 @@ use rustpython_parser::location::Location;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolScope, SymbolTableError> {
|
pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolScope, SymbolTableError> {
|
||||||
let mut builder = SymbolTableBuilder::new();
|
let mut builder: SymbolTableBuilder = Default::default();
|
||||||
builder.enter_scope();
|
builder.enter_scope();
|
||||||
builder.scan_program(program)?;
|
builder.scan_program(program)?;
|
||||||
assert_eq!(builder.scopes.len(), 1);
|
assert_eq!(builder.scopes.len(), 1);
|
||||||
|
@ -26,7 +26,7 @@ pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolScope, SymbolTa
|
||||||
pub fn statements_to_symbol_table(
|
pub fn statements_to_symbol_table(
|
||||||
statements: &[ast::LocatedStatement],
|
statements: &[ast::LocatedStatement],
|
||||||
) -> Result<SymbolScope, SymbolTableError> {
|
) -> Result<SymbolScope, SymbolTableError> {
|
||||||
let mut builder = SymbolTableBuilder::new();
|
let mut builder: SymbolTableBuilder = Default::default();
|
||||||
builder.enter_scope();
|
builder.enter_scope();
|
||||||
builder.scan_statements(statements)?;
|
builder.scan_statements(statements)?;
|
||||||
assert_eq!(builder.scopes.len(), 1);
|
assert_eq!(builder.scopes.len(), 1);
|
||||||
|
@ -73,16 +73,18 @@ impl From<SymbolTableError> for CompileError {
|
||||||
type SymbolTableResult = Result<(), SymbolTableError>;
|
type SymbolTableResult = Result<(), SymbolTableError>;
|
||||||
|
|
||||||
impl SymbolScope {
|
impl SymbolScope {
|
||||||
pub fn new() -> Self {
|
pub fn lookup(&self, name: &str) -> Option<&SymbolRole> {
|
||||||
|
self.symbols.get(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for SymbolScope {
|
||||||
|
fn default() -> Self {
|
||||||
SymbolScope {
|
SymbolScope {
|
||||||
symbols: HashMap::new(),
|
symbols: HashMap::new(),
|
||||||
sub_scopes: vec![],
|
sub_scopes: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(&self, name: &str) -> Option<&SymbolRole> {
|
|
||||||
self.symbols.get(name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for SymbolScope {
|
impl std::fmt::Debug for SymbolScope {
|
||||||
|
@ -153,13 +155,15 @@ pub struct SymbolTableBuilder {
|
||||||
pub scopes: Vec<SymbolScope>,
|
pub scopes: Vec<SymbolScope>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolTableBuilder {
|
impl Default for SymbolTableBuilder {
|
||||||
pub fn new() -> Self {
|
fn default() -> Self {
|
||||||
SymbolTableBuilder { scopes: vec![] }
|
SymbolTableBuilder { scopes: vec![] }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SymbolTableBuilder {
|
||||||
pub fn enter_scope(&mut self) {
|
pub fn enter_scope(&mut self) {
|
||||||
let scope = SymbolScope::new();
|
let scope = Default::default();
|
||||||
self.scopes.push(scope);
|
self.scopes.push(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue