Code Formatter for Ring

73 views
Skip to first unread message

Bert Mariani

unread,
Sep 6, 2025, 4:23:20 PM (8 days ago) Sep 6
to The Ring Programming Language
Hello Mahmoud

###  Ring Install from GitHub
c:\ring>ringpm install RingCodeFormatter from umariani   

Installing RingCodeFormatter (master) ---> CodeFormatter (1.0.0)
Download Package Files For RingCodeFormatter
Download File : CodeFormatter.ring

========================
GitHub GUI Source - shows files

========================
### NEEDS to Run from RingNotePad not the CMD Line
### C:\ring\tools\ringpm\packages\CodeFormatter/CodeFormatter.ring

Question: 
After download to ...tools\ringpm ...
Why is every 2nd line spaced ?

- Does not use Token Parser
- Also detects Unclosed Blocks
       MsgWarning:
       WARNING - Code validation errors:
       MsgErrors.:
       ERROR: 1 unclosed blocks detected

========================================
### Start of CodeFormatter.ring  shows Options

    RING CODE FORMATTER v2.3                
    Interactive Version with Test Suite

Available Options:
  1. Read from File   <<<<< Use Option 1 to Read Code from File.
  2. Format code from manual input
  3. Run basic formatting test
  4. Test operator formatting
  5. Test quote handling
  6. Test complex nested structures
  7. Test comment preservation
  8. Test tab/space conversion
  9. Test GUI formatting
 10. Validate code structure
 11. Configuration settings
 12. About this formatter
 13. Exit

==============================
### Sample Output

<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>
<<<                                  >>>
<<<        FORMATTED CODE:           >>>
<<<                                  >>>
<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

func test()
   for i = 1 to 10
      if i % 2 = 0
         see "Even: " + i // Coment after see
      else
         see "Odd: " + i
      ok
   next

// Slash 1 comment
// Slash 2 again

# Pound 1 pound
# Pound 2 again

   for outer = 1 to 3
      see "Outer loop: " + outer
      for inner = 1 to 2
         see "  Inner loop: " + inner
         for deep = 1 to 2
            see "    Deep loop: " + deep
         next
      next
   next

/* multi 1 Line of 3
   multi 2
   nulti 3
*/

   a = 88 // Coment after A-88
   b = 77 # Comment after B-77
   See "Data: " + a + nl
   return
end

Statistics:
  Original lines: 35
  Formatted lines: 35
  Indent size: 3 spaces

MsgWarning:
MsgErrors.:

Press Enter to continue...

STEPS-Create-RingPM-CodeFormatter.txt
CodeFormatter.ring

Bert Mariani

unread,
Sep 6, 2025, 4:30:30 PM (8 days ago) Sep 6
to The Ring Programming Language
Hello ALL

#--- NEEDS to Install following Library to Run the Dialog to Read from File
load "dialog.ring"       // ringpm install dialog from ysdragon

Mahmoud Fayed

unread,
Sep 6, 2025, 10:50:17 PM (7 days ago) Sep 6
to The Ring Programming Language
Hello Bert

(1) Thanks for sharing and using RingPM :D

(2) Ring 1.24 contains RingFmt tool: ring/tools/ringfmt at master · ring-lang/ring

When you build Ring 1.24 from source code, using ring/build/buildvc_x64.bat
You can use ringfmt directly as any other tool like ring, ring2exe, ringpm, ringrepl, etc.

(3) In RingFmt we use ring_state_stringtokens() to get the file tokens which is important for multiple reasons

* Better Performance, since this function uses Ring Scanner to get the file tokens
* Handling different Ring features like 
** Multi-line Comments
** Multi-line Literals
** Separating Keywords/Operators/Identifiers/EndLines
** Executing Scanner Commands
* Reducing testing efforts where we test how RingFmt combine tokens, which reduce input/output space compared to processing source code files directly

(4) In RingFmt, we just use (stdlibcore.ring & tokenslib.ring) - No other extensions/libraries
So, building/using RingFmt is similar to other tools like Ring2EXE and RingPM

(5) Testing RingFmt requires trying it with ring/language/tests/scripts, ring/applications, ring/samples, ring/tools, PWCT2 source code, etc. This means testing different coding styles and being sure that the tool is usable with any project and doesn't generate wrong code or unacceptable format.

Summary: RingFmt development in Ring 1.24 is almost done, I expect to finish testing, add more features during the next few days. So I focus on this (Developing/Testing RingFmt) and I can't invest effort in testing other implementations.

RingFmt is already based on our efforts as a team, since I studied Bert & Azzeddine source code, and this is stated when we run RingFmt

===========================================================================
RingFmt (Source code formatter and beautifier)
===========================================================================
2025, Mahmoud Fayed <msfcl...@yahoo.com>
Prototyping: Bert Mariani & Azzeddine Remmal
===========================================================================


Greetings,
Mahmoud

Mahmoud Fayed

unread,
Sep 6, 2025, 11:05:45 PM (7 days ago) Sep 6
to The Ring Programming Language
Hello Bert

Please revise the package.ring file to avoid missing files

installingpackage.png

Greetings,
Mahmoud

On Saturday, September 6, 2025 at 11:23:20 PM UTC+3 Bert Mariani wrote:

Mahmoud Fayed

unread,
Sep 6, 2025, 11:25:41 PM (7 days ago) Sep 6
to The Ring Programming Language
Hello Bert

Trying RingCodeFormatter (shared in this topic) with source code file from PWCT2 project

Would lead to errors as in the next screen shot:
errors.png

i.e. When using multi-line comments, or writing if-statement conditions in multiple lines ----> Wrong indentation

Such errors don't exist in official RingFmt (In Ring 1.24) 
---> Why? 
-------> (1) Because RingFmt process source code files as Tokens (produced by Ring Scanner) and we have specific types of tokens (Keyword, Operator, Identifier, String Literal, Number, EndLine, Comment) - and we know that the file contains N tokens where the relationship between these tokens is (next-to), at each token we think about the token type, the type of the previous token or the type of the next token. This leads to limited number of possibilities. The code is written taking these possibilities in mind and handling only the cases that matters.
-------> (2) Because of testing, trying to discover errors, getting better understanding of the problem, and handling such errors to avoid it in a general way that avoid the original cause of the error.

Greetings,
Mahmoud 
 

On Saturday, September 6, 2025 at 11:23:20 PM UTC+3 Bert Mariani wrote:

Bert Mariani

unread,
Sep 7, 2025, 2:35:31 PM (7 days ago) Sep 7
to The Ring Programming Language
Hello Mahmoud

c:\ring>ringpm install RingCodeFormatter from umariani
Installing RingCodeFormatter (master) ---> CodeFormatter (1.0.1)

Download Package Files For RingCodeFormatter
Download File : CodeFormatter.ring

I had Deleted the lib.ring and main.ring in GitHub Gui
Should have done the same thing on my Laptop for the package.ring file
In package.ring removed the references to those files.
A bit of a back and forth to make sure both sides agreed to what was posted.

Attached are my notes on creating the RingPM files
    - STEPS-Create-RingPM-CodeFormatter.txt

Note:
  - Make sure the files on the Laptop directory Match those in GitHub Gui
  - Edit package.ring to remove any files not used, Update the version in 2 places
  - Check GitHub Gui  package.ring to make sure it matches
  - 
STEPS-Create-RingPM-CodeFormatter.txt

Bert Mariani

unread,
Sep 7, 2025, 3:17:58 PM (7 days ago) Sep 7
to The Ring Programming Language
Hello Mahmoud

I typed in the code (similar) from your screen shot.
Used CodeFormatter.ring    ### WORKS in Ring 1.23
   - Option 2. Format code from manual input

This Output is Below
 - The IF / ELSEIF /  }    aligns, but a MIX of style , Should have used extra ( ...)  around the IF .. OR
 - The   { ... } After the "OR"  seem to match but are unnecessary when extra (...) are used

func TreeKeyPress
   nKey = oView()
   cText = oView()
   if.oView() = 0 { # no Ctl
# Keys from
      if (( nkey >= 65 ) and (nKey <= 90 )) or
         (( nkey >= 1575 ) and (nKey <= 1610 ))  {
            UpdateSearchField(cText)
         }
      elseif nKey = 63 # writing ? operator
         UpdateSearchField(cText)
      }
      oView(False)


==========================
Formatting all sorts of code 
Is becoming similar to  Arabian Tales:   One Thousand and One Nights 

=========================
USING Extra (...) on the AND OR condition

func TreeKeyPress
   nKey = oView()
   cText = oView()
   if.oView() = 0 { # no Ctl
# Keys from
      if ( (( nkey >= 65 ) and (nKey <= 90 )) or
         (( nkey >= 1575 ) and (nKey <= 1610 )) )
         UpdateSearchField(cText)
      elseif nKey = 63 # writing ? operator
         UpdateSearchField(cText)
      }
      oView(False)


MsgWarning:
WARNING - Code validation errors:
 |
MsgErrors.:
ERROR: 1 unclosed blocks detected
 |

Reply all
Reply to author
Forward
0 new messages