Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add include declaration #670

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,23 @@ pub enum AnalyzerError {
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(unknown_include_way),
help(""),
url(
"https://doc.veryl-lang.org/book/06_appendix/02_semantic_error.html#unknown_include_way"
)
)]
#[error("\"{name}\" is not valid include way")]
UnknownIncludeWay {
name: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(unknown_member),
Expand Down Expand Up @@ -596,6 +613,22 @@ pub enum AnalyzerError {
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(include_failure),
help(""),
url("https://doc.veryl-lang.org/book/06_appendix/02_semantic_error.html#include_failure")
)]
#[error("\"{name}\" can't be read because \"{cause}\"")]
IncludeFailure {
name: String,
cause: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},
}

impl AnalyzerError {
Expand Down Expand Up @@ -890,6 +923,14 @@ impl AnalyzerError {
}
}

pub fn unknown_include_way(name: &str, source: &str, token: &TokenRange) -> Self {
AnalyzerError::UnknownIncludeWay {
name: name.to_string(),
input: AnalyzerError::named_source(source, token),
error_location: token.into(),
}
}

pub fn unknown_member(name: &str, member: &str, source: &str, token: &TokenRange) -> Self {
AnalyzerError::UnknownMember {
name: name.to_string(),
Expand Down Expand Up @@ -977,4 +1018,13 @@ impl AnalyzerError {
error_location: token.into(),
}
}

pub fn include_failure(name: &str, cause: &str, source: &str, token: &TokenRange) -> Self {
AnalyzerError::IncludeFailure {
name: name.to_string(),
cause: cause.to_string(),
input: AnalyzerError::named_source(source, token),
error_location: token.into(),
}
}
}
12 changes: 6 additions & 6 deletions crates/analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod check_assignment;
pub mod check_attribute;
pub mod check_direction;
pub mod check_embed;
pub mod check_embed_include;
pub mod check_enum;
pub mod check_function;
pub mod check_identifier;
Expand All @@ -15,7 +15,7 @@ pub mod create_symbol_table;
pub mod create_type_dag;
use check_attribute::*;
use check_direction::*;
use check_embed::*;
use check_embed_include::*;
use check_enum::*;
use check_function::*;
use check_identifier::*;
Expand All @@ -36,7 +36,7 @@ use self::{check_assignment::CheckAssignment, create_type_dag::CreateTypeDag};
pub struct Pass1Handlers<'a> {
check_attribute: CheckAttribute<'a>,
check_direction: CheckDirection<'a>,
check_embed: CheckEmbed<'a>,
check_embed_include: CheckEmbedInclude<'a>,
check_identifier: CheckIdentifier<'a>,
check_number: CheckNumber<'a>,
check_reset: CheckReset<'a>,
Expand All @@ -49,7 +49,7 @@ impl<'a> Pass1Handlers<'a> {
Self {
check_attribute: CheckAttribute::new(text),
check_direction: CheckDirection::new(text),
check_embed: CheckEmbed::new(text),
check_embed_include: CheckEmbedInclude::new(text),
check_identifier: CheckIdentifier::new(text, lint_opt),
check_number: CheckNumber::new(text),
check_reset: CheckReset::new(text),
Expand All @@ -62,7 +62,7 @@ impl<'a> Pass1Handlers<'a> {
vec![
&mut self.check_attribute as &mut dyn Handler,
&mut self.check_direction as &mut dyn Handler,
&mut self.check_embed as &mut dyn Handler,
&mut self.check_embed_include as &mut dyn Handler,
&mut self.check_identifier as &mut dyn Handler,
&mut self.check_number as &mut dyn Handler,
&mut self.check_reset as &mut dyn Handler,
Expand All @@ -75,7 +75,7 @@ impl<'a> Pass1Handlers<'a> {
let mut ret = Vec::new();
ret.append(&mut self.check_attribute.errors);
ret.append(&mut self.check_direction.errors);
ret.append(&mut self.check_embed.errors);
ret.append(&mut self.check_embed_include.errors);
ret.append(&mut self.check_identifier.errors);
ret.append(&mut self.check_number.errors);
ret.append(&mut self.check_reset.errors);
Expand Down
55 changes: 0 additions & 55 deletions crates/analyzer/src/handlers/check_embed.rs

This file was deleted.

96 changes: 96 additions & 0 deletions crates/analyzer/src/handlers/check_embed_include.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::analyzer_error::AnalyzerError;
use std::fs;
use std::path::PathBuf;
use veryl_parser::resource_table;
use veryl_parser::veryl_grammar_trait::*;
use veryl_parser::veryl_token::TokenSource;
use veryl_parser::veryl_walker::{Handler, HandlerPoint};
use veryl_parser::ParolError;

pub struct CheckEmbedInclude<'a> {
pub errors: Vec<AnalyzerError>,
text: &'a str,
point: HandlerPoint,
}

impl<'a> CheckEmbedInclude<'a> {
pub fn new(text: &'a str) -> Self {
Self {
errors: Vec::new(),
text,
point: HandlerPoint::Before,
}
}
}

impl<'a> Handler for CheckEmbedInclude<'a> {
fn set_point(&mut self, p: HandlerPoint) {
self.point = p;
}
}

impl<'a> VerylGrammarTrait for CheckEmbedInclude<'a> {
fn embed_declaration(&mut self, arg: &EmbedDeclaration) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
let way = arg.identifier.identifier_token.to_string();
let lang = arg.identifier0.identifier_token.to_string();

if !EMBED_WAY.contains(&way.as_str()) {
self.errors.push(AnalyzerError::unknown_embed_way(
&way,
self.text,
&arg.identifier.as_ref().into(),
));
}

if !EMBED_LANG.contains(&lang.as_str()) {
self.errors.push(AnalyzerError::unknown_embed_lang(
&lang,
self.text,
&arg.identifier0.as_ref().into(),
));
}
}
Ok(())
}

fn include_declaration(&mut self, arg: &IncludeDeclaration) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
let way = arg.identifier.identifier_token.to_string();

if !INCLUDE_WAY.contains(&way.as_str()) {
self.errors.push(AnalyzerError::unknown_include_way(
&way,
self.text,
&arg.identifier.as_ref().into(),
));
}

let path = arg.string_literal.string_literal_token.to_string();
let path = path.strip_prefix('"').unwrap();
let path = path.strip_suffix('"').unwrap();
if let TokenSource::File(x) = arg.identifier.identifier_token.token.source {
let mut base = resource_table::get_path_value(x).unwrap();
if base.starts_with("file://") {
base = PathBuf::from(base.to_string_lossy().strip_prefix("file://").unwrap());
}
let base = base.parent().unwrap();
let path = base.join(path);
let text = fs::read_to_string(&path);
if let Err(err) = text {
self.errors.push(AnalyzerError::include_failure(
path.to_string_lossy().as_ref(),
&err.to_string(),
self.text,
&arg.string_literal.string_literal_token.token.into(),
));
}
}
}
Ok(())
}
}

const EMBED_WAY: [&str; 1] = ["inline"];
const EMBED_LANG: [&str; 1] = ["sv"];
const INCLUDE_WAY: [&str; 1] = ["inline"];
24 changes: 23 additions & 1 deletion crates/emitter/src/emitter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::aligner::{Aligner, Location};
use std::fs;
use veryl_analyzer::attribute::Attribute as Attr;
use veryl_analyzer::attribute_table;
use veryl_analyzer::namespace::Namespace;
Expand All @@ -9,7 +10,7 @@ use veryl_analyzer::{msb_table, namespace_table};
use veryl_metadata::{Build, BuiltinType, ClockType, Format, Metadata, ResetType};
use veryl_parser::resource_table::{self, StrId};
use veryl_parser::veryl_grammar_trait::*;
use veryl_parser::veryl_token::{Token, VerylToken};
use veryl_parser::veryl_token::{Token, TokenSource, VerylToken};
use veryl_parser::veryl_walker::VerylWalker;
use veryl_parser::Stringifier;

Expand Down Expand Up @@ -2826,6 +2827,24 @@ impl VerylWalker for Emitter {
}
}

/// Semantic action for non-terminal 'IncludeDeclaration'
fn include_declaration(&mut self, arg: &IncludeDeclaration) {
if arg.identifier.identifier_token.to_string() == "inline" {
let path = arg.string_literal.string_literal_token.to_string();
let path = path.strip_prefix('"').unwrap();
let path = path.strip_suffix('"').unwrap();
if let TokenSource::File(x) = arg.identifier.identifier_token.token.source {
let base = resource_table::get_path_value(x).unwrap();
let base = base.parent().unwrap();
let path = base.join(path);
// File existence is checked at analyzer
let text = fs::read_to_string(path).unwrap();
let text = text.replace('\r', "");
self.str(&text);
}
}
}

/// Semantic action for non-terminal 'DescriptionGroup'
fn description_group(&mut self, arg: &DescriptionGroup) {
for x in &arg.description_group_list {
Expand Down Expand Up @@ -2860,6 +2879,9 @@ impl VerylWalker for Emitter {
// file scope import is not emitted at SystemVerilog
DescriptionItem::ImportDeclaration(_) => (),
DescriptionItem::EmbedDeclaration(x) => self.embed_declaration(&x.embed_declaration),
DescriptionItem::IncludeDeclaration(x) => {
self.include_declaration(&x.include_declaration)
}
};
}

Expand Down
13 changes: 13 additions & 0 deletions crates/formatter/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,19 @@ impl VerylWalker for Formatter {
self.str(&text);
}

/// Semantic action for non-terminal 'IncludeDeclaration'
fn include_declaration(&mut self, arg: &IncludeDeclaration) {
self.include(&arg.include);
self.space(1);
self.l_paren(&arg.l_paren);
self.identifier(&arg.identifier);
self.comma(&arg.comma);
self.space(1);
self.string_literal(&arg.string_literal);
self.r_paren(&arg.r_paren);
self.semicolon(&arg.semicolon);
}

/// Semantic action for non-terminal 'DescriptionGroup'
fn description_group(&mut self, arg: &DescriptionGroup) {
for x in &arg.description_group_list {
Expand Down
1 change: 1 addition & 0 deletions crates/languageserver/src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const KEYWORDS: &[&str] = &[
"if_reset",
"if",
"import",
"include",
"initial",
"inout",
"input",
Expand Down
Loading
Loading