Skip to content

Commit

Permalink
rename Grammar to Schema (#476)
Browse files Browse the repository at this point in the history
as it contains not only grammar, but hand authored notes, versions, with
more metadata coming in the future.
  • Loading branch information
OmarTawfik authored May 24, 2023
1 parent 0cdfe86 commit c803a68
Show file tree
Hide file tree
Showing 135 changed files with 204 additions and 195 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"**/Pipfile.lock": true
},
"triggerTaskOnSave.tasks": {
"Validate Solidity Grammar": ["crates/solidity/inputs/schema/grammar/**/*.yml"]
"Validate Solidity Schema": ["crates/solidity/inputs/schema/definition/**/*.yml"]
},
"typescript.tsdk": "node_modules/typescript/lib",
"yaml.validate": false // Disable LSP validation for YAML files, as it is handled by our own cargo tasks
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"version": "2.0.0",
"tasks": [
{
"label": "Validate Solidity Grammar",
"label": "Validate Solidity Schema",
"type": "shell",
"command": "scripts/solidity/check-grammar.sh",
"command": "scripts/solidity/validate-schema.sh",
"options": {
"env": {
"VSCODE_PROBLEM_MATCHER": "true"
Expand Down
18 changes: 9 additions & 9 deletions crates/codegen/ebnf/src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::VecDeque, mem::discriminant};

use codegen_schema::types::{
grammar::Grammar,
production::{Production, ProductionRef},
schema::Schema,
};
use semver::Version;

Expand All @@ -12,18 +12,18 @@ pub trait GenerateEbnf {
fn generate_ebnf(&self) -> EbnfNode;
}

pub struct EbnfSerializer<'grammar> {
grammar: &'grammar Grammar,
base_production: &'grammar str,
pub struct EbnfSerializer<'schema> {
schema: &'schema Schema,
base_production: &'schema str,

buffer: String,
queue: VecDeque<(String, Option<String>, EbnfNode)>,
}

impl<'grammar> EbnfSerializer<'grammar> {
impl<'schema> EbnfSerializer<'schema> {
pub fn serialize_version(
grammar: &'grammar Grammar,
production: &'grammar ProductionRef,
schema: &'schema Schema,
production: &'schema ProductionRef,
version: &Version,
) -> Option<String> {
let body = match production.as_ref() {
Expand All @@ -42,7 +42,7 @@ impl<'grammar> EbnfSerializer<'grammar> {
};

let mut instance = Self {
grammar,
schema,
buffer: String::new(),
base_production: production.name(),
queue: VecDeque::new(),
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'grammar> EbnfSerializer<'grammar> {
}

fn display_name(&self, name: &String) -> String {
if let Some(production) = self.grammar.productions.get(name) {
if let Some(production) = self.schema.productions.get(name) {
if matches!(production.as_ref(), Production::Scanner { .. }) {
return format!("«{name}»");
}
Expand Down
30 changes: 15 additions & 15 deletions crates/codegen/schema/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ use codegen_utils::{

use crate::{
types::{
grammar::{Grammar, GrammarSection, GrammarTopic},
manifest::{ManifestFile, ManifestSection, ProductionsFile},
schema::{Schema, SchemaSection, SchemaTopic},
},
validation::Model,
yaml,
};

impl Grammar {
pub fn compile(codegen: &mut CodegenContext, manifest_dir: PathBuf) -> CodegenResult<Grammar> {
let manifest_path = manifest_dir.join("manifest.yml");
impl Schema {
pub fn compile(codegen: &mut CodegenContext, schema_dir: PathBuf) -> CodegenResult<Schema> {
let manifest_path = schema_dir.join("manifest.yml");
let manifest_file = yaml::files::File::<ManifestFile>::load(codegen, manifest_path)?;

let mut model = Model::new(&manifest_file)?;

let sections = load_sections(
codegen,
&mut model,
&manifest_dir,
&schema_dir,
&manifest_file.value.sections,
)?;

Expand All @@ -37,12 +37,12 @@ impl Grammar {
.map(|(name, production)| (name.to_owned(), production.clone()))
.collect();

return Ok(Grammar {
return Ok(Schema {
title: manifest_file.value.title,
versions: manifest_file.value.versions,
sections,

manifest_dir,
schema_dir,
productions,
});
}
Expand All @@ -51,26 +51,26 @@ impl Grammar {
fn load_sections(
codegen: &mut CodegenContext,
model: &mut Model,
grammar_dir: &PathBuf,
schema_dir: &PathBuf,
sections: &Vec<ManifestSection>,
) -> CodegenResult<Vec<GrammarSection>> {
let mut results = Vec::<GrammarSection>::new();
) -> CodegenResult<Vec<SchemaSection>> {
let mut results = Vec::<SchemaSection>::new();
let mut errors = CodegenErrors::new();

for section in sections {
let mut topics = Vec::<GrammarTopic>::new();
let mut topics = Vec::<SchemaTopic>::new();

for topic in &section.topics {
let productions_path = grammar_dir
let productions_path = schema_dir
.join(&section.path)
.join(&topic.path)
.join(GrammarTopic::productions_file());
.join(SchemaTopic::productions_file());

match yaml::files::File::<ProductionsFile>::load(codegen, productions_path) {
Ok(productions_file) => {
model.add_productions_file(&productions_file);

topics.push(GrammarTopic {
topics.push(SchemaTopic {
title: topic.title.to_owned(),
path: topic.path.to_owned(),
productions: productions_file
Expand All @@ -86,7 +86,7 @@ fn load_sections(
};
}

results.push(GrammarSection {
results.push(SchemaSection {
title: section.title.to_owned(),
path: section.path.to_owned(),
topics,
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/schema/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod manifest; // internal, used for serialization/validation only

pub mod grammar;
pub mod parser;
pub mod precedence_parser;
pub mod production;
pub mod scanner;
pub mod schema;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, path::PathBuf};
use std::path::PathBuf;

use indexmap::IndexMap;
use semver::Version;
Expand All @@ -8,32 +8,32 @@ use super::production::ProductionRef;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Grammar {
pub struct Schema {
pub title: String,
pub sections: Vec<GrammarSection>,
pub sections: Vec<SchemaSection>,
pub versions: Vec<Version>,

pub manifest_dir: PathBuf,
pub productions: HashMap<String, ProductionRef>,
pub schema_dir: PathBuf,
pub productions: IndexMap<String, ProductionRef>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct GrammarSection {
pub struct SchemaSection {
pub title: String,
pub path: String,
pub topics: Vec<GrammarTopic>,
pub topics: Vec<SchemaTopic>,
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct GrammarTopic {
pub struct SchemaTopic {
pub title: String,
pub path: String,
pub productions: IndexMap<String, ProductionRef>,
}

impl GrammarTopic {
impl SchemaTopic {
// TODO(OmarTawfik): This method is definetely used.
// Need to isolate and report the bug to the rustc team.
#[allow(dead_code)]
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/schema/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::BTreeSet;

use semver::Version;

use crate::types::grammar::Grammar;
use crate::types::schema::Schema;

impl Grammar {
impl Schema {
pub fn collect_version_breaks<'a>(&'a self) -> BTreeSet<Version> {
let mut version_breaks = BTreeSet::new();
version_breaks.insert(self.versions.first().cloned().unwrap());
Expand Down
12 changes: 6 additions & 6 deletions crates/codegen/schema/src/validation/rules/manifest_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use codegen_utils::errors::CodegenErrors;

use crate::{
types::grammar::GrammarTopic,
types::schema::SchemaTopic,
validation::{
ast::{
files::ManifestFile,
Expand All @@ -20,13 +20,13 @@ pub fn check(model: &Model, errors: &mut CodegenErrors) {
}

struct PathsChecker {
grammar_dir: PathBuf,
schema_dir: PathBuf,
}

impl PathsChecker {
fn new(model: &Model) -> Self {
return Self {
grammar_dir: model.manifest_file.path.parent().unwrap().to_owned(),
schema_dir: model.manifest_file.path.parent().unwrap().to_owned(),
};
}
}
Expand All @@ -38,7 +38,7 @@ impl Visitor for PathsChecker {
reporter: &mut Reporter,
) -> VisitorResponse {
for section in &manifest_file.ast.value.sections {
let section_path = self.grammar_dir.join(&section.path.value);
let section_path = self.schema_dir.join(&section.path.value);

if !section_path.exists() {
reporter.report(&section.path.cst_node, Errors::PathNotFound(section_path));
Expand All @@ -54,8 +54,8 @@ impl Visitor for PathsChecker {
}

for path in [
topic_path.join(GrammarTopic::productions_file()),
topic_path.join(GrammarTopic::notes_file()),
topic_path.join(SchemaTopic::productions_file()),
topic_path.join(SchemaTopic::notes_file()),
] {
if !path.exists() {
reporter.report(&topic.path.cst_node, Errors::PathNotFound(path));
Expand Down
20 changes: 10 additions & 10 deletions crates/codegen/spec/src/grammar.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::path::PathBuf;

use codegen_schema::types::grammar::Grammar;
use codegen_schema::types::schema::Schema;
use semver::Version;

use crate::{markdown::MarkdownWriter, navigation::NavigationEntry, snippets::Snippets};

pub fn generate_supported_versions_page(grammar: &Grammar) -> NavigationEntry {
let versions = &grammar.versions;
let version_breaks = grammar.collect_version_breaks();
pub fn generate_supported_versions_page(schema: &Schema) -> NavigationEntry {
let versions = &schema.versions;
let version_breaks = schema.collect_version_breaks();

let mut page = MarkdownWriter::new();

page.write_header(1, "Supported Versions");

page.write_newline();
page.write_text(&format!("This specification compiles information from {all_count} publicly released versions of {language}. Among which, {breaks_count} versions had syntax-related changes:", all_count = versions.len(), language = grammar.title, breaks_count = version_breaks.len()));
page.write_text(&format!("This specification compiles information from {all_count} publicly released versions of {language}. Among which, {breaks_count} versions had syntax-related changes:", all_count = versions.len(), language = schema.title, breaks_count = version_breaks.len()));

page.write_newline();
for version in version_breaks {
Expand All @@ -37,17 +37,17 @@ pub fn generate_supported_versions_page(grammar: &Grammar) -> NavigationEntry {
}

pub fn generate_grammar_dir(
grammar: &Grammar,
schema: &Schema,
snippets: &Snippets,
repo_root: &PathBuf,
) -> NavigationEntry {
let mut pages = Vec::<NavigationEntry>::new();

for version in grammar.collect_version_breaks() {
for version in schema.collect_version_breaks() {
pages.push(NavigationEntry::Page {
title: format!("v{version}"),
path: format!("v{version}"),
contents: generate_grammar_page(grammar, snippets, repo_root, &version),
contents: generate_grammar_page(schema, snippets, repo_root, &version),
});
}

Expand All @@ -59,7 +59,7 @@ pub fn generate_grammar_dir(
}

fn generate_grammar_page(
grammar: &Grammar,
schema: &Schema,
snippets: &Snippets,
repo_root: &PathBuf,
version: &Version,
Expand All @@ -68,7 +68,7 @@ fn generate_grammar_page(

page.write_header(1, "Grammar");

for section in &grammar.sections {
for section in &schema.sections {
page.write_newline();
page.write_header(2, &section.title);

Expand Down
6 changes: 3 additions & 3 deletions crates/codegen/spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod snippets;
use std::path::PathBuf;

use anyhow::Result;
use codegen_schema::types::grammar::Grammar;
use codegen_schema::types::schema::Schema;
use codegen_utils::context::CodegenContext;

use crate::{
Expand All @@ -17,11 +17,11 @@ use crate::{
snippets::Snippets,
};

pub trait GrammarSpecGeneratorExtensions {
pub trait SpecGeneratorExtensions {
fn generate_spec(&self, codegen: &mut CodegenContext, output_dir: &PathBuf) -> Result<()>;
}

impl GrammarSpecGeneratorExtensions for Grammar {
impl SpecGeneratorExtensions for Schema {
fn generate_spec(&self, codegen: &mut CodegenContext, output_dir: &PathBuf) -> Result<()> {
let snippets = Snippets::new(self, output_dir);
snippets.write_files(codegen)?;
Expand Down
Loading

0 comments on commit c803a68

Please sign in to comment.