mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-20 19:45:21 +00:00
Fix clippy warnings. Add proper names to symboltables.
This commit is contained in:
parent
99b3532cae
commit
9b1771b483
1 changed files with 17 additions and 12 deletions
|
@ -31,6 +31,9 @@ pub fn statements_to_symbol_table(
|
||||||
/// Captures all symbols in the current scope, and has a list of subscopes in this scope.
|
/// Captures all symbols in the current scope, and has a list of subscopes in this scope.
|
||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct SymbolTable {
|
pub struct SymbolTable {
|
||||||
|
/// The name of this symbol table. Often the name of the class or function.
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
/// A set of symbols present on this scope level.
|
/// A set of symbols present on this scope level.
|
||||||
pub symbols: IndexMap<String, Symbol>,
|
pub symbols: IndexMap<String, Symbol>,
|
||||||
|
|
||||||
|
@ -40,8 +43,9 @@ pub struct SymbolTable {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolTable {
|
impl SymbolTable {
|
||||||
fn new() -> Self {
|
fn new(name: String) -> Self {
|
||||||
SymbolTable {
|
SymbolTable {
|
||||||
|
name,
|
||||||
symbols: Default::default(),
|
symbols: Default::default(),
|
||||||
sub_tables: vec![],
|
sub_tables: vec![],
|
||||||
}
|
}
|
||||||
|
@ -211,18 +215,20 @@ impl SymbolTableAnalyzer {
|
||||||
} else {
|
} else {
|
||||||
// TODO: comment this out and make it work properly:
|
// TODO: comment this out and make it work properly:
|
||||||
/*
|
/*
|
||||||
|
*/
|
||||||
let found_in_outer_scope = self
|
let found_in_outer_scope = self
|
||||||
.tables
|
.tables
|
||||||
.iter()
|
.iter()
|
||||||
|
.skip(1)
|
||||||
.any(|t| t.symbols.contains_key(&symbol.name));
|
.any(|t| t.symbols.contains_key(&symbol.name));
|
||||||
|
|
||||||
if found_in_outer_scope {
|
if found_in_outer_scope {
|
||||||
// Symbol is in some outer scope.
|
// Symbol is in some outer scope.
|
||||||
|
symbol.is_free = true;
|
||||||
} else {
|
} else {
|
||||||
// Well, it must be a global then :)
|
// Well, it must be a global then :)
|
||||||
// symbol.scope = SymbolScope::Global;
|
// symbol.scope = SymbolScope::Global;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -257,8 +263,7 @@ enum ExpressionContext {
|
||||||
|
|
||||||
impl SymbolTableBuilder {
|
impl SymbolTableBuilder {
|
||||||
fn prepare(&mut self) {
|
fn prepare(&mut self) {
|
||||||
let table = SymbolTable::new();
|
self.enter_block("top")
|
||||||
self.tables.push(table);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(&mut self) -> Result<SymbolTable, SymbolTableError> {
|
fn finish(&mut self) -> Result<SymbolTable, SymbolTableError> {
|
||||||
|
@ -268,9 +273,9 @@ impl SymbolTableBuilder {
|
||||||
Ok(symbol_table)
|
Ok(symbol_table)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_block(&mut self) {
|
fn enter_block(&mut self, name: &str) {
|
||||||
// let parent = Some(self.tables.last().unwrap().clone());
|
// let parent = Some(self.tables.last().unwrap().clone());
|
||||||
let table = SymbolTable::new();
|
let table = SymbolTable::new(name.to_string());
|
||||||
self.tables.push(table);
|
self.tables.push(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,7 +348,7 @@ impl SymbolTableBuilder {
|
||||||
if let Some(expression) = returns {
|
if let Some(expression) = returns {
|
||||||
self.scan_expression(expression, &ExpressionContext::Load)?;
|
self.scan_expression(expression, &ExpressionContext::Load)?;
|
||||||
}
|
}
|
||||||
self.enter_function(args)?;
|
self.enter_function(name, args)?;
|
||||||
self.scan_statements(body)?;
|
self.scan_statements(body)?;
|
||||||
self.leave_block();
|
self.leave_block();
|
||||||
}
|
}
|
||||||
|
@ -355,7 +360,7 @@ impl SymbolTableBuilder {
|
||||||
decorator_list,
|
decorator_list,
|
||||||
} => {
|
} => {
|
||||||
self.register_name(name, SymbolUsage::Assigned)?;
|
self.register_name(name, SymbolUsage::Assigned)?;
|
||||||
self.enter_block();
|
self.enter_block(name);
|
||||||
self.scan_statements(body)?;
|
self.scan_statements(body)?;
|
||||||
self.leave_block();
|
self.leave_block();
|
||||||
self.scan_expressions(bases, &ExpressionContext::Load)?;
|
self.scan_expressions(bases, &ExpressionContext::Load)?;
|
||||||
|
@ -607,7 +612,7 @@ impl SymbolTableBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Lambda { args, body } => {
|
Lambda { args, body } => {
|
||||||
self.enter_function(args)?;
|
self.enter_function("lambda", args)?;
|
||||||
self.scan_expression(body, &ExpressionContext::Load)?;
|
self.scan_expression(body, &ExpressionContext::Load)?;
|
||||||
self.leave_block();
|
self.leave_block();
|
||||||
}
|
}
|
||||||
|
@ -620,7 +625,7 @@ impl SymbolTableBuilder {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_function(&mut self, args: &ast::Parameters) -> SymbolTableResult {
|
fn enter_function(&mut self, name: &str, args: &ast::Parameters) -> SymbolTableResult {
|
||||||
// Evaluate eventual default parameters:
|
// Evaluate eventual default parameters:
|
||||||
self.scan_expressions(&args.defaults, &ExpressionContext::Load)?;
|
self.scan_expressions(&args.defaults, &ExpressionContext::Load)?;
|
||||||
for kw_default in &args.kw_defaults {
|
for kw_default in &args.kw_defaults {
|
||||||
|
@ -639,7 +644,7 @@ impl SymbolTableBuilder {
|
||||||
self.scan_parameter_annotation(name)?;
|
self.scan_parameter_annotation(name)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.enter_block();
|
self.enter_block(name);
|
||||||
|
|
||||||
// Fill scope with parameter names:
|
// Fill scope with parameter names:
|
||||||
self.scan_parameters(&args.args)?;
|
self.scan_parameters(&args.args)?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue