source: trunk/forge/source/wiki/forge-wiki-parsers.adb@ 19

Last change on this file since 19 was 19, checked in by vadim.godunko, 7 years ago

Initial implementation of Wiki parser.

  • Property svn:keywords set to Author Date Revision
File size: 6.3 KB
Line 
1------------------------------------------------------------------------------
2-- --
3-- Forge on Ada --
4-- --
5-- Runtime Library Component --
6-- --
7------------------------------------------------------------------------------
8-- --
9-- Copyright © 2014, Vadim Godunko <vgodunko@gmail.com> --
10-- All rights reserved. --
11-- --
12-- Redistribution and use in source and binary forms, with or without --
13-- modification, are permitted provided that the following conditions --
14-- are met: --
15-- --
16-- * Redistributions of source code must retain the above copyright --
17-- notice, this list of conditions and the following disclaimer. --
18-- --
19-- * Redistributions in binary form must reproduce the above copyright --
20-- notice, this list of conditions and the following disclaimer in the --
21-- documentation and/or other materials provided with the distribution. --
22-- --
23-- * Neither the name of the Vadim Godunko, IE nor the names of its --
24-- contributors may be used to endorse or promote products derived from --
25-- this software without specific prior written permission. --
26-- --
27-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
28-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
29-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
30-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
31-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
32-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
33-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR --
34-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF --
35-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING --
36-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS --
37-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
38-- --
39------------------------------------------------------------------------------
40-- $Revision: 19 $ $Date: 2014-05-31 17:15:26 +0000 (Sat, 31 May 2014) $
41------------------------------------------------------------------------------
42with Ada.Text_IO; use Ada.Text_IO;
43
44with Ada.Unchecked_Deallocation;
45
46with League.Characters.Latin;
47with League.String_Vectors;
48
49with Forge.Wiki.Block_Parsers.Paragraphs;
50
51package body Forge.Wiki.Parsers is
52
53 procedure Initialize_Block_Regexp (Self : in out Wiki_Parser'Class);
54 -- Build and compile block detection regexp.
55
56 procedure Free is
57 new Ada.Unchecked_Deallocation
58 (Forge.Wiki.Block_Parsers.Abstract_Block_Parser'Class,
59 Block_Parser_Access);
60
61 Base_Expression : constant League.Strings.Universal_String
62 := League.Strings.To_Universal_String
63 ("(^\p{White_Space}*(\P{White_Space}))" -- Regular paragraph
64 & "|(^\p{White_Space}*$)"); -- Separator ('empty') line
65 Para_Block_Match : constant Positive := 1;
66 Para_Offset_Match : constant Positive := 2;
67 Separator_Block_Match : constant Positive := 3;
68 -- Base regular expression to handle paragraph's start line, block's
69 -- continuation line and block separator line.
70
71 -----------------------------
72 -- Initialize_Block_Regexp --
73 -----------------------------
74
75 procedure Initialize_Block_Regexp (Self : in out Wiki_Parser'Class) is
76 begin
77 Self.Block_Regexp := League.Regexps.Compile (Base_Expression);
78 end Initialize_Block_Regexp;
79
80 -----------
81 -- Parse --
82 -----------
83
84 procedure Parse
85 (Self : in out Wiki_Parser'Class; Data : League.Strings.Universal_String)
86 is
87 Lines : constant League.String_Vectors.Universal_String_Vector
88 := Data.Split (League.Characters.Latin.Line_Feed);
89 Line : Positive := 1;
90 Match : League.Regexps.Regexp_Match;
91 Offset : Natural;
92
93 begin
94 Self.Initialize_Block_Regexp;
95 Self.Block_State := null;
96
97 while Line <= Lines.Length loop
98 Match := Self.Block_Regexp.Find_Match (Lines (Line));
99
100 Put_Line (Integer'Image (Match.Capture_Count) & ' ' & Boolean'Image (Match.Is_Matched));
101
102 for C in 1 .. Match.Capture_Count loop
103 Put_Line (Integer'Image (Match.First_Index (C)) & " .." & Integer'Image (Match.Last_Index (C)));
104 end loop;
105
106 if Match.First_Index (Para_Block_Match)
107 <= Match.Last_Index (Para_Block_Match)
108 then
109 Offset := Match.First_Index (Para_Offset_Match);
110
111 if Self.Block_State = null then
112 Put_Line (" paragraph started");
113
114 Self.Block_State :=
115 new Forge.Wiki.Block_Parsers.Paragraphs.Paragraph_Block_Parser;
116 Self.Block_State.Start_Block;
117 Self.Block_State.Line (Lines (Line).Tail_From (Offset));
118
119 else
120 Put_Line (" paragraph matched");
121 Self.Block_State.Line (Lines (Line).Tail_From (Offset));
122 end if;
123
124 else
125 Put_Line (" block separator matched");
126 Self.Block_State.End_Block;
127 Free (Self.Block_State);
128
129 if not Self.Block_Stack.Is_Empty then
130 Self.Block_State := Self.Block_Stack.Last_Element;
131 Self.Block_Stack.Delete_Last;
132 end if;
133 end if;
134
135 Line := Line + 1;
136 end loop;
137 end Parse;
138
139end Forge.Wiki.Parsers;
Note: See TracBrowser for help on using the repository browser.