Changeset 21


Ignore:
Timestamp:
Jun 1, 2014, 2:09:39 AM (7 years ago)
Author:
vadim.godunko
Message:

Compute block parsing regular expression from provided by block parsers.

Remove hardcoded block parsing regular expression.

Location:
trunk/forge/source/wiki
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/forge/source/wiki/forge-wiki-block_parsers-lists.adb

    r20 r21  
    8383   begin
    8484      Forge.Wiki.Parsers.Register_Block_Parser
    85        (League.Strings.To_Universal_String (""),
     85       (League.Strings.To_Universal_String
     86         ("^\p{White_Space}*\-\p{White_Space}*(\P{White_Space})"),
    8687        1,
    8788        1,
  • trunk/forge/source/wiki/forge-wiki-parsers.adb

    r20 r21  
    6666      Total_Groups  : Positive;
    6767      Offset_Group  : Positive;
    68 --      Match_Index   : Positive;
    6968      Parser_Tag    : Ada.Tags.Tag;
    7069   end record;
     
    7675   Paragraph_Registry : Block_Parser_Information;
    7776
    78    Base_Expression       : constant League.Strings.Universal_String
     77   Separator_Expression  : constant League.Strings.Universal_String
    7978     := League.Strings.To_Universal_String
    80          ("(^\p{White_Space}*(\P{White_Space}))"  --  Regular paragraph
    81             & "|(^\p{White_Space}*$)");           --  Separator ('empty') line
    82    Para_Block_Match      : constant Positive := 1;
    83    Para_Offset_Match     : constant Positive := 2;
    84    Separator_Block_Match : constant Positive := 3;
    85    --  Base regular expression to handle paragraph's start line, block's
    86    --  continuation line and block separator line.
     79         ("|(^\p{White_Space}*$)");
     80   --  Regular expression for separator ('empty') line
    8781
    8882   -------------------------
     
    112106
    113107   procedure Initialize_Block_Regexp (Self : in out Wiki_Parser'Class) is
    114    begin
    115       Self.Block_Regexp := League.Regexps.Compile (Base_Expression);
     108      use type League.Strings.Universal_String;
     109
     110      Expression : League.Strings.Universal_String;
     111      Group      : Positive := 1;
     112
     113      procedure Append (Block : Block_Parser_Information);
     114
     115      ------------
     116      -- Append --
     117      ------------
     118
     119      procedure Append (Block : Block_Parser_Information) is
     120      begin
     121         if not Expression.Is_Empty then
     122            Expression.Append ('|');
     123         end if;
     124
     125         Expression.Append ('(' & Block.Regexp_String & ')');
     126         Self.Block_Info.Append
     127          ((Match_Group  => Group,
     128            Offset_Group => Group + Block.Offset_Group,
     129            Parser_Tag   => Block.Parser_Tag));
     130         Group := Group + Block.Total_Groups + 1;
     131      end Append;
     132
     133   begin
     134      for Block of Block_Registry loop
     135         Append (Block);
     136      end loop;
     137
     138      Append (Paragraph_Registry);
     139      Expression.Append (Separator_Expression);
     140      Self.Separator_Group := Group;
     141
     142      Self.Block_Regexp := League.Regexps.Compile (Expression);
    116143   end Initialize_Block_Regexp;
    117144
     
    128155      Match  : League.Regexps.Regexp_Match;
    129156      Offset : Natural;
     157      Found  : Boolean;
    130158
    131159   begin
     
    136164         Match := Self.Block_Regexp.Find_Match (Lines (Line));
    137165
    138 --         Put_Line (Integer'Image (Match.Capture_Count) & ' ' & Boolean'Image (Match.Is_Matched));
    139 --
    140 --         for C in 1 .. Match.Capture_Count loop
    141 --            Put_Line (Integer'Image (Match.First_Index (C)) & " .." & Integer'Image (Match.Last_Index (C)));
    142 --         end loop;
    143 
    144          if Match.First_Index (Para_Block_Match)
    145               <= Match.Last_Index (Para_Block_Match)
    146          then
    147             Offset := Match.First_Index (Para_Offset_Match);
    148 
    149             if Self.Block_State = null then
    150 --               Put_Line ("  paragraph started");
    151 
    152                Self.Block_State :=
    153                  Create_Block_Parser (Paragraph_Registry.Parser_Tag);
    154                Self.Block_State.Start_Block;
    155                Self.Block_State.Line (Lines (Line).Tail_From (Offset));
    156 
    157             else
    158 --               Put_Line ("  paragraph matched");
    159                Self.Block_State.Line (Lines (Line).Tail_From (Offset));
     166         Found := False;
     167
     168         for Index in 1 .. Natural (Self.Block_Info.Length) loop
     169            if Match.First_Index (Self.Block_Info (Index).Match_Group)
     170                 <= Match.Last_Index (Self.Block_Info (Index).Match_Group)
     171            then
     172               Offset :=
     173                 Match.First_Index (Self.Block_Info (Index).Match_Group);
     174
     175               if Self.Block_State = null then
     176                  Self.Block_State :=
     177                    Create_Block_Parser (Self.Block_Info (Index).Parser_Tag);
     178                  Self.Block_State.Start_Block;
     179                  Self.Block_State.Line (Lines (Line).Tail_From (Offset));
     180
     181               else
     182                  if Index /= Self.Block_Info.Last_Index then
     183                     Put_Line (Standard_Error, "WARNING!");
     184                     --  This is start of next block element, not handled right
     185                     --  now.
     186                  end if;
     187
     188                  Self.Block_State.Line (Lines (Line).Tail_From (Offset));
     189               end if;
     190
     191               Found := True;
     192
     193               exit;
    160194            end if;
    161 
    162          else
    163 --            Put_Line ("  block separator matched");
     195         end loop;
     196
     197         if not Found then
    164198            Self.Block_State.End_Block;
    165199            Free (Self.Block_State);
     
    199233   begin
    200234      Block_Registry.Append ((Regexp_String, Total_Groups, Offset_Group, Tag));
    201       Put_Line ("Register: " & Ada.Tags.External_Tag (Tag));
    202235   end Register_Block_Parser;
    203236
  • trunk/forge/source/wiki/forge-wiki-parsers.ads

    r20 r21  
    109109private
    110110
     111   type Block_Expression_Item is record
     112      Match_Group  : Positive;
     113      Offset_Group : Positive;
     114      Parser_Tag   : Ada.Tags.Tag;
     115   end record;
     116
     117   package Block_Expression_Vectors is
     118     new Ada.Containers.Vectors (Positive, Block_Expression_Item);
     119
    111120--   type Initial_Block_Parser is new Abstract_Block_Parser with null record;
    112121
     
    118127
    119128   type Wiki_Parser is tagged limited record
    120       Block_Regexp : League.Regexps.Regexp_Pattern;
    121       Block_State  : Block_Parser_Access;
    122       Block_Stack  : Block_Parser_Vectors.Vector;
     129      Block_Regexp    : League.Regexps.Regexp_Pattern;
     130      Block_Info      : Block_Expression_Vectors.Vector;
     131      Separator_Group : Positive;
     132      Block_State     : Block_Parser_Access;
     133      Block_Stack     : Block_Parser_Vectors.Vector;
    123134   end record;
    124135
Note: See TracChangeset for help on using the changeset viewer.