Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
45 changes: 22 additions & 23 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ jobs:
run: cargo build --all-features
- name: Test
run: cargo test --all-targets --all-features
env:
CARGO_INCREMENTAL: '0'
RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
- name: Install grcov
run: |
if [[ ! -f ~/.cargo/bin/grcov ]]; then
cargo install grcov
fi
# env:
# CARGO_INCREMENTAL: '0'
# RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
# RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Copt-level=0 -Cllvm-args=--inline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort'
# - name: Install grcov
# run: |
# if [[ ! -f ~/.cargo/bin/grcov ]]; then
# cargo install grcov
# fi
- name: Save cache
uses: actions/cache/save@v3
with:
Expand All @@ -58,17 +58,16 @@ jobs:
~/.cargo/registry/cache/
~/.cargo/git/db/
key: ${{ runner.os }}-cargo
- name: Run grcov
run: |
mkdir ./target/debug/coverage/
grcov . -s . -b ./target/debug/ -o ./target/debug/coverage/ --ignore-not-existing --excl-line="grcov-excl-line|#\\[derive\\(|//!|///" --excl-start="grcov-excl-start" --excl-stop="grcov-excl-end" --ignore="*.cargo/*" --ignore="src/lib.rs" --ignore="tests/*"
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
directory: ./target/debug/coverage/
files: ./target/debug/coverage/lcov
verbose: true
fail_ci_if_error: true

# - name: Run grcov
# run: |
# mkdir ./target/debug/coverage/
# grcov . -s . -b ./target/debug/ -o ./target/debug/coverage/ --ignore-not-existing --excl-line="grcov-excl-line|#\\[derive\\(|//!|///" --excl-start="grcov-excl-start" --excl-stop="grcov-excl-end" --ignore="*.cargo/*" --ignore="src/lib.rs" --ignore="tests/*"
# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v3
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
# with:
# directory: ./target/debug/coverage/
# files: ./target/debug/coverage/lcov
# verbose: true
# fail_ci_if_error: true
16 changes: 13 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
[package]
name = "semantic-analyzer"
version = "0.4.5"
version = "0.4.7"
authors = ["Evgeny Ukhanov <mrlsd@ya.ru>"]
description = "Semantic analyzer library for compilers written in Rust for semantic analysis of programming languages AST"
keywords = ["compiler", "semantic-analisis", "semantic-alalyzer", "compiler-design", "semantic"]
categories = ["compilers", "development-tools", "development-tools::build-utils"]
license = "MIT"
edition = "2021"
edition = "2024"
homepage = "https://github.com/mrLSD/semantic-analyzer-rs"
repository = "https://github.com/mrLSD/semantic-analyzer-rs"
# Edition 2024 requires Rust 1.85.0 or later
rust-version = "1.85.0"
Comment thread
mrLSD marked this conversation as resolved.
Outdated

[lints.clippy]
pedantic = { level = "deny", priority = -1 }
nursery = { level = "deny", priority = -1 }
as_conversions = "deny"
module_name_repetitions = "allow"
doc_lazy_continuation = "allow"
too_long_first_doc_paragraph = "allow"

[lib]
doctest = false

[dependencies]
nom_locate = "4.2"
nom_locate = "5.0"
serde = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
Expand Down
22 changes: 11 additions & 11 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::types::semantic::{ExtendedExpression, SemanticContextInstruction};
use nom_locate::LocatedSpan;
#[cfg(feature = "codec")]
use serde::{
Deserialize, Serialize,
de::{self, Deserializer, MapAccess, Visitor},
ser::{SerializeStruct, Serializer},
Deserialize, Serialize,
};
use std::convert::Infallible;
use std::marker::PhantomData;
Expand Down Expand Up @@ -44,7 +44,7 @@ impl<'a> Ident<'a> {
}
}

impl<'a> std::fmt::Display for Ident<'a> {
impl std::fmt::Display for Ident<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.fragment())
}
Expand All @@ -58,7 +58,7 @@ impl<'a> From<&'a str> for Ident<'a> {

/// Ident Serializer
#[cfg(feature = "codec")]
impl<'a> Serialize for Ident<'a> {
impl Serialize for Ident<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down Expand Up @@ -227,7 +227,7 @@ impl GetLocation for FunctionName<'_> {
}
}

impl<'a> std::fmt::Display for FunctionName<'a> {
impl std::fmt::Display for FunctionName<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.fragment())
}
Expand Down Expand Up @@ -406,7 +406,7 @@ impl GetLocation for StructTypes<'_> {
}
}

impl<'a> GetName for StructTypes<'a> {
impl GetName for StructTypes<'_> {
fn name(&self) -> String {
(*self.name.fragment()).to_string()
}
Expand All @@ -430,7 +430,7 @@ pub enum Type<'a> {
Array(Box<Self>, u32),
}

impl<'a> GetName for Type<'a> {
impl GetName for Type<'_> {
fn name(&self) -> String {
match self {
Self::Primitive(primitive) => primitive.name(),
Expand Down Expand Up @@ -471,7 +471,7 @@ pub struct ConstantExpression<'a> {
#[cfg_attr(feature = "codec", serde(borrow))]
pub value: ConstantValue<'a>,
/// Constant expression optional expression operation with other constant expression declarations.
pub operation: Option<(ExpressionOperations, Box<ConstantExpression<'a>>)>,
pub operation: Option<(ExpressionOperations, Box<Self>)>,
}

/// `Constant` constant declaration element of AST.
Expand Down Expand Up @@ -711,7 +711,7 @@ pub struct Expression<'a, I: SemanticContextInstruction, E: ExtendedExpression<I
#[cfg_attr(feature = "codec", serde(borrow))]
pub expression_value: ExpressionValue<'a, I, E>,
/// Optional expression operation with other expression value
pub operation: Option<(ExpressionOperations, Box<Expression<'a, I, E>>)>,
pub operation: Option<(ExpressionOperations, Box<Self>)>,
}

impl<I: SemanticContextInstruction, E: ExtendedExpression<I>> GetLocation for Expression<'_, I, E> {
Expand Down Expand Up @@ -860,7 +860,7 @@ pub struct ExpressionLogicCondition<'a, I: SemanticContextInstruction, E: Extend
#[cfg_attr(feature = "codec", serde(borrow))]
pub left: ExpressionCondition<'a, I, E>,
/// Optional right side contain logic operation to other `ExpressionLogicCondition`
pub right: Option<(LogicCondition, Box<ExpressionLogicCondition<'a, I, E>>)>,
pub right: Option<(LogicCondition, Box<Self>)>,
}

/// `IfCondition` if-condition control flow element of AST.
Expand Down Expand Up @@ -898,7 +898,7 @@ pub struct IfStatement<'a, I: SemanticContextInstruction, E: ExtendedExpression<
/// If-else-body statement - body of else-condition success
pub else_statement: Option<IfBodyStatements<'a, I, E>>,
/// Else-if-body statement - body of else-if-condition success
pub else_if_statement: Option<Box<IfStatement<'a, I, E>>>,
pub else_if_statement: Option<Box<Self>>,
}

impl<I: SemanticContextInstruction, E: ExtendedExpression<I>> GetLocation
Expand Down Expand Up @@ -999,7 +999,7 @@ pub enum LoopBodyStatement<'a, I: SemanticContextInstruction, E: ExtendedExpress
Binding(Binding<'a, I, E>),
FunctionCall(FunctionCall<'a, I, E>),
If(IfStatement<'a, I, E>),
Loop(Vec<LoopBodyStatement<'a, I, E>>),
Loop(Vec<Self>),
Return(Expression<'a, I, E>),
Break,
Continue,
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![deny(clippy::pedantic, clippy::nursery, clippy::as_conversions)]
#![allow(clippy::module_name_repetitions, clippy::doc_lazy_continuation)]
//! # Semantic Analyzer
//! The semantic analyzer consists of the following basic elements:
//! - AST is an abstract syntax tree that implements a predefined set of
Expand Down
37 changes: 17 additions & 20 deletions src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::types::semantic::{
};
use crate::types::types::{Type, TypeName};
use crate::types::{
error, Binding, Constant, ConstantName, Function, FunctionCall, FunctionName,
FunctionParameter, FunctionStatement, InnerValueName, LabelName, LetBinding, Value,
Binding, Constant, ConstantName, Function, FunctionCall, FunctionName, FunctionParameter,
FunctionStatement, InnerValueName, LabelName, LetBinding, Value, error,
};
#[cfg(feature = "codec")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -470,15 +470,15 @@ where
};
let let_data: LetBinding = data.clone().into();

if let Some(ty) = &let_data.value_type {
if &expr_result.expr_type != ty {
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::WrongLetType,
let_data.to_string(),
data.location(),
));
return;
}
if let Some(ty) = &let_data.value_type
&& &expr_result.expr_type != ty
{
self.add_error(error::StateErrorResult::new(
error::StateErrorKind::WrongLetType,
let_data.to_string(),
data.location(),
));
return;
}
let let_ty = expr_result.expr_type.clone();

Expand Down Expand Up @@ -748,7 +748,7 @@ where
if_body_state.borrow_mut().jump_function_return(res);
if_body_state.borrow_mut().set_return();
return_is_called = true;
};
}
}
}
}
Expand Down Expand Up @@ -952,14 +952,11 @@ where
.borrow_mut()
.get_and_set_next_label(&"if_else".to_string().into());
// Set if-end label from previous context
let label_if_end = label_end.clone().map_or_else(
|| {
if_body_state
.borrow_mut()
.get_and_set_next_label(&"if_end".to_string().into())
},
|label| label,
);
let label_if_end = label_end.clone().unwrap_or_else(|| {
if_body_state
.borrow_mut()
.get_and_set_next_label(&"if_end".to_string().into())
});
// To set if-end as single return point check is it previously set
let is_set_label_if_end = label_end.is_some();
let is_else = data.else_statement.is_some() || data.else_if_statement.is_some();
Expand Down
6 changes: 3 additions & 3 deletions src/types/block_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct BlockState<I: SemanticContextInstruction> {
deserialize_with = "rc_serializer::deserialize_option"
)
)]
pub parent: Option<Rc<RefCell<BlockState<I>>>>,
pub parent: Option<Rc<RefCell<Self>>>,
/// children states
#[cfg_attr(
feature = "codec",
Expand All @@ -59,7 +59,7 @@ pub struct BlockState<I: SemanticContextInstruction> {
deserialize_with = "rc_serializer::deserialize_vec"
)
)]
pub children: Vec<Rc<RefCell<BlockState<I>>>>,
pub children: Vec<Rc<RefCell<Self>>>,
/// Semantic stack context for Block state
context: SemanticStack<I>,
}
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<I: SemanticContextInstruction> ExtendedSemanticContext<I> for BlockState<I>
#[cfg(feature = "codec")]
pub mod rc_serializer {
use super::{Rc, RefCell};
use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer, ser::SerializeSeq};

/// Serializer for `Rc<RefCell<T>`.
#[allow(clippy::missing_errors_doc)]
Expand Down
6 changes: 3 additions & 3 deletions src/types/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct ExpressionLogicCondition {
/// Left expression condition
pub left: ExpressionCondition,
/// Optional right expression condition with logic condition
pub right: Option<(LogicCondition, Box<ExpressionLogicCondition>)>,
pub right: Option<(LogicCondition, Box<Self>)>,
}

impl<I: SemanticContextInstruction, E: ExtendedExpression<I>>
Expand Down Expand Up @@ -145,7 +145,7 @@ pub struct IfStatement {
/// Basic else-body, if if-condition is false
pub else_statement: Option<IfBodyStatements>,
/// Basic else-if-body
pub else_if_statement: Option<Box<IfStatement>>,
pub else_if_statement: Option<Box<Self>>,
}

impl<I: SemanticContextInstruction, E: ExtendedExpression<I>> From<ast::IfStatement<'_, I, E>>
Expand Down Expand Up @@ -202,7 +202,7 @@ pub enum LoopBodyStatement {
Binding(Binding),
FunctionCall(FunctionCall),
If(IfStatement),
Loop(Vec<LoopBodyStatement>),
Loop(Vec<Self>),
Return(Expression),
Break,
Continue,
Expand Down
2 changes: 1 addition & 1 deletion src/types/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub struct Expression {
/// Expression value
pub expression_value: ExpressionValue,
/// Optional expression operation under other `Expression`
pub operation: Option<(ExpressionOperations, Box<Expression>)>,
pub operation: Option<(ExpressionOperations, Box<Self>)>,
}

impl Display for Expression {
Expand Down
2 changes: 1 addition & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub struct ConstantExpression {
/// Constant value for expression operation
pub value: ConstantValue,
/// Optional expression operation and next constant expression entry point
pub operation: Option<(ExpressionOperations, Box<ConstantExpression>)>,
pub operation: Option<(ExpressionOperations, Box<Self>)>,
}

impl From<ast::ConstantExpression<'_>> for ConstantExpression {
Expand Down
15 changes: 8 additions & 7 deletions tests/const_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use semantic_analyzer::ast::{self, CodeLocation, GetLocation, GetName, Ident};
use semantic_analyzer::types::error::StateErrorKind;
use semantic_analyzer::types::expression::ExpressionOperations;
use semantic_analyzer::types::{
semantic::SemanticStackContext, Constant, ConstantExpression, ConstantName, ConstantValue,
PrimitiveValue,
Constant, ConstantExpression, ConstantName, ConstantValue, PrimitiveValue,
semantic::SemanticStackContext,
};

mod utils;
Expand Down Expand Up @@ -163,11 +163,12 @@ fn const_declaration_with_operations() {
"Errors: {:?}",
t.state.errors[0]
);
assert!(!t
.state
.global
.constants
.contains_key(&const_name1.clone().into()));
assert!(
!t.state
.global
.constants
.contains_key(&const_name1.clone().into())
);
t.clean_errors();

// constant2
Expand Down
4 changes: 2 additions & 2 deletions tests/expressions_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use semantic_analyzer::types::expression::{
};
use semantic_analyzer::types::semantic::{ExtendedSemanticContext, SemanticStackContext};
use semantic_analyzer::types::{
Constant, ConstantExpression, ConstantName, ConstantValue, Function, PrimitiveValue, Value,
ValueName,
block_state::BlockState,
error::StateErrorKind,
expression::ExpressionResultValue,
types::{PrimitiveTypes, Type},
Constant, ConstantExpression, ConstantName, ConstantValue, Function, PrimitiveValue, Value,
ValueName,
};
use std::cell::RefCell;
use std::marker::PhantomData;
Expand Down
2 changes: 1 addition & 1 deletion tests/function_call_tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::{CustomExpression, CustomExpressionInstruction, SemanticTest};
use semantic_analyzer::ast;
use semantic_analyzer::ast::{CodeLocation, GetLocation, GetName, Ident};
use semantic_analyzer::types::FunctionCall;
use semantic_analyzer::types::block_state::BlockState;
use semantic_analyzer::types::error::StateErrorKind;
use semantic_analyzer::types::types::{PrimitiveTypes, Type};
use semantic_analyzer::types::FunctionCall;
use std::cell::RefCell;
use std::rc::Rc;

Expand Down
Loading