Skip to content

Add new spacing rules pending from DML style guide #108

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions example_files/example_lint_cfg.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"sp_reserved": {},
"sp_brace": {},
"sp_punct": {},
"sp_binop": {},
"sp_ternary" : {},
"nsp_funpar": {},
"nsp_inparen": {},
"nsp_unary": {},
"nsp_trailing": {},
"nsp_ptrdecl": {},
"sp_ptrdecl": {},
"long_lines": { "max_length": 80 },
"indent_size": { "indentation_spaces": 4 },
"indent_no_tabs": {},
Expand Down
8 changes: 8 additions & 0 deletions src/analysis/parsing/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::lint::{DMLStyleError,
rules::{spacing::{NspFunparArgs,
NspInparenArgs,
NspUnaryArgs,
SpBinopArgs,
SpTernaryArgs,
SpPunctArgs},
CurrentRules},
AuxParams};
Expand Down Expand Up @@ -92,6 +94,9 @@ impl TreeElement for BinaryExpressionContent {
fn subs(&self) -> TreeElements<'_> {
create_subs!(&self.left, &self.operation, &self.right)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_binop.check(acc, SpBinopArgs::from_binary_expression_content(self));
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -151,6 +156,9 @@ impl TreeElement for TertiaryExpressionContent {
create_subs!(&self.left, &self.left_operation,
&self.middle, &self.right_operation, &self.right)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_ternary.check(acc, SpTernaryArgs::from_tertiary_expression_content(self));
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
6 changes: 6 additions & 0 deletions src/analysis/parsing/misc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::lint::rules::spacing::{NspPtrDeclArgs, SpPtrDeclArgs};
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
// © 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0 and MIT
use crate::span::Range;
Expand Down Expand Up @@ -591,6 +593,10 @@ impl TreeElement for CDeclContent {
create_subs!(&self.consttok, &self.base,
&self.modifiers, &self.decl)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_ptrdecl.check(acc, SpPtrDeclArgs::from_cdecl(self));
rules.nsp_ptrdecl.check(acc, NspPtrDeclArgs::from_cdecl(self));
}
}

// corresponds to cdecl in grammar
Expand Down
7 changes: 7 additions & 0 deletions src/analysis/parsing/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use log::error;

use crate::lint::rules::indentation::IndentEmptyLoopArgs;
use crate::lint::rules::spacing::SpReservedArgs;
use crate::span::Range;
use crate::analysis::parsing::lexer::TokenKind;
use crate::analysis::parsing::statement;
Expand Down Expand Up @@ -437,6 +438,7 @@ impl TreeElement for IfContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.nsp_inparen.check(acc, NspInparenArgs::from_if(self));
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_if(self));
rules.sp_reserved.check(acc, SpReservedArgs::from_if(self));
}
}

Expand Down Expand Up @@ -550,6 +552,7 @@ impl TreeElement for WhileContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_while(self));
rules.indent_empty_loop.check(acc, IndentEmptyLoopArgs::from_while_content(self, aux.depth));
rules.sp_reserved.check(acc, SpReservedArgs::from_while(self));
}
}

Expand Down Expand Up @@ -865,6 +868,7 @@ impl TreeElement for ForContent {
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: AuxParams) {
rules.indent_paren_expr.check(acc, IndentParenExprArgs::from_for(self));
rules.indent_empty_loop.check(acc, IndentEmptyLoopArgs::from_for_content(self, aux.depth));
rules.sp_reserved.check(acc, SpReservedArgs::from_for(self));
}
}

Expand Down Expand Up @@ -1284,6 +1288,9 @@ impl TreeElement for AfterContent {
&self.callexpression,
&self.semi)
}
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: AuxParams) {
rules.sp_reserved.check(acc, SpReservedArgs::from_after_content(self));
}
}

impl Parse<StatementContent> for AfterContent {
Expand Down
5 changes: 1 addition & 4 deletions src/analysis/parsing/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use crate::analysis::parsing::parser::{doesnt_understand_tokens,
FileParser, Parse, ParseContext,
FileInfo};
use crate::analysis::LocalDMLError;
use crate::lint::rules::spacing::{SpBracesArgs,
NspInparenArgs,
NspFunparArgs,
SpPunctArgs};
use crate::lint::rules::spacing::{NspFunparArgs, NspInparenArgs, SpBracesArgs, SpPunctArgs};
use crate::lint::rules::indentation::{IndentCodeBlockArgs, IndentClosingBraceArgs, IndentParenExprArgs};
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
use crate::analysis::reference::{Reference, ReferenceKind};
Expand Down
3 changes: 3 additions & 0 deletions src/lint/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Below are listed the currently supported rules for linting:

## Spacing
- SpReserved, `sp_reserved`: around reserved words, such as `if`, `else`, `default`, `size`, `const` and `in`, except when a reserved word is used as an identifier (e.g., `local uint8 *data;`). Currently supported reserved words: `if`, `for` and `while`.
- SpBinop, `sp_binop`: spaces around binary operators except for derefencing operators (dot `a.b` and arrow `a->b` )
- SpTernary, `sp_ternary`: spaces around `?` and `:` in ternary conditional expressions
- SpBraces, `sp_brace`: spaces around braces (`{` and `}`)
- SpPunct, `sp_punct`: spaces after but not before colon, semicolon and comma
- NspFunpar, `nsp_funpar`: no spaces between a function/method name and its opening parenthesis
Expand Down
83 changes: 31 additions & 52 deletions src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ use std::path::{Path, PathBuf};
use log::{debug, error, trace};
use serde::{Deserialize, Serialize};
use rules::{instantiate_rules, CurrentRules, RuleType};
use rules::{spacing::{SpBraceOptions, SpPunctOptions, NspFunparOptions,
NspInparenOptions, NspUnaryOptions, NspTrailingOptions},
indentation::{LongLineOptions, IndentSizeOptions, IndentCodeBlockOptions,
IndentNoTabOptions, IndentClosingBraceOptions, IndentParenExprOptions, IndentSwitchCaseOptions, IndentEmptyLoopOptions},
use rules::{spacing::{SpReservedOptions,
SpBraceOptions,
SpPunctOptions,
SpBinopOptions,
NspFunparOptions,
SpTernaryOptions,
SpPtrDeclOptions,
NspPtrDeclOptions,
NspInparenOptions,
NspUnaryOptions,
NspTrailingOptions},
indentation::{LongLineOptions, IndentSizeOptions, IndentCodeBlockOptions,
IndentNoTabOptions, IndentClosingBraceOptions, IndentParenExprOptions,
IndentSwitchCaseOptions, IndentEmptyLoopOptions},
};
use crate::analysis::{DMLError, IsolatedAnalysis, LocalDMLError};
use crate::analysis::parsing::tree::TreeElement;
Expand Down Expand Up @@ -45,11 +55,21 @@ pub fn maybe_parse_lint_cfg(path: PathBuf) -> Option<LintCfg> {
#[serde(default)]
#[serde(deny_unknown_fields)]
pub struct LintCfg {
#[serde(default)]
pub sp_reserved: Option<SpReservedOptions>,
#[serde(default)]
pub sp_brace: Option<SpBraceOptions>,
#[serde(default)]
pub sp_punct: Option<SpPunctOptions>,
#[serde(default)]
pub sp_binop: Option<SpBinopOptions>,
#[serde(default)]
pub sp_ternary: Option<SpTernaryOptions>,
#[serde(default)]
pub sp_ptrdecl: Option<SpPtrDeclOptions>,
#[serde(default)]
pub nsp_ptrdecl: Option<NspPtrDeclOptions>,
#[serde(default)]
pub nsp_funpar: Option<NspFunparOptions>,
#[serde(default)]
pub nsp_inparen: Option<NspInparenOptions>,
Expand Down Expand Up @@ -84,8 +104,13 @@ fn get_true() -> bool {
impl Default for LintCfg {
fn default() -> LintCfg {
LintCfg {
sp_reserved: Some(SpReservedOptions{}),
sp_brace: Some(SpBraceOptions{}),
sp_punct: Some(SpPunctOptions{}),
sp_binop: Some(SpBinopOptions{}),
sp_ternary: Some(SpTernaryOptions{}),
sp_ptrdecl: Some(SpPtrDeclOptions{}),
nsp_ptrdecl: Some(NspPtrDeclOptions{}),
nsp_funpar: Some(NspFunparOptions{}),
nsp_inparen: Some(NspInparenOptions{}),
nsp_unary: Some(NspUnaryOptions{}),
Expand Down Expand Up @@ -204,41 +229,6 @@ pub mod tests {
use std::str::FromStr;
use crate::{analysis::{parsing::{parser::FileInfo, structure::{self, TopAst}}, FileSpec}, vfs::TextFile};

pub static SOURCE: &str = "
dml 1.4;

bank sb_cr {
group monitor {

register MKTME_KEYID_MASK {
method get() -> (uint64) {
local uint64 physical_address_mask = mse.srv10nm_mse_mktme.get_key_addr_mask();
this.Mask.set(physical_address_mask);
this.function_with_args('some_string',
integer,
floater);
return this.val;
}
}

register TDX_KEYID_MASK {
method get() -> (uint64) {
local uint64 tdx_keyid_mask = mse.srv10nm_mse_tdx.get_key_addr_mask();
local uint64 some_uint = (is_this_real) ? then_you_might_like_this_value : or_this_one;
this.Mask.set(tdx_keyid_mask);
return this.val;
}
}
}
}

/*
This is ONEEEE VEEEEEERY LLOOOOOOONG COOOMMMEENTT ON A SINGLEEEE LINEEEEEEEEEEEEEE
and ANOTHEEEER VEEEEEERY LLOOOOOOONG COOOMMMEENTT ON A SINGLEEEE LINEEEEEEEEEEEEEE
*/

";

pub fn create_ast_from_snippet(source: &str) -> TopAst {
use logos::Logos;
use crate::analysis::parsing::lexer::TokenKind;
Expand All @@ -264,19 +254,8 @@ pub mod tests {
env!("CARGO_MANIFEST_DIR"),
EXAMPLE_CFG);
let example_cfg = parse_lint_cfg(example_path.into()).unwrap();
println!("Example LintCfg: {:#?}", example_cfg);
println!("LintCfg::default(): {:#?}", LintCfg::default());
assert_eq!(example_cfg, LintCfg::default());
}

#[test]
#[ignore]
fn test_main() {
use crate::lint::{begin_style_check, LintCfg};
use crate::lint::rules:: instantiate_rules;
let ast = create_ast_from_snippet(SOURCE);
let cfg = LintCfg::default();
let rules = instantiate_rules(&cfg);
let _lint_errors = begin_style_check(ast, SOURCE.to_string(), &rules);
assert!(_lint_errors.is_ok());
assert!(!_lint_errors.unwrap().is_empty());
}
}
29 changes: 26 additions & 3 deletions src/lint/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ pub mod indentation;
#[cfg(test)]
pub mod tests;

use spacing::{SpBracesRule,
SpPunctRule, NspFunparRule, NspInparenRule,
NspUnaryRule, NspTrailingRule};
use spacing::{NspFunparRule,
NspInparenRule,
NspTrailingRule,
NspUnaryRule,
SpBracesRule,
SpBinopRule,
SpTernaryRule,
SpPtrDeclRule,
NspPtrDeclRule,
SpPunctRule,
SpReservedRule};
use indentation::{LongLinesRule, IndentNoTabRule, IndentCodeBlockRule, IndentClosingBraceRule, IndentParenExprRule, IndentSwitchCaseRule, IndentEmptyLoopRule};
use crate::lint::{LintCfg, DMLStyleError};
use crate::analysis::{LocalDMLError, parsing::tree::ZeroRange};

pub struct CurrentRules {
pub sp_reserved: SpReservedRule,
pub sp_brace: SpBracesRule,
pub sp_punct: SpPunctRule,
pub sp_binop: SpBinopRule,
pub sp_ternary: SpTernaryRule,
pub sp_ptrdecl: SpPtrDeclRule,
pub nsp_ptrdecl: NspPtrDeclRule,
pub nsp_funpar: NspFunparRule,
pub nsp_inparen: NspInparenRule,
pub nsp_unary: NspUnaryRule,
Expand All @@ -29,8 +42,13 @@ pub struct CurrentRules {

pub fn instantiate_rules(cfg: &LintCfg) -> CurrentRules {
CurrentRules {
sp_reserved: SpReservedRule { enabled: cfg.sp_reserved.is_some() },
sp_brace: SpBracesRule { enabled: cfg.sp_brace.is_some() },
sp_punct: SpPunctRule { enabled: cfg.sp_punct.is_some() },
sp_binop: SpBinopRule { enabled: cfg.sp_binop.is_some() },
sp_ternary: SpTernaryRule { enabled: cfg.sp_ternary.is_some() },
sp_ptrdecl: SpPtrDeclRule { enabled: cfg.sp_ptrdecl.is_some() },
nsp_ptrdecl: NspPtrDeclRule { enabled: cfg.nsp_ptrdecl.is_some() },
nsp_funpar: NspFunparRule { enabled: cfg.nsp_funpar.is_some() },
nsp_inparen: NspInparenRule { enabled: cfg.nsp_inparen.is_some() },
nsp_unary: NspUnaryRule { enabled: cfg.nsp_unary.is_some() },
Expand Down Expand Up @@ -64,8 +82,13 @@ pub trait Rule {

#[derive(PartialEq, Debug, Clone, Eq, Hash)]
pub enum RuleType {
SpReserved,
SpBraces,
SpPunct,
SpBinop,
SpTernary,
SpPtrDecl,
NspPtrDecl,
NspFunpar,
NspInparen,
NspUnary,
Expand Down
Loading