git.delta.rocks / jrsonnet / refs/commits / c647485a7362

difftreelog

docs(lexer) deny(missing_docs)

uwmlytnkYaroslav Bolyukin2026-05-14parent: #b3405f3.patch.diff
in: master

5 files changed

modifiedCargo.lockdiffbeforeafterboth
2567 "insta",2567 "insta",
2568 "jrsonnet-gcmodule",2568 "jrsonnet-gcmodule",
2569 "jrsonnet-ir",2569 "jrsonnet-ir",
2570 "jrsonnet-lexer",
2570 "peg",2571 "peg",
2571]2572]
25722573
modifiedcrates/jrsonnet-ir/src/visit.rsdiffbeforeafterboth
8 ObjBody, ObjComp, ObjMembers, Slice, SliceDesc, TrivialVal,8 ObjBody, ObjComp, ObjMembers, Slice, SliceDesc, TrivialVal,
9};9};
1010
11/// Expr visitor
11pub trait Visitor: Sized {12pub trait Visitor: Sized {
13 /// The input contains an `Expr`.
14 ///
15 /// The default implementation forwards to [`visit_expr`]
12 fn visit_expr(&mut self, e: &Expr) {16 fn visit_expr(&mut self, e: &Expr) {
13 visit_expr(self, e)17 visit_expr(self, e)
14 }18 }
19 /// Called for every encountered `import`/`importbin`/`importstr` expression
20 ///
21 /// `as_expression` is `true` for non-str/bin `imports`
15 fn visit_import(&mut self, _as_expression: bool, _value: IStr) {}22 fn visit_import(&mut self, _as_expression: bool, _value: IStr) {}
16}23}
1724
23 }30 }
24}31}
2532
33/// Use [`Visitor`] to recursively visit [`Destruct`]
26#[allow(unused_variables, reason = "used with exp-destruct")]34#[allow(unused_variables, reason = "used with exp-destruct")]
27pub fn visit_destruct<V: Visitor>(v: &mut V, destruct: &Destruct) {35pub fn visit_destruct<V: Visitor>(v: &mut V, destruct: &Destruct) {
28 match destruct {36 match destruct {
58 }66 }
59}67}
6068
69/// Use [`Visitor`] to recursively visit [`IfSpecData`]
61pub fn visit_if_spec<V: Visitor>(v: &mut V, cond: &IfSpecData) {70pub fn visit_if_spec<V: Visitor>(v: &mut V, cond: &IfSpecData) {
62 let IfSpecData { span: _, cond } = cond;71 let IfSpecData { span: _, cond } = cond;
63 v.visit_expr(cond);72 v.visit_expr(cond);
64}73}
6574
75/// Use [`Visitor`] to recursively visit [`CompSpec`]
66pub fn visit_comp_spec<V: Visitor>(v: &mut V, c: &CompSpec) {76pub fn visit_comp_spec<V: Visitor>(v: &mut V, c: &CompSpec) {
67 match c {77 match c {
68 CompSpec::IfSpec(cond) => visit_if_spec(v, cond),78 CompSpec::IfSpec(cond) => visit_if_spec(v, cond),
85 }95 }
86}96}
97
98/// Use [`Visitor`] to recursively visit [`ExprParams`]
87pub fn visit_params<V: Visitor>(v: &mut V, par: &ExprParams) {99pub fn visit_params<V: Visitor>(v: &mut V, par: &ExprParams) {
88 let ExprParams {100 let ExprParams {
89 exprs,101 exprs,
99 }111 }
100}112}
101113
114/// Use [`Visitor`] to recursively visit [`BindSpec`]
102pub fn visit_bind_spec<V: Visitor>(v: &mut V, bind: &BindSpec) {115pub fn visit_bind_spec<V: Visitor>(v: &mut V, bind: &BindSpec) {
103 match bind {116 match bind {
104 BindSpec::Field { into, value } => {117 BindSpec::Field { into, value } => {
116 }129 }
117}130}
118131
132/// Use [`Visitor`] to recursively visit [`FieldMember`]
119pub fn visit_field_member<V: Visitor>(v: &mut V, mem: &FieldMember) {133pub fn visit_field_member<V: Visitor>(v: &mut V, mem: &FieldMember) {
120 let FieldMember {134 let FieldMember {
121 name,135 name,
134 v.visit_expr(value);148 v.visit_expr(value);
135}149}
136150
151/// Use [`Visitor`] to recursively visit [`ObjBody`]
137pub fn visit_obj_body<V: Visitor>(v: &mut V, obj_body: &ObjBody) {152pub fn visit_obj_body<V: Visitor>(v: &mut V, obj_body: &ObjBody) {
138 match obj_body {153 match obj_body {
139 ObjBody::MemberList(obj_members) => {154 ObjBody::MemberList(obj_members) => {
169 }184 }
170}185}
171186
187/// Use [`Visitor`] to recursively visit [`AssertStmt`]
172pub fn visit_assert_stmt<V: Visitor>(v: &mut V, ass: &AssertStmt) {188pub fn visit_assert_stmt<V: Visitor>(v: &mut V, ass: &AssertStmt) {
173 let AssertStmt { assertion, message } = ass;189 let AssertStmt { assertion, message } = ass;
174 v.visit_expr(assertion);190 v.visit_expr(assertion);
177 }193 }
178}194}
195
196/// Use [`Visitor`] to recursively visit [`Expr`]
179pub fn visit_expr<V: Visitor>(v: &mut V, e: &Expr) {197pub fn visit_expr<V: Visitor>(v: &mut V, e: &Expr) {
180 match e {198 match e {
181 Expr::Identity(_span, _identity_kind) => {}199 Expr::Identity(_span, _identity_kind) => {}
modifiedcrates/jrsonnet-lexer/src/lex.rsdiffbeforeafterboth
22
3use logos::Logos;3use logos::Logos;
44
5// use rowan::{TextRange, TextSize};
6use crate::{5use crate::{
7 Span,6 Span,
8 generated::syntax_kinds::SyntaxKind,7 generated::syntax_kinds::SyntaxKind,
9 string_block::{StringBlockError, lex_str_block},8 string_block::{StringBlockError, lex_str_block},
10};9};
1110
11/// Jsonnet code lexer, use it instead of [`logos::Lexer`] to properly handle custom lexing errors
12/// for items such as [`SyntaxKind::STRING_BLOCK`]
12pub struct Lexer<'a> {13pub struct Lexer<'a> {
13 inner: logos::Lexer<'a, SyntaxKind>,14 inner: logos::Lexer<'a, SyntaxKind>,
14}15}
1516
16impl<'a> Lexer<'a> {17impl<'a> Lexer<'a> {
18 /// Create the lexer for the passed jsonnet code
17 pub fn new(input: &'a str) -> Self {19 pub fn new(input: &'a str) -> Self {
18 Self {20 Self {
19 inner: SyntaxKind::lexer(input),21 inner: SyntaxKind::lexer(input),
69 }71 }
70}72}
7173
74/// Parsed lexeme
72#[derive(Clone, Copy, Debug)]75#[derive(Clone, Copy, Debug)]
73pub struct Lexeme<'s> {76pub struct Lexeme<'s> {
77 /// Token kind, e.g [`SyntaxKind::COLON`]
74 pub kind: SyntaxKind,78 pub kind: SyntaxKind,
79 /// Token string value can be used to extract the actual jsonnet value
75 pub text: &'s str,80 pub text: &'s str,
81 /// Token range
76 pub range: Span,82 pub range: Span,
77}83}
7884
85/// Lex jsonnet code
79pub fn lex(input: &str) -> Vec<Lexeme<'_>> {86pub fn lex(input: &str) -> Vec<Lexeme<'_>> {
80 Lexer::new(input).collect()87 Lexer::new(input).collect()
81}88}
modifiedcrates/jrsonnet-lexer/src/lib.rsdiffbeforeafterboth
1//! Jrsonnet jsonnet lexer shared between multiple parser implementations
2#![deny(missing_docs)]
3
1mod generated;4mod generated;
2mod lex;5mod lex;
3pub mod string;6pub mod string;
4pub mod string_block;7pub mod string_block;
58
9/// Represents code range in bytes
6#[derive(Clone, Copy, Debug)]10#[derive(Clone, Copy, Debug)]
7pub struct Span(pub u32, pub u32);11pub struct Span(pub u32, pub u32);
812
modifiedcrates/jrsonnet-lexer/src/string_block.rsdiffbeforeafterboth
1//! String block literal `|||-\n\tText\n|||` lexing/parsing
2
3/// Syntax errors that might be encountered in string block expression
1#[derive(Clone, Copy, Debug, PartialEq, Eq)]4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub enum StringBlockError {5pub enum StringBlockError {
6 /// Unexpected EOF after the string block start tokens
3 UnexpectedEnd,7 UnexpectedEnd,
8 /// There should be a newline after `|||` or `|||-` string token
4 MissingNewLine,9 MissingNewLine,
10 /// String block should end with `|||`
5 MissingTermination,11 MissingTermination,
12 /// Whitespace mismatch in the string block - every new line should have the same start as the first one.
13 // TODO: Expand error definition so it is harder to make a mistake?
6 MissingIndent,14 MissingIndent,
7}15}
816
141 }149 }
142}150}
143151
152/// Collect string block line chunks into a [`Vec`]
144pub fn collect_lexed_str_block(input: &str) -> Result<CollectStrBlock<'_>, StringBlockError> {153pub fn collect_lexed_str_block(input: &str) -> Result<CollectStrBlock<'_>, StringBlockError> {
145 let mut collect = CollectStrBlock {154 let mut collect = CollectStrBlock {
146 truncate: false,155 truncate: false,
152 Ok(collect)161 Ok(collect)
153}162}
154163
164/// Collect string block line chunks into a [`Vec`]
155pub struct CollectStrBlock<'s> {165pub struct CollectStrBlock<'s> {
166 /// Is this a truncated string block? `|||-`
167 /// If not - assume [`lines`] contain an extra empty line
156 pub truncate: bool,168 pub truncate: bool,
169 /// The lines of a string block
157 pub lines: Vec<&'s str>,170 pub lines: Vec<&'s str>,
158 input: &'s str,171 input: &'s str,
159 offset: usize,172 offset: usize,