Abbreviations, simple: Difference between revisions
Content added Content deleted
(Ada version) |
(Ada: Simplify using Find_Token) |
||
Line 617: | Line 617: | ||
with Ada.Containers.Vectors; |
with Ada.Containers.Vectors; |
||
with Ada.Strings.Fixed; |
with Ada.Strings.Fixed; |
||
with Ada.Strings.Maps.Constants; |
|||
with Ada.Strings.Unbounded; |
with Ada.Strings.Unbounded; |
||
with Ada. |
with Ada.Text_IO; |
||
procedure Abbreviations_Simple is |
procedure Abbreviations_Simple is |
||
Line 624: | Line 625: | ||
use Ada.Strings.Unbounded; |
use Ada.Strings.Unbounded; |
||
subtype Ustring is Unbounded_String; |
subtype Ustring is Unbounded_String; |
||
Empty_String : constant Ustring := Null_Unbounded_String; |
|||
type Word_Entry is record |
type Word_Entry is record |
||
Line 631: | Line 631: | ||
end record; |
end record; |
||
package |
package Command_Vectors |
||
is new Ada.Containers.Vectors (Index_Type => Positive, |
is new Ada.Containers.Vectors (Index_Type => Positive, |
||
Element_Type => Word_Entry); |
Element_Type => Word_Entry); |
||
use Abbreviation_Vectors; |
|||
Commands : Command_Vectors.Vector; |
|||
type Scan_State is (Idle, In_Word, After_Word, In_Count); |
|||
⚫ | |||
Last_Was_Word : Boolean := False; |
|||
procedure Append (Word_List : String) is |
|||
Abbrev : Vector; |
|||
use Ada.Strings; |
|||
⚫ | |||
Count : Ustring; |
|||
function Is_Word (Item : String) return Boolean |
|||
is (Fixed.Count (Item, Maps.Constants.Letter_Set) /= 0); |
|||
is |
|||
procedure |
procedure Process (Token : String) is |
||
Min : constant Natural := (if Count = "" |
|||
⚫ | |||
⚫ | |||
begin |
begin |
||
if |
if Is_Word (Token) then |
||
if Last_Was_Word then |
|||
Append (Abbrev, Word_Entry'(Word => Word, Min => Min)); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
Last_Word := To_Unbounded_String (Token); |
|||
⚫ | |||
else -- Token is expected to be decimal |
|||
Commands.Append ((Word => Last_Word, |
|||
⚫ | |||
⚫ | |||
end if; |
end if; |
||
end Process; |
|||
Count := Empty_String; |
|||
end Flush; |
|||
Token_First : Positive := Word_List'First; |
|||
Token_Last : Natural; |
|||
begin |
begin |
||
while Token_First in Word_List'Range loop |
|||
case Char is |
|||
Fixed.Find_Token (Word_List, Maps.Constants.Alphanumeric_Set, |
|||
when 'a' .. 'z' | 'A' .. 'Z' => |
|||
Token_First, Inside, |
|||
Token_First, Token_Last); |
|||
exit when Token_Last = 0; |
|||
State := In_Word; |
|||
when In_Word => null; |
|||
⚫ | |||
⚫ | |||
Process (Word_List (Token_First .. Token_Last)); |
|||
case State is |
|||
when Idle | In_Word => |
|||
State := In_Count; |
|||
when In_Count => null; |
|||
when After_Word => |
|||
State := In_Count; |
|||
Count := Empty_String; |
|||
end case; |
|||
Append (Count, Char); |
|||
Token_First := Token_Last + 1; |
|||
case State is |
|||
when In_Word => |
|||
State := After_Word; |
|||
when In_Count => |
|||
Flush; |
|||
⚫ | |||
when Idle | After_Word => null; |
|||
end case; |
|||
when others => |
|||
raise Constraint_Error with "unexpected input"; |
|||
end case; |
|||
end loop; |
end loop; |
||
case State is |
|||
when In_Count => |
|||
Flush; |
|||
⚫ | |||
when In_Word => |
|||
State := After_Word; |
|||
when others => |
|||
null; |
|||
end case; |
|||
end Append; |
end Append; |
||
Line 718: | Line 689: | ||
end if; |
end if; |
||
for Candidate of |
for Candidate of Commands loop |
||
declare |
declare |
||
Upper_Cand : constant String := To_Upper (To_String (Candidate.Word)); |
Upper_Cand : constant String := To_Upper (To_String (Candidate.Word)); |
||
Line 743: | Line 714: | ||
procedure Put_Match (To : String) is |
procedure Put_Match (To : String) is |
||
use Ada. |
use Ada.Text_IO; |
||
begin |
begin |
||
Put ("Match to '"); Put (To); |
Put ("Match to '"); Put (To); |
||
Line 761: | Line 732: | ||
A ("2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1"); |
A ("2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1"); |
||
Put_Match ("riG"); |
|||
Put_Match ("rePEAT"); |
Put_Match ("rePEAT"); |
||
Put_Match ("copies"); |
Put_Match ("copies"); |
||
Line 775: | Line 747: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Match to 'riG' is 'RIGHT' |
|||
Match to 'rePEAT' is 'REPEAT' |
Match to 'rePEAT' is 'REPEAT' |
||
Match to 'copies' is '*error*' |
Match to 'copies' is '*error*' |