Recent

Author Topic: Online Pascal code beautifier.  (Read 4758 times)

bee

  • Sr. Member
  • ****
  • Posts: 356
Online Pascal code beautifier.
« on: June 02, 2018, 09:10:21 am »
In case someone need it, here I've made an online Pascal code beautifier. It's utilizing Jedi Code Formatter that I modified as a console app or JCF CLI (command line interface) as the backend beautifier engine.

Hopefully it's gonna be useful to other Pascal fellows out there. Thank you :)
-Bee-

A long time pascal lover.

yann

  • Newbie
  • Posts: 3
Re: Online Pascal code beautifier.
« Reply #1 on: February 26, 2019, 10:55:27 pm »
Nice!

dbannon

  • Hero Member
  • *****
  • Posts: 564
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Online Pascal code beautifier.
« Reply #2 on: February 27, 2019, 12:28:49 am »
Nice indeed !

But (always some 'buts') :

My code could do with some beautifying but I don't particularly like your defaults. Its changed my 4 space tabs to 2 space (but not in cases ?). I guess you like a two space indent - would it be possible to make it configurable ?

(fpc does have a fully configurable lint like tool but I have not tried it ...)

Anyway, thats a nice system ! Thanks.

Davo
Lazarus 1.8, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Thaddy

  • Hero Member
  • *****
  • Posts: 7885
Re: Online Pascal code beautifier.
« Reply #3 on: February 27, 2019, 01:52:28 am »
You can also run ptop as a cgi, btw..Did you know that?
Hamlet 1.4 (nothing wrong with the Danish, btw)

bee

  • Sr. Member
  • ****
  • Posts: 356
Re: Online Pascal code beautifier.
« Reply #4 on: February 27, 2019, 03:13:02 am »
Nice indeed !
Thank you. :)

My code could do with some beautifying but I don't particularly like your defaults. Its changed my 4 space tabs to 2 space (but not in cases ?). I guess you like a two space indent - would it be possible to make it configurable ?
I've been thinking about that but (there's always some 'buts' of course) I don't have times to play around with it anymore. Let's see what I can do when I have time with it again. Sorry.

Anyway, thats a nice system ! Thanks.
You're welcome. FYI, it serves about 30-50 formatting requests per day. I know it's not much, but I'm glad to see it's useful to someone else out there.

You can also run ptop as a cgi, btw.. Did you know that?
Of course. But I prefer JCF for some reasons. Nothing wrong with that, right? 😊
-Bee-

A long time pascal lover.

tr_escape

  • Sr. Member
  • ****
  • Posts: 375
  • sector name toys | respect to spectre
    • Github:
Re: Online Pascal code beautifier.
« Reply #5 on: February 27, 2019, 06:27:38 am »
I just try by crc16.pas it gives:



JCF OUTPUT MESSAGE:
Formatting test.pas:
test.pas(233,23) Error Exception TEParseError unexpected token in factor Near >
Aborted due to error
...done!

I also tried another unit and I realised you haven't got any column limiter like as 80


Thats unit:

Code: Pascal  [Select]
  1. (*
  2.  * Library: crc16
  3.  * File:    crc16.pas
  4.  * Huge parts based on libcrc sourcecode
  5.  * Orginal Author:  Lammert Bies (libcrc in c)
  6.  *
  7.  * Pascal version Author: Thaddy de Koning (Pascal translation and additional code)
  8.  *
  9.  * This file is licensed under the MIT License as stated below
  10.  *
  11.  * Original C version Copyright (c) 1999-2016 Lammert Bies
  12.  * Pascal version Copyright (c) 2017 Thaddy de Koning
  13.  *
  14.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  15.  * of this software and associated documentation files (the "Software"), to deal
  16.  * in the Software without restriction, including without limitation the rights
  17.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18.  * copies of the Software, and to permit persons to whom the Software is
  19.  * furnished to do so, subject to the following conditions:
  20.  *
  21.  * The above copyright notice and this permission notice shall be included in all
  22.  * copies or substantial portions of the Software.
  23.  *
  24.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30.  * SOFTWARE.
  31.  *)
  32.  
  33. unit crc16;
  34. {$mode objfpc}
  35. interface
  36. const
  37. { Polynomials }
  38.   CRC_POLY_16 = $A001;    
  39.   CRC_POLY_CCITT = $1021;    
  40.   CRC_POLY_DNP = $A6BC;    
  41.   CRC_POLY_KERMIT = $8408;    
  42.   CRC_POLY_SICK = $8005;  
  43.    
  44. { Start values }  
  45.   CRC_START_16 = $0000;    
  46.   CRC_START_MODBUS = $FFFF;    
  47.   CRC_START_XMODEM = $0000;    
  48.   CRC_START_CCITT_1D0F = $1D0F;    
  49.   CRC_START_CCITT_FFFF = $FFFF;    
  50.   CRC_START_KERMIT = $0000;    
  51.   CRC_START_SICK = $0000;    
  52.   CRC_START_DNP = $0000;    
  53.  
  54. {$push}{$J+}
  55.   crc_tab16_init:Boolean = true;
  56.   crc_tab16: array[0..255] of word =(
  57.   $0000, $C0C1, $C181, $0140, $C301, $03C0, $0280, $C241,
  58.   $C601, $06C0, $0780, $C741, $0500, $C5C1, $C481, $0440,
  59.   $CC01, $0CC0, $0D80, $CD41, $0F00, $CFC1, $CE81, $0E40,
  60.   $0A00, $CAC1, $CB81, $0B40, $C901, $09C0, $0880, $C841,
  61.   $D801, $18C0, $1980, $D941, $1B00, $DBC1, $DA81, $1A40,
  62.   $1E00, $DEC1, $DF81, $1F40, $DD01, $1DC0, $1C80, $DC41,
  63.   $1400, $D4C1, $D581, $1540, $D701, $17C0, $1680, $D641,
  64.   $D201, $12C0, $1380, $D341, $1100, $D1C1, $D081, $1040,
  65.   $F001, $30C0, $3180, $F141, $3300, $F3C1, $F281, $3240,
  66.   $3600, $F6C1, $F781, $3740, $F501, $35C0, $3480, $F441,
  67.   $3C00, $FCC1, $FD81, $3D40, $FF01, $3FC0, $3E80, $FE41,
  68.   $FA01, $3AC0, $3B80, $FB41, $3900, $F9C1, $F881, $3840,
  69.   $2800, $E8C1, $E981, $2940, $EB01, $2BC0, $2A80, $EA41,
  70.   $EE01, $2EC0, $2F80, $EF41, $2D00, $EDC1, $EC81, $2C40,
  71.   $E401, $24C0, $2580, $E541, $2700, $E7C1, $E681, $2640,
  72.   $2200, $E2C1, $E381, $2340, $E101, $21C0, $2080, $E041,
  73.   $A001, $60C0, $6180, $A141, $6300, $A3C1, $A281, $6240,
  74.   $6600, $A6C1, $A781, $6740, $A501, $65C0, $6480, $A441,
  75.   $6C00, $ACC1, $AD81, $6D40, $AF01, $6FC0, $6E80, $AE41,
  76.   $AA01, $6AC0, $6B80, $AB41, $6900, $A9C1, $A881, $6840,
  77.   $7800, $B8C1, $B981, $7940, $BB01, $7BC0, $7A80, $BA41,
  78.   $BE01, $7EC0, $7F80, $BF41, $7D00, $BDC1, $BC81, $7C40,
  79.   $B401, $74C0, $7580, $B541, $7700, $B7C1, $B681, $7640,
  80.   $7200, $B2C1, $B381, $7340, $B101, $71C0, $7080, $B041,
  81.   $5000, $90C1, $9181, $5140, $9301, $53C0, $5280, $9241,
  82.   $9601, $56C0, $5780, $9741, $5500, $95C1, $9481, $5440,
  83.   $9C01, $5CC0, $5D80, $9D41, $5F00, $9FC1, $9E81, $5E40,
  84.   $5A00, $9AC1, $9B81, $5B40, $9901, $59C0, $5880, $9841,
  85.   $8801, $48C0, $4980, $8941, $4B00, $8BC1, $8A81, $4A40,
  86.   $4E00, $8EC1, $8F81, $4F40, $8D01, $4DC0, $4C80, $8C41,
  87.   $4400, $84C1, $8581, $4540, $8701, $47C0, $4680, $8641,
  88.   $8201, $42C0, $4380, $8341, $4100, $81C1, $8081, $4040 );
  89.  
  90.   crc_tabccitt_init:boolean = true;
  91.   crc_tabccitt:array[0..255] of word = (
  92.   $0000, $1021, $2042, $3063, $4084, $50A5, $60C6, $70E7,
  93.   $8108, $9129, $A14A, $B16B, $C18C, $D1AD, $E1CE, $F1EF,
  94.   $1231, $0210, $3273, $2252, $52B5, $4294, $72F7, $62D6,
  95.   $9339, $8318, $B37B, $A35A, $D3BD, $C39C, $F3FF, $E3DE,
  96.   $2462, $3443, $0420, $1401, $64E6, $74C7, $44A4, $5485,
  97.   $A56A, $B54B, $8528, $9509, $E5EE, $F5CF, $C5AC, $D58D,
  98.   $3653, $2672, $1611, $0630, $76D7, $66F6, $5695, $46B4,
  99.   $B75B, $A77A, $9719, $8738, $F7DF, $E7FE, $D79D, $C7BC,
  100.   $48C4, $58E5, $6886, $78A7, $0840, $1861, $2802, $3823,
  101.   $C9CC, $D9ED, $E98E, $F9AF, $8948, $9969, $A90A, $B92B,
  102.   $5AF5, $4AD4, $7AB7, $6A96, $1A71, $0A50, $3A33, $2A12,
  103.   $DBFD, $CBDC, $FBBF, $EB9E, $9B79, $8B58, $BB3B, $AB1A,
  104.   $6CA6, $7C87, $4CE4, $5CC5, $2C22, $3C03, $0C60, $1C41,
  105.   $EDAE, $FD8F, $CDEC, $DDCD, $AD2A, $BD0B, $8D68, $9D49,
  106.   $7E97, $6EB6, $5ED5, $4EF4, $3E13, $2E32, $1E51, $0E70,
  107.   $FF9F, $EFBE, $DFDD, $CFFC, $BF1B, $AF3A, $9F59, $8F78,
  108.   $9188, $81A9, $B1CA, $A1EB, $D10C, $C12D, $F14E, $E16F,
  109.   $1080, $00A1, $30C2, $20E3, $5004, $4025, $7046, $6067,
  110.   $83B9, $9398, $A3FB, $B3DA, $C33D, $D31C, $E37F, $F35E,
  111.   $02B1, $1290, $22F3, $32D2, $4235, $5214, $6277, $7256,
  112.   $B5EA, $A5CB, $95A8, $8589, $F56E, $E54F, $D52C, $C50D,
  113.   $34E2, $24C3, $14A0, $0481, $7466, $6447, $5424, $4405,
  114.   $A7DB, $B7FA, $8799, $97B8, $E75F, $F77E, $C71D, $D73C,
  115.   $26D3, $36F2, $0691, $16B0, $6657, $7676, $4615, $5634,
  116.   $D94C, $C96D, $F90E, $E92F, $99C8, $89E9, $B98A, $A9AB,
  117.   $5844, $4865, $7806, $6827, $18C0, $08E1, $3882, $28A3,
  118.   $CB7D, $DB5C, $EB3F, $FB1E, $8BF9, $9BD8, $ABBB, $BB9A,
  119.   $4A75, $5A54, $6A37, $7A16, $0AF1, $1AD0, $2AB3, $3A92,
  120.   $FD2E, $ED0F, $DD6C, $CD4D, $BDAA, $AD8B, $9DE8, $8DC9,
  121.   $7C26, $6C07, $5C64, $4C45, $3CA2, $2C83, $1CE0, $0CC1,
  122.   $EF1F, $FF3E, $CF5D, $DF7C, $AF9B, $BFBA, $8FD9, $9FF8,
  123.   $6E17, $7E36, $4E55, $5E74, $2E93, $3EB2, $0ED1, $1EF0);
  124.  
  125. crc_tabkermit_init:boolean = true;
  126. var
  127. crc_tabkermit:array[0..255] of word = (
  128.   $0000, $1189, $2312, $329B, $4624, $57AD, $6536, $74BF,
  129.   $8C48, $9DC1, $AF5A, $BED3, $CA6C, $DBE5, $E97E, $F8F7,
  130.   $1081, $0108, $3393, $221A, $56A5, $472C, $75B7, $643E,
  131.   $9CC9, $8D40, $BFDB, $AE52, $DAED, $CB64, $F9FF, $E876,
  132.   $2102, $308B, $0210, $1399, $6726, $76AF, $4434, $55BD,
  133.   $AD4A, $BCC3, $8E58, $9FD1, $EB6E, $FAE7, $C87C, $D9F5,
  134.   $3183, $200A, $1291, $0318, $77A7, $662E, $54B5, $453C,
  135.   $BDCB, $AC42, $9ED9, $8F50, $FBEF, $EA66, $D8FD, $C974,
  136.   $4204, $538D, $6116, $709F, $0420, $15A9, $2732, $36BB,
  137.   $CE4C, $DFC5, $ED5E, $FCD7, $8868, $99E1, $AB7A, $BAF3,
  138.   $5285, $430C, $7197, $601E, $14A1, $0528, $37B3, $263A,
  139.   $DECD, $CF44, $FDDF, $EC56, $98E9, $8960, $BBFB, $AA72,
  140.   $6306, $728F, $4014, $519D, $2522, $34AB, $0630, $17B9,
  141.   $EF4E, $FEC7, $CC5C, $DDD5, $A96A, $B8E3, $8A78, $9BF1,
  142.   $7387, $620E, $5095, $411C, $35A3, $242A, $16B1, $0738,
  143.   $FFCF, $EE46, $DCDD, $CD54, $B9EB, $A862, $9AF9, $8B70,
  144.   $8408, $9581, $A71A, $B693, $C22C, $D3A5, $E13E, $F0B7,
  145.   $0840, $19C9, $2B52, $3ADB, $4E64, $5FED, $6D76, $7CFF,
  146.   $9489, $8500, $B79B, $A612, $D2AD, $C324, $F1BF, $E036,
  147.   $18C1, $0948, $3BD3, $2A5A, $5EE5, $4F6C, $7DF7, $6C7E,
  148.   $A50A, $B483, $8618, $9791, $E32E, $F2A7, $C03C, $D1B5,
  149.   $2942, $38CB, $0A50, $1BD9, $6F66, $7EEF, $4C74, $5DFD,
  150.   $B58B, $A402, $9699, $8710, $F3AF, $E226, $D0BD, $C134,
  151.   $39C3, $284A, $1AD1, $0B58, $7FE7, $6E6E, $5CF5, $4D7C,
  152.   $C60C, $D785, $E51E, $F497, $8028, $91A1, $A33A, $B2B3,
  153.   $4A44, $5BCD, $6956, $78DF, $0C60, $1DE9, $2F72, $3EFB,
  154.   $D68D, $C704, $F59F, $E416, $90A9, $8120, $B3BB, $A232,
  155.   $5AC5, $4B4C, $79D7, $685E, $1CE1, $0D68, $3FF3, $2E7A,
  156.   $E70E, $F687, $C41C, $D595, $A12A, $B0A3, $8238, $93B1,
  157.   $6B46, $7ACF, $4854, $59DD, $2D62, $3CEB, $0E70, $1FF9,
  158.   $F78F, $E606, $D49D, $C514, $B1AB, $A022, $92B9, $8330,
  159.   $7BC7, $6A4E, $58D5, $495C, $3DE3, $2C6A, $1EF1, $0F78);
  160.  
  161. crc_tabdnp_init:Boolean = true;
  162. crc_tabdnp:array[0..255] of word =(
  163.   $0000, $365E, $6CBC, $5AE2, $D978, $EF26, $B5C4, $839A,
  164.   $FF89, $C9D7, $9335, $A56B, $26F1, $10AF, $4A4D, $7C13,
  165.   $B26B, $8435, $DED7, $E889, $6B13, $5D4D, $07AF, $31F1,
  166.   $4DE2, $7BBC, $215E, $1700, $949A, $A2C4, $F826, $CE78,
  167.   $29AF, $1FF1, $4513, $734D, $F0D7, $C689, $9C6B, $AA35,
  168.   $D626, $E078, $BA9A, $8CC4, $0F5E, $3900, $63E2, $55BC,
  169.   $9BC4, $AD9A, $F778, $C126, $42BC, $74E2, $2E00, $185E,
  170.   $644D, $5213, $08F1, $3EAF, $BD35, $8B6B, $D189, $E7D7,
  171.   $535E, $6500, $3FE2, $09BC, $8A26, $BC78, $E69A, $D0C4,
  172.   $ACD7, $9A89, $C06B, $F635, $75AF, $43F1, $1913, $2F4D,
  173.   $E135, $D76B, $8D89, $BBD7, $384D, $0E13, $54F1, $62AF,
  174.   $1EBC, $28E2, $7200, $445E, $C7C4, $F19A, $AB78, $9D26,
  175.   $7AF1, $4CAF, $164D, $2013, $A389, $95D7, $CF35, $F96B,
  176.   $8578, $B326, $E9C4, $DF9A, $5C00, $6A5E, $30BC, $06E2,
  177.   $C89A, $FEC4, $A426, $9278, $11E2, $27BC, $7D5E, $4B00,
  178.   $3713, $014D, $5BAF, $6DF1, $EE6B, $D835, $82D7, $B489,
  179.   $A6BC, $90E2, $CA00, $FC5E, $7FC4, $499A, $1378, $2526,
  180.   $5935, $6F6B, $3589, $03D7, $804D, $B613, $ECF1, $DAAF,
  181.   $14D7, $2289, $786B, $4E35, $CDAF, $FBF1, $A113, $974D,
  182.   $EB5E, $DD00, $87E2, $B1BC, $3226, $0478, $5E9A, $68C4,
  183.   $8F13, $B94D, $E3AF, $D5F1, $566B, $6035, $3AD7, $0C89,
  184.   $709A, $46C4, $1C26, $2A78, $A9E2, $9FBC, $C55E, $F300,
  185.   $3D78, $0B26, $51C4, $679A, $E400, $D25E, $88BC, $BEE2,
  186.   $C2F1, $F4AF, $AE4D, $9813, $1B89, $2DD7, $7735, $416B,
  187.   $F5E2, $C3BC, $995E, $AF00, $2C9A, $1AC4, $4026, $7678,
  188.   $0A6B, $3C35, $66D7, $5089, $D313, $E54D, $BFAF, $89F1,
  189.   $4789, $71D7, $2B35, $1D6B, $9EF1, $A8AF, $F24D, $C413,
  190.   $B800, $8E5E, $D4BC, $E2E2, $6178, $5726, $0DC4, $3B9A,
  191.   $DC4D, $EA13, $B0F1, $86AF, $0535, $336B, $6989, $5FD7,
  192.   $23C4, $159A, $4F78, $7926, $FABC, $CCE2, $9600, $A05E,
  193.   $6E26, $5878, $029A, $34C4, $B75E, $8100, $DBE2, $EDBC,
  194.   $91AF, $A7F1, $FD13, $CB4D, $48D7, $7E89, $246B, $1235);
  195. {$pop}
  196.  
  197. { Array initializations for different CRC's }
  198. procedure init_crc16_tab;
  199. procedure init_crcccitt_tab;
  200. procedure init_crckermit_tab;
  201. procedure init_crcdnp_tab;
  202.  
  203. { CRC16 implementations }
  204. function crc_16( const input_str:Pbyte;num_bytes:integer ):word;inline;
  205. function crc_modbus( const input_str:Pbyte; num_bytes:integer):word;inline;
  206. function crc_ccitt_generic( const input_str:PByte; num_bytes:integer;start_value:word ):word;inline;
  207. function crc_xmodem( const input_str:PByte;num_bytes:integer):word;inline;
  208. function crc_ccitt_1d0f( const input_str:PByte;num_bytes:integer):word;inline;
  209. function crc_ccitt_ffff( const input_str:PByte;num_bytes:integer):word;inline;
  210. function crc_kermit( const input_str:PByte; num_bytes:integer ):word;inline;
  211. function crc_dnp( const input_str:PByte; num_bytes:Integer):word;
  212. function crc_sick( const input_str:PByte; num_bytes:integer ):word;
  213.  
  214. { Update functions }
  215. function update_crc_ccitt( crc:word;c:byte ):word; inline;
  216. function update_crc_kermit( crc:word; c:byte ):word;
  217. function update_crc_dnp(crc:word; c:byte ):word;inline;
  218. function update_crc_sick(crc:word; c,prev_byte:Byte ):word;inline;
  219.  
  220. implementation
  221.  
  222. procedure init_crc16_tab;
  223. var
  224.  i,j,crc,c:word;
  225. begin
  226.   for i := 0 to 255 do
  227.   begin
  228.         crc := 0;
  229.         c   := i;
  230.         for j := 0 to 7 do
  231.         begin
  232.           if  (crc xor c) and $0001  > 0 then
  233.         crc := ( crc >> 1 ) xor CRC_POLY_16
  234.           else                      
  235.             crc :=   crc >> 1;
  236.           c := c >> 1;
  237.     end;
  238.         crc_tab16[i] := crc;
  239.   end;
  240.   crc_tab16_init := true;
  241. end;
  242.  
  243. procedure init_crcccitt_tab;
  244. var
  245.  i,j,crc,c:word;
  246. begin
  247.   for i := 0 to 255 do
  248.   begin
  249.         crc := 0;
  250.         c   := i << 8;
  251.         for j :=0 to 7 do
  252.         begin
  253.           if (crc xor c) and $8000 <> 0 then
  254.             crc := ( crc << 1 ) xor CRC_POLY_CCITT
  255.           else                      
  256.             crc := crc << 1;
  257.       c := c << 1;
  258.         end;
  259.         crc_tabccitt[i] := crc;
  260.   end;
  261.   crc_tabccitt_init := true;
  262. end;
  263.  
  264. procedure init_crckermit_tab;
  265. var
  266.   i,j,crc,c:word;
  267. begin
  268.   for i:=0 to 255 do
  269.   begin
  270.     crc := 0;
  271.         c   := i;
  272.         for j := 0 to 7 do
  273.         begin
  274.           if ( (crc xor c) and $0001 ) <> 0 then
  275.             crc := ( crc >> 1 ) xor CRC_POLY_KERMIT
  276.           else                      
  277.             crc := crc >> 1;
  278.           c := c >> 1;
  279.         end;
  280.     crc_tabkermit[i] := crc;
  281.   end;
  282.   crc_tabkermit_init := true;
  283. end;
  284.  
  285. procedure init_crcdnp_tab;
  286. var
  287.   i,j,crc,c:word;
  288. begin
  289.   for i := 0 to 255 do
  290.   begin
  291.     crc := 0;
  292.         c := i;
  293.         for j :=0 to 7 do
  294.         begin
  295.       if ( (crc xor c) and $0001 ) <> 0 then
  296.         crc := ( crc >> 1 ) xor CRC_POLY_DNP
  297.           else                      
  298.             crc := crc >> 1;
  299.           c := c >> 1;
  300.         end;
  301.     crc_tabdnp[i] := crc;
  302.   end;
  303.   crc_tabdnp_init := true;
  304. end;
  305.  
  306. function crc_16( const input_str:Pbyte;num_bytes:integer ):word;
  307. var
  308.   ptr:PByte;
  309.   a:integer;
  310. begin
  311.         if not crc_tab16_init then init_crc16_tab;
  312.         result := CRC_START_16;
  313.         ptr := input_str;
  314.         if ptr <> nil then for a :=0 to pred(num_bytes) do
  315.     begin      
  316.           Result := (Result >> 8) xor crc_tab16[ (Result xor ptr^) and $00FF ];
  317.           inc(ptr);
  318.         end;
  319. end;
  320.  
  321. function crc_modbus( const input_str:Pbyte; num_bytes:integer):word;
  322. var
  323.   ptr:PByte;
  324.   a:integer;
  325. begin
  326.   if not crc_tab16_init then init_crc16_tab;
  327.   result := CRC_START_MODBUS;
  328.   ptr := input_str;
  329.   if ptr <> nil then for a := 0 to pred(num_bytes) do
  330.   begin
  331.         result := (result >> 8) xor crc_tab16[ (result xor ptr^) and $00FF ];
  332.         inc(ptr);
  333.   end;
  334. end;
  335.  
  336. function crc_ccitt_generic( const input_str:PByte; num_bytes:integer;start_value:word ):word;
  337. var
  338.  ptr:PByte;
  339.  a:integer;
  340. begin
  341.   if not crc_tabccitt_init then init_crcccitt_tab;
  342.   Result := start_value;
  343.   ptr := input_str;
  344.   if ptr <> nil then for  a := 0 to pred(num_bytes) do
  345.   begin
  346.     Result := (Result << 8) xor crc_tabccitt[ ((Result >> 8) xor ptr^) and $00FF ];
  347.         inc(ptr);
  348.   end;
  349. end;
  350.  
  351. function update_crc_ccitt( crc:word;c:byte ):word; inline;
  352. begin
  353.   if not crc_tabccitt_init then init_crcccitt_tab;
  354.   result := (crc << 8) xor crc_tabccitt[ ((crc >> 8) xor word(c)) and $00FF ];
  355. end;
  356.  
  357. function crc_xmodem( const input_str:PByte;num_bytes:integer):word;inline;
  358. begin
  359.  result:= crc_ccitt_generic( input_str, num_bytes, CRC_START_XMODEM );
  360. end;
  361.  
  362. function crc_ccitt_1d0f( const input_str:PByte;num_bytes:integer):word;inline;
  363. begin
  364.  result:= crc_ccitt_generic( input_str, num_bytes, CRC_START_CCITT_1D0F );
  365. end;
  366.  
  367. function crc_ccitt_ffff( const input_str:PByte;num_bytes:integer):word;inline;
  368. begin
  369.  result:= crc_ccitt_generic( input_str, num_bytes, CRC_START_CCITT_FFFF );
  370. end;
  371.  
  372. function crc_kermit( const input_str:PByte; num_bytes:integer ):word;
  373. var
  374.   crc,low_byte,high_byte:word;
  375.   ptr:PByte;
  376.   a:integer;
  377. begin
  378.   if not crc_tabkermit_init then init_crckermit_tab;
  379.   crc := CRC_START_KERMIT;
  380.   ptr := input_str;
  381.   if ptr <> nil then for a := 0 to pred(num_bytes) do
  382.   begin
  383.     crc := (crc >> 8) xor crc_tabkermit[ (crc xor word(ptr^)) and $00FF ];
  384.     inc(ptr);
  385.   end;
  386.   low_byte  := (crc and $ff00) >> 8;
  387.   high_byte := (crc and $00ff) << 8;
  388.   crc := low_byte or high_byte;
  389.   Result := crc;
  390. end;
  391.  
  392. function update_crc_kermit( crc:word; c:byte ):word;
  393. begin
  394.   if not crc_tabkermit_init then  init_crckermit_tab;
  395.   Result := (crc >> 8) xor crc_tabkermit[ (crc xor dword(c)) and $00FF ];
  396. end;
  397.  
  398. function crc_dnp( const input_str:PByte; num_bytes:Integer):word;
  399. var
  400.   low_byte,high_byte:word;
  401.   ptr:PByte;
  402.   a:integer;
  403. begin
  404.   if not crc_tabdnp_init then init_crcdnp_tab;
  405.   Result := CRC_START_DNP;
  406.   ptr := input_str;
  407.   if ptr <> nil then for a :=0 to pred(num_bytes) do
  408.   begin
  409.     Result := (Result >> 8) xor crc_tabdnp[ (Result xor ptr^) and $00FF ];
  410.     inc(ptr);
  411.   end;
  412.   Result := not Result;
  413.   low_byte := (Result and $ff00) >> 8;
  414.   high_byte := (Result and $00ff) << 8;
  415.   Result := low_byte or high_byte;
  416. end;
  417.  
  418. function update_crc_dnp(crc:word; c:byte ):word;inline;
  419. begin
  420.   if not crc_tabdnp_init then init_crcdnp_tab;
  421.   result := (crc >> 8) xor crc_tabdnp[ (crc xor dword(c)) and $00FF ];
  422. end;
  423.  
  424. function crc_sick( const input_str:PByte; num_bytes:integer ):word;
  425. var
  426.   low_byte,high_byte,short_c,short_p:word;
  427.   ptr:PByte;
  428.   a:integer;
  429. begin
  430.   Result := CRC_START_SICK;
  431.   ptr:= input_str;
  432.   short_p := 0;
  433.   if ptr <> nil then for a := 0 to pred(num_bytes) do
  434.   begin
  435.     short_c := $00FF and word(ptr^);
  436.     if ( Result and $8000 ) <> 0 then
  437.       Result := ( Result << 1 ) xor CRC_POLY_SICK      
  438.         else                
  439.           Result := Result << 1;
  440.         Result := Result xor ( short_c or short_p );
  441.         short_p := short_c << 8;
  442.     inc(ptr);
  443.   end;
  444.  
  445.   low_byte  := (Result and $FF00) >> 8;
  446.   high_byte := (Result and $00FF) << 8;
  447.   Result := low_byte or high_byte;
  448. end;
  449.  
  450. function update_crc_sick(crc:word; c,prev_byte:Byte ):word;inline;
  451. var
  452.   short_c,short_p:word;
  453. begin
  454.   short_c := $00FF and dword(c);
  455.   short_p := ( $00FF and dword(prev_byte) ) << 8;
  456.   if ( crc and $8000 ) <> 0 then
  457.     crc := ( crc << 1 ) xor CRC_POLY_SICK
  458.   else                
  459.     crc :=   crc << 1;
  460.   crc := crc xor ( short_c or short_p );
  461.   result := crc;
  462. end;
  463.  
  464.  
  465. end.
  466.  


bee

  • Sr. Member
  • ****
  • Posts: 356
Re: Online Pascal code beautifier.
« Reply #6 on: February 27, 2019, 08:08:17 am »
JCF OUTPUT MESSAGE:
Formatting test.pas:
test.pas(233,23) Error Exception TEParseError unexpected token in factor Near >
Aborted due to error
…done!
Keep in mind that it's using Jedi Code Formatter engine, not my own. I just removed the dependency of GUI so I can run it as a console application and the back-end of a web application. So if you find any errors with the formatter, the actual problem lies in JCF's engine, not my web application. You should report it to JCF's author.

I also tried another unit and I realised you haven't got any column limiter like as 80
It's 2019 now. Our screen size is no longer 80x25 chars. I set the column limiter to 120.
-Bee-

A long time pascal lover.

Thaddy

  • Hero Member
  • *****
  • Posts: 7885
Re: Online Pascal code beautifier.
« Reply #7 on: February 27, 2019, 08:42:06 am »
The problem with the error in JCF is that it only really supports Delphi syntax so it chokes on >> and << instead of shr and shl. It is also Windows oriented. Hence I suggested ptop which is in turn FreePascal oriented and cross-platform
Both are not perfect. Both are not able to handle the full combined syntax of FreePascal (all modes) and Delphi (especially newer ones) correctly.

IOW: both really need an update to the current versions, they are both lacking in maintenance.

(Side note: I translated that crc unit from C and left C style constructs like << and >>  that FPC supports intact.)
« Last Edit: February 27, 2019, 08:51:27 am by Thaddy »
Hamlet 1.4 (nothing wrong with the Danish, btw)

bee

  • Sr. Member
  • ****
  • Posts: 356
Re: Online Pascal code beautifier.
« Reply #8 on: February 27, 2019, 09:25:43 am »
The problem with the error in JCF is that it only really supports Delphi syntax so it chokes on >> and << instead of shr and shl. It is also Windows oriented. Hence I suggested ptop which is in turn FreePascal oriented and cross-platform
I was taking JCF that's being used by Lazarus. So, it is cross platform. In fact, I'm using it with VS Code on Linux and Mac (I'm not using Windows, btw).

Both are not perfect. Both are not able to handle the full combined syntax of FreePascal (all modes) and Delphi (especially newer ones) correctly.
Indeed.

IOW: both really need an update to the current versions, they are both lacking in maintenance.
Agree.

(Side note: I translated that crc unit from C and left C style constructs like << and >>  that FPC supports intact.)
It's just personal preference, but I prefer to use shl and shr instead of << and >>.
« Last Edit: February 27, 2019, 09:32:15 am by bee »
-Bee-

A long time pascal lover.

Thaddy

  • Hero Member
  • *****
  • Posts: 7885
Re: Online Pascal code beautifier.
« Reply #9 on: February 27, 2019, 10:27:31 am »
I just started to update one of them  O:-) :ptop.
Already implemented operators (that was easy) , now working on the two generics modes. If all goes well I will also implement the correct handling of C operators: += -=, *= >> << etc.
It really needs quite some work. Not surprising since its origins are even before 1984. If I fail I will simply start a new one from scratch. We have some quite fast full Pascal parsers nowadays in the distribution. May look like overkill for a source beautifier, but has a higher chance of doing it right. But first things first: let's patch ptop for its most obvious failings.
« Last Edit: February 27, 2019, 10:31:23 am by Thaddy »
Hamlet 1.4 (nothing wrong with the Danish, btw)

bee

  • Sr. Member
  • ****
  • Posts: 356
Re: Online Pascal code beautifier.
« Reply #10 on: February 27, 2019, 02:07:30 pm »
I just started to update one of them  O:-) :ptop.
Already implemented operators (that was easy) , now working on the two generics modes. If all goes well I will also implement the correct handling of C operators: += -=, *= >> << etc.
It really needs quite some work. Not surprising since its origins are even before 1984. If I fail I will simply start a new one from scratch. We have some quite fast full Pascal parsers nowadays in the distribution. May look like overkill for a source beautifier, but has a higher chance of doing it right. But first things first: let's patch ptop for its most obvious failings.
Great, Thaddy. If all goes well, I might use it too.

The main reason I made online Pascal code beautifier simply because there's none of it. If you ask Google for code beautifier, it will give you online beautifier for almost any kind of popular programming languages out there, but none for Pascal. So, I think I should made one, although it's not perfect but at least it's available. The first thing came to my mind is JCF, simply because I used to use it regularly on Delphi and Lazarus, so I worked on it and made an online JCF-CLI. Perhaps it's not perfect but some people do use it. I'm glad to see it useful. It also an evidence that online Pascal code beautifier is needed. 😊
« Last Edit: February 27, 2019, 02:09:28 pm by bee »
-Bee-

A long time pascal lover.

rvk

  • Hero Member
  • *****
  • Posts: 3664
Re: Online Pascal code beautifier.
« Reply #11 on: February 27, 2019, 02:28:58 pm »
You can also run ptop as a cgi, btw..Did you know that?
First thing I always do after installing Lazarus is installing jcfidelazarus.
Why is there no standard package for ptop provided in Lazarus? Or am I just missing it?
Although ptop.exe itself is provided in the bin-folder.

I now there is this, but of course this should be a standard provided package with Lazarus. As far as I can see it isn't even provided in the Online Package Manager.


Thaddy

  • Hero Member
  • *****
  • Posts: 7885
Re: Online Pascal code beautifier.
« Reply #12 on: February 27, 2019, 02:38:36 pm »
It is provided as standard by FPC, not Lazarus....
The full source of ptop is provided too....Feel free to assist  8-) It is in the utils directory: ptop.pp and ptopu.pp.
I frankly have no clue why Lazarus prefers a patched Jedi version..... It is just as bad....

Expect initially a couple of minor patches in the next few weeks. (the ones I am working on)
« Last Edit: February 27, 2019, 02:48:54 pm by Thaddy »
Hamlet 1.4 (nothing wrong with the Danish, btw)

rvk

  • Hero Member
  • *****
  • Posts: 3664
Re: Online Pascal code beautifier.
« Reply #13 on: February 27, 2019, 03:19:35 pm »
The full source of ptop is provided too....Feel free to assist  8-) It is in the utils directory: ptop.pp and ptopu.pp.
I frankly have no clue why Lazarus prefers a patched Jedi version..... It is just as bad....
I'm not interested in patching ptop itself (because I'm sure you will do a fantastic job with it 8-)) but I'm sometimes kinda urked about the fact JCL-formatter get's stuck with incomplete code. I didn't know ptop could do a better job there, because there is no standard integration provided in Lazarus IDE. Not even via a provided package (like it is done with JCL-formatter). Even the code-formatter in Delphi does a much better job.

If ptop has any shot at being successful in the Lazarus environment, it should have the ptopforlazarus package included. I'll try to see how far that one is. Maybe it can be added to the Online Package Manager.