Skip to content

Commit

Permalink
Exclude comparison operators from modifier logic (#6370)
Browse files Browse the repository at this point in the history
* Exclude comparison operators from modifier logic

* CLI for SpanTree debug_print
  • Loading branch information
kazcw authored Apr 24, 2023
1 parent 2d16830 commit d6828b5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
9 changes: 9 additions & 0 deletions app/gui/language/ast/impl/src/opr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,15 @@ mod tests {
test_enumerating(&chain, &a_plus_b_plus_c, &[&a, &b, &c]);
}

#[test]
fn infix_section() {
let a = Ast::var("a");
let a_plus = Ast::section_left(a.clone(), "+");
let chain = Chain::try_new(&a_plus).unwrap();
expect_at(&chain.target, &a);
test_enumerating(&chain, &a_plus, &[&a]);
}

#[test]
fn infix_chain_tests_right() {
let a = Ast::var("a");
Expand Down
22 changes: 9 additions & 13 deletions app/gui/language/parser/src/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,21 +436,17 @@ impl Translate {
infix(lhs, opr, rhs).map(|opr_app| self.finish_ast(opr_app, builder))
}

/// Translate an operator or multiple-operator erorr into the [`Ast`] representation.
/// Translate an operator or multiple-operator error into the [`Ast`] representation.
fn translate_operators(&mut self, opr: &tree::OperatorOrError) -> WithInitialSpace<Ast> {
match opr {
Ok(name) => match name.code.repr.strip_suffix('=') {
Some(mod_name) if mod_name.contains(|c| c != '=') => {
let opr_builder = self.start_ast();
let token = self.visit_token(name);
token.map(|_| {
let name = mod_name.to_string();
let opr = ast::Mod { name };
self.finish_ast(opr, opr_builder)
})
}
_ => self.translate_operator(name),
},
Ok(name) if name.properties.is_modifier() => {
let opr_builder = self.start_ast();
self.visit_token(name).map(|name| {
let name = name.strip_suffix('=').map(|s| s.to_owned()).unwrap_or(name);
self.finish_ast(ast::Mod { name }, opr_builder)
})
}
Ok(name) => self.translate_operator(name),
Err(names) => {
let opr_builder = self.start_ast();
let mut span_info = SpanSeedBuilder::new();
Expand Down
39 changes: 39 additions & 0 deletions app/gui/language/span-tree/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Command-line debug tool for `SpanTree`. Accepts a single line of Enso source code as an
//! argument, and prints a debug representation of the resulting `SpanTree`.
// === Standard Linter Configuration ===
#![deny(non_ascii_idents)]
#![warn(unsafe_code)]
#![allow(clippy::bool_to_int_with_if)]
#![allow(clippy::let_and_return)]
// === Non-Standard Linter Configuration ===
#![warn(missing_docs)]
#![warn(trivial_casts)]
#![warn(trivial_numeric_casts)]
#![warn(unused_import_braces)]
#![warn(unused_qualifications)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]

use span_tree::generate;
use span_tree::generate::SpanTreeGenerator;
use span_tree::SpanTree;



// ===================
// === Entry point ===
// ===================

#[allow(missing_docs)]
pub fn main() {
let mut args = std::env::args();
let _ = args.next().unwrap();
let code = args.next().unwrap();

let parser = parser::Parser::new();
let ast = parser.parse_line_ast(&code).unwrap();
let tree: SpanTree = ast.generate_tree(&generate::context::Empty).unwrap();
let tree = tree.debug_print(&code);
println!("{tree}");
}
8 changes: 8 additions & 0 deletions lib/rust/parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,14 @@ fn analyze_operator(token: &str) -> token::OperatorProperties {
if token.ends_with("->") && !token.starts_with("<-") {
operator = operator.as_right_associative();
}
if token.ends_with('=') && !token.bytes().all(|c| c == b'=') {
match token {
// Inclusive comparison operators are not modifiers.
">=" | "<=" => (),
// Any other operator ending with "=" is a modifier.
_ => operator = operator.as_modifier(),
}
}
match token {
// Operators that can be unary.
"\\" =>
Expand Down
11 changes: 11 additions & 0 deletions lib/rust/parser/src/syntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ pub struct OperatorProperties {
// Special properties
is_compile_time_operation: bool,
is_right_associative: bool,
is_modifier: bool,
// Unique operators
is_decimal: bool,
is_type_annotation: bool,
Expand Down Expand Up @@ -375,6 +376,11 @@ impl OperatorProperties {
Self { is_right_associative: true, ..self }
}

/// Return a copy of this operator, modified to be flagged as an modified-assignment operator.
pub fn as_modifier(self) -> Self {
Self { is_modifier: true, ..self }
}

/// Return a copy of this operator, modified to be flagged as special.
pub fn as_special(self) -> Self {
Self { is_special: true, ..self }
Expand Down Expand Up @@ -467,6 +473,11 @@ impl OperatorProperties {
self.is_assignment
}

/// Return whether this operator is a modified-assignment operator.
pub fn is_modifier(&self) -> bool {
self.is_modifier
}

/// Return whether this operator is the arrow operator.
pub fn is_arrow(&self) -> bool {
self.is_arrow
Expand Down

0 comments on commit d6828b5

Please sign in to comment.