-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove named parsers from codegen (#529)
Based on recent CST updates, we promoted named structures to top-level productions, thus making them redundant. This PR removes them, along with a lot of special cases to handle them in other parts of the codebase.
- Loading branch information
1 parent
7ccca87
commit f77330a
Showing
10 changed files
with
137 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
crates/codegen/schema/src/validation/rules/definitions/operators/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
types::{LanguageDefinitionRef, PrecedenceParserRef, ProductionRef}, | ||
validation::visitors::{run_visitor, LocationRef, Reporter, Visitor}, | ||
}; | ||
|
||
pub struct Operators { | ||
language: LanguageDefinitionRef, | ||
current_production: Option<ProductionRef>, | ||
operators_already_seen: HashMap<String, ProductionRef>, | ||
} | ||
|
||
impl Operators { | ||
pub fn validate(language: &LanguageDefinitionRef, reporter: &mut Reporter) { | ||
let mut instance = Self { | ||
language: language.to_owned(), | ||
|
||
current_production: None, | ||
operators_already_seen: HashMap::new(), | ||
}; | ||
|
||
run_visitor(&mut instance, language, reporter); | ||
} | ||
} | ||
|
||
impl Visitor for Operators { | ||
fn visit_production( | ||
&mut self, | ||
production: &ProductionRef, | ||
_location: &LocationRef, | ||
_reporter: &mut Reporter, | ||
) -> bool { | ||
self.current_production = Some(production.to_owned()); | ||
return true; | ||
} | ||
|
||
fn visit_parser( | ||
&mut self, | ||
_parser: &crate::types::ParserRef, | ||
_location: &LocationRef, | ||
_reporter: &mut Reporter, | ||
) -> bool { | ||
return false; // skip | ||
} | ||
|
||
fn visit_scanner( | ||
&mut self, | ||
_scanner: &crate::types::ScannerRef, | ||
_location: &LocationRef, | ||
_reporter: &mut Reporter, | ||
) -> bool { | ||
return false; // skip | ||
} | ||
|
||
fn visit_precedence_parser( | ||
&mut self, | ||
parser: &PrecedenceParserRef, | ||
location: &LocationRef, | ||
reporter: &mut Reporter, | ||
) -> bool { | ||
for operator in parser.operators.iter() { | ||
let name = &operator.name; | ||
if self.language.productions.contains_key(name) { | ||
reporter.report(location, Errors::OperatorNamedAsProduction(name.to_owned())); | ||
continue; | ||
} | ||
|
||
let current_production = self.current_production.as_ref().unwrap(); | ||
|
||
match self.operators_already_seen.get(name) { | ||
Some(existing_production) => { | ||
if current_production.name == existing_production.name { | ||
// Operators can share a common name under the same production. | ||
continue; | ||
} | ||
|
||
reporter.report( | ||
location, | ||
Errors::OperatorDefinedInAnotherProduction( | ||
name.to_owned(), | ||
existing_production.name.to_owned(), | ||
), | ||
); | ||
} | ||
None => { | ||
self.operators_already_seen | ||
.insert(name.to_owned(), current_production.to_owned()); | ||
} | ||
}; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
enum Errors { | ||
#[error("Operator '{0}' cannot have the same name as a top-level production.")] | ||
OperatorNamedAsProduction(String), | ||
#[error("Operator '{0}' is defined in another production '{1}'.")] | ||
OperatorDefinedInAnotherProduction(String, String), | ||
} |
142 changes: 0 additions & 142 deletions
142
crates/codegen/schema/src/validation/rules/definitions/parsers/mod.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.