question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[suggestion] Allow bracketless version of if/while/for/foreach

See original GitHub issue

Instead of mandatory () around an expression in listed keywords an option would exist to parse them without brackets.

Now:

if (expr) {

}

With suggestion option toggled on:

if expr {

}

Since EE already supports lightweighted syntactic constructs (myVar = 0; instead of [type] myVar = 0;), this would a nice addition for Python like scripting.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
codingsebcommented, Jan 22, 2021

Some small breaking changes are linked to this issue. So I think the next version of EE that will come with all of these scripts customization stuff will be a 1.5.x.x major version.

Also, the possibility to set arrays for block keywords syntax options seem harder than I thought especially for all brackets stuff that support nesting (imbrication) and for what we need to now which end bracket close which start bracket.

1reaction
codingsebcommented, Jan 13, 2021

OK I am currently working on this. And your solution is good but I can not implement it in this strict manner. Here a few reasons :

  1. I will need a notion of brackets and a notion separator for head and body separation. This because brackets can be nested ((something) - (otherThing)) and a separator not so the parsing process is different.
  2. We need at least one of these 2 notions to be mandatory otherwise it is very hard to know what is the head and what is the body in some cases.
  3. The parsing process for the different kind of syntaxes of the head part is different of the parsing process of the different kind of syntaxes for the body (especially if we want to be able to use indentation for body blocks like in python).

Also take note that this is include in a refactoring of the script evaluation part of EE to be more flexible. I also try to add the following features :

  1. An option to chose how to separate expressions ; or something else like end of line.
  2. A way to redefine scripts keywords or create a custom one

So here is what I came up with so far :
(I try to be language agnostic but also to prevent syntax evaluation conflicts or errors so it’s a little bit different of your proposition)

public enum SyntaxForHeadExpressionInScriptBlocksKeywords
{
    HeadBrackets,
    SeparatorBetweenHeadAndBlock,
    Both,
    Any
}

public enum SyntaxForScriptBlocksIdentifier
{
    OptionalBracketsForStartAndEndWhenSingleStatement,
    MandatoryBracketsForStartAndEnd,
    Indentation
}

// ...

/// <summary>
/// To specify the character or string that begin the head statements of script blocks keywords (if, else if, for, foreach while, do.. while)
/// Default value : <c>"("</c>
/// </summary>
public string OptionScriptBlocksKeywordsHeadStatementsStartBracket { get; set; } = "(";

/// <summary>
/// To specify the character or string that end the head statements of script blocks keywords (if, else if, for, foreach while, do.. while)
/// Default value : <c>")"</c>
/// </summary>
public string OptionScriptBlocksKeywordsHeadExpressionEndBracket { get; set; } = ")";

/// <summary>
/// To specify the character or string that separate the head statements and the block statements of script blocks keywords (if, else if, for, foreach while, do.. while)
/// Default value : <c>":"</c>
/// </summary>
public string OptionScriptBlockKeywordsHeadExpressionAndBlockSeparator { get; set; } = ":";

/// <summary>
/// Specify how to detect the separation between head expression and the block of code is made in script block keyword (if, else if, for, foreach while, do.. while)
/// Default value : <c>HeadBrackets</c>
/// </summary>
public SyntaxForHeadExpressionInScriptBlocksKeywords OptionSyntaxForHeadExpressionInScriptBlocksKeywords { get; set; } = SyntaxForHeadExpressionInScriptBlocksKeywords.HeadBrackets;

/// <summary>
/// To specify the character or string that start a block of code used in script blocks keywords (if, else if, for, foreach while, do.. while) and multiline lambda.
/// Default value : <c>"{"</c>
/// </summary>
public string OptionScriptBlockStartBrackets { get; set; } = "{";

/// <summary>
/// To specify the character or string that end a block of code used in script blocks keywords (if, else if, for, foreach while, do.. while) and multiline lambda.
/// Default value : <c>"}"</c>
/// </summary>
public string OptionScriptBlockEndBrackets { get; set; } = "}";

/// <summary>
/// Specify the syntax to use to detect a block of code in script blocks keywords  (if, else if, for, foreach while, do.. while) and multiline lambda
/// Default value
/// </summary>
public SyntaxForScriptBlocksIdentifier OptionSyntaxForScriptBlocksIdentifier { get; set; } = SyntaxForScriptBlocksIdentifier.OptionalBracketsForStartAndEndWhenSingleStatement;

It should already allow a few different syntaxes :

C# like syntax

if(condition) DoSomeThingElse();

if(condition)
{
    // Do a lot of stuff or only one
}

Python like syntax

if condition:
    DoSomething

Pascal like syntax

if condition then
begin
    DoSomething();
end

Some exotic syntax

if |condition|
    DoSomething()
Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP - If/else, for, foreach, while - without curly braces?
The curly brace is not required however, for readability and maintenance, many developers would consider it bad style not to include them.
Read more >
Foreach-loop with break/return vs. while-loop with explicit ...
If the loop's contents cannot fit on your screen and has several returns in the loop, there is an argument to be made...
Read more >
Code Inspection: For-loop can be converted into foreach-loop
When you iterate through a collection without modifying it or accessing the iteration variable, it is possible to use foreach instead of the ......
Read more >
Repeat escaped foreach until count is met
I'm trying to repeat a foreach loop from a files field until the count meets at least 10 images. The foreach needs to...
Read more >
Is it bad practice to use foreach instead of while? : r/PHP
Always use FOREACH when iterating over a whole array, or at least from the top of an array to a BREAK condition. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found