📜 ⬆️ ⬇️

Perl Structural Pattern Building

We put an antimat on the same project on the basis of Ilya Soldatkin's Antimat.pm .
After some time it became necessary to correct it. It was decided that we would not consider the word “fig” to be obscene and the customer wanted to add a couple more words. How to be? The base template of the module is a string of 12706 characters. Walking along it and looking for something to rule — you don't wish the enemy.
A cursory study of the pattern found that it is divided into separate blocks for different word forms, which are listed through the logical OR (| - pipe). Each block can also consist of several alternative subblocks, for which grouping and enumeration are used round brackets and also OR.
I thought to present this template in a more readable form. Old proven recipe - structured programming - operator with a new line, nested blocks with a shift to the right. In order not to mess up with manual splitting and following the old rule “laziness is the engine of progress”, I wrote a simple parser on my knee:

  #include <stdio.h>
 void
 new_line (int d)
 {
   int i;
   putc ('\ n', stdout);
   for (i = 0; i <d; i ++) putc (', stdout);
 }
 int
 main (void)
 {
   int d = 0;
   char c, l = 0;
   while (! feof (stdin)) {
     c = getc (stdin);
     if (c == '(') {d + = 2; new_line (d);}
     putc (c, stdout); l ++;
     if (c == ')') {d- = 2; if (d <= 0) l = 0;}
     if (c == '|') {new_line (d);}
   }
 }


As a result, instead of "snakes" of the form:
  a [\ W _] * s [\ W _] * s (?: [\ W _] * e [\ W _] * s)? | f [\ W _] * u [\ W _] * c [\ W _] * k (?: [\ W _] * i ............ 

')
Got something more readable:
  a [\ W _] * s [\ W _] * s
   (?: [\ W _] * e [\ W _] * s)? |
 f [\ W _] * u [\ W _] * c [\ W _] * k
   (?: [\ W _] * i [\ W _] * n [\ W _] * g)? |
 ..................


The result is certainly not perfect, when moving to a new unit there are glitches of the form:
  ........
         (?: [\ W _] * [iiu])?)?)) |

   (?: [nhn] [\ W _] * [ee] [\ W _] *)?
   (?:
 ........


Those. for a block starting with an open bracket, formatting floats a little.
As it became easier to live, it stopped there. Now it is much easier to work with a dictionary, and upon completion of work to collect everything anew into a single line, remove spaces and new characters.

This method of structuring can be used in the design or editing of fairly complex templates - visibility is higher, and therefore less likely to make a mistake.

Source: https://habr.com/ru/post/75165/


All Articles