|
| 1 | +with Ada.Text_IO; use Ada.Text_IO; |
| 2 | + |
| 3 | +with GNAT.Strings; use GNAT.Strings; |
| 4 | +with GNATCOLL.Mmap; use GNATCOLL.Mmap; |
| 5 | + |
| 6 | +with Langkit_Support.File_Readers; use Langkit_Support.File_Readers; |
| 7 | +with Langkit_Support.Slocs; use Langkit_Support.Slocs; |
| 8 | +with Libfoolang.Analysis; use Libfoolang.Analysis; |
| 9 | +with Libfoolang.Common; use Libfoolang.Common; |
| 10 | + |
| 11 | +with Support; use Support; |
| 12 | + |
| 13 | +procedure Main is |
| 14 | + |
| 15 | + Empty_File : constant String := "empty.txt"; |
| 16 | + Empty_Buffer : aliased constant String := ""; |
| 17 | + |
| 18 | + Example_File : constant String := "main-iso-8859-1.txt"; |
| 19 | + Example_Buffer : String_Access := Read_Whole_File (Example_File); |
| 20 | + |
| 21 | + procedure Check |
| 22 | + (From_Buffer : Boolean := False; |
| 23 | + Empty_File : Boolean := False; |
| 24 | + Wrong_Encoding : Boolean := False; |
| 25 | + With_File_Reader : Boolean := False); |
| 26 | + |
| 27 | + ----------- |
| 28 | + -- Check -- |
| 29 | + ----------- |
| 30 | + |
| 31 | + procedure Check |
| 32 | + (From_Buffer : Boolean := False; |
| 33 | + Empty_File : Boolean := False; |
| 34 | + Wrong_Encoding : Boolean := False; |
| 35 | + With_File_Reader : Boolean := False) |
| 36 | + is |
| 37 | + Charset : constant String := |
| 38 | + (if Wrong_Encoding then "utf-8" else "iso-8859-1"); |
| 39 | + Filename : constant String := |
| 40 | + (if Empty_File then Main.Empty_File else Example_File); |
| 41 | + Buffer : constant access constant String := |
| 42 | + (if Empty_File then Empty_Buffer'Access else Example_Buffer); |
| 43 | + |
| 44 | + Ctx : Analysis_Context; |
| 45 | + U : Analysis_Unit; |
| 46 | + begin |
| 47 | + -- Put some label for this check |
| 48 | + |
| 49 | + Put ("== "); |
| 50 | + Put (if From_Buffer then "buffer" else "file"); |
| 51 | + Put (" | "); |
| 52 | + Put (if Empty_File then "empty-file" else "example-file"); |
| 53 | + Put (" | "); |
| 54 | + Put (if Wrong_Encoding then "wrong-encoding" else "correct-encoding"); |
| 55 | + Put (" | "); |
| 56 | + Put (if With_File_Reader then "file-reader" else "default"); |
| 57 | + Put_Line (" =="); |
| 58 | + New_Line; |
| 59 | + |
| 60 | + -- Parse the source according to requested settings |
| 61 | + |
| 62 | + Ctx := Create_Context |
| 63 | + (File_Reader => (if With_File_Reader |
| 64 | + then Get_File_Reader |
| 65 | + else No_File_Reader_Reference)); |
| 66 | + if From_Buffer then |
| 67 | + U := Ctx.Get_From_Buffer |
| 68 | + (Filename => Filename, |
| 69 | + Charset => Charset, |
| 70 | + Buffer => Buffer.all); |
| 71 | + else |
| 72 | + U := Ctx.Get_From_File |
| 73 | + (Filename => Filename, Charset => Charset); |
| 74 | + end if; |
| 75 | + |
| 76 | + -- Display parsing errors, if any |
| 77 | + |
| 78 | + if U.Has_Diagnostics then |
| 79 | + Put_Line ("Errors:"); |
| 80 | + for D of U.Diagnostics loop |
| 81 | + Put_Line (" " & U.Format_GNU_Diagnostic (D)); |
| 82 | + end loop; |
| 83 | + New_Line; |
| 84 | + end if; |
| 85 | + |
| 86 | + -- Summarize the content of the parsed unit |
| 87 | + |
| 88 | + if U.Root.Is_Null then |
| 89 | + Put_Line ("No root node"); |
| 90 | + else |
| 91 | + Put_Line ("Root node children:" & U.Root.Children_Count'Image); |
| 92 | + declare |
| 93 | + D : constant Token_Data_Type := Data (U.First_Token); |
| 94 | + begin |
| 95 | + Put_Line |
| 96 | + ("First token: " |
| 97 | + & Kind (D)'Image |
| 98 | + & " at " & Image (Sloc_Range (D))); |
| 99 | + end; |
| 100 | + declare |
| 101 | + D : constant Token_Data_Type := Data (U.Last_Token); |
| 102 | + begin |
| 103 | + Put_Line |
| 104 | + ("Last token: " |
| 105 | + & Kind (D)'Image |
| 106 | + & " at " & Image (Sloc_Range (D))); |
| 107 | + end; |
| 108 | + end if; |
| 109 | + New_Line; |
| 110 | + end Check; |
| 111 | + |
| 112 | +begin |
| 113 | + -- Get_From_File |
| 114 | + |
| 115 | + Check; |
| 116 | + Check (With_File_Reader => True); |
| 117 | + |
| 118 | + Check (Empty_File => True); |
| 119 | + Check (Empty_File => True, With_File_Reader => True); |
| 120 | + |
| 121 | + Check (Wrong_Encoding => True); |
| 122 | + Check (Wrong_Encoding => True, With_File_Reader => True); |
| 123 | + |
| 124 | + -- Get_From_Buffer |
| 125 | + |
| 126 | + Check (From_Buffer => True); |
| 127 | + Check (From_Buffer => True, Empty_File => True); |
| 128 | + Check (From_Buffer => True, Wrong_Encoding => True); |
| 129 | + |
| 130 | + Free (Example_Buffer); |
| 131 | +end Main; |
0 commit comments